Proj¶
pyproj.Proj¶
-
class
pyproj.
Proj
(projparams=None, preserve_units=True, **kwargs)[source]¶ Bases:
pyproj._proj.Proj
performs cartographic transformations (converts from longitude,latitude to native map projection x,y coordinates and vice versa) using proj (https://github.com/OSGeo/proj.4/wiki).
A Proj class instance is initialized with proj map projection control parameter key/value pairs. The key/value pairs can either be passed in a dictionary, or as keyword arguments, or as a proj4 string (compatible with the proj command). See http://www.remotesensing.org/geotiff/proj_list for examples of key/value pairs defining different map projections.
Calling a Proj class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y native map projection coordinates (in meters). If optional keyword ‘inverse’ is True (default is False), the inverse transformation from x/y to lon/lat is performed. If optional keyword ‘errcheck’ is True (default is False) an exception is raised if the transformation is invalid. If errcheck=False and the transformation is invalid, no exception is raised and 1.e30 is returned. If the optional keyword ‘preserve_units’ is True, the units in map projection coordinates are not forced to be meters.
Works with numpy and regular python array objects, python sequences and scalars.
-
__call__
(*args, **kw)[source]¶ Calling a Proj class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y native map projection coordinates (in meters). If optional keyword ‘inverse’ is True (default is False), the inverse transformation from x/y to lon/lat is performed. If optional keyword ‘errcheck’ is True (default is False) an exception is raised if the transformation is invalid. If errcheck=False and the transformation is invalid, no exception is raised and 1.e30 is returned.
Inputs should be doubles (they will be cast to doubles if they are not, causing a slight performance hit).
Works with numpy and regular python array objects, python sequences and scalars, but is fastest for array objects.
-
__init__
(projparams=None, preserve_units=True, **kwargs)[source]¶ initialize a Proj class instance.
See the proj documentation (https://github.com/OSGeo/proj.4/wiki) for more information about projection parameters.
- Parameters
projparams (int, str, dict, pyproj.CRS) – A proj.4 or WKT string, proj.4 dict, EPSG integer, or a pyproj.CRS instnace.
preserve_units (bool) – If false, will ensure +units=m.
**kwargs – proj.4 projection parameters.
Example usage:
>>> from pyproj import Proj >>> p = Proj(proj='utm',zone=10,ellps='WGS84', preserve_units=False) # use kwargs >>> x,y = p(-120.108, 34.36116666) >>> 'x=%9.3f y=%11.3f' % (x,y) 'x=765975.641 y=3805993.134' >>> 'lon=%8.3f lat=%5.3f' % p(x,y,inverse=True) 'lon=-120.108 lat=34.361' >>> # do 3 cities at a time in a tuple (Fresno, LA, SF) >>> lons = (-119.72,-118.40,-122.38) >>> lats = (36.77, 33.93, 37.62 ) >>> x,y = p(lons, lats) >>> 'x: %9.3f %9.3f %9.3f' % x 'x: 792763.863 925321.537 554714.301' >>> 'y: %9.3f %9.3f %9.3f' % y 'y: 4074377.617 3763936.941 4163835.303' >>> lons, lats = p(x, y, inverse=True) # inverse transform >>> 'lons: %8.3f %8.3f %8.3f' % lons 'lons: -119.720 -118.400 -122.380' >>> 'lats: %8.3f %8.3f %8.3f' % lats 'lats: 36.770 33.930 37.620' >>> p2 = Proj('+proj=utm +zone=10 +ellps=WGS84', preserve_units=False) # use proj4 string >>> x,y = p2(-120.108, 34.36116666) >>> 'x=%9.3f y=%11.3f' % (x,y) 'x=765975.641 y=3805993.134' >>> p = Proj(init="epsg:32667", preserve_units=False) >>> 'x=%12.3f y=%12.3f (meters)' % p(-114.057222, 51.045) 'x=-1783506.250 y= 6193827.033 (meters)' >>> p = Proj("+init=epsg:32667") >>> 'x=%12.3f y=%12.3f (feet)' % p(-114.057222, 51.045) 'x=-5851386.754 y=20320914.191 (feet)' >>> # test data with radian inputs >>> p1 = Proj(init="epsg:4214") >>> x1, y1 = p1(116.366, 39.867) >>> '{:.3f} {:.3f}'.format(x1, y1) '2.031 0.696' >>> x2, y2 = p1(x1, y1, inverse=True) >>> '{:.3f} {:.3f}'.format(x2, y2) '116.366 39.867'
-
definition_string
()[source]¶ Returns formal definition string for projection
>>> Proj('+init=epsg:4326').definition_string() 'proj=longlat datum=WGS84 no_defs ellps=WGS84 towgs84=0,0,0' >>>
-
has_inverse
¶ Returns true if this projection has an inverse
-
is_latlong
()[source]¶ - Returns
bool
- Return type
True if projection in geographic (lon/lat) coordinates.
-
pyproj.transform¶
-
pyproj.
transform
(p1, p2, x, y, z=None, radians=False)[source]¶ x2, y2, z2 = transform(p1, p2, x1, y1, z1)
Transform points between two coordinate systems defined by the Proj instances p1 and p2.
The points x1,y1,z1 in the coordinate system defined by p1 are transformed to x2,y2,z2 in the coordinate system defined by p2.
z1 is optional, if it is not set it is assumed to be zero (and only x2 and y2 are returned). If the optional keyword ‘radians’ is True (default is False), then all input and output coordinates will be in radians instead of the default of degrees for geographic input/output projections.
In addition to converting between cartographic and geographic projection coordinates, this function can take care of datum shifts (which cannot be done using the __call__ method of the Proj instances). It also allows for one of the coordinate systems to be geographic (proj = ‘latlong’).
x,y and z can be numpy or regular python arrays, python lists/tuples or scalars. Arrays are fastest. For projections in geocentric coordinates, values of x and y are given in meters. z is always meters.
Example usage:
>>> # projection 1: UTM zone 15, grs80 ellipse, NAD83 datum >>> # (defined by epsg code 26915) >>> p1 = Proj(init='epsg:26915', preserve_units=False) >>> # projection 2: UTM zone 15, clrk66 ellipse, NAD27 datum >>> p2 = Proj(init='epsg:26715', preserve_units=False) >>> # find x,y of Jefferson City, MO. >>> x1, y1 = p1(-92.199881,38.56694) >>> # transform this point to projection 2 coordinates. >>> x2, y2 = transform(p1,p2,x1,y1) >>> '%9.3f %11.3f' % (x1,y1) '569704.566 4269024.671' >>> '%9.3f %11.3f' % (x2,y2) '569722.342 4268814.028' >>> '%8.3f %5.3f' % p2(x2,y2,inverse=True) ' -92.200 38.567' >>> # process 3 points at a time in a tuple >>> lats = (38.83,39.32,38.75) # Columbia, KC and StL Missouri >>> lons = (-92.22,-94.72,-90.37) >>> x1, y1 = p1(lons,lats) >>> x2, y2 = transform(p1,p2,x1,y1) >>> xy = x1+y1 >>> '%9.3f %9.3f %9.3f %11.3f %11.3f %11.3f' % xy '567703.344 351730.944 728553.093 4298200.739 4353698.725 4292319.005' >>> xy = x2+y2 >>> '%9.3f %9.3f %9.3f %11.3f %11.3f %11.3f' % xy '567721.149 351747.558 728569.133 4297989.112 4353489.645 4292106.305' >>> lons, lats = p2(x2,y2,inverse=True) >>> xy = lons+lats >>> '%8.3f %8.3f %8.3f %5.3f %5.3f %5.3f' % xy ' -92.220 -94.720 -90.370 38.830 39.320 38.750' >>> # test datum shifting, installation of extra datum grid files. >>> p1 = Proj(proj='latlong',datum='WGS84') >>> x1 = -111.5; y1 = 45.25919444444 >>> p2 = Proj(proj="utm",zone=10,datum='NAD27', preserve_units=False) >>> x2, y2 = transform(p1, p2, x1, y1) >>> "%s %s" % (str(x2)[:9],str(y2)[:9]) '1402285.9 5076292.4' >>> from pyproj import CRS >>> c1 = CRS(proj='latlong',datum='WGS84') >>> x1 = -111.5; y1 = 45.25919444444 >>> c2 = CRS(proj="utm",zone=10,datum='NAD27') >>> x2, y2 = transform(c1, c2, x1, y1) >>> "%s %s" % (str(x2)[:9],str(y2)[:9]) '1402291.0 5076289.5' >>> x3, y3 = transform("epsg:4326", "epsg:3857", 33, 98) >>> "%.3f %.3f" % (x3, y3) '10909310.098 3895303.963' >>> pj = Proj(init="epsg:4214") >>> pjx, pjy = pj(116.366, 39.867) >>> xr, yr = transform(pj, Proj(4326), pjx, pjy, radians=True) >>> "%.3f %.3f" % (xr, yr) '2.031 0.696'
pyproj.itransform¶
-
pyproj.
itransform
(p1, p2, points, switch=False, radians=False)[source]¶ points2 = transform(p1, p2, points1) Iterator/generator version of the function pyproj.transform. Transform points between two coordinate systems defined by the Proj instances p1 and p2. This function can be used as an alternative to pyproj.transform when there is a need to transform a big number of coordinates lazily, for example when reading and processing from a file. Points1 is an iterable/generator of coordinates x1,y1(,z1) or lon1,lat1(,z1) in the coordinate system defined by p1. Points2 is an iterator that returns tuples of x2,y2(,z2) or lon2,lat2(,z2) coordinates in the coordinate system defined by p2. z are provided optionally.
- Points1 can be:
a tuple/list of tuples/lists i.e. for 2d points: [(xi,yi),(xj,yj),….(xn,yn)]
a Nx3 or Nx2 2d numpy array where N is the point number
a generator of coordinates (xi,yi) for 2d points or (xi,yi,zi) for 3d
If optional keyword ‘switch’ is True (default is False) then x, y or lon,lat coordinates of points are switched to y, x or lat, lon. If the optional keyword ‘radians’ is True (default is False), then all input and output coordinates will be in radians instead of the default of degrees for geographic input/output projections.
Example usage:
>>> # projection 1: WGS84 >>> # (defined by epsg code 4326) >>> p1 = Proj(init='epsg:4326', preserve_units=False) >>> # projection 2: GGRS87 / Greek Grid >>> p2 = Proj(init='epsg:2100', preserve_units=False) >>> # Three points with coordinates lon, lat in p1 >>> points = [(22.95, 40.63), (22.81, 40.53), (23.51, 40.86)] >>> # transform this point to projection 2 coordinates. >>> for pt in itransform(p1,p2,points): '%6.3f %7.3f' % pt '411200.657 4498214.742' '399210.500 4487264.963' '458703.102 4523331.451' >>> for pt in itransform(4326, 2100, points): '{:.3f} {:.3f}'.format(*pt) '2221638.801 2637034.372' '2212924.125 2619851.898' '2238294.779 2703763.736' >>> pj = Proj(init="epsg:4214") >>> pjx, pjy = pj(116.366, 39.867) >>> for pt in itransform(pj, Proj(4326), [(pjx, pjy)]): '{:.3f} {:.3f}'.format(*pt) '2.031 0.696'