3

Is there a command line tool to convert a shapefile projection in the .prj file into corresponding EPSG code?

I know that there is website http://prj2epsg.org/search for doing this online, but I wanted a command line tool to use in shell scripts (for things like shp2pgqsl) without involving internet.

Also, is there such a tool for extracting EPSG code from the auxiliary information in a GeoTiff file?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
tinlyx
  • 11,057
  • 18
  • 71
  • 119

2 Answers2

3

Here is the source to the underlying Java library, prj2epsg, used behind the scenes on the webpage you list. Basically, it uses a Lucene index to get the best match between the WKT in your prj file and an underlying EPSG. As AndreJ has already said, this may not be 100% perfect, but Lucene excels at partial/fuzzy matching. gdalsrsinfo might be easier to get up and running, though.

You could do this easily enough in Postgres if you had the fuzzystrmatch contrib module installed, by testing against spatial_ref_sys, which holds all the EPSG codes in WKT reprsentation. Something like:

select srid from spatial_ref_sys where levenshtein(srtext, prj_text) < 3 order by levenshtein(srtext, prj_text);

where 3 is an arbitrary number for illustrative purposes. The Levenshtein number gives the number of changes necessary to go from one string to another, so the lower the number, the closer together and more likely your prj file is to a given EPSG.

As for tiff tags, looks at the answer in this Stack Overflow post. The tiffile.py implementation looks like the easiest to implement/modify.

John Powell
  • 13,649
  • 5
  • 46
  • 62
2

There is no such tool working 100% perfectly, because there are many EPSG codes that share the same projection parameters.

You can run gdalsrsinfo on any Geotiff or shapefile .prj file to get the proj string out of the definition.

For some intelligent ways of guessing, follow the answers given here:

Identifying Coordinate System of Shapefile when Unknown?

AndreJ
  • 76,698
  • 5
  • 86
  • 162