Copy WFS data to PostGIS using ogr2ogr (PART 3): automating the import procedure

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 Task Scheduler.

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 Security options. 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 not option.
  • Then we go to the Triggers tab and click on New…
  • A New Trigger window has opened in which we will set when our task must be run.
  • At the top we set Begin this task to 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 Weekly.
  • 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 … weeks can be set to 1 to have it executed every week.
  • In the Advanced Settings you must make sure that Enabled is 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 Triggers tab.
  • We now go to the Actions tab and click on New… and there we will set what we want to run.
  • As Action we choose Start a program.
  • Via the button Browse… we select the batch file that we have created earlier. In our example, that is importmydata.bat .
  • 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:\myfolderwithbatchfiles for example. Click OK to close this window. You will see that our settings are displayed in the Actions tab.
  • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.