From 63a5becca1784c168e19069a83eb045dcd9ef114 Mon Sep 17 00:00:00 2001 From: Karim Bahgat Date: Sat, 1 Aug 2015 02:34:04 +0200 Subject: [PATCH] Added better testing (but still incomplete), properly parse proj4 unprojected, fixed flexible meter unit parsing, changed and docified websc --- pycrs/parser.py | 10 +++- pycrs/parser.pyc | Bin 11237 -> 11374 bytes pycrs/units.py | 2 +- pycrs/units.pyc | Bin 1529 -> 1532 bytes pycrs/webscrape.py | 37 +++++++++---- pycrs/webscrape.pyc | Bin 2352 -> 2334 bytes tester.py | 127 ++++++++++++++++++++++++++++---------------- 7 files changed, 116 insertions(+), 60 deletions(-) diff --git a/pycrs/parser.py b/pycrs/parser.py index 2b0c943..552e6ed 100644 --- a/pycrs/parser.py +++ b/pycrs/parser.py @@ -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 diff --git a/pycrs/parser.pyc b/pycrs/parser.pyc index 090d0c5dcc9a3a281e5550e4cc171f063cb176cd..8208f5c941ef1b399eae8387ea0dd7fe8c8c7324 100644 GIT binary patch delta 1236 zcmaKsy>AmS7{(vpr8%{!zXqS(tke`uNWH zn+%%(3~KAt1`T68mn!M(zIP#;affj@ar8HJ>PNtE3H#4pi2@meTV{6p=E+xvBAMcfajINs{j`Px$&-aTb6DXeUUGE zA_MtJ0P>>RYo0@7xLF*D)@gZ_M1#%~cUQ*Pd zq)I+TcBfDisH~tj5-R%;xlM&C3i>7?x8hS|H>rqQ@&v4jYvO+yKIZcq1&^zyG?sgqw$S9ZF8{KZtZOC zw7FT^>-RdQ)x6s_TfKuWH?5v+-fo*)y#tGTKMSAOn)jjjnx)6LR+bnuy@S%*kUdL2 z0&I1ggMn8pAF|)x%W~^nlm*#5o7ZNwu*WKoSz!FM@{iGT^sw4t+uo1rz1bIpR;a;a M+Ewp;?IJ7w2bPWb2mk;8 delta 1077 zcmaKrzl#$=6vw}Fo7+J24CIG*oBffyB%AEHXJIF3r-fiCqE>=#6(L|Qf_Kc`!o|vV z;QGTtB!y@?{{<@>OAEmtT%mg-pqWv{pnWO`ms_uy7=Z* z`Dd%z22iGPg~s-8dtPz-mbk7i1b#LJ(p8L3W3qz8#Z+KlK(1nZWKYgu=;}EI(={X> zWC;&!j1MiE(9%gA=p+&xPHI6eVdz7)^iqs~gcb|vJwtvD1q@|m{SS)71$C;VO9eUA z{!b~=7~1ZZl67M+DJ3R8zRYHAHXAw6N!g;zO;*+y3iwbY79MKDxW8a-Bi|kgg;2*p z9~kN|l?jy2em1he$XHWyh{6G*rq|m{TO2q>e#-1HERb zpF<=RLIVSR$(S1C6e)#NWGK(pt%!1ngd#|{w}E7npJ$CPwu#AE3@HK%W=QKu&Kc)> z{OB8cNkKX0=|mylldI20psASruI{~*mQBlTI@g7n!Uz^G~>&A$;D;6}7Hzts6C)Scd2abAVt z^ZC25Aw*k!?!Q#e29L#8K`71gb(k4S*daWSa25wc3Qz|lP?DjBiGc@1F)*aCF=Vp=g*H1f_AxRVZoa^z G!wdkJ)E^lD diff --git a/pycrs/webscrape.py b/pycrs/webscrape.py index 7cb8d3c..63e2f5f 100644 --- a/pycrs/webscrape.py +++ b/pycrs/webscrape.py @@ -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__": diff --git a/pycrs/webscrape.pyc b/pycrs/webscrape.pyc index dd753cd96426ac0782b95a1a9facf073f795403e..d87b7f284cb0c68386f4681806bedf55b908ce66 100644 GIT binary patch delta 531 zcmYjOO-lnY5N*0`v8x5Gil2vJ5h6ABS(9u_PvW_!vK~eJ z1)_Ko1dsk1Z~g%%yDJ#Tn`D^Gd-L*AeXK57KNWl5{y6h;*~(+w#jV`Yl3OIX(^_G4 zPRac1dxemN_=}8A<}kTvN+f~}xb`CMx4@GMG;R6}L>s8@1G|ZMkNPao&=&HVYUokP zspcY3&>VpBBn^4cf%wjvS#Xn94ccW2DD*G6&(hQz%?}AViodPRaaBT2(ee6S{pJ)N z2r0#%#o@BDNhak`h;9@bEuLthk|1e*3p8kfWIB=o@BmcEJl^K4l?ik0gnR?cBRvRN z9UK;_jZ!$3bG3 zlg{+VhvQ|Gp%hnJcy2}-Jyik}>oG}v5ZF#C*G)Z&pDZ^Xldegu@_+JeU?RBRv#GKK*1Ht_2!?(1XY7?n?5Ih{>-O{Ozos5%oA%WV5{mo{qnG{ z`=$th1uSBy?=s1B%`urcCD2tPfTghk5}-$p0gnhhBEXnIi{cC=D{g+3_oWwEe z=ZSS8K(q^?bRM{RM}mqOL7vyB2p!$>4$O*w2Py91q^A%Ky=BGKUafMCIA2c diff --git a/tester.py b/tester.py index 36f3bf1..d0c4402 100644 --- a/tester.py +++ b/tester.py @@ -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)