Package pyproj

Source Code for Package pyproj

  1  """ 
  2  Cython wrapper to provide python interfaces to 
  3  PROJ.4 (http://trac.osgeo.org/proj/) functions. 
  4   
  5  Performs cartographic transformations and geodetic computations. 
  6   
  7  The Proj class can convert from geographic (longitude,latitude) 
  8  to native map projection (x,y) coordinates and vice versa, or 
  9  from one map projection coordinate system directly to another. 
 10  The module variable pj_list is a dictionary containing all the 
 11  available projections and their descriptions. 
 12   
 13  The Geod class can perform forward and inverse geodetic, or 
 14  Great Circle, computations.  The forward computation involves 
 15  determining latitude, longitude and back azimuth of a terminus 
 16  point given the latitude and longitude of an initial point, plus 
 17  azimuth and distance. The inverse computation involves 
 18  determining the forward and back azimuths and distance given the 
 19  latitudes and longitudes of an initial and terminus point. 
 20   
 21  Input coordinates can be given as python arrays, lists/tuples, 
 22  scalars or numpy/Numeric/numarray arrays. Optimized for objects 
 23  that support the Python buffer protocol (regular python and 
 24  numpy array objects). 
 25   
 26  Download: http://python.org/pypi/pyproj 
 27   
 28  Requirements: python 2.4 or higher. 
 29   
 30  Example scripts are in 'test' subdirectory of source distribution. 
 31  The 'test()' function will run the examples in the docstrings. 
 32   
 33  Contact:  Jeffrey Whitaker <jeffrey.s.whitaker@noaa.gov 
 34   
 35  copyright (c) 2006 by Jeffrey Whitaker. 
 36   
 37  Permission to use, copy, modify, and distribute this software 
 38  and its documentation for any purpose and without fee is hereby 
 39  granted, provided that the above copyright notice appear in all 
 40  copies and that both the copyright notice and this permission 
 41  notice appear in supporting documentation. THE AUTHOR DISCLAIMS 
 42  ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 
 43  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT 
 44  SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR 
 45  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
 46  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
 47  NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
 48  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ 
 49   
 50  from pyproj import _proj 
 51  from pyproj.datadir import pyproj_datadir 
 52  __version__ =  _proj.__version__ 
 53  set_datapath =  _proj.set_datapath 
 54  from array import array 
 55  import os, math 
 56  #import numpy as np 
 57  pj_list={ 
 58  'aea': "Albers Equal Area", 
 59  'aeqd': "Azimuthal Equidistant", 
 60  'airy': "Airy", 
 61  'aitoff': "Aitoff", 
 62  'alsk': "Mod. Stererographics of Alaska", 
 63  'apian': "Apian Globular I", 
 64  'august': "August Epicycloidal", 
 65  'bacon': "Bacon Globular", 
 66  'bipc': "Bipolar conic of western hemisphere", 
 67  'boggs': "Boggs Eumorphic", 
 68  'bonne': "Bonne (Werner lat_1=90)", 
 69  'cass': "Cassini", 
 70  'cc': "Central Cylindrical", 
 71  'cea': "Equal Area Cylindrical", 
 72  'chamb': "Chamberlin Trimetric", 
 73  'collg': "Collignon", 
 74  'crast': "Craster Parabolic (Putnins P4)", 
 75  'denoy': "Denoyer Semi-Elliptical", 
 76  'eck1': "Eckert I", 
 77  'eck2': "Eckert II", 
 78  'eck3': "Eckert III", 
 79  'eck4': "Eckert IV", 
 80  'eck5': "Eckert V", 
 81  'eck6': "Eckert VI", 
 82  'eqc': "Equidistant Cylindrical (Plate Caree)", 
 83  'eqdc': "Equidistant Conic", 
 84  'etmerc': "Extended Transverse Mercator" , 
 85  'euler': "Euler", 
 86  'fahey': "Fahey", 
 87  'fouc': "Foucaut", 
 88  'fouc_s': "Foucaut Sinusoidal", 
 89  'gall': "Gall (Gall Stereographic)", 
 90  'geocent': "Geocentric", 
 91  'geos': "Geostationary Satellite View", 
 92  'gins8': "Ginsburg VIII (TsNIIGAiK)", 
 93  'gn_sinu': "General Sinusoidal Series", 
 94  'gnom': "Gnomonic", 
 95  'goode': "Goode Homolosine", 
 96  'gs48': "Mod. Stererographics of 48 U.S.", 
 97  'gs50': "Mod. Stererographics of 50 U.S.", 
 98  'hammer': "Hammer & Eckert-Greifendorff", 
 99  'hatano': "Hatano Asymmetrical Equal Area", 
100  'healpix': "HEALPix", 
101  'rhealpix': "rHEALPix", 
102  'igh':  "Interrupted Goode Homolosine", 
103  'imw_p': "Internation Map of the World Polyconic", 
104  'isea':  "Icosahedral Snyder Equal Area", 
105  'kav5': "Kavraisky V", 
106  'kav7': "Kavraisky VII", 
107  'krovak': "Krovak", 
108  'labrd': "Laborde", 
109  'laea': "Lambert Azimuthal Equal Area", 
110  'lagrng': "Lagrange", 
111  'larr': "Larrivee", 
112  'lask': "Laskowski", 
113  'lonlat': "Lat/long (Geodetic)", 
114  'latlon': "Lat/long (Geodetic alias)", 
115  'latlong': "Lat/long (Geodetic alias)", 
116  'longlat': "Lat/long (Geodetic alias)", 
117  'lcc': "Lambert Conformal Conic", 
118  'lcca': "Lambert Conformal Conic Alternative", 
119  'leac': "Lambert Equal Area Conic", 
120  'lee_os': "Lee Oblated Stereographic", 
121  'loxim': "Loximuthal", 
122  'lsat': "Space oblique for LANDSAT", 
123  'mbt_s': "McBryde-Thomas Flat-Polar Sine", 
124  'mbt_fps': "McBryde-Thomas Flat-Pole Sine (No. 2)", 
125  'mbtfpp': "McBride-Thomas Flat-Polar Parabolic", 
126  'mbtfpq': "McBryde-Thomas Flat-Polar Quartic", 
127  'mbtfps': "McBryde-Thomas Flat-Polar Sinusoidal", 
128  'merc': "Mercator", 
129  'mil_os': "Miller Oblated Stereographic", 
130  'mill': "Miller Cylindrical", 
131  'moll': "Mollweide", 
132  'murd1': "Murdoch I", 
133  'murd2': "Murdoch II", 
134  'murd3': "Murdoch III", 
135  'natearth': "Natural Earth", 
136  'nell': "Nell", 
137  'nell_h': "Nell-Hammer", 
138  'nicol': "Nicolosi Globular", 
139  'nsper': "Near-sided perspective", 
140  'nzmg': "New Zealand Map Grid", 
141  'ob_tran': "General Oblique Transformation", 
142  'ocea': "Oblique Cylindrical Equal Area", 
143  'oea': "Oblated Equal Area", 
144  'omerc': "Oblique Mercator", 
145  'ortel': "Ortelius Oval", 
146  'ortho': "Orthographic", 
147  'pconic': "Perspective Conic", 
148  'poly': "Polyconic (American)", 
149  'putp1': "Putnins P1", 
150  'putp2': "Putnins P2", 
151  'putp3': "Putnins P3", 
152  'putp3p': "Putnins P3'", 
153  'putp4p': "Putnins P4'", 
154  'putp5': "Putnins P5", 
155  'putp5p': "Putnins P5'", 
156  'putp6': "Putnins P6", 
157  'putp6p': "Putnins P6'", 
158  'qua_aut': "Quartic Authalic", 
159  'robin': "Robinson", 
160  'rouss': "Roussilhe Stereographic", 
161  'rpoly': "Rectangular Polyconic", 
162  'sinu': "Sinusoidal (Sanson-Flamsteed)", 
163  'somerc': "Swiss. Obl. Mercator", 
164  'stere': "Stereographic", 
165  'sterea': "Oblique Stereographic Alternative", 
166  'gstmerc': "Gauss-Schreiber Transverse Mercator (aka Gauss-Laborde Reunion)", 
167  'tcc': "Transverse Central Cylindrical", 
168  'tcea': "Transverse Cylindrical Equal Area", 
169  'tissot': "Tissot Conic", 
170  'tmerc': "Transverse Mercator", 
171  'tpeqd': "Two Point Equidistant", 
172  'tpers': "Tilted perspective", 
173  'ups': "Universal Polar Stereographic", 
174  'urm5': "Urmaev V", 
175  'urmfps': "Urmaev Flat-Polar Sinusoidal", 
176  'utm': "Universal Transverse Mercator (UTM)", 
177  'vandg': "van der Grinten (I)", 
178  'vandg2': "van der Grinten II", 
179  'vandg3': "van der Grinten III", 
180  'vandg4': "van der Grinten IV", 
181  'vitk1': "Vitkovsky I", 
182  'wag1': "Wagner I (Kavraisky VI)", 
183  'wag2': "Wagner II", 
184  'wag3': "Wagner III", 
185  'wag4': "Wagner IV", 
186  'wag5': "Wagner V", 
187  'wag6': "Wagner VI", 
188  'wag7': "Wagner VII", 
189  'weren': "Werenskiold I", 
190  'wink1': "Winkel I", 
191  'wink2': "Winkel II", 
192  'wintri': "Winkel Tripel"} 
193   
194  pj_ellps={ 
195  "MERIT":        {'a':6378137.0,'rf':298.257,'description':"MERIT 1983"}, 
196  "SGS85":        {'a':6378136.0,'rf':298.257,'description':"Soviet Geodetic System 85"}, 
197  "GRS80":        {'a':6378137.0,'rf':298.257222101,'description':"GRS 1980(IUGG, 1980)"}, 
198  "IAU76":        {'a':6378140.0,'rf':298.257,'description':"IAU 1976"}, 
199  "airy":         {'a':6377563.396,'b':6356256.910,'description':"Airy 1830"}, 
200  "APL4.9":       {'a':6378137.0,'rf':298.25,'description':"Appl. Physics. 1965"}, 
201  "NWL9D":        {'a':6378145.0,'rf':298.25,'description':" Naval Weapons Lab., 1965"}, 
202  "mod_airy":     {'a':6377340.189,'b':6356034.446,'description':"Modified Airy"}, 
203  "andrae":       {'a':6377104.43,'rf':300.0,'description':"Andrae 1876 (Den., Iclnd.)"}, 
204  "aust_SA":      {'a':6378160.0,'rf':298.25,'description':"Australian Natl & S. Amer. 1969"}, 
205  "GRS67":        {'a':6378160.0,'rf':298.2471674270,'description':"GRS 67(IUGG 1967)"}, 
206  "bessel":       {'a':6377397.155,'rf':299.1528128,'description':"Bessel 1841"}, 
207  "bess_nam":     {'a':6377483.865,'rf':299.1528128,'description':"Bessel 1841 (Namibia)"}, 
208  "clrk66":       {'a':6378206.4,'b':6356583.8,'description':"Clarke 1866"}, 
209  "clrk80":       {'a':6378249.145,'rf':293.4663,'description':"Clarke 1880 mod."}, 
210  "CPM":          {'a':6375738.7,'rf':334.29,'description':"Comm. des Poids et Mesures 1799"}, 
211  "delmbr":       {'a':6376428.,'rf':311.5,'description':"Delambre 1810 (Belgium)"}, 
212  "engelis":      {'a':6378136.05,'rf':298.2566,'description':"Engelis 1985"}, 
213  "evrst30":      {'a':6377276.345,'rf':300.8017,'description':"Everest 1830"}, 
214  "evrst48":      {'a':6377304.063,'rf':300.8017,'description':"Everest 1948"}, 
215  "evrst56":      {'a':6377301.243,'rf':300.8017,'description':"Everest 1956"}, 
216  "evrst69":      {'a':6377295.664,'rf':300.8017,'description':"Everest 1969"}, 
217  "evrstSS":      {'a':6377298.556,'rf':300.8017,'description':"Everest (Sabah & Sarawak)"}, 
218  "fschr60":      {'a':6378166.,'rf':298.3,'description':"Fischer (Mercury Datum) 1960"}, 
219  "fschr60m":     {'a':6378155.,'rf':298.3,'description':"Modified Fischer 1960"}, 
220  "fschr68":      {'a':6378150.,'rf':298.3,'description':"Fischer 1968"}, 
221  "helmert":      {'a':6378200.,'rf':298.3,'description':"Helmert 1906"}, 
222  "hough":        {'a':6378270.0,'rf':297.,'description':"Hough"}, 
223  "intl":         {'a':6378388.0,'rf':297.,'description':"International 1909 (Hayford)"}, 
224  "krass":        {'a':6378245.0,'rf':298.3,'description':"Krassovsky, 1942"}, 
225  "kaula":        {'a':6378163.,'rf':298.24,'description':"Kaula 1961"}, 
226  "lerch":        {'a':6378139.,'rf':298.257,'description':"Lerch 1979"}, 
227  "mprts":        {'a':6397300.,'rf':191.,'description':"Maupertius 1738"}, 
228  "new_intl":     {'a':6378157.5,'b':6356772.2,'description':"New International 1967"}, 
229  "plessis":      {'a':6376523.,'b':6355863.,'description':"Plessis 1817 (France)"}, 
230  "SEasia":       {'a':6378155.0,'b':6356773.3205,'description':"Southeast Asia"}, 
231  "walbeck":      {'a':6376896.0,'b':6355834.8467,'description':"Walbeck"}, 
232  "WGS60":        {'a':6378165.0,'rf':298.3,'description':"WGS 60"}, 
233  "WGS66":        {'a':6378145.0,'rf':298.25,'description':"WGS 66"}, 
234  "WGS72":        {'a':6378135.0,'rf':298.26,'description':"WGS 72"}, 
235  "WGS84":        {'a':6378137.0,'rf':298.257223563,'description':"WGS 84"}, 
236  "sphere":       {'a':6370997.0,'b':6370997.0,'description':"Normal Sphere"}, 
237  } 
238   
239  #if not os.path.isdir(pyproj_datadir): 
240  #    msg="proj data directory not found. Expecting it at: %s"%pyproj_datadir 
241  #    raise IOError(msg) 
242   
243  set_datapath(pyproj_datadir) 
244   
245 -class Proj(_proj.Proj):
246 """ 247 performs cartographic transformations (converts from 248 longitude,latitude to native map projection x,y coordinates and 249 vice versa) using proj (http://trac.osgeo.org/proj/). 250 251 A Proj class instance is initialized with proj map projection 252 control parameter key/value pairs. The key/value pairs can 253 either be passed in a dictionary, or as keyword arguments, 254 or as a proj4 string (compatible with the proj command). See 255 http://www.remotesensing.org/geotiff/proj_list for examples of 256 key/value pairs defining different map projections. 257 258 Calling a Proj class instance with the arguments lon, lat will 259 convert lon/lat (in degrees) to x/y native map projection 260 coordinates (in meters). If optional keyword 'inverse' is True 261 (default is False), the inverse transformation from x/y to 262 lon/lat is performed. If optional keyword 'radians' is True 263 (default is False) lon/lat are interpreted as radians instead of 264 degrees. If optional keyword 'errcheck' is True (default is 265 False) an exception is raised if the transformation is invalid. 266 If errcheck=False and the transformation is invalid, no 267 exception is raised and 1.e30 is returned. If the optional keyword 268 'preserve_units' is True, the units in map projection coordinates 269 are not forced to be meters. 270 271 Works with numpy and regular python array objects, python 272 sequences and scalars. 273 """ 274
275 - def __new__(self, projparams=None, preserve_units=False, **kwargs):
276 """ 277 initialize a Proj class instance. 278 279 Proj4 projection control parameters must either be given in a 280 dictionary 'projparams' or as keyword arguments. See the proj 281 documentation (http://trac.osgeo.org/proj/) for more information 282 about specifying projection parameters. 283 284 Example usage: 285 286 >>> from pyproj import Proj 287 >>> p = Proj(proj='utm',zone=10,ellps='WGS84') # use kwargs 288 >>> x,y = p(-120.108, 34.36116666) 289 >>> 'x=%9.3f y=%11.3f' % (x,y) 290 'x=765975.641 y=3805993.134' 291 >>> 'lon=%8.3f lat=%5.3f' % p(x,y,inverse=True) 292 'lon=-120.108 lat=34.361' 293 >>> # do 3 cities at a time in a tuple (Fresno, LA, SF) 294 >>> lons = (-119.72,-118.40,-122.38) 295 >>> lats = (36.77, 33.93, 37.62 ) 296 >>> x,y = p(lons, lats) 297 >>> 'x: %9.3f %9.3f %9.3f' % x 298 'x: 792763.863 925321.537 554714.301' 299 >>> 'y: %9.3f %9.3f %9.3f' % y 300 'y: 4074377.617 3763936.941 4163835.303' 301 >>> lons, lats = p(x, y, inverse=True) # inverse transform 302 >>> 'lons: %8.3f %8.3f %8.3f' % lons 303 'lons: -119.720 -118.400 -122.380' 304 >>> 'lats: %8.3f %8.3f %8.3f' % lats 305 'lats: 36.770 33.930 37.620' 306 >>> p2 = Proj('+proj=utm +zone=10 +ellps=WGS84') # use proj4 string 307 >>> x,y = p2(-120.108, 34.36116666) 308 >>> 'x=%9.3f y=%11.3f' % (x,y) 309 'x=765975.641 y=3805993.134' 310 >>> p = Proj(init="epsg:32667") 311 >>> 'x=%12.3f y=%12.3f (meters)' % p(-114.057222, 51.045) 312 'x=-1783486.760 y= 6193833.196 (meters)' 313 >>> p = Proj("+init=epsg:32667",preserve_units=True) 314 >>> 'x=%12.3f y=%12.3f (feet)' % p(-114.057222, 51.045) 315 'x=-5851322.810 y=20320934.409 (feet)' 316 """ 317 # if projparams is None, use kwargs. 318 if projparams is None: 319 if len(kwargs) == 0: 320 raise RuntimeError('no projection control parameters specified') 321 else: 322 projstring = _dict2string(kwargs) 323 elif type(projparams) == str: 324 # if projparams is a string, interpret as a proj4 init string. 325 projstring = projparams 326 else: # projparams a dict 327 projstring = _dict2string(projparams) 328 # make sure units are meters if preserve_units is False. 329 if not projstring.count('+units=') and not preserve_units: 330 projstring = '+units=m '+projstring 331 else: 332 kvpairs = [] 333 for kvpair in projstring.split(): 334 if kvpair.startswith('+units') and not preserve_units: 335 k,v = kvpair.split('=') 336 kvpairs.append(k+'=m ') 337 else: 338 kvpairs.append(kvpair+' ') 339 projstring = ''.join(kvpairs) 340 # look for EPSG, replace with epsg (EPSG only works 341 # on case-insensitive filesystems). 342 projstring = projstring.replace('EPSG','epsg') 343 return _proj.Proj.__new__(self, projstring)
344
345 - def __call__(self, *args, **kw):
346 #,lon,lat,inverse=False,radians=False,errcheck=False): 347 """ 348 Calling a Proj class instance with the arguments lon, lat will 349 convert lon/lat (in degrees) to x/y native map projection 350 coordinates (in meters). If optional keyword 'inverse' is True 351 (default is False), the inverse transformation from x/y to 352 lon/lat is performed. If optional keyword 'radians' is True 353 (default is False) the units of lon/lat are radians instead of 354 degrees. If optional keyword 'errcheck' is True (default is 355 False) an exception is raised if the transformation is invalid. 356 If errcheck=False and the transformation is invalid, no 357 exception is raised and 1.e30 is returned. 358 359 Instead of calling with lon, lat, a single ndarray of 360 shape n,2 may be used, and one of the same shape will 361 be returned; this is more efficient. 362 363 Inputs should be doubles (they will be cast to doubles if they 364 are not, causing a slight performance hit). 365 366 Works with numpy and regular python array objects, python 367 sequences and scalars, but is fastest for array objects. 368 """ 369 inverse = kw.get('inverse', False) 370 radians = kw.get('radians', False) 371 errcheck = kw.get('errcheck', False) 372 #if len(args) == 1: 373 # latlon = np.array(args[0], copy=True, 374 # order='C', dtype=float, ndmin=2) 375 # if inverse: 376 # _proj.Proj._invn(self, latlon, radians=radians, errcheck=errcheck) 377 # else: 378 # _proj.Proj._fwdn(self, latlon, radians=radians, errcheck=errcheck) 379 # return latlon 380 lon, lat = args 381 # process inputs, making copies that support buffer API. 382 inx, xisfloat, xislist, xistuple = _copytobuffer(lon) 383 iny, yisfloat, yislist, yistuple = _copytobuffer(lat) 384 # call proj4 functions. inx and iny modified in place. 385 if inverse: 386 _proj.Proj._inv(self, inx, iny, radians=radians, errcheck=errcheck) 387 else: 388 _proj.Proj._fwd(self, inx, iny, radians=radians, errcheck=errcheck) 389 # if inputs were lists, tuples or floats, convert back. 390 outx = _convertback(xisfloat,xislist,xistuple,inx) 391 outy = _convertback(yisfloat,yislist,xistuple,iny) 392 return outx, outy
393
394 - def to_latlong(self):
395 """returns an equivalent Proj in the corresponding lon/lat 396 coordinates. (see pj_latlong_from_proj() in the Proj.4 C API)""" 397 return _proj.Proj.to_latlong(self)
398
399 - def is_latlong(self):
400 """returns True if projection in geographic (lon/lat) coordinates""" 401 return _proj.Proj.is_latlong(self)
402
403 - def is_geocent(self):
404 """returns True if projection in geocentric (x/y) coordinates""" 405 return _proj.Proj.is_geocent(self)
406
407 -def transform(p1, p2, x, y, z=None, radians=False):
408 """ 409 x2, y2, z2 = transform(p1, p2, x1, y1, z1, radians=False) 410 411 Transform points between two coordinate systems defined by the 412 Proj instances p1 and p2. 413 414 The points x1,y1,z1 in the coordinate system defined by p1 are 415 transformed to x2,y2,z2 in the coordinate system defined by p2. 416 417 z1 is optional, if it is not set it is assumed to be zero (and 418 only x2 and y2 are returned). 419 420 In addition to converting between cartographic and geographic 421 projection coordinates, this function can take care of datum 422 shifts (which cannot be done using the __call__ method of the 423 Proj instances). It also allows for one of the coordinate 424 systems to be geographic (proj = 'latlong'). 425 426 If optional keyword 'radians' is True (default is False) and p1 427 is defined in geographic coordinate (pj.is_latlong() is True), 428 x1,y1 is interpreted as radians instead of the default degrees. 429 Similarly, if p2 is defined in geographic coordinates and 430 radians=True, x2, y2 are returned in radians instead of degrees. 431 if p1.is_latlong() and p2.is_latlong() both are False, the 432 radians keyword has no effect. 433 434 x,y and z can be numpy or regular python arrays, python 435 lists/tuples or scalars. Arrays are fastest. For projections in 436 geocentric coordinates, values of x and y are given in meters. 437 z is always meters. 438 439 Example usage: 440 441 >>> # projection 1: UTM zone 15, grs80 ellipse, NAD83 datum 442 >>> # (defined by epsg code 26915) 443 >>> p1 = Proj(init='epsg:26915') 444 >>> # projection 2: UTM zone 15, clrk66 ellipse, NAD27 datum 445 >>> p2 = Proj(init='epsg:26715') 446 >>> # find x,y of Jefferson City, MO. 447 >>> x1, y1 = p1(-92.199881,38.56694) 448 >>> # transform this point to projection 2 coordinates. 449 >>> x2, y2 = transform(p1,p2,x1,y1) 450 >>> '%9.3f %11.3f' % (x1,y1) 451 '569704.566 4269024.671' 452 >>> '%9.3f %11.3f' % (x2,y2) 453 '569722.342 4268814.027' 454 >>> '%8.3f %5.3f' % p2(x2,y2,inverse=True) 455 ' -92.200 38.567' 456 >>> # process 3 points at a time in a tuple 457 >>> lats = (38.83,39.32,38.75) # Columbia, KC and StL Missouri 458 >>> lons = (-92.22,-94.72,-90.37) 459 >>> x1, y1 = p1(lons,lats) 460 >>> x2, y2 = transform(p1,p2,x1,y1) 461 >>> xy = x1+y1 462 >>> '%9.3f %9.3f %9.3f %11.3f %11.3f %11.3f' % xy 463 '567703.344 351730.944 728553.093 4298200.739 4353698.725 4292319.005' 464 >>> xy = x2+y2 465 >>> '%9.3f %9.3f %9.3f %11.3f %11.3f %11.3f' % xy 466 '567721.149 351747.558 728569.133 4297989.112 4353489.644 4292106.305' 467 >>> lons, lats = p2(x2,y2,inverse=True) 468 >>> xy = lons+lats 469 >>> '%8.3f %8.3f %8.3f %5.3f %5.3f %5.3f' % xy 470 ' -92.220 -94.720 -90.370 38.830 39.320 38.750' 471 >>> # test datum shifting, installation of extra datum grid files. 472 >>> p1 = Proj(proj='latlong',datum='WGS84') 473 >>> x1 = -111.5; y1 = 45.25919444444 474 >>> p2 = Proj(proj="utm",zone=10,datum='NAD27') 475 >>> x2, y2 = transform(p1, p2, x1, y1) 476 >>> "%12.3f %12.3f" % (x2,y2) 477 ' 1402285.991 5076292.423' 478 """ 479 # process inputs, making copies that support buffer API. 480 inx, xisfloat, xislist, xistuple = _copytobuffer(x) 481 iny, yisfloat, yislist, yistuple = _copytobuffer(y) 482 if z is not None: 483 inz, zisfloat, zislist, zistuple = _copytobuffer(z) 484 else: 485 inz = None 486 # call pj_transform. inx,iny,inz buffers modified in place. 487 _proj._transform(p1,p2,inx,iny,inz,radians) 488 # if inputs were lists, tuples or floats, convert back. 489 outx = _convertback(xisfloat,xislist,xistuple,inx) 490 outy = _convertback(yisfloat,yislist,xistuple,iny) 491 if inz is not None: 492 outz = _convertback(zisfloat,zislist,zistuple,inz) 493 return outx, outy, outz 494 else: 495 return outx, outy
496
497 -def _copytobuffer_return_scalar(x):
498 try: 499 # inx,isfloat,islist,istuple 500 return array('d',(float(x),)),True,False,False 501 except: 502 raise TypeError('input must be an array, list, tuple or scalar')
503
504 -def _copytobuffer(x):
505 """ 506 return a copy of x as an object that supports the python Buffer 507 API (python array if input is float, list or tuple, numpy array 508 if input is a numpy array). returns copyofx, isfloat, islist, 509 istuple (islist is True if input is a list, istuple is true if 510 input is a tuple, isfloat is true if input is a float). 511 """ 512 # make sure x supports Buffer API and contains doubles. 513 isfloat = False; islist = False; istuple = False 514 # first, if it's a numpy array scalar convert to float 515 # (array scalars don't support buffer API) 516 if hasattr(x,'shape'): 517 if x.shape == (): 518 return _copytobuffer_return_scalar(x) 519 else: 520 try: 521 # typecast numpy arrays to double. 522 # (this makes a copy - which is crucial 523 # since buffer is modified in place) 524 x.dtype.char 525 inx = x.astype('d') 526 # inx,isfloat,islist,istuple 527 return inx,False,False,False 528 except: 529 try: # perhaps they are Numeric/numarrays? 530 # sorry, not tested yet. 531 # i don't know Numeric/numarrays has `shape'. 532 x.typecode() 533 inx = x.astype('d') 534 # inx,isfloat,islist,istuple 535 return inx,False,False,False 536 except: 537 raise TypeError('input must be an array, list, tuple or scalar') 538 else: 539 # perhaps they are regular python arrays? 540 if hasattr(x, 'typecode'): 541 #x.typecode 542 inx = array('d',x) 543 # try to convert to python array 544 # a list. 545 elif type(x) == list: 546 inx = array('d',x) 547 islist = True 548 # a tuple. 549 elif type(x) == tuple: 550 inx = array('d',x) 551 istuple = True 552 # a scalar? 553 else: 554 return _copytobuffer_return_scalar(x) 555 return inx,isfloat,islist,istuple
556
557 -def _convertback(isfloat,islist,istuple,inx):
558 # if inputs were lists, tuples or floats, convert back to original type. 559 if isfloat: 560 return inx[0] 561 elif islist: 562 return inx.tolist() 563 elif istuple: 564 return tuple(inx) 565 else: 566 return inx
567
568 -def _dict2string(projparams):
569 # convert a dict to a proj4 string. 570 pjargs = [] 571 for key,value in projparams.items(): 572 pjargs.append('+'+key+"="+str(value)+' ') 573 return ''.join(pjargs)
574
575 -class Geod(_proj.Geod):
576 """ 577 performs forward and inverse geodetic, or Great Circle, 578 computations. The forward computation (using the 'fwd' method) 579 involves determining latitude, longitude and back azimuth of a 580 computations. The forward computation (using the 'fwd' method) 581 involves determining latitude, longitude and back azimuth of a 582 terminus point given the latitude and longitude of an initial 583 point, plus azimuth and distance. The inverse computation (using 584 the 'inv' method) involves determining the forward and back 585 azimuths and distance given the latitudes and longitudes of an 586 initial and terminus point. 587 """
588 - def __new__(self, initstring=None, **kwargs):
589 """ 590 initialize a Geod class instance. 591 592 Geodetic parameters for specifying the ellipsoid 593 can be given in a dictionary 'initparams', as keyword arguments, 594 or as as proj4 geod initialization string. 595 Following is a list of the ellipsoids that may be defined using the 596 'ellps' keyword (these are stored in the model variable pj_ellps):: 597 598 MERIT a=6378137.0 rf=298.257 MERIT 1983 599 SGS85 a=6378136.0 rf=298.257 Soviet Geodetic System 85 600 GRS80 a=6378137.0 rf=298.257222101 GRS 1980(IUGG, 1980) 601 IAU76 a=6378140.0 rf=298.257 IAU 1976 602 airy a=6377563.396 b=6356256.910 Airy 1830 603 APL4.9 a=6378137.0. rf=298.25 Appl. Physics. 1965 604 airy a=6377563.396 b=6356256.910 Airy 1830 605 APL4.9 a=6378137.0. rf=298.25 Appl. Physics. 1965 606 NWL9D a=6378145.0. rf=298.25 Naval Weapons Lab., 1965 607 mod_airy a=6377340.189 b=6356034.446 Modified Airy 608 andrae a=6377104.43 rf=300.0 Andrae 1876 (Den., Iclnd.) 609 aust_SA a=6378160.0 rf=298.25 Australian Natl & S. Amer. 1969 610 GRS67 a=6378160.0 rf=298.247167427 GRS 67(IUGG 1967) 611 bessel a=6377397.155 rf=299.1528128 Bessel 1841 612 bess_nam a=6377483.865 rf=299.1528128 Bessel 1841 (Namibia) 613 clrk66 a=6378206.4 b=6356583.8 Clarke 1866 614 clrk80 a=6378249.145 rf=293.4663 Clarke 1880 mod. 615 CPM a=6375738.7 rf=334.29 Comm. des Poids et Mesures 1799 616 delmbr a=6376428. rf=311.5 Delambre 1810 (Belgium) 617 engelis a=6378136.05 rf=298.2566 Engelis 1985 618 evrst30 a=6377276.345 rf=300.8017 Everest 1830 619 evrst48 a=6377304.063 rf=300.8017 Everest 1948 620 evrst56 a=6377301.243 rf=300.8017 Everest 1956 621 evrst69 a=6377295.664 rf=300.8017 Everest 1969 622 evrstSS a=6377298.556 rf=300.8017 Everest (Sabah & Sarawak) 623 fschr60 a=6378166. rf=298.3 Fischer (Mercury Datum) 1960 624 fschr60m a=6378155. rf=298.3 Modified Fischer 1960 625 fschr68 a=6378150. rf=298.3 Fischer 1968 626 helmert a=6378200. rf=298.3 Helmert 1906 627 hough a=6378270.0 rf=297. Hough 628 helmert a=6378200. rf=298.3 Helmert 1906 629 hough a=6378270.0 rf=297. Hough 630 intl a=6378388.0 rf=297. International 1909 (Hayford) 631 krass a=6378245.0 rf=298.3 Krassovsky, 1942 632 kaula a=6378163. rf=298.24 Kaula 1961 633 lerch a=6378139. rf=298.257 Lerch 1979 634 mprts a=6397300. rf=191. Maupertius 1738 635 new_intl a=6378157.5 b=6356772.2 New International 1967 636 plessis a=6376523. b=6355863. Plessis 1817 (France) 637 SEasia a=6378155.0 b=6356773.3205 Southeast Asia 638 walbeck a=6376896.0 b=6355834.8467 Walbeck 639 WGS60 a=6378165.0 rf=298.3 WGS 60 640 WGS66 a=6378145.0 rf=298.25 WGS 66 641 WGS72 a=6378135.0 rf=298.26 WGS 72 642 WGS84 a=6378137.0 rf=298.257223563 WGS 84 643 sphere a=6370997.0 b=6370997.0 Normal Sphere (r=6370997) 644 645 The parameters of the ellipsoid may also be set directly using 646 the 'a' (semi-major or equatorial axis radius) keyword, and 647 any one of the following keywords: 'b' (semi-minor, 648 or polar axis radius), 'e' (eccentricity), 'es' (eccentricity 649 squared), 'f' (flattening), or 'rf' (reciprocal flattening). 650 651 See the proj documentation (http://trac.osgeo.org/proj/) for more 652 653 See the proj documentation (http://trac.osgeo.org/proj/) for more 654 information about specifying ellipsoid parameters (specifically, 655 the chapter 'Specifying the Earth's figure' in the main Proj 656 users manual). 657 658 Example usage: 659 660 >>> from pyproj import Geod 661 >>> g = Geod(ellps='clrk66') # Use Clarke 1966 ellipsoid. 662 >>> # specify the lat/lons of some cities. 663 >>> boston_lat = 42.+(15./60.); boston_lon = -71.-(7./60.) 664 >>> portland_lat = 45.+(31./60.); portland_lon = -123.-(41./60.) 665 >>> newyork_lat = 40.+(47./60.); newyork_lon = -73.-(58./60.) 666 >>> london_lat = 51.+(32./60.); london_lon = -(5./60.) 667 >>> # compute forward and back azimuths, plus distance 668 >>> # between Boston and Portland. 669 >>> az12,az21,dist = g.inv(boston_lon,boston_lat,portland_lon,portland_lat) 670 >>> "%7.3f %6.3f %12.3f" % (az12,az21,dist) 671 '-66.531 75.654 4164192.708' 672 >>> # compute latitude, longitude and back azimuth of Portland, 673 >>> # given Boston lat/lon, forward azimuth and distance to Portland. 674 >>> endlon, endlat, backaz = g.fwd(boston_lon, boston_lat, az12, dist) 675 >>> "%6.3f %6.3f %13.3f" % (endlat,endlon,backaz) 676 '45.517 -123.683 75.654' 677 >>> # compute the azimuths, distances from New York to several 678 >>> # cities (pass a list) 679 >>> lons1 = 3*[newyork_lon]; lats1 = 3*[newyork_lat] 680 >>> lons2 = [boston_lon, portland_lon, london_lon] 681 >>> lats2 = [boston_lat, portland_lat, london_lat] 682 >>> az12,az21,dist = g.inv(lons1,lats1,lons2,lats2) 683 >>> for faz,baz,d in list(zip(az12,az21,dist)): "%7.3f %7.3f %9.3f" % (faz,baz,d) 684 ' 54.663 -123.448 288303.720' 685 '-65.463 79.342 4013037.318' 686 ' 51.254 -71.576 5579916.651' 687 >>> g2 = Geod('+ellps=clrk66') # use proj4 style initialization string 688 >>> az12,az21,dist = g2.inv(boston_lon,boston_lat,portland_lon,portland_lat) 689 >>> "%7.3f %6.3f %12.3f" % (az12,az21,dist) 690 '-66.531 75.654 4164192.708' 691 """ 692 # if initparams is a proj-type init string, 693 # convert to dict. 694 ellpsd = {} 695 if initstring is not None: 696 for kvpair in initstring.split(): 697 k,v = kvpair.split('=') 698 k = k.lstrip('+') 699 if k in ['a','b','rf','f','es','e']: 700 v = float(v) 701 ellpsd[k] = v 702 # merge this dict with kwargs dict. 703 kwargs = dict(list(kwargs.items()) + list(ellpsd.items())) 704 self.sphere = False 705 if 'ellps' in kwargs: 706 # ellipse name given, look up in pj_ellps dict 707 ellps_dict = pj_ellps[kwargs['ellps']] 708 a = ellps_dict['a'] 709 if ellps_dict['description']=='Normal Sphere': 710 self.sphere = True 711 if 'b' in ellps_dict: 712 b = ellps_dict['b'] 713 es = 1. - (b * b) / (a * a) 714 f = (a - b)/a 715 elif 'rf' in ellps_dict: 716 f = 1./ellps_dict['rf'] 717 b = a*(1. - f) 718 es = 1. - (b * b) / (a * a) 719 else: 720 # a (semi-major axis) and one of 721 # b the semi-minor axis 722 # rf the reciprocal flattening 723 # f flattening 724 # es eccentricity squared 725 # must be given. 726 a = kwargs['a'] 727 if 'b' in kwargs: 728 b = kwargs['b'] 729 es = 1. - (b * b) / (a * a) 730 f = (a - b)/a 731 elif 'rf' in kwargs: 732 f = 1./kwargs['rf'] 733 b = a*(1. - f) 734 es = 1. - (b * b) / (a * a) 735 elif 'f' in kwargs: 736 f = kwargs['f'] 737 b = a*(1. - f) 738 es = 1. - (b/a)**2 739 elif 'es' in kwargs: 740 es = kwargs['es'] 741 b = math.sqrt(a**2 - es*a**2) 742 f = (a - b)/a 743 elif 'e' in kwargs: 744 es = kwargs['e']**2 745 b = math.sqrt(a**2 - es*a**2) 746 f = (a - b)/a 747 else: 748 b = a 749 f = 0. 750 es = 0. 751 #msg='ellipse name or a, plus one of f,es,b must be given' 752 #raise ValueError(msg) 753 if math.fabs(f) < 1.e-8: self.sphere = True 754 self.a = a 755 self.b = b 756 self.f = f 757 self.es = es 758 return _proj.Geod.__new__(self, a, f)
759
760 - def fwd(self, lons, lats, az, dist, radians=False):
761 """ 762 forward transformation - Returns longitudes, latitudes and back 763 azimuths of terminus points given longitudes (lons) and 764 latitudes (lats) of initial points, plus forward azimuths (az) 765 and distances (dist). 766 latitudes (lats) of initial points, plus forward azimuths (az) 767 and distances (dist). 768 769 Works with numpy and regular python array objects, python 770 sequences and scalars. 771 772 if radians=True, lons/lats and azimuths are radians instead of 773 degrees. Distances are in meters. 774 """ 775 # process inputs, making copies that support buffer API. 776 inx, xisfloat, xislist, xistuple = _copytobuffer(lons) 777 iny, yisfloat, yislist, yistuple = _copytobuffer(lats) 778 inz, zisfloat, zislist, zistuple = _copytobuffer(az) 779 ind, disfloat, dislist, distuple = _copytobuffer(dist) 780 _proj.Geod._fwd(self, inx, iny, inz, ind, radians=radians) 781 # if inputs were lists, tuples or floats, convert back. 782 outx = _convertback(xisfloat,xislist,xistuple,inx) 783 outy = _convertback(yisfloat,yislist,xistuple,iny) 784 outz = _convertback(zisfloat,zislist,zistuple,inz) 785 return outx, outy, outz
786
787 - def inv(self,lons1,lats1,lons2,lats2,radians=False):
788 """ 789 inverse transformation - Returns forward and back azimuths, plus 790 distances between initial points (specified by lons1, lats1) and 791 terminus points (specified by lons2, lats2). 792 793 Works with numpy and regular python array objects, python 794 sequences and scalars. 795 796 if radians=True, lons/lats and azimuths are radians instead of 797 degrees. Distances are in meters. 798 """ 799 # process inputs, making copies that support buffer API. 800 inx, xisfloat, xislist, xistuple = _copytobuffer(lons1) 801 iny, yisfloat, yislist, yistuple = _copytobuffer(lats1) 802 inz, zisfloat, zislist, zistuple = _copytobuffer(lons2) 803 ind, disfloat, dislist, distuple = _copytobuffer(lats2) 804 _proj.Geod._inv(self,inx,iny,inz,ind,radians=radians) 805 # if inputs were lists, tuples or floats, convert back. 806 outx = _convertback(xisfloat,xislist,xistuple,inx) 807 outy = _convertback(yisfloat,yislist,xistuple,iny) 808 outz = _convertback(zisfloat,zislist,zistuple,inz) 809 return outx, outy, outz
810
811 - def npts(self, lon1, lat1, lon2, lat2, npts, radians=False):
812 """ 813 Given a single initial point and terminus point (specified by 814 python floats lon1,lat1 and lon2,lat2), returns a list of 815 longitude/latitude pairs describing npts equally spaced 816 intermediate points along the geodesic between the initial and 817 terminus points. 818 819 if radians=True, lons/lats are radians instead of degrees. 820 821 Example usage: 822 823 >>> from pyproj import Geod 824 >>> g = Geod(ellps='clrk66') # Use Clarke 1966 ellipsoid. 825 >>> # specify the lat/lons of Boston and Portland. 826 >>> g = Geod(ellps='clrk66') # Use Clarke 1966 ellipsoid. 827 >>> # specify the lat/lons of Boston and Portland. 828 >>> boston_lat = 42.+(15./60.); boston_lon = -71.-(7./60.) 829 >>> portland_lat = 45.+(31./60.); portland_lon = -123.-(41./60.) 830 >>> # find ten equally spaced points between Boston and Portland. 831 >>> lonlats = g.npts(boston_lon,boston_lat,portland_lon,portland_lat,10) 832 >>> for lon,lat in lonlats: '%6.3f %7.3f' % (lat, lon) 833 '43.528 -75.414' 834 '44.637 -79.883' 835 '45.565 -84.512' 836 '46.299 -89.279' 837 '46.830 -94.156' 838 '47.149 -99.112' 839 '47.251 -104.106' 840 '47.136 -109.100' 841 '46.805 -114.051' 842 '46.262 -118.924' 843 >>> # test with radians=True (inputs/outputs in radians, not degrees) 844 >>> import math 845 >>> dg2rad = math.radians(1.) 846 >>> rad2dg = math.degrees(1.) 847 >>> lonlats = g.npts(dg2rad*boston_lon,dg2rad*boston_lat,dg2rad*portland_lon,dg2rad*portland_lat,10,radians=True) 848 >>> for lon,lat in lonlats: '%6.3f %7.3f' % (rad2dg*lat, rad2dg*lon) 849 '43.528 -75.414' 850 '44.637 -79.883' 851 '45.565 -84.512' 852 '46.299 -89.279' 853 '46.830 -94.156' 854 '47.149 -99.112' 855 '47.251 -104.106' 856 '47.136 -109.100' 857 '46.805 -114.051' 858 '46.262 -118.924' 859 """ 860 lons, lats = _proj.Geod._npts(self, lon1, lat1, lon2, lat2, npts, radians=radians) 861 return list(zip(lons, lats))
862
863 -def test():
864 """run the examples in the docstrings using the doctest module""" 865 import doctest, pyproj 866 doctest.testmod(pyproj,verbose=True)
867 868 if __name__ == "__main__": test() 869