Skip to content

Commit

Permalink
return None and raise warning when transform_geom dst_ogr_geom is NULL (
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 authored and sgillies committed Oct 24, 2019
1 parent cdd3542 commit d0e94b5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
23 changes: 15 additions & 8 deletions fiona/_transform.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import

import logging
import warnings

from fiona cimport _cpl, _crs, _csl, _geometry
from fiona._crs cimport OGRSpatialReferenceH
Expand Down Expand Up @@ -129,12 +130,10 @@ def _transform_geom(
cdef void *src_ogr_geom = NULL
cdef void *dst_ogr_geom = NULL
cdef int i

if src_crs and dst_crs:
src = _crs_from_crs(src_crs)
dst = _crs_from_crs(dst_crs)
transform = _crs.OCTNewCoordinateTransformation(src, dst)

# Transform options.
options = _csl.CSLSetNameValue(
options, "DATELINEOFFSET",
Expand All @@ -148,10 +147,19 @@ def _transform_geom(
<const OGRGeometry *>src_ogr_geom,
<OGRCoordinateTransformation *>transform,
options)
g = _geometry.GeomBuilder().build(dst_ogr_geom)

_geometry.OGR_G_DestroyGeometry(dst_ogr_geom)
_geometry.OGR_G_DestroyGeometry(src_ogr_geom)
if dst_ogr_geom == NULL:
g = None
warnings.warn(
"Full reprojection failed, but partial is possible. To enable partial "
"reprojection wrap the transform_geom call like so:\n"
"with fiona.Env(OGR_ENABLE_PARTIAL_REPROJECTION=True):\n"
" transform_geom(...)"
)
else:
g = _geometry.GeomBuilder().build(dst_ogr_geom)
_geometry.OGR_G_DestroyGeometry(dst_ogr_geom)
if src_ogr_geom != NULL:
_geometry.OGR_G_DestroyGeometry(src_ogr_geom)
_crs.OCTDestroyCoordinateTransformation(transform)
if options != NULL:
_csl.CSLDestroy(options)
Expand All @@ -161,8 +169,7 @@ def _transform_geom(
else:
g = geom

if precision >= 0:

if precision >= 0 and g is not None:
if g['type'] == 'Point':
coords = list(g['coordinates'])
x, y = coords[:2]
Expand Down
26 changes: 26 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,29 @@
def test_transform_geom_with_z(geom):
"""Transforming a geom with Z succeeds"""
g2 = transform.transform_geom("epsg:4326", "epsg:3857", geom, precision=3)


def test_transform_geom_null_dest():
failed_geom = {
'type': 'Polygon',
'coordinates': ((
(81.2180196471443, 6.197141424988303),
(80.34835696810447, 5.968369859232141),
(79.87246870312859, 6.763463446474915),
(79.69516686393516, 8.200843410673372),
(80.14780073437967, 9.824077663609557),
(80.83881798698664, 9.268426825391174),
(81.3043192890718, 8.564206244333675),
(81.78795901889143, 7.523055324733178),
(81.63732221876066, 6.481775214051936),
(81.2180196471443, 6.197141424988303)
),)
}
with pytest.warns(UserWarning):
assert transform.transform_geom(
src_crs="epsg:4326",
dst_crs="epsg:32628",
geom=failed_geom,
antimeridian_cutting=True,
precision=2,
) is None

0 comments on commit d0e94b5

Please sign in to comment.