1

I am using https://pypi.org/project/pyshp/ to read records and geometry. Is there a way to read CRS data without pyproj or fiona?

gene
  • 54,868
  • 3
  • 110
  • 187

1 Answers1

2

PyShp does not provide CRS (pyshp: Map Projections, Shapefiles, and Python

if you don't want to use pyproj, osgeo.osr, Fiona or Geopandas to deal with the .prj files you can:

1) simply read the .prj file (but you have to interpret it)

filepath= 'test.prj'
proj = open(filepath, "r").read()
print(proj)
PROJCS["Belge_1972_Belgian_Lambert_72",GEOGCS["GCS_Belge 1972",DATUM["D_Belge_1972",SPHEROID["International_1924",6378388,297]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["standard_parallel_1",51.16666723333333],PARAMETER["standard_parallel_2",49.8333339],PARAMETER["latitude_of_origin",90],PARAMETER["central_meridian",4.367486666666666],PARAMETER["false_easting",150000.013],PARAMETER["false_northing",5400088.438],UNIT["Meter",1]]

2) use a Python module that use SQLite database containing mappings of Well-known Text strings to EPSG code or search the prj2epsg.org API as sridentify, epsg-ident and others

from sridentify import Sridentify
proj = Sridentify()
proj.from_file(filepath)
print(ident.prj)
PROJCS["Belge_1972_Belgian_Lambert_72",GEOGCS["GCS_Belge 1972",DATUM["D_Belge_1972",SPHEROID["International_1924",6378388,297]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["standard_parallel_1",51.16666723333333],PARAMETER["standard_parallel_2",49.8333339],PARAMETER["latitude_of_origin",90],PARAMETER["central_meridian",4.367486666666666],PARAMETER["false_easting",150000.013],PARAMETER["false_northing",5400088.438],UNIT["Meter",1]]
print(proj.get_epsg())
31370

3) or directly use the prj2epsg.org API as in Shapefile PRJ to PostGIS SRID lookup table? or Find srid of a shapefile in python 3

import urllib.parse
import urllib.request
import json
proj = open(filepath, "r").read() 
query = {
  'exact': True,
  'error': True,
  'mode': 'wkt',
  'terms': proj}
webres = 'http://prj2epsg.org/search.json'
data = urllib.parse.urlencode(query).encode("utf-8")
req = urllib.request.Request(webres)
with urllib.request.urlopen(req, data=data) as f:
    jres = json.loads(f.read())
    if jres['codes']:
        srid = int(jres['codes'][0]['code'])
        print(srid)
31370
gene
  • 54,868
  • 3
  • 110
  • 187