mirror of
https://github.com/wassname/PyCRS.git
synced 2026-09-12 12:10:50 +08:00
Colleted esri_wkt and esri_wkt parsing into one function which works, and added find() methods in each mod for easier name-to-obj parsing
This commit is contained in:
+140
-37
@@ -22,22 +22,44 @@
|
||||
|
||||
# +unit and +to_metre are what makes up 'UNIT["Meter",1.0]'
|
||||
|
||||
from . import datums
|
||||
from . import directions
|
||||
|
||||
################
|
||||
|
||||
# ONLY PURE VALUES INSIDE ELLIPSOID...
|
||||
def find(paramname, crstype, strict=False):
|
||||
if not strict:
|
||||
paramname = paramname.lower()
|
||||
for itemname,item in globals().items():
|
||||
if itemname.startswith("_"):
|
||||
continue
|
||||
try:
|
||||
if hasattr(item, crstype):
|
||||
itemname = getattr(item, crstype)
|
||||
if not strict:
|
||||
itemname = itemname.lower()
|
||||
if paramname == itemname:
|
||||
return item
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
##+a Semimajor radius of the ellipsoid axis
|
||||
class SemiMajorRadius:
|
||||
proj4 = "+a"
|
||||
def __init__(self, value):
|
||||
pass
|
||||
# NOT CURRENTLY USED, BUT SHOULD BE
|
||||
# FOR NOW ONLY SET AS PURE VALUES INSIDE ELLIPSOID...
|
||||
|
||||
##+b Semiminor radius of the ellipsoid axis
|
||||
class SemiMinorRadius:
|
||||
proj4 = "+b"
|
||||
def __init__(self, value):
|
||||
pass
|
||||
####+a Semimajor radius of the ellipsoid axis
|
||||
##class SemiMajorRadius:
|
||||
## proj4 = "+a"
|
||||
## def __init__(self, value):
|
||||
## pass
|
||||
##
|
||||
####+b Semiminor radius of the ellipsoid axis
|
||||
##class SemiMinorRadius:
|
||||
## proj4 = "+b"
|
||||
## def __init__(self, value):
|
||||
## pass
|
||||
|
||||
|
||||
|
||||
@@ -49,6 +71,7 @@ class Azimuth:
|
||||
esri_wkt = "azimuth"
|
||||
ogc_wkt = "azimuth"
|
||||
geotiff = "AzimuthAngle"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -59,10 +82,14 @@ class Azimuth:
|
||||
return 'PARAMETER["Azimuth",%s]' % self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["Azimuth",%s]' % self.value
|
||||
|
||||
##+datum Datum name (see `proj -ld`)
|
||||
class Datum:
|
||||
proj4 = "+datum"
|
||||
ogc_wkt = "DATUM"
|
||||
esri_wkt = "DATUM"
|
||||
|
||||
def __init__(self, name, ellipsoid, datumshift=None):
|
||||
"""
|
||||
Arguments:
|
||||
@@ -77,6 +104,8 @@ class Datum:
|
||||
def to_proj4(self):
|
||||
if self.datumshift:
|
||||
return "%s %s" % (self.ellips.to_proj4(), self.datumshift.to_proj4())
|
||||
elif isinstance(self.name, datums.Unknown):
|
||||
return "%s" % self.ellips.to_proj4()
|
||||
else:
|
||||
return "+datum=%s %s" % (self.name.proj4, self.ellips.to_proj4())
|
||||
|
||||
@@ -87,7 +116,10 @@ class Datum:
|
||||
return 'DATUM["%s", %s]' % (self.name.ogc_wkt, self.ellips.to_ogc_wkt())
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
if self.datumshift:
|
||||
return 'DATUM["%s", %s, %s]' % (self.name.esri_wkt, self.ellips.to_esri_wkt(), self.datumshift.to_esri_wkt())
|
||||
else:
|
||||
return 'DATUM["%s", %s]' % (self.name.esri_wkt, self.ellips.to_esri_wkt())
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -95,6 +127,10 @@ class Datum:
|
||||
|
||||
##+ellps Ellipsoid name (see `proj -le`)
|
||||
class Ellipsoid:
|
||||
proj4 = "+ellps"
|
||||
ogc_wkt = "SPHEROID"
|
||||
esri_wkt = "SPHEROID"
|
||||
|
||||
def __init__(self, name, semimaj_ax=None, inv_flat=None):
|
||||
"""
|
||||
Arguments:
|
||||
@@ -119,7 +155,7 @@ class Ellipsoid:
|
||||
return 'SPHEROID["%s", %s, %s]' % (self.name.ogc_wkt, self.semimaj_ax, self.inv_flat)
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'SPHEROID["%s", %s, %s]' % (self.name.esri_wkt, self.semimaj_ax, self.inv_flat)
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -127,6 +163,9 @@ class Ellipsoid:
|
||||
|
||||
#GEOGCS
|
||||
class GeogCS:
|
||||
ogc_wkt = "GEOGCS"
|
||||
esri_wkt = "GEOGCS"
|
||||
|
||||
def __init__(self, name, datum, prime_mer, angunit, twin_ax=None):
|
||||
"""
|
||||
Arguments:
|
||||
@@ -143,16 +182,20 @@ class GeogCS:
|
||||
self.twin_ax = twin_ax
|
||||
|
||||
def to_proj4(self):
|
||||
return "%s %s %s" % (self.datum.to_proj4(), self.prime_mer.to_proj4(), self.angunit.to_proj4() ) #+axis= AND #, self.twin_ax[0].proj4, self.twin_ax[1].proj4 )
|
||||
# dont parse axis to proj4, because in proj4, axis only applies to the cs, ie the projcs (not the geogcs, where wkt can specify with axis)
|
||||
return "%s %s %s" % (self.datum.to_proj4(), self.prime_mer.to_proj4(), self.angunit.to_proj4() )
|
||||
|
||||
def to_ogc_wkt(self):
|
||||
return 'GEOGCS["%s", %s, %s, %s, AXIS["Lon", %s], AXIS["Lat", %s]]' % (self.name, self.datum.to_ogc_wkt(), self.prime_mer.to_ogc_wkt(), self.angunit.to_ogc_wkt(), self.twin_ax[0].ogc_wkt, self.twin_ax[1].ogc_wkt )
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'GEOGCS["%s", %s, %s, %s, AXIS["Lon", %s], AXIS["Lat", %s]]' % (self.name, self.datum.to_esri_wkt(), self.prime_mer.to_esri_wkt(), self.angunit.to_esri_wkt(), self.twin_ax[0].esri_wkt, self.twin_ax[1].esri_wkt )
|
||||
|
||||
#PROJCS
|
||||
class ProjCS:
|
||||
ogc_wkt = "PROJCS"
|
||||
esri_wkt = "PROJCS"
|
||||
|
||||
def __init__(self, name, geogcs, proj, params, unit, twin_ax=None):
|
||||
"""
|
||||
Arguments:
|
||||
@@ -173,7 +216,6 @@ class ProjCS:
|
||||
string = "%s %s " % (self.proj.to_proj4(), self.geogcs.to_proj4())
|
||||
string += " ".join(param.to_proj4() for param in self.params)
|
||||
string += " %s" % self.unit.to_proj4()
|
||||
# in proj4, axis only applies to the cs, ie the projcs (not the geogcs, where wkt can specify with axis)
|
||||
string += " +axis=" + self.twin_ax[0].proj4 + self.twin_ax[1].proj4 + "u" # up set as default because only proj4 can set it I think...
|
||||
return string
|
||||
|
||||
@@ -185,11 +227,18 @@ class ProjCS:
|
||||
return string
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
string = 'PROJCS["%s", %s, %s, ' % (self.name, self.geogcs.to_esri_wkt(), self.proj.to_esri_wkt() )
|
||||
string += ", ".join(param.to_esri_wkt() for param in self.params)
|
||||
string += ', %s' % self.unit.to_esri_wkt()
|
||||
string += ', AXIS["X", %s], AXIS["Y", %s]]' % (self.twin_ax[0].esri_wkt, self.twin_ax[1].esri_wkt )
|
||||
return string
|
||||
|
||||
##+k Scaling factor (old name)
|
||||
##+k_0 Scaling factor (new name)
|
||||
class ScalingFactor:
|
||||
proj4 = "+k"
|
||||
ogc_wkt = "scale_factor"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -200,6 +249,7 @@ class ScalingFactor:
|
||||
return 'PARAMETER["scale_factor", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
# REALLY??
|
||||
raise Exception("Paramater not supported by ESRI WKT")
|
||||
|
||||
def to_geotiff(self):
|
||||
@@ -208,6 +258,10 @@ class ScalingFactor:
|
||||
|
||||
##+lat_0 Latitude of origin
|
||||
class LatitudeOrigin:
|
||||
proj4 = "+lat_0"
|
||||
ogc_wkt = "latitude_of_origin"
|
||||
esri_wkt = "Latitude_Of_Origin"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -219,7 +273,7 @@ class LatitudeOrigin:
|
||||
return 'PARAMETER["latitude_of_origin", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
raise Exception("Paramater not supported by ESRI WKT")
|
||||
return 'PARAMETER["Latitude_Of_Origin", %s]' %self.value
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -227,6 +281,10 @@ class LatitudeOrigin:
|
||||
|
||||
##+lat_1 Latitude of first standard parallel
|
||||
class LatitudeFirstStndParallel:
|
||||
proj4 = "+lat_1"
|
||||
ogc_wkt = "standard_parallel_1"
|
||||
esri_wkt = "Standard_Parallel_1"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -237,7 +295,7 @@ class LatitudeFirstStndParallel:
|
||||
return 'PARAMETER["standard_parallel_1", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["Standard_Parallel_1", %s]' %self.value
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -245,6 +303,10 @@ class LatitudeFirstStndParallel:
|
||||
|
||||
##+lat_2 Latitude of second standard parallel
|
||||
class LatitudeSecondStndParallel:
|
||||
proj4 = "+lat_2"
|
||||
ogc_wkt = "standard_parallel_2"
|
||||
esri_wkt = "Standard_Parallel_2"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -255,7 +317,7 @@ class LatitudeSecondStndParallel:
|
||||
return 'PARAMETER["standard_parallel_2", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["Standard_Parallel_2", %s]' %self.value
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -263,6 +325,10 @@ class LatitudeSecondStndParallel:
|
||||
|
||||
##+lat_ts Latitude of true scale
|
||||
class LatitudeTrueScale:
|
||||
proj4 = "lat_ts"
|
||||
ogc_wkt = "Standard_Parallel_1"
|
||||
esri_wkt = "Standard_Parallel_1"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -273,7 +339,7 @@ class LatitudeTrueScale:
|
||||
return 'PARAMETER["Standard_Parallel_1", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["Standard_Parallel_1", %s]' %self.value
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -281,6 +347,10 @@ class LatitudeTrueScale:
|
||||
|
||||
##+lon_0 Central meridian
|
||||
class CentralMeridian:
|
||||
proj4 = "+lon_0"
|
||||
ogc_wkt = "Central_Meridian"
|
||||
esri_wkt = "Central_Meridian"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -291,7 +361,7 @@ class CentralMeridian:
|
||||
return 'PARAMETER["Central_Meridian", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["Central_Meridian", %s]' %self.value
|
||||
|
||||
def to_geotiff(self):
|
||||
pass
|
||||
@@ -300,6 +370,9 @@ class CentralMeridian:
|
||||
##+lonc ? Longitude used with Oblique Mercator and possibly a few others
|
||||
class LongitudeCenter:
|
||||
proj4 = "+lonc"
|
||||
ogc_wkt = "Longitude_Of_Center"
|
||||
esri_wkt = "Longitude_Of_Center"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -310,7 +383,7 @@ class LongitudeCenter:
|
||||
return 'PARAMETER["Longitude_Of_Center", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["Longitude_Of_Center", %s]' %self.value
|
||||
|
||||
##+lon_wrap Center longitude to use for wrapping (see below)
|
||||
|
||||
@@ -318,6 +391,10 @@ class LongitudeCenter:
|
||||
|
||||
##+pm Alternate prime meridian (typically a city name, see below)
|
||||
class PrimeMeridian:
|
||||
proj4 = "+pm"
|
||||
ogc_wkt = "PRIMEM"
|
||||
esri_wkt = "PRIMEM"
|
||||
|
||||
def __init__(self, value):
|
||||
"""
|
||||
Arguments:
|
||||
@@ -333,10 +410,14 @@ class PrimeMeridian:
|
||||
return 'PRIMEM["Greenwich", %s]' %self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PRIMEM["Greenwich", %s]' %self.value
|
||||
|
||||
##+proj Projection name (see `proj -l`)
|
||||
class Projection:
|
||||
proj4 = "+proj"
|
||||
ogc_wkt = "PROJECTION"
|
||||
esri_wkt = "PROJECTION"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -347,7 +428,7 @@ class Projection:
|
||||
return 'PROJECTION["%s"]' %self.value.ogc_wkt
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PROJECTION["%s"]' %self.value.esri_wkt
|
||||
|
||||
##+zone UTM zone
|
||||
|
||||
@@ -355,6 +436,9 @@ class Projection:
|
||||
|
||||
##+towgs84 3 or 7 term datum transform parameters (see below)
|
||||
class DatumShift:
|
||||
proj4 = "+towgs84"
|
||||
ogc_wkt = "TOWGS84"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -369,6 +453,8 @@ class DatumShift:
|
||||
|
||||
##+to_meter Multiplier to convert map units to 1.0m
|
||||
class MeterMultiplier:
|
||||
proj4 = "+to_meter"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -380,10 +466,12 @@ class MeterMultiplier:
|
||||
return bytes(self.value)
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return bytes(self.value)
|
||||
|
||||
##+units meters, US survey feet, etc.
|
||||
class UnitType:
|
||||
proj4 = "+units"
|
||||
|
||||
def __init__(self, value):
|
||||
"""
|
||||
Arguments:
|
||||
@@ -400,10 +488,13 @@ class UnitType:
|
||||
return bytes(self.value.ogc_wkt)
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return bytes(self.value.esri_wkt)
|
||||
|
||||
# special...
|
||||
class Unit:
|
||||
ogc_wkt = "UNIT"
|
||||
esri_wkt = "UNIT"
|
||||
|
||||
def __init__(self, unittype, metermultiplier):
|
||||
self.unittype = unittype
|
||||
self.metermultiplier = metermultiplier
|
||||
@@ -415,10 +506,13 @@ class Unit:
|
||||
return 'UNIT["%s", %s]' %(self.unittype.to_ogc_wkt(), self.metermultiplier.to_ogc_wkt())
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'UNIT["%s", %s]' %(self.unittype.to_esri_wkt(), self.metermultiplier.to_esri_wkt())
|
||||
|
||||
# angular unit
|
||||
class AngularUnit:
|
||||
ogc_wkt = "UNIT"
|
||||
esri_wkt = "UNIT"
|
||||
|
||||
def __init__(self, unittype, metermultiplier):
|
||||
self.unittype = unittype
|
||||
self.metermultiplier = metermultiplier
|
||||
@@ -431,7 +525,7 @@ class AngularUnit:
|
||||
return 'UNIT["%s", %s]' %(self.unittype.to_ogc_wkt(), self.metermultiplier.to_ogc_wkt())
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'UNIT["%s", %s]' %(self.unittype.to_esri_wkt(), self.metermultiplier.to_esri_wkt())
|
||||
|
||||
##+x_0 False easting
|
||||
class FalseEasting:
|
||||
@@ -450,7 +544,7 @@ class FalseEasting:
|
||||
return 'PARAMETER["false_easting", %s]' % self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["False_Easting", %s]' % self.value
|
||||
|
||||
##+y_0 False northing
|
||||
class FalseNorthing:
|
||||
@@ -469,10 +563,14 @@ class FalseNorthing:
|
||||
return 'PARAMETER["false_northing", %s]' % self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["False_Northing", %s]' % self.value
|
||||
|
||||
##+h Satellite height
|
||||
class SatelliteHeight:
|
||||
proj4 = "+h"
|
||||
ogc_wkt = "satellite_height"
|
||||
esri_wkt = "satellite_height"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -483,10 +581,12 @@ class SatelliteHeight:
|
||||
return 'PARAMETER["satellite_height", %s]' % self.value
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return 'PARAMETER["satellite_height", %s]' % self.value
|
||||
|
||||
##+tilt Tilt angle
|
||||
class TiltAngle:
|
||||
proj4 = "+tilt"
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -494,10 +594,10 @@ class TiltAngle:
|
||||
return "+tilt=%s" %self.value
|
||||
|
||||
def to_ogc_wkt(self):
|
||||
raise Exception("Paramater not supported by OGC WKT")
|
||||
raise Exception("Parameter not supported by OGC WKT")
|
||||
|
||||
def to_esri_wkt(self):
|
||||
raise Exception("Paramater not supported by ESRI WKT")
|
||||
raise Exception("Parameter not supported by ESRI WKT")
|
||||
|
||||
|
||||
# then the final CRS object which is instantiated with all of these?
|
||||
@@ -508,12 +608,15 @@ class CRS:
|
||||
self.toplevel = toplevel
|
||||
|
||||
def to_proj4(self):
|
||||
return "%s +no_defs" % self.toplevel.to_proj4()
|
||||
if isinstance(self.toplevel, ProjCS):
|
||||
return "%s +no_defs" % self.toplevel.to_proj4()
|
||||
elif isinstance(self.toplevel, GeogCS):
|
||||
return "+proj=longlat %s +no_defs" % self.toplevel.to_proj4()
|
||||
|
||||
def to_ogc_wkt(self):
|
||||
return "%s" % self.toplevel.to_ogc_wkt()
|
||||
|
||||
def to_esri_wkt(self):
|
||||
return self.to_ogc_wkt()
|
||||
return "%s" % self.toplevel.to_esri_wkt()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user