Пример #1
0
    def project(self,
                geometry: ogr.Geometry,
                srs: int or ogr.osr.SpatialReference,
                copy: bool = True) -> ogr.Geometry:
        """
        Project a geometry to a spatial reference.

        :param geometry: the geometry to project
        :type geometry:  :py:class:`ogr.Geometry`
        :param srs: the spatial reference (or SRID) to which we're going to project
        :type srs:  :py:class:`ogr.osr.SpatialReference` or ``int``
        :param copy: Make a copy of the geometry or modify the current one?
        :type copy:  ``bool``
        :return: the projected geometry
        :rtype:  :py:class:`ogr.Geometry`
        """
        # The incoming spatial reference argument might be spatial reference, or it might be an SRID.  Figure out
        # which one we're dealing with and make sure we end up with a spatial reference.
        _srs = srs if isinstance(
            srs, ogr.osr.SpatialReference) else self.get_spatial_reference(
                srid=srs)
        # The object we ultimately transform depends on the 'copy' parameter.
        _geometry = geometry if not copy else geometry.Clone()
        # Perform the transformation.
        _geometry.TransformTo(_srs)
        # Return whatever we have.
        return _geometry
Пример #2
0
def convert_geometry(
        geometry: ogr.Geometry,
        new_spatialreference: osr.SpatialReference) -> ogr.Geometry:
    """Converts the geometry to the new spatial reference if possible
    Arguments:
        geometry - The geometry to transform
        new_spatialreference - The spatial reference to change to
    Returns:
        The transformed geometry or the original geometry. If either the
        new Spatial Reference parameter is None, or the geometry doesn't
        have a spatial reference, then the original geometry is returned.
    """
    if not new_spatialreference or not geometry:
        return geometry

    return_geometry = geometry
    try:
        geom_sr = geometry.GetSpatialReference()
        if int(osgeo.__version__[0]) >= 3:
            # GDAL 3 changes axis order: https://github.com/OSGeo/gdal/issues/1546
            # pylint: disable=no-member
            geom_sr.SetAxisMappingStrategy(
                osgeo.osr.OAMS_TRADITIONAL_GIS_ORDER)
        if geom_sr and not new_spatialreference.IsSame(geom_sr):
            transform = osr.CreateCoordinateTransformation(
                geom_sr, new_spatialreference)
            new_geom = geometry.Clone()
            if new_geom:
                new_geom.Transform(transform)
                return_geometry = new_geom
    except Exception as ex:
        logging.warning("Exception caught while transforming geometries: %s",
                        str(ex))
        logging.warning("    Returning original geometry")

    return return_geometry
Пример #3
0
def get_length(linestring_geom: ogr.Geometry, coordinate_transformer):
    cloned_geom = linestring_geom.Clone()
    cloned_geom.Transform(coordinate_transformer)
    return cloned_geom.Length()