dkoug Blog

Find srid of a shapefile in python 3

Python -- Posted on Dec. 23, 2018

Find srid of shapefile in python 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import urllib.parse
import urllib.request
import json
from osgeo import ogr, osr

prj_file = open('file.prj', 'r')
prj_txt = prj_file.read()
prj_file.close()

query = {
    'exact': True,
    'error': True,
    'mode': 'wkt',
    'terms': prj_txt}
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)