Files
PyCRS/pycrs/loader.py
T
Karim Bahgat a8aad24a13 Added almost all params, made front API usable, and added crs transform draw testing.
Next step figure out more stable proj4 parsing of datums and proj etc,
with adding more specific names.
Then consistently make to_esriwkt work.
2015-06-30 04:14:12 +02:00

58 lines
1.4 KiB
Python

import json
import urllib2
from . import parser
#################
# USER FUNCTIONS
#################
# convenience methods for loading from different sources
def from_url(url, format=None):
# first get string from url
string = urllib2.urlopen(url).read()
# then determine parser
if format:
# user specified format
format = format.lower().replace(" ", "_")
func = parser.__getattr__("from_%s" % format)
else:
# unknown format
func = parser.from_unknown_text
# then load
crs = func(string)
return crs
def from_file(filepath):
if filepath.endswith(".prj"):
string = open(filepath, "r").read()
return parser.from_esri_wkt(string)
elif filepath.endswith((".geojson",".json")):
crsinfo = json.load(filepath)["crs"]
if crsinfo["type"] == "name":
string = crsinfo["properties"]["name"]
return parser.from_unknown_text(string)
elif crsinfo["type"] == "link":
url = crsinfo["properties"]["name"]
type = crsinfo["properties"].get("type")
return from_url(url, format=type)
else: raise Exception("invalid geojson crs type: must be either name or link")
## elif filepath.endswith((".tif",".tiff",".geotiff")):
## pass
## # ...