Sometimes a web service you use in your GIS application is offline and that results in a GIS application that doesn’t work as it should or doesn’t show the correct data. To avoid these problems you should have a local copy of the data of that web service. For a WFS service the data can be copied to a PostGIS database. This can be done using the command line utility ogr2ogr. This isn’t too hard but some people might be afraid to use the command line. So I wrote this guide about how to handle this from installation to automating the import procedure. Because this guide became pretty long, I divided it up into 3 parts. Part 1 was about the installation of GDAL and ogr2ogr. Part 2 was about the use of ogr2ogr in a Windows batch file and part 3 is about automating the import procedure.
PART 3: automating the import procedure from WFS to PostGIS
Now that we have a fully functional batch file , we still have to execute it to import the data into PostGIS. As mentioned earlier, this can be done by double-clicking the
importmydata.bat file in Windows Explorer. In this way the import procedure is carried out only once and of course we want this to happen automatically every day or every week. In Windows this can easily be automated.
Windows Task Scheduler
The tool that we are going to use is installed by default in Windows on your PC or server and is called
Creating a new task
When you start the Task Scheduler, you will see that the screen has 3 columns:
- the left column is the navigation panel
- the middle column is the main screen
- the right column contains shortcuts to the most used actions.
You can create a new task in the
Task Scheduler in the following way:
- Click in the right column on the action Create Task…
- A new window will open that has various tabs. We will go through this one by one to create the new task.
- We start in the General tab. There we will give our task a name that we can clearly recognize afterwards. In this example, we call the task
Import WFS Bus stops De Lijn.
- We also set the
Securityoptions. By default I always use
Run only when user is logged on. If you notice that your tasks are not being run properly, because you are not logged in to your PC/server when you want to run your task, you can choose the
Run whether user is logged on or notoption.
- Then we go to the Triggers tab and click on
New Triggerwindow has opened in which we will set when our task must be run.
- At the top we set
Begin this taskto
On a schedule.
- The Settings after that depend mainly on your own wishes and on the update frequency of the data in the WFS. In our example we set the task to
- If you have selected
Weekly, the options for when that will be will appear. You choose a starting day and starting time and you select the day of the week on which you want the task to be run. In a professional GIS environment, the most interesting moment will be at night during the weekend. The box
Recur every … weekscan be set to 1 to have it executed every week.
- In the
Advanced Settingsyou must make sure that
Enabledis checked. The other settings in this part of the window aren’t necessary for our example. Click OK to close this window. You will see that our settings are displayed in the
- We now go to the
Actionstab and click on
New…and there we will set what we want to run.
Start a program.
- Via the button
Browse…we select the batch file that we have created earlier. In our example, that is
- This is not necessary, but in the box
Start in (optional), I usually put the folder where my batch file is located. This could be
C:\myfolderwithbatchfilesfor example. Click OK to close this window. You will see that our settings are displayed in the
- We don’t have to change anything in the Conditions and Settings tabs. Click OK to save all settings.
From now on our batch file will be run once a week and this way the data of the online WFS will be copied in our PostGIS database. It is advisable to check the comments of the PostGIS tables occasionally to see when the import procedure was last run. Otherwise if the import procedure goes wrong, you’re data will be outdated. The most common reasons for errors during the import procedure are a changed URL of the WFS and a changed name of the table in the WFS.
Modifying an existing task
You can also adjust an existing task. You do this in the following way:
- In the left column, select
Task Scheduler Library. A list of all your tasks will then appear in the middle column of the window. Some software also installs tasks that need to be performed here, so it may be that the list contains tasks that you do not recognize immediately.
- To edit a task, double-click on the name of your task. You will get the same window as when creating a new task, but with the data that you entered earlier.
- Make the necessary changes and click OK to save the changes.
You can close the Windows Task Scheduler program when you are finished, because the planned tasks are carried out in the background anyway.