Added better testing (but still incomplete), properly parse proj4 unprojected, fixed flexible meter unit parsing, changed and docified websc

This commit is contained in:
Karim Bahgat
2015-08-01 02:34:04 +02:00
parent c13f27a95d
commit 63a5becca1
7 changed files with 116 additions and 60 deletions
+9 -1
View File
@@ -445,7 +445,14 @@ def from_proj4(string, strict=False):
# get predefined proj def
projname = partdict["+proj"]
projdef = projections.find(projname, "proj4", strict)()
projclass = projections.find(projname, "proj4", strict)
if projclass:
projdef = projclass()
elif projname == "longlat":
# proj4 special case, longlat as projection name means unprojected geogcs
projdef = None
else:
raise Exception("The specified +proj name could not be found")
else:
raise Exception("Could not find required +proj element")
@@ -572,6 +579,7 @@ def from_proj4(string, strict=False):
crs = parameters.CRS(projcs)
else:
# means projdef was None, ie unprojected longlat geogcs
crs = parameters.CRS(geogcs)
# FINISHED
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -15,7 +15,7 @@ def find(unitname, crstype, strict=False):
if unitname == itemname:
return item
# special handling of wkt meters which has multiple possibilities
elif isinstance(itemname, Meter) and crstype.endswith("wkt") and not strict and unitname in ("meters","meter","metre"):
elif isinstance(item(), Meter) and crstype.endswith("wkt") and not strict and unitname in ("meters","meter","metre"):
return item
except:
pass
BIN
View File
Binary file not shown.
+26 -11
View File
@@ -2,7 +2,10 @@ import urllib2
import re
def build_crs_table(savepath):
"""
Build crs table of all equivalent format variations by scraping spatialreference.org.
Takes a while.
"""
# create table
outfile = open(savepath, "wb")
@@ -55,20 +58,32 @@ def build_crs_table(savepath):
def crscode_to_string(codetype, code, format):
"""
Lookup crscode on spatialreference.org and return in specified format.
- codetype: "epsg", "esri", or "sr-org"
- code: The code
- format: The crs format of the returned string. One of "ogcwkt", "esriwkt", or "proj4", but also several others...
"""
link = 'http://spatialreference.org/ref/%s/%s/%s/' %(codetype,code,format)
result = urllib2.urlopen(link).read()
return result
def crsstring_to_string(string, newformat):
# search string, if string is correct there should only be one correct match
link = 'http://spatialreference.org/ref/?search=%s' %string
searchresults = urllib2.urlopen(link).read()
# pick the first result
# ...regex...
# go to its url, with extension for the newformat
link = 'http://spatialreference.org/ref/%s/%s/%s/' %(codetype,code,newformat)
result = urllib2.urlopen(link).read()
return result
##def crsstring_to_string(string, newformat):
## """
## Search unknown crs string for a match on spatialreference.org.
## Not very reliable, the search engine does not properly lookup all text.
## If string is correct there should only be one correct match.
## Warning: Not finished yet.
## """
## link = 'http://spatialreference.org/ref/?search=%s' %string
## searchresults = urllib2.urlopen(link).read()
## # pick the first result
## # ...regex...
## # go to its url, with extension for the newformat
## link = 'http://spatialreference.org/ref/%s/%s/%s/' %(codetype,code,newformat)
## result = urllib2.urlopen(link).read()
## return result
##if __name__ == "__main__":
Binary file not shown.
+80 -47
View File
@@ -64,53 +64,73 @@ def render_world(crs):
###########################
# OGC WKT
print("--------")
print("testing ogc wkt")
print("")
#crs = pycrs.parser.from_sr_code(54030)
# Source string generator
def sourcestrings(format):
# TODO: now bunch of randoms, instead add only most commonly used ones
yield pycrs.webscrape.crscode_to_string("esri", 54030, format)
yield pycrs.webscrape.crscode_to_string("sr-org", 7898, format)
yield pycrs.webscrape.crscode_to_string("sr-org", 6978, format)
yield pycrs.webscrape.crscode_to_string("epsg", 4324, format)
yield pycrs.webscrape.crscode_to_string("sr-org", 6618, format)
yield pycrs.webscrape.crscode_to_string("sr-org", 22, format)
yield pycrs.webscrape.crscode_to_string("esri", 54031, format)
# add more...
# Testing format outputs
def testoutputs(crs):
print("To:\n")
try: result = crs.to_ogc_wkt()
except: result = "Fail"
print("ogc_wkt: %s \n" % result)
try: result = crs.to_esri_wkt()
except: result = "Fail"
print("esri_wkt: %s \n" % result)
try: result = crs.to_proj4()
except: result = "Fail"
print("proj4: %s \n" % result)
# Misc crs for testing
#crs = pycrs.webscrape.crscode_to_string("esri", 54030, "proj4")
#crs = pycrs.webscrape.crscode_to_string("sr-org", 6978, "proj4")
#crs = pycrs.parser.from_sr_code(7898)
#crs = pycrs.parser.from_epsg_code(4324)
#crs = pycrs.parser.from_sr_code(6618)
#crs = pycrs.parser.from_sr_code(22)
#crs = pycrs.parser.from_esri_code(54031)
crs = pycrs.parser.from_sr_code(6978)
wkt = crs.to_ogc_wkt()
print("Original:", wkt)
print("")
crs = pycrs.parser.from_ogc_wkt(wkt)
print("Reconstructed:", crs.to_ogc_wkt())
print("")
#render_world(crs)
###########################
# PROJ4
print("--------")
print("testing proj4")
print("")
proj4 = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
#proj4 = "+proj=longlat +ellps=WGS84 +datum=WGS84"
#proj4 = "+proj=aea +lat_1=24 +lat_2=31.5 +lat_0=24 +lon_0=-84 +x_0=400000 +y_0=0 +ellps=GRS80 +units=m +no_defs "
#proj4 = "+proj=larr +datum=WGS84 +lon_0=0 +lat_ts=45 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs"
#proj4 = "+proj=nsper +datum=WGS84 +ellps=WGS84 +lon_0=-60 +lat_0=40 +h=2000000000000000000000000"
print("Original:", proj4)
print("")
#crs = pycrs.parser.from_esri_code(54030)
#crs = pycrs.parser.from_sr_code(7898)
#crs = pycrs.parser.from_epsg_code(4324)
#crs = pycrs.parser.from_sr_code(6618)
crs = pycrs.parser.from_proj4(proj4)
#crs = pycrs.parser.from_ogc_wkt(wkt)
print("Reconstructed:", crs.to_proj4())
###########################
# From OGC WKT
print("--------")
print("Testing from ogc wkt:")
print("")
for wkt in sourcestrings("ogcwkt"):
# test parsing
try:
print("From:\n")
print(wkt)
print("")
crs = pycrs.parser.from_ogc_wkt(wkt)
# test outputs
testoutputs(crs)
except Exception as err:
print(err)
#render_world(crs)
@@ -118,20 +138,33 @@ print("")
###########################
# ESRI WKT/PRJ FILE
# From PROJ4
print("--------")
print("testing esri prj file")
print("Testing from proj4:")
print("")
crs = pycrs.loader.from_file("testfiles/natearth.prj")
wkt = crs.to_esri_wkt()
print("Original:", wkt)
proj4 = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
print(proj4)
print("")
crs = pycrs.parser.from_esri_wkt(wkt)
print("Reconstructed:", crs.to_esri_wkt())
print("")
crs = pycrs.parser.from_proj4(proj4)
testoutputs(crs)
#render_world(crs)
###########################
# From ESRI WKT/PRJ FILE
print("--------")
print("Testing from esri prj file:")
print("")
wkt = open("testfiles/natearth.prj").read()
print(wkt)
print("")
crs = pycrs.loader.from_file("testfiles/natearth.prj")
testoutputs(crs)
#render_world(crs)