6

In the past, we used prj2espg.org to find the srid/espg code from .prj file of shapefiles in order to import them in PostgreSQL. Unfortunately, the website seems to be down since November 2019 (probably because Boundless was acquired by planet.com).

Does anyone have an alternative solution?

I tried using GDAL or looking directly into spatial_ref_sys from PostGIS. I can retrieve some information about the projection like the proj4 description, but if I look into spatial_ref_sys, I could retrieve multiple results for a search...

0

4 Answers 4

2

Responding to my own question here ...

In order to replace prj2epsg.org API, I used GDAL which allow me to parse projection files (.prj) or shapefiles (.shp) in order to recover some information (such as the EPSG or the PROJ4).

I had a lot of variety with the results using .prj/.shp and various versions of GDAL. I decided ultimately to use a recent version of GDAL (for support and stability) and shapefiles (because I was able to find EPSG directly more often).

When I am not able to found EPSG directly, I discovered that I could query the spatial_ref_sys from Postgis to retrieve projection. I query this table using the PROJCS attribute of my shapefile, which gathers a few matching projections. In order to match with the correct projection, I compute a score for each projection based on the PROJ4 of my files and proj4text attribute in the table by transforming then into dicts and counting the number of similarities. The final results seem to always be the correct EPSG code. I did not have enough cases and data to really test on many shapefiles, but anyone is free to try this method...

2

You can download and host the system yourself at https://github.com/boundlessgeo/prj2epsg There is a chance that the system will come back when we finish getting the Boundless domains transferred to OSGeo.

Update

It seems that the Boundless and PlanetFederal repos are now gone.

The code is still available in my fork at https://github.com/ianturton/prj2epsg

3
  • Tried to compile it yesterday, but some dependencies are unavailable. Made an issue right there about the problem github.com/planetfederal/prj2epsg/issues/2. Some repo were moved but the new locations were taken down.
    – lelabo_m
    Commented Aug 26, 2020 at 8:25
  • I've added a PR to fix that
    – Ian Turton
    Commented Aug 26, 2020 at 9:09
  • I am able to build the .war but once I drop it into Tomcat, I get HTTP Status 404. Any ideas ? Thanks for the PR by the way.
    – lelabo_m
    Commented Aug 26, 2020 at 14:29
1

This code was very likely stolen from a different stack overflow question (I can't find the question now) but this is what I'm using while the site is down:

import sys
from osgeo import osr

def esriprj2standards(shapeprj_path):
   prj_file = open(shapeprj_path, 'r')
   prj_txt = prj_file.read()
   srs = osr.SpatialReference()
   srs.ImportFromESRI([prj_txt])
   print 'Shape prj is: %s' % prj_txt
   print 'WKT is: %s' % srs.ExportToWkt()
   print 'Proj4 is: %s' % srs.ExportToProj4()
   srs.AutoIdentifyEPSG()
   print 'EPSG is: %s' % srs.GetAuthorityCode(None)

esriprj2standards(sys.argv[1])
~                                

and then you can run that as so:

./path/to/script/prj2epsg.py shapefileInQuestion.prj

2
  • 1
    That's what I also used, at least to get the proj4 string. I found that the GetAuthorityCode is not always able to get the EPSG code. Maybe I used strange shapefiles and this task is easier with a shapefile of better quality.
    – lelabo_m
    Commented Oct 9, 2020 at 8:09
  • Yeah, this ends up not working great for shapefiles that have custom or non-standard projections. Probably the most accurate EPSG look-up tool I've found since is just importing the layer into QGIS and going to the layer properties. Commented Oct 13, 2020 at 18:11
0

Just do add another possibility here, in case you have installed GDAL like I have (might need to change the path to the file itself):

You could check the epsg file that serves gdal as a lookup table. Implemented it in my own system like this:

import sys
import fnmatch
import warnings
from osgeo import osr

osr.UseExceptions()

def get_epsg(spatial_ref):
    # type: (osr.SpatialReference) -> int
    """
    Extract the EPSG code from an osr.SpatialReference object.

    :param spatial_ref: osr.SpatialReference object
    :return: EPSG code
    """
    try:
        spatial_ref.AutoIdentifyEPSG()
        return int(spatial_ref.GetAttrValue('AUTHORITY', 1))
    except RuntimeError:
        print('Could not auto-identify EPSG code! Trying to look it up in the database...')
        proj_4 = spatial_ref.ExportToProj4()
        pattern = '*' + '*'.join(proj_4.split(' '))
        if 'win' in sys.platform.lower():
            epsg_db = r'c:\Program Files\GDAL\projlib\epsg'
        else:
            epsg_db = r'/usr/share/proj/epsg'
        with open(epsg_db, 'r') as f:
            lines = f.readlines()
        results = []
        for l in lines:
            if fnmatch.fnmatch(l, pattern):
                epsg = int(l.split('>')[0].split('<')[-1])
                results.append(epsg)
        if len(results) > 1:
            warnings.warn('Found more than one match! Choosing the one with the "largest" EPSG code!')
            return sorted(results)[-1]
        return False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.