Menu

[r1715]: / trunk / toolkits / basemap / src / proj4.pyx  Maximize  Restore  History

Download this file

216 lines (200 with data), 6.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""Pyrex code to provide python interfaces to PROJ.4 functions.
Make changes to this file, not the c-wrappers that Pyrex generates."""
import math
cdef double _rad2dg, _dg2rad
_dg2rad = math.radians(1.)
_rad2dg = math.degrees(1.)
cdef extern from "proj_api.h":
ctypedef double *projPJ
ctypedef struct projUV:
double u
double v
projPJ pj_init_plus(char *)
projUV pj_fwd(projUV, projPJ)
projUV pj_inv(projUV, projPJ)
# get 32 bit integer type
cdef extern from "inttypes.h":
ctypedef long int32_t
# Functions from numarray API
cdef extern from "numarray/libnumarray.h":
ctypedef int32_t maybelong
# numarray types.
ctypedef enum NumarrayType:
tAny
tBool
tInt8
tUInt8
tInt16
tUInt16
tInt32
tUInt32
tInt64
tUInt64
tFloat32
tFloat64
tComplex32
tComplex64
tObject
tDefault
tLong
cdef struct PyArray_Descr:
int type_num # PyArray_TYPES
int elsize # bytes for 1 element
char type # One of "cb1silfdFD " Object array not supported
# function pointers omitted
ctypedef class numarray._numarray._numarray [object PyArrayObject]:
cdef char *data
cdef int nd
cdef maybelong *dimensions
cdef maybelong *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags
cdef maybelong *_dimensions
cdef maybelong *_strides
# creates a new numeric object from a data pointer.
_numarray NA_vNewArray(void *, NumarrayType, int, maybelong *)
# The numarray initialization funtion
double import_libnumarray()
# this function must be called to initialize the numarray C API.
import_libnumarray()
cdef class Proj:
cdef char *pjinitstring
cdef double *projpj
cdef object projparams
def __new__(self, projparams):
"""
initialize a Proj class instance.
Input 'projparams' is a dictionary containing proj map
projection control parameter key/value pairs.
See the proj documentation (http://proj.maptools.org) for details.
"""
cdef double *projpj
# set units to meters.
if not projparams.has_key('units'):
projparams['units']='m'
elif projparams['units'] != 'm':
print 'resetting units to meters ...'
projparams['units']='m'
# make sure proj parameter specified.
# (no other checking done in proj parameters)
if 'proj' not in projparams.keys():
raise KeyError, "need to specify proj parameter"
pjargs = []
for key,value in projparams.iteritems():
pjargs.append('+'+key+"="+str(value)+' ')
pjinitstring = ''.join(pjargs)
self.projparams = projparams
projpj = pj_init_plus(pjinitstring)
self.projpj = projpj
def __reduce__(self):
"""special method that allows projlib.Proj instance to be pickled"""
return (self.__class__,(self.projparams,))
def fwd_array(self, _numarray lons, _numarray lats):
"""
forward transformation - lons,lats to x,y.
x, y, lons, lats are numarrays.
"""
cdef projUV projxyout, projlonlatin
cdef long ndim, i
cdef double u, v
cdef double *lonsdata, *latsdata
ndim = 1
for i from 0 <= i < lons.nd:
ndim = ndim*lons.dimensions[i]
lonsdata = <double *>lons.data
latsdata = <double *>lats.data
for i from 0 <= i < ndim:
projlonlatin.u = _dg2rad*lonsdata[i]
projlonlatin.v = _dg2rad*latsdata[i]
projxyout = pj_fwd(projlonlatin,self.projpj)
lonsdata[i] = projxyout.u
latsdata[i] = projxyout.v
x = NA_vNewArray(<void *>lonsdata, tFloat64, lons.nd, lons.dimensions)
y = NA_vNewArray(<void *>latsdata, tFloat64, lats.nd, lats.dimensions)
return x,y
def fwd(self, lons, lats):
"""
forward transformation - lons,lats to x,y.
x, y, lons, lats can be scalars, sequences or numarrays.
"""
cdef projUV projxyout, projlonlatin
cdef double u, v
try:
shape = lons.shape
isnumarray = True
except:
isnumarray = False
if isnumarray:
x,y = self.fwd_array(lons,lats)
else:
try: # inputs are lists
ndim = len(lons)
x = []; y = []
for i from 0 <= i < ndim:
projlonlatin.u = _dg2rad*lons[i]
projlonlatin.v = _dg2rad*lats[i]
projxyout = pj_fwd(projlonlatin,self.projpj)
x.append(projxyout.u)
y.append(projxyout.v)
except: # inputs are scalars
projlonlatin.u = lons*_dg2rad
projlonlatin.v = lats*_dg2rad
projxyout = pj_fwd(projlonlatin,self.projpj)
x = projxyout.u
y = projxyout.v
return x,y
def inv_array(self, _numarray x, _numarray y):
"""
inverse transformation - x,y to lons,lats
x, y, lons, lats are numarrays.
"""
cdef projUV projxyin, projlonlatout
cdef long ndim, i
cdef double u, v
cdef double *xdata, *ydata
ndim = 1
for i from 0 <= i < x.nd:
ndim = ndim*x.dimensions[i]
xdata = <double *>x.data
ydata = <double *>y.data
for i from 0 <= i < ndim:
projxyin.u = xdata[i]
projxyin.v = ydata[i]
projlonlatout = pj_inv(projxyin,self.projpj)
xdata[i] = _rad2dg*projlonlatout.u
ydata[i] = _rad2dg*projlonlatout.v
lons = NA_vNewArray(<void *>xdata, tFloat64, x.nd, x.dimensions)
lats = NA_vNewArray(<void *>ydata, tFloat64, y.nd, y.dimensions)
return lons, lats
def inv(self, x, y):
"""
inverse transformation - x,y to lons,lats
x, y, lons, lats can be scalars, sequences or numarrays.
"""
cdef projUV projxyin, projlonlatout
cdef double u, v
try:
shape = x.shape
isnumarray = True
except:
isnumarray = False
if isnumarray:
lons, lats = self.inv_array(x,y)
else:
try: # inputs are lists
ndim = len(x)
lons = []; lats = []
for i from 0 <= i < ndim:
projxyin.u = x[i]
projxyin.v = y[i]
projlonlatout = pj_inv(projxyin, self.projpj)
lons.append(projlonlatout.u*_rad2dg)
lats.append(projlonlatout.v*_rad2dg)
except: # inputs are scalars.
projxyin.u = x
projxyin.v = y
projlonlatout = pj_inv(projxyin, self.projpj)
lons = projlonlatout.u*_rad2dg
lats = projlonlatout.v*_rad2dg
return lons,lats
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.