Sunday, March 24, 2013

Convert GeoPDF Maps to GeoTIFF with GDAL

The first thought a GIS Professional gets on seeing a GeoPDF map is how to
USGS Topo as GeoPDF
get it into GIS application. This particularly would be the case in US where USGS maps are all provided as high quality GeoPDFs and the analyst would want to derive maximum information from the maps.

A good way to get the map into a GIS application would be in the form of high resolution GeoTIFF. USGS National Map suggests using Global Mapper for the purpose [PDF]. When a similar requirement came to me I tried the operation with trial version of Global Mapper.

To my surprise I faced numerous errors and all my attempts failed. Being a long-term FOSS4G supporter I went ahead to check for FOSS4G tools which can help me out. I learnt that GDAL came with the capabilities since 1.8.0 release. I also learnt that from 1.10 release (which is currently under active development) GDAL has the capabilities to retrieve the PDF layer names which can be turned on/off. Armed with this information I went ahead to experiment and was successful.  

This post explains all that you need to know about using GDAL 1.10 to convert USGS GeoPDF maps to GeoTIFF. 

Installing GDAL 1.10 on Ubuntu

As GDAL 1.10 is currently under development we will be using SVN to pull the latest sources and install it in an exclusive location. With this installation I had three versions of GDAL on my Ubuntu 12.04 PC - 1.7* (which came with QGIS), 1.9* (came with OpenGeo suite) and 1.10 (for experimentation).

Note: If you are installing GDAL and using SVN for the first time you may have to install several pre-requisites by understanding the missing dependency errors. If you already have Ubuntu-GIS repository and working installations of GIS applications like GRASS there won't be any major issues.

Here are the commands to be used for installing GDAL 1.10.

Step 1: Create a directory where all source code will be stored. (This main directory can be used to place the sources of in-development versions of say GRASS GIS)
sudo mkdir /usr/local/src

Step 2: Create the installation directory. I chose opt as I had other GDAL versions and wanted to keep an in-development build separate.
sudo mkdir /opt/gdal_trunk

Step 3: Take over directory ownerships. Replace userid and groupid with your particulars:
sudo chown userid:groupid /usr/local/src

Step 4: Grant read-write-execute permissions for the userid and groupid to src directory:
sudo chmod ug+rwx /usr/local/src

Step 5: Use the change directory command to go into the directory:
cd /usr/local/src

Step 6: Download GDAL 1.10
svn checkout gdal_trunk

Step 7: Enter gdal_trunk directory
cd /usr/local/src/gdal_trunk

Step 8: Configuration - The prefix option specifies the installation directory, --with specifies the compilation must happen with poppler [this is important for GeoPDF to GeoTIFF conversion. Without poppler support the operation fails with error code 4] and podofo support.
./configure --prefix=/opt/gdal_trunk --with-poppler --with-podofo

Step 9: Compile, install and ldconfig
make -j2 && sudo make install && sudo ldconfig

With this you will have a working installation of GDAL 1.10 inside /usr/opt/gdal_trunk. Congratulations! :)

Using GDAL 1.10 for GeoPDF to GeoTIFF conversion

Note: Correct file paths accordingly. The commands are to be typed/pasted at once.

For illustration I have used a USGS GeoPDF belonging to North Carolina state [Download (~19 MB)]. Open the PDF in Adobe Reader or use this GDAL command to get metadata and layer information in a text file.

/opt/gdal_trunk/bin/gdalinfo Downloads/NC_Windsor_North_20110909_TM_geo.pdf -mdd LAYERS >> Downloads/NC_Windsor_North.txt

I have copied the resulting file here onto Google Drive.

One can decide which layer(s) to retain and which to turn off with the above step. For instance, I decided to turn off the "Map_Collar" and "Map_Frame.Projection_and_Grids" layers. The USGS document recommending Global Mapper warns that removing map collar layer discards important metadata including coordinate annotation. Unless we remove that the map won't integrate well with other external layers which we use (as it will have all map layout elements like scale bars, creation information etc.).

The final step is to run gdal_translate for converting the GeoPDF to GeoTIFF with necessary flags.

/opt/gdal_trunk/bin/gdal_translate -of GTiff -co "TILED=YES" -co "TFW=YES" Downloads/NC_Windsor_North_20110909_TM_geo.pdf  Downloads/Windsor_North.tif --config GDAL_PDF_LAYERS_OFF "Map_Collar","Map_Frame.Projection_and_Grids" --config GDAL_PDF_DPI 300

Here is the output GeoTIFF opened in QGIS.
GeoTIFF Output
I had a vector layer for road centrelines for the same area. After doing necessary projection transformations both of them could be overlaid perfectly. [SpatialThoughts has a nice detailed post on this].
Street Centrelines overlaid on GeoTIFF image
Hope you enjoyed the post. Let me know how the conversion went via comments.