Adding a .prj file to existing data files
While teaching a GeoServer course recently, we were trying to add a collection of tif and world files to GeoServer as an image mosaic. But the operation kept failing as GeoServer was unable to work out the projection of the files.
This problem can be avoided by adding a
.prj file to the tif file to help
GeoServer out. However we had hundreds of files and a certain national mapping
agency had just assumed that everyone knew its files were in
Later, I worked up a quick solution to this problem. GeoTools is capable of writing out a WKT representation of a projection and Java has no problem walking a directory tree matching a regular expression.
Getting the WKT of a projection is trivial:
Walking the directory tree was a little trickier but uses an anonymous
method of the
The full code can be found in this
snippet. The usage is pretty
simple to just add a
.prj file to a single file (say a shapefile):
java AddProj epsg:27700 file.shp
Or to deal with a whole directory
java AddProj epsg:27700 /data/os-data/rasters/streetview/*.tif
Which adds a
.prj file to all the
.tif files in that directory and all
Obviously you can use other EPSG codes if your data supplier assumes that everyone knows their projection is the only one in the world.