rwingnut
Observer
A friend had shown my the really cool PDF Maps Android app by Avenza. This app allows you to navigate on top of a pdf or tiff image (geoTiff, geoPdf, or georeference PDF) that has been georeferenced. It's been discussed on this forum before. A lot of maps are available in geopdf, both free and nominally priced in the Avenza store. But, I have a few maps that I have notated with trails that are not georeferenced that I'd like to use. So, I set out to learn how to georeference these files for use with PDF Maps. Keep in mind, if you value your time at more than $1/hour, it will be cheaper to just buy the map. But for those cases when the map you want isn't georeferenced, here is the workflow that I've come up with. You'll want to be familiar (or willing to become familiar) with using the command line terminal of your computer.
Tools Used:
Gimp: http://www.gimp.org/
- grapical tool for image manipulation
Imagemagick: http://www.imagemagick.org/
- command line tool for image manipulation
Qgis (Quantum GIS): http://www.qgis.org/en/site/
- tool for determining georeferencing data
GDAL (Geospatial Data Abstraction Library)
- command line tool for creating the output files, this might come with Qgis, but I installed it separately.
I've done all this on Linux, but I believe these tools are also available on Windows PC. If not, OSGeo Live, a linux virtual machine configured for doing GIS work, can be run in VirtualBox on Windows and has these tools pre-installed.
Step 1: Prepare the image.
Use Gimp or your favorite image manipulation program to rotate and crop your image. The image needs to be flattened and reduced to a tiff with 8-bit color depth. This can probably be done with your image manipulation program, but I like running the image through Imagemagick convert just to make sure its properly prepaired:
>> convert <inputfile.pdf> -depth 8 -background white -flatten file1.tiff
Replace the <...> with the file names that you are using
Step 2: Create georeference data.
Open Qgis, select Plugins -> Manage and Install Plugins... Make sure that gdal and georeference plugin are checked.
Select Raster -> Georeferencer -> Georeferencer
In the Georeferencer select File -> Open Raster and open your tiff file. It will ask what CRS or coordinate system to use. This is the coordinate system in which you will be selecting georeferencing points. If you have UTM coordinates printed on your image and you want to use those, select the appropriate zone, for me in New Mexico, that's: NAD83 (EPSG:26913) for NAD83 / UTM zone 13 N If you have lat/lng or you're going to use landmarks and lookup a lat/lng use: WGS 84 (EPSG:4326) for gps lat/lng
Now, you want to find points in this image that you can assign a coordinate to. If your image has grid lines, it's easy. If not, look for landmarks such as trail heads, or road intersections. Find these same landmarks in Google Maps or Google Earth. Mathematically, you'll need a minimum of 2 points spread both horizontally and vertically. I'd recommend at least 4 so that any imprecision in your process gets averaged out. Plus, you can run an analysis in Qgis that will give you the error in agreement (residuals) of all your points. Sort of a sanity check.
For each of these points, use the 'Add Point' tool* to select your landmark and enter the coordinates.
* One of the buttons on the top has two intersecting lines and a yellow star, that's 'Add Point'
The Qgis georeferencer is supposed to be able to create the geopdf, but I've never been able to get the files to come out right. PDF Maps just says its an incompatible file. It has to do with the specific GDAL command-line options run in the background. Instead, there's a button that will give you the command-line switches to apply the georeference data with, that I add additional switched to.
Select Settings -> Transformation Settings
- Transformation Type: Polynomial 1, 2, or 3 (I haven't experimented with this, but can effect accuracy of highly distorted maps.
- Resampling Method: Lanczos (This is supposed to be the best but takes longer to compute, I haven't noticed a difference)
- Output Raster: output file name (required by the software, but we won't use it)
- Target SRS: output coordinate system (again, required by the software, but we won't use it)
Select File -> Start Georeferencing...
This will update the table on the bottom. The residuals will tell you how much in agreement the coordinate are in. This should be 1 few pixels or less. If not, there is someting wrong with the points entered earlier.
If all looks good, select File -> Generate GDAL script. Copy this script to a text editor.
Step3: Georeference our image.
Now it's time to build the command line that will create the file that you need. To apply the coordinates, copy and paste all of the "-gcp x_pixel y_pixel x_coord y_coord" values into the <gcp> tag below and run on your command line:
>> gdal_translate -of GTiff -co "PROFILE=GeoTIFF" <gcp> file1.tiff file2.tiff
Now its time to apply all of the geolocation data, using the same EPSG number as was used during georeferencing:
>> gdalwarp -t_srs EPSG:4326 -r lanczos -co "PROFILE=GeoTIFF" file2.tiff file3.tiff
Now we convert the file to a pdf
>> gdal_translate -of file3.tiff georeferenced.pdf
Not a simple process, but once you've done it once, it's not so bad. I tried a lot of different methods trying to get Qgis to output a compatible file, but found a lot of things that didn't work. Hope this works for you.
Tools Used:
Gimp: http://www.gimp.org/
- grapical tool for image manipulation
Imagemagick: http://www.imagemagick.org/
- command line tool for image manipulation
Qgis (Quantum GIS): http://www.qgis.org/en/site/
- tool for determining georeferencing data
GDAL (Geospatial Data Abstraction Library)
- command line tool for creating the output files, this might come with Qgis, but I installed it separately.
I've done all this on Linux, but I believe these tools are also available on Windows PC. If not, OSGeo Live, a linux virtual machine configured for doing GIS work, can be run in VirtualBox on Windows and has these tools pre-installed.
Step 1: Prepare the image.
Use Gimp or your favorite image manipulation program to rotate and crop your image. The image needs to be flattened and reduced to a tiff with 8-bit color depth. This can probably be done with your image manipulation program, but I like running the image through Imagemagick convert just to make sure its properly prepaired:
>> convert <inputfile.pdf> -depth 8 -background white -flatten file1.tiff
Replace the <...> with the file names that you are using
Step 2: Create georeference data.
Open Qgis, select Plugins -> Manage and Install Plugins... Make sure that gdal and georeference plugin are checked.
Select Raster -> Georeferencer -> Georeferencer
In the Georeferencer select File -> Open Raster and open your tiff file. It will ask what CRS or coordinate system to use. This is the coordinate system in which you will be selecting georeferencing points. If you have UTM coordinates printed on your image and you want to use those, select the appropriate zone, for me in New Mexico, that's: NAD83 (EPSG:26913) for NAD83 / UTM zone 13 N If you have lat/lng or you're going to use landmarks and lookup a lat/lng use: WGS 84 (EPSG:4326) for gps lat/lng
Now, you want to find points in this image that you can assign a coordinate to. If your image has grid lines, it's easy. If not, look for landmarks such as trail heads, or road intersections. Find these same landmarks in Google Maps or Google Earth. Mathematically, you'll need a minimum of 2 points spread both horizontally and vertically. I'd recommend at least 4 so that any imprecision in your process gets averaged out. Plus, you can run an analysis in Qgis that will give you the error in agreement (residuals) of all your points. Sort of a sanity check.
For each of these points, use the 'Add Point' tool* to select your landmark and enter the coordinates.
* One of the buttons on the top has two intersecting lines and a yellow star, that's 'Add Point'
The Qgis georeferencer is supposed to be able to create the geopdf, but I've never been able to get the files to come out right. PDF Maps just says its an incompatible file. It has to do with the specific GDAL command-line options run in the background. Instead, there's a button that will give you the command-line switches to apply the georeference data with, that I add additional switched to.
Select Settings -> Transformation Settings
- Transformation Type: Polynomial 1, 2, or 3 (I haven't experimented with this, but can effect accuracy of highly distorted maps.
- Resampling Method: Lanczos (This is supposed to be the best but takes longer to compute, I haven't noticed a difference)
- Output Raster: output file name (required by the software, but we won't use it)
- Target SRS: output coordinate system (again, required by the software, but we won't use it)
Select File -> Start Georeferencing...
This will update the table on the bottom. The residuals will tell you how much in agreement the coordinate are in. This should be 1 few pixels or less. If not, there is someting wrong with the points entered earlier.
If all looks good, select File -> Generate GDAL script. Copy this script to a text editor.
Step3: Georeference our image.
Now it's time to build the command line that will create the file that you need. To apply the coordinates, copy and paste all of the "-gcp x_pixel y_pixel x_coord y_coord" values into the <gcp> tag below and run on your command line:
>> gdal_translate -of GTiff -co "PROFILE=GeoTIFF" <gcp> file1.tiff file2.tiff
Now its time to apply all of the geolocation data, using the same EPSG number as was used during georeferencing:
>> gdalwarp -t_srs EPSG:4326 -r lanczos -co "PROFILE=GeoTIFF" file2.tiff file3.tiff
Now we convert the file to a pdf
>> gdal_translate -of file3.tiff georeferenced.pdf
Not a simple process, but once you've done it once, it's not so bad. I tried a lot of different methods trying to get Qgis to output a compatible file, but found a lot of things that didn't work. Hope this works for you.