Commit 69535b7b authored by Justin Bronn's avatar Justin Bronn
Browse files

The `OGRGeometry.coord_dim` property may now be set; implemented a work-around...

The `OGRGeometry.coord_dim` property may now be set; implemented a work-around for an OGR bug that changed geometries to 3D after transformation.  Refs #11433.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@11628 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent e5ab340d
Loading
Loading
Loading
Loading
+37 −16
Original line number Diff line number Diff line
@@ -179,10 +179,17 @@ class OGRGeometry(GDALBase):
        "Returns 0 for points, 1 for lines, and 2 for surfaces."
        return capi.get_dims(self.ptr)

    @property
    def coord_dim(self):
    def _get_coord_dim(self):
        "Returns the coordinate dimension of the Geometry."
        return capi.get_coord_dims(self.ptr)
        return capi.get_coord_dim(self.ptr)

    def _set_coord_dim(self, dim):
        "Sets the coordinate dimension of this Geometry."
        if not dim in (2, 3):
            raise ValueError('Geometry dimension must be either 2 or 3')
        capi.set_coord_dim(self.ptr, dim)

    coord_dim = property(_get_coord_dim, _set_coord_dim)

    @property
    def geom_count(self):
@@ -363,6 +370,16 @@ class OGRGeometry(GDALBase):
            klone = self.clone()
            klone.transform(coord_trans)
            return klone

        # Have to get the coordinate dimension of the original geometry
        # so it can be used to reset the transformed geometry's dimension
        # afterwards.  This is done because of GDAL bug (in versions prior
        # to 1.7) that turns geometries 3D after transformation, see:
        #  http://trac.osgeo.org/gdal/changeset/17792
        orig_dim = self.coord_dim

        # Depending on the input type, use the appropriate OGR routine
        # to perform the transformation.
        if isinstance(coord_trans, CoordTransform):
            capi.geom_transform(self.ptr, coord_trans.ptr)
        elif isinstance(coord_trans, SpatialReference):
@@ -373,6 +390,10 @@ class OGRGeometry(GDALBase):
        else:
            raise TypeError('Transform only accepts CoordTransform, SpatialReference, string, and integer objects.')

        # Setting with original dimension, see comment above.
        if self.coord_dim != orig_dim:
            self.coord_dim = orig_dim

    def transform_to(self, srs):
        "For backwards-compatibility."
        self.transform(srs)
+2 −1
Original line number Diff line number Diff line
@@ -83,7 +83,8 @@ get_geom_srs = srs_output(lgdal.OGR_G_GetSpatialReference, [c_void_p])
get_area = double_output(lgdal.OGR_G_GetArea, [c_void_p])
get_centroid = void_output(lgdal.OGR_G_Centroid, [c_void_p, c_void_p])
get_dims = int_output(lgdal.OGR_G_GetDimension, [c_void_p])
get_coord_dims = int_output(lgdal.OGR_G_GetCoordinateDimension, [c_void_p])
get_coord_dim = int_output(lgdal.OGR_G_GetCoordinateDimension, [c_void_p])
set_coord_dim = void_output(lgdal.OGR_G_SetCoordinateDimension, [c_void_p, c_int], errcheck=False)

get_geom_count = int_output(lgdal.OGR_G_GetGeometryCount, [c_void_p])
get_geom_name = const_string_output(lgdal.OGR_G_GetGeometryName, [c_void_p])
+12 −0
Original line number Diff line number Diff line
@@ -319,6 +319,18 @@ class OGRGeomTest(unittest.TestCase):
            self.assertAlmostEqual(trans.x, p.x, prec)
            self.assertAlmostEqual(trans.y, p.y, prec)

    def test09c_transform_dim(self):
        "Testing coordinate dimension is the same on transformed geometries."
        ls_orig = OGRGeometry('LINESTRING(-104.609 38.255)', 4326)
        ls_trans = OGRGeometry('LINESTRING(992385.4472045 481455.4944650)', 2774)
        
        prec = 3
        ls_orig.transform(ls_trans.srs)
        # Making sure the coordinate dimension is still 2D.
        self.assertEqual(2, ls_orig.coord_dim)
        self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec)
        self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec)

    def test10_difference(self):
        "Testing difference()."
        for i in xrange(len(topology_geoms)):