|
1 | 1 | include "base.pxi"
|
2 | 2 |
|
| 3 | +import copy |
3 | 4 | import warnings
|
| 5 | +from collections import namedtuple |
4 | 6 |
|
5 | 7 | cimport cython
|
6 | 8 |
|
7 | 9 | from pyproj._datadir cimport pyproj_context_initialize
|
8 | 10 | from pyproj.compat import cstrencode, pystrdecode
|
9 | 11 | from pyproj.exceptions import ProjError
|
10 | 12 |
|
11 |
| -# # version number string for PROJ |
| 13 | +# version number string for PROJ |
12 | 14 | proj_version_str = "{0}.{1}.{2}".format(
|
13 | 15 | PROJ_VERSION_MAJOR,
|
14 | 16 | PROJ_VERSION_MINOR,
|
15 | 17 | PROJ_VERSION_PATCH
|
16 | 18 | )
|
17 | 19 |
|
18 |
| -cdef class Proj: |
| 20 | + |
| 21 | +Factors = namedtuple("Factors", |
| 22 | + [ |
| 23 | + "meridional_scale", |
| 24 | + "parallel_scale", |
| 25 | + "areal_scale", |
| 26 | + "angular_distortion", |
| 27 | + "meridian_parallel_angle", |
| 28 | + "meridian_convergence", |
| 29 | + "tissot_semimajor", |
| 30 | + "tissot_semiminor", |
| 31 | + "dx_dlam", |
| 32 | + "dx_dphi", |
| 33 | + "dy_dlam", |
| 34 | + "dy_dphi", |
| 35 | + ] |
| 36 | +) |
| 37 | +Factors.__doc__ = """ |
| 38 | +.. versionadded:: 2.6.0 |
| 39 | +
|
| 40 | +These are the scaling and angular distortion factors. |
| 41 | +
|
| 42 | +See `PJ_FACTORS documentation <https://proj.org/development/reference/datatypes.html?highlight=pj_factors#c.PJ_FACTORS>`__ |
| 43 | +
|
| 44 | +Parameters |
| 45 | +---------- |
| 46 | +meridional_scale: List[float] |
| 47 | + Meridional scale at coordinate. |
| 48 | +parallel_scale: List[float] |
| 49 | + Parallel scale at coordinate. |
| 50 | +areal_scale: List[float] |
| 51 | + Areal scale factor at coordinate. |
| 52 | +angular_distortion: List[float] |
| 53 | + Angular distortion at coordinate. |
| 54 | +meridian_parallel_angle: List[float] |
| 55 | + Meridian/parallel angle at coordinate. |
| 56 | +meridian_convergence: List[float] |
| 57 | + Meridian convergence at coordinate. Sometimes also described as *grid declination*. |
| 58 | +tissot_semimajor: List[float] |
| 59 | + Maximum scale factor. |
| 60 | +tissot_semiminor: List[float] |
| 61 | + Minimum scale factor. |
| 62 | +dx_dlam: List[float] |
| 63 | + Partial derivative of coordinate. |
| 64 | +dx_dphi: List[float] |
| 65 | + Partial derivative of coordinate. |
| 66 | +dy_dlam: List[float] |
| 67 | + Partial derivative of coordinate. |
| 68 | +dy_dphi: List[float] |
| 69 | + Partial derivative of coordinate. |
| 70 | +""" |
| 71 | + |
| 72 | + |
| 73 | +cdef class _Proj: |
19 | 74 | def __cinit__(self):
|
20 | 75 | self.projobj = NULL
|
21 | 76 | self.context = NULL
|
@@ -187,19 +242,134 @@ cdef class Proj:
|
187 | 242 | ybuff.data[iii] = projlonlatout.uv.v
|
188 | 243 | ProjError.clear()
|
189 | 244 |
|
| 245 | + @cython.boundscheck(False) |
| 246 | + @cython.wraparound(False) |
| 247 | + def _get_factors(self, longitude, latitude, bint radians, bint errcheck): |
| 248 | + """ |
| 249 | + Calculates the projection factors PJ_FACTORS |
| 250 | +
|
| 251 | + Equivalent to `proj -S` command line. |
| 252 | + """ |
| 253 | + cdef PyBuffWriteManager lonbuff = PyBuffWriteManager(longitude) |
| 254 | + cdef PyBuffWriteManager latbuff = PyBuffWriteManager(longitude) |
| 255 | + |
| 256 | + if not lonbuff.len or not (lonbuff.len == latbuff.len): |
| 257 | + raise ProjError('longitude and latitude must be same size') |
| 258 | + |
| 259 | + # prepare the factors output |
| 260 | + meridional_scale = copy.copy(longitude) |
| 261 | + parallel_scale = copy.copy(longitude) |
| 262 | + areal_scale = copy.copy(longitude) |
| 263 | + angular_distortion = copy.copy(longitude) |
| 264 | + meridian_parallel_angle = copy.copy(longitude) |
| 265 | + meridian_convergence = copy.copy(longitude) |
| 266 | + tissot_semimajor = copy.copy(longitude) |
| 267 | + tissot_semiminor = copy.copy(longitude) |
| 268 | + dx_dlam = copy.copy(longitude) |
| 269 | + dx_dphi = copy.copy(longitude) |
| 270 | + dy_dlam = copy.copy(longitude) |
| 271 | + dy_dphi = copy.copy(longitude) |
| 272 | + cdef PyBuffWriteManager meridional_scale_buff = PyBuffWriteManager(meridional_scale) |
| 273 | + cdef PyBuffWriteManager parallel_scale_buff = PyBuffWriteManager(parallel_scale) |
| 274 | + cdef PyBuffWriteManager areal_scale_buff = PyBuffWriteManager(areal_scale) |
| 275 | + cdef PyBuffWriteManager angular_distortion_buff = PyBuffWriteManager(angular_distortion) |
| 276 | + cdef PyBuffWriteManager meridian_parallel_angle_buff = PyBuffWriteManager(meridian_parallel_angle) |
| 277 | + cdef PyBuffWriteManager meridian_convergence_buff = PyBuffWriteManager(meridian_convergence) |
| 278 | + cdef PyBuffWriteManager tissot_semimajor_buff = PyBuffWriteManager(tissot_semimajor) |
| 279 | + cdef PyBuffWriteManager tissot_semiminor_buff = PyBuffWriteManager(tissot_semiminor) |
| 280 | + cdef PyBuffWriteManager dx_dlam_buff = PyBuffWriteManager(dx_dlam) |
| 281 | + cdef PyBuffWriteManager dx_dphi_buff = PyBuffWriteManager(dx_dphi) |
| 282 | + cdef PyBuffWriteManager dy_dlam_buff = PyBuffWriteManager(dy_dlam) |
| 283 | + cdef PyBuffWriteManager dy_dphi_buff = PyBuffWriteManager(dy_dphi) |
| 284 | + |
| 285 | + # calculate the factors |
| 286 | + cdef PJ_COORD pj_coord = proj_coord(0, 0, 0, HUGE_VAL) |
| 287 | + cdef PJ_FACTORS pj_factors |
| 288 | + cdef int errno = 0 |
| 289 | + cdef bint invalid_coord = 0 |
| 290 | + cdef Py_ssize_t iii |
| 291 | + with nogil: |
| 292 | + for iii in range(lonbuff.len): |
| 293 | + pj_coord.uv.u = lonbuff.data[iii] |
| 294 | + pj_coord.uv.v = latbuff.data[iii] |
| 295 | + if not radians: |
| 296 | + pj_coord.uv.u *= _DG2RAD |
| 297 | + pj_coord.uv.v *= _DG2RAD |
| 298 | + |
| 299 | + # set both to HUGE_VAL if inf or nan |
| 300 | + proj_errno_reset(self.projobj) |
| 301 | + if pj_coord.uv.v == HUGE_VAL \ |
| 302 | + or pj_coord.uv.v != pj_coord.uv.v \ |
| 303 | + or pj_coord.uv.u == HUGE_VAL \ |
| 304 | + or pj_coord.uv.u != pj_coord.uv.u: |
| 305 | + invalid_coord = True |
| 306 | + else: |
| 307 | + invalid_coord = False |
| 308 | + pj_factors = proj_factors(self.projobj, pj_coord) |
| 309 | + |
| 310 | + errno = proj_errno(self.projobj) |
| 311 | + if errcheck and errno: |
| 312 | + with gil: |
| 313 | + raise ProjError("proj error: {}".format( |
| 314 | + pystrdecode(proj_errno_string(errno)))) |
| 315 | + |
| 316 | + if errno or invalid_coord: |
| 317 | + meridional_scale_buff.data[iii] = HUGE_VAL |
| 318 | + parallel_scale_buff.data[iii] = HUGE_VAL |
| 319 | + areal_scale_buff.data[iii] = HUGE_VAL |
| 320 | + angular_distortion_buff.data[iii] = HUGE_VAL |
| 321 | + meridian_parallel_angle_buff.data[iii] = HUGE_VAL |
| 322 | + meridian_convergence_buff.data[iii] = HUGE_VAL |
| 323 | + tissot_semimajor_buff.data[iii] = HUGE_VAL |
| 324 | + tissot_semiminor_buff.data[iii] = HUGE_VAL |
| 325 | + dx_dlam_buff.data[iii] = HUGE_VAL |
| 326 | + dx_dphi_buff.data[iii] = HUGE_VAL |
| 327 | + dy_dlam_buff.data[iii] = HUGE_VAL |
| 328 | + dy_dphi_buff.data[iii] = HUGE_VAL |
| 329 | + else: |
| 330 | + meridional_scale_buff.data[iii] = pj_factors.meridional_scale |
| 331 | + parallel_scale_buff.data[iii] = pj_factors.parallel_scale |
| 332 | + areal_scale_buff.data[iii] = pj_factors.areal_scale |
| 333 | + angular_distortion_buff.data[iii] = pj_factors.angular_distortion * _RAD2DG |
| 334 | + meridian_parallel_angle_buff.data[iii] = pj_factors.meridian_parallel_angle * _RAD2DG |
| 335 | + meridian_convergence_buff.data[iii] = pj_factors.meridian_convergence * _RAD2DG |
| 336 | + tissot_semimajor_buff.data[iii] = pj_factors.tissot_semimajor |
| 337 | + tissot_semiminor_buff.data[iii] = pj_factors.tissot_semiminor |
| 338 | + dx_dlam_buff.data[iii] = pj_factors.dx_dlam |
| 339 | + dx_dphi_buff.data[iii] = pj_factors.dx_dphi |
| 340 | + dy_dlam_buff.data[iii] = pj_factors.dy_dlam |
| 341 | + dy_dphi_buff.data[iii] = pj_factors.dy_dphi |
| 342 | + |
| 343 | + ProjError.clear() |
| 344 | + |
| 345 | + return Factors( |
| 346 | + meridional_scale=meridional_scale, |
| 347 | + parallel_scale=parallel_scale, |
| 348 | + areal_scale=areal_scale, |
| 349 | + angular_distortion=angular_distortion, |
| 350 | + meridian_parallel_angle=meridian_parallel_angle, |
| 351 | + meridian_convergence=meridian_convergence, |
| 352 | + tissot_semimajor=tissot_semimajor, |
| 353 | + tissot_semiminor=tissot_semiminor, |
| 354 | + dx_dlam=dx_dlam, |
| 355 | + dx_dphi=dx_dphi, |
| 356 | + dy_dlam=dy_dlam, |
| 357 | + dy_dphi=dy_dphi, |
| 358 | + ) |
| 359 | + |
190 | 360 | def __repr__(self):
|
191 | 361 | return "Proj('{srs}', preserve_units=True)".format(srs=self.srs)
|
192 | 362 |
|
193 |
| - def _is_exact_same(self, Proj other): |
| 363 | + def _is_exact_same(self, _Proj other): |
194 | 364 | return proj_is_equivalent_to(
|
195 | 365 | self.projobj, other.projobj, PJ_COMP_STRICT) == 1
|
196 | 366 |
|
197 |
| - def _is_equivalent(self, Proj other): |
| 367 | + def _is_equivalent(self, _Proj other): |
198 | 368 | return proj_is_equivalent_to(
|
199 | 369 | self.projobj, other.projobj, PJ_COMP_EQUIVALENT) == 1
|
200 | 370 |
|
201 | 371 | def is_exact_same(self, other):
|
202 | 372 | """Compares Proj objects to see if they are exactly the same."""
|
203 |
| - if not isinstance(other, Proj): |
| 373 | + if not isinstance(other, _Proj): |
204 | 374 | return False
|
205 | 375 | return self._is_exact_same(other)
|
0 commit comments