Later posts on this blog will assume a working install of GDAL/OGR, so it’s prudent that I first demonstrate how to get a fresh cut of GDAL/OGR up-and-running on a Windows system.
Windows users have a few options for installing GDAL (see this question at gis.stackexchange,which will point you to Christoph Gohlke’s binaries –scroll down to GDAL, and the OSGeo4W Installer, among other approaches). However, I’ll guide you through what works for me, which can be summarized as follows:
1) Install Python
2) Install the GDAL binaries published by Tamas Szekeres
3) Append your environment Path variable
4) Add the GDAL_DATA environment variable
5) Finally, perform a quick test to make sure everything worked.
First, Install Python
(For reference purposes, I used version 2.7 for Win32):
· Start at the Python homepage
· At left, find “Quick Links (2.7.2)” and select “Windows Installer”
· Complete the download and start the installation
· Accept the default options suggested by the installer
Once Python is installed, launch IDLE (the built-in Python IDE) to get specific details about your Python installation:
Follow Windows (Start)→Programs→Python 2.7→IDLE (Python GUI)
This figure emphasizes where to look:
In this case, I’m noting the following values:
· “MSC v.1500”
· “on win32”
Your values may be different than mine. For instance, you may have v.1400? Or maybe you installed 64-bit Python on a 64-bit system? Whatever you did, you’ll need to be consistent from here on out. Since these values will be important in the next step, you may want to keep the IDLE window open for a few minutes.
Next Install the GDAL Binaries
From the GDAL homepage, you’d navigate to Downloads→Windows, and open Tamas Szekeres’ Windows binaries. When Tamas’ page loads, you’ll need to identify the proper download package for your version of Python, system bit-depth, and VC dependencies.
Note: VC2005, VC2008, VC2010 refer to various dependencies upon C++ redistributable libraries. To get your very own, just Google “c++ redistributable” and pick up the most current release. Alternatively, if you want to play it uber-safe, download and install Visual C++ Express 2010, and it will automagically qualify you for MSVC2010 builds (which equates to the compiler used to build GDAL).
At Tamas’ site, do the following:
· Scroll down to “GDAL and MapServer latest release versions”
· Find a release corresponding to both your installed C++ redistributable library AND your Python install. For instance, I used “release-1500-gdal-1-8-0-mapserver-6-0-0″, which corresponds to MSVC2008 (Win32) in the left column (note the figure).
Selecting a release will take you to a new screen promoting a handful
of EXE and MSI files—to use ogr2ogr, you’ll need two of these.
· Download and install gdal-18-xxxx-core.msi (where xxxx is 1310, 1400,
1500, or 1600, which matches your Python setup). I recommend keeping the defaults while installing.
· Next download and install the GDAL binaries for your version of Python (i.e. 2.6, 2.7, 3.1). Your choice should match the bit-depth and version of Python you installed in the first place. In keeping with my running example, I used GDAL-1.8.0.win32-py2.7.msi. Once again, run the installer and accept the defaults.
Note: The GDAL installer should detect the Python instance installed in the previous section (see Figure 1-9). If not, first review the steps in the previous section to see if you omitted something (this may indicate the GDAL binaries do not match your installation of Python), otherwise select “Python from another location” and proceed cautiously.
Set the Environment Variables
We’re nearly finished installing GDAL. In this section, we’ll configure the Windows environment variables (Path and GDAL_DATA) so the GDAL/OGR utilities will be easily asccessible from the command line.
Append the Path Variable
First, append the GDAL installation directory onto the existing Path variable. Do the following to edit the Path variable:
· Open the Control Panel (Start→Control Panel), open System and select “Advanced System Settings”.
Note: Alternatively, you can open the Windows (Start) menu, right-click “Computer”, select Properties, then select “Advanced System Settings”, which I find easier.
· Select “Environment Variables” (near the bottom).
· Find “Path” among the list options and select it.
· With “Path” selected, click the “Edit” button.
· When the Edit System Variable dialog appears, scroll to the end of the series, add a semi-colon (;) to separate the prior path from the new one you’re about to enter, then add your GDAL installation path. I added exactly this: ;C:\Program Files (x86)\GDAL
· Keep the Environment Variables dialog open for the next task.
Warning: Be sure to confirm your GDAL installation directory. I installed GDAL on Windows XP, but if you run a newer version of Windows, your installation path may be different from mine.
Add the GDAL_DATA Variable
Next we’ll add a new System Variable, GDAL_DATA, which helps the various GDAL utilities find the gdal-data folder. Without access to the files in the gdal-data folder, GDAL may have trouble recognizing some projections and coordinate systems, among other things.
In Windows, create the GDAL_DATA variable as follows:
· In the Environment Variables dialog, click “New” (near the bottom).
· In the New Variable dialog, enter GDAL_DATA for the name value.
· For value, enter the path to your gdal-data folder; it should be one level below your GDAL directory, which you can confirm by scouting the GDAL folder. I used: C:\Program Files (x86)\GDAL\gdal-data
Once again, confirm the gdal-data directory makes sense relative to your GDAL installation folder.
· At this point, commit your changes by selecting “OK” on each dialog.
[Update 1 Oct, 2013]
Add the GDAL_DRIVER_PATH Variable
In the same way you added the GDAL_DATA variable, add a GDAL_DRIVER_PATH variable. This variable is necessary for the Python environment to locate various GDAL driver plugins. Like the gdal-data directory, the gdalplugins directory should be one level below your GDAL installation. Of course, confirm the path on your system. (I’m on Win 7 at this point–note this is inconsistent with the screenshot, above).
On my system, the gdalplugins directory is: C:\Program Files (x86)\GDAL\gdalplugins
For those who already have GDAL installed along with the Python bindings–If you’re trying to write custom scripts and you’re getting errors like ERROR 4: ..does not exist ..is not recognized as a supported dataset name, you may simply need to add this additional environment variable. This is especially true if you followed my tutorial for setting up GDAL on your system in the first place! In case of the latter, my apologies.
Confirming a Successful GDAL Installation
To confirm the availability of ogr2ogr, just open a new command terminal and attempt to call ogr2ogr. If your installation is successful, ogr2ogr will start running, fail to find any valid runtime arguments, and close, leaving some helpful usage hints in its wake.
To test ogr2ogr in a Windows Command Prompt, do the following:
· Open the Windows (Start) menu, and at the bottom (“Search programs and files”), type cmd and hit Enter; this will open the Command Prompt.
· With the terminal open, type ogr2ogr and hit Enter (see the image).
Because we added the GDAL installation directory to the Path variable, Windows will attempt to find a valid ogr2ogr executable in the GDAL folder. If all goes well your command prompt should resemble the following figure (the ogr2ogr command is highlighted).
[Update 27 Jan, 2014]
Change in testing/confirming a successful install
My initial recommendation to just type “ogr2ogr”, which resulted in an error (which I warned you’d get), was confusing for several people. In hindsight, I agree it was a poor choice on my part. So I like this better:
At the command prompt, type this:
And the OGR module should respond with something like this:
C:\Users\elrobis>gdalinfo --version GDAL 1.9.2, released 2012/10/08
Assuming you get a version number and a release date, and not an error, you should have a clean install of GDAL/OGR to work with. Be warned, though, getting the version output doesn’t confirm that your environment is setup properly, if that stuff isn’t right, you’ll likely get errors when you actually apply GDAL or OGR to some task.
And voila! Assuming you see the Usage/Help output, you should be good to go with a fresh GDAL/OGR installation.
If you plan to create your own Python utility scripts with the OGR Python bindings, I further recommend the following Python libraries: