Copy WFS data to PostGIS using ogr2ogr (PART 1): installation of ogr2ogr

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 is about the installation of GDAL and ogr2ogr. Part 2 is about the use of ogr2ogr in a Windows batch file and part 3 is about automating the import procedure.

PART 1: installing GDAL and ogr2ogr

The utility ogr2ogr is part of the conversion library  GDAL and is used to convert simple vector data from one file format to another. Since version 2.0 ogr2ogr also supports PostGIS data. The advantage of GDAL is that it’s available for many different operating systems and distro’s and that commands work similar on these different systems.

In this guide I will use Windows because that’s the operating system I use the most. I have used this procedure on Windows 7, Windows 10, Windows Server 2008 and Windows Server 2012 and with all these Operating Systems it works in exactly the same way. For installation on Linux and Mac you can find installation guides on the GDAL website.

In the background GDAL is used by many different GIS applications to do all kinds of editing tasks on vector and raster data. This makes that chances are you already have GDAL and ogr2ogr installed if you have one of those applications on your PC or server. For example if you have the latest version of QGIS installed, GDAL is also installed along  and you don’t have to do that again.

Installing GDAL as part of OSGeo4W

On the GDAL website you can find that there are multiple ways to install it on Windows.  For this manual we will use OSGeo4W to install GDAL.  You can use OSGeo4W in some different ways to install software, but for this manual we will show the simplest way to install frequently used GIS applications.

Installation:

  1. Download the installer: OSGeo4W is available in two versions: 32bit and 64bit.
  2. According to your Windows version, you have to pick the correct version of these two. If you own a recent PC or server you probably need the 64bit version.
  3. Start the downloaded file. In some cases you’ll need administrator rights on the machine you want to install the software on.
  4. In the window that appears, select “Express Desktop Install” and click “Next”.
  5. In the next screen check at least “GDAL”. If you haven’t installed QGIS or GRASS GIS previously, you can also check these two so they’ll be installed all at once. They are great tools. Click “Next”.

After this, all selected software will be downloaded and installed. If that’s done, ogr2ogr can be used. If you used the 32bit version, ogr2ogr is probably installed in C:\OSGeo4W\bin\. If you used the64bit version, ogr2ogr is probably installed in  C:\OSGeo4W64\bin\. In the rest of this manual we’ll assume you installed the 64bit version.

Checking the ogr2ogr version

If you installed GDAL with the previous procedure or with any other method, we can check the version of the installed program. We’ll use the command line to do so.  In Windows 7 you can find it in the “Start menu” under “Accessories”. In Windows 10 you can find the command line by pressing the Windows key and the X at the same time.

Checking ogr2ogr:

  1. Open the Windows command line.
  2. Enter the command where ogr2ogr. Windows will then check if it can find ogr2ogr on your system and if so, where it’s located. In the example below my computer found two copies of ogr2ogr. If there’s more then one copy, Windows will always use the first one listed. That’s why we are going to check which version that is.
  3. Enter the command ogr2ogr --version. In the example below the result is GDAL 1.8.0. To use ogr2ogr with PostGIS that version is too old, because the minimum requirement is version 2.0.
  4. Because we also installed GDAL using OSGeo4W, we can tell Windows we want to use that version. To do so enter the command C:\OSGeo4W64\bin\ogr2ogr --version. In the example below that will show that is GDAL 2.2.3. That is a good version to use, so from now on we can use ogr2ogr, but we’ll have to remember to always tell Windows to use the OSGeo4W version.

It is possible that your computer has only one version of ogr2ogr, and of course that’s no problem. It’s also possible Windows can’t find the program at all if you enter the command ogr2ogr, then you will always have to tell Windows where ogr2ogr can be found, by entering the path before the command like in C:\OSGeo4W64\bin\ogr2ogr.

If you don’t have much experience in using the Windows command line, you can find a good introduction here.

Checking psql:

Besides ogr2ogr we optionally will also use the command psql (see part 2). Psql is installed together with QGIS. Now we’ll check if psql is available.

  1. Open the Windows command line.
  2. Enter the command where psql. Windows will then check if it can find psql on your system and if so, where it’s located.
  3. Enter the command psql --version.
  4. If Windows can’t find psql with the where command, we’ll have to tell Windows where to find it. Since GDAL is installed with OSGeo4W, we can tell Windows we want to use that version. To do so enter the command C:\OSGeo4W64\bin\psql --version. If this works as expected, we’ll have to remember to always tell Windows where to find psql. If Windows finds another version of psql, that will probably work just fine. If PostGreSQL is installed on your PC or server, that also has a version of psql installed and that version can also be used. In that case psql can be available at C:\Program Files (x86)\PostgreSQL\9.3\bin\psql.exe.

 

This guide will continue in parts 2 and 3, but those aren’t translated yet. Currently they are only available in Dutch: PART 2 and PART 3.

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.