示例#1
0
 def ogr(self):
     "Returns the OGR Geometry for this Geometry."
     if gdal.HAS_GDAL:
         if self.srid:
             return gdal.OGRGeometry(self.wkb, self.srid)
         else:
             return gdal.OGRGeometry(self.wkb)
     else:
         raise GEOSException('GDAL required to convert to an OGRGeometry.')
示例#2
0
 def ogr(self):
     "Returns the OGR Geometry for this Geometry."
     if not gdal.HAS_GDAL:
         raise GEOSException('GDAL required to convert to an OGRGeometry.')
     if self.srid:
         try:
             return gdal.OGRGeometry(self.wkb, self.srid)
         except SRSException:
             pass
     return gdal.OGRGeometry(self.wkb)
 def transform(self, ct, clone=False):
     """
     Requires GDAL. Transforms the geometry according to the given
     transformation object, which may be an integer SRID, and WKT or
     PROJ.4 string. By default, the geometry is transformed in-place and
     nothing is returned. However if the `clone` keyword is set, then this
     geometry will not be modified and a transformed clone will be returned
     instead.
     """
     srid = self.srid
     if gdal.HAS_GDAL and srid:
         # Creating an OGR Geometry, which is then transformed.
         g = gdal.OGRGeometry(self.wkb, srid)
         g.transform(ct)
         # Getting a new GEOS pointer
         ptr = wkb_r().read(g.wkb)
         if clone:
             # User wants a cloned transformed geometry returned.
             return GEOSGeometry(ptr, srid=g.srid)
         if ptr:
             # Reassigning pointer, and performing post-initialization setup
             # again due to the reassignment.
             capi.destroy_geom(self.ptr)
             self.ptr = ptr
             self._post_init(g.srid)
         else:
             raise GEOSException('Transformed WKB was invalid.')
示例#4
0
def geom_to_json(geom, target_srs):
    srs = srs_cache.get(geom.srid, None)
    if not srs:
        srs = geom.srs
        srs_cache[geom.srid] = srs

    if target_srs:
        ct_id = '%s-%s' % (geom.srid, target_srs.srid)
        ct = coord_transforms.get(ct_id, None)
        if not ct:
            ct = CoordTransform(srs, target_srs)
            coord_transforms[ct_id] = ct
    else:
        ct = None

    if ct:
        wkb = geom.wkb
        geom = gdal.OGRGeometry(wkb, srs)
        geom.transform(ct)
        geom_name = geom.geom_name.lower()
    else:
        geom_name = geom.geom_type.lower()

    # Accelerated path for points
    if geom_name == 'point':
        if target_srs.projected:
            digits = 2
        else:
            digits = 7
        coords = [round(n, digits) for n in [geom.x, geom.y]]
        return {'type': 'Point', 'coordinates': coords}

    s = geom.geojson
    return json.loads(s)
示例#5
0
    def __init__(self, geo_input, srid=None):
        """
        The base constructor for GEOS geometry objects, and may take the
        following inputs:

         * strings:
            - WKT
            - HEXEWKB (a PostGIS-specific canonical form)
            - GeoJSON (requires GDAL)
         * buffer:
            - WKB

        The `srid` keyword is used to specify the Source Reference Identifier
        (SRID) number for this Geometry.  If not set, the SRID will be None.
        """
        if isinstance(geo_input, bytes):
            geo_input = force_text(geo_input)
        if isinstance(geo_input, six.string_types):
            wkt_m = wkt_regex.match(geo_input)
            if wkt_m:
                # Handling WKT input.
                if wkt_m.group('srid'):
                    srid = int(wkt_m.group('srid'))
                g = wkt_r().read(force_bytes(wkt_m.group('wkt')))
            elif hex_regex.match(geo_input):
                # Handling HEXEWKB input.
                g = wkb_r().read(force_bytes(geo_input))
            elif json_regex.match(geo_input):
                # Handling GeoJSON input.
                if not gdal.HAS_GDAL:
                    raise ValueError(
                        'Initializing geometry from JSON input requires GDAL.')
                g = wkb_r().read(gdal.OGRGeometry(geo_input).wkb)
            else:
                raise ValueError(
                    'String or unicode input unrecognized as WKT EWKT, and HEXEWKB.'
                )
        elif isinstance(geo_input, GEOM_PTR):
            # When the input is a pointer to a geometry (GEOM_PTR).
            g = geo_input
        elif isinstance(geo_input, six.memoryview):
            # When the input is a buffer (WKB).
            g = wkb_r().read(geo_input)
        elif isinstance(geo_input, GEOSGeometry):
            g = capi.geom_clone(geo_input.ptr)
        else:
            # Invalid geometry type.
            raise TypeError('Improper geometry input type: %s' %
                            str(type(geo_input)))

        if g:
            # Setting the pointer object with a valid pointer.
            self.ptr = g
        else:
            raise GEOSException(
                'Could not initialize GEOS Geometry with given input.')

        # Post-initialization setup.
        self._post_init(srid)
示例#6
0
    def __init__(self, geo_input, srid=None):
        """
        The base constructor for GEOS geometry objects, and may take the
        following inputs:

         * strings:
            - WKT
            - HEXEWKB (a PostGIS-specific canonical form)
            - GeoJSON (requires GDAL)
         * buffer:
            - WKB

        The `srid` keyword is used to specify the Source Reference Identifier
        (SRID) number for this Geometry.  If not set, the SRID will be None.
        """
        if isinstance(geo_input, basestring):
            if isinstance(geo_input, unicode):
                # Encoding to ASCII, WKT or HEXEWKB doesn't need any more.
                geo_input = geo_input.encode('ascii')

            wkt_m = wkt_regex.match(geo_input)
            if wkt_m:
                # Handling WKT input.
                if wkt_m.group('srid'): srid = int(wkt_m.group('srid'))
                g = io.wkt_r.read(wkt_m.group('wkt'))
            elif hex_regex.match(geo_input):
                # Handling HEXEWKB input.
                g = io.wkb_r.read(geo_input)
            elif gdal.GEOJSON and gdal.geometries.json_regex.match(geo_input):
                # Handling GeoJSON input.
                g = io.wkb_r.read(gdal.OGRGeometry(geo_input).wkb)
            else:
                raise ValueError(
                    'String or unicode input unrecognized as WKT EWKT, and HEXEWKB.'
                )
        elif isinstance(geo_input, GEOM_PTR):
            # When the input is a pointer to a geomtry (GEOM_PTR).
            g = geo_input
        elif isinstance(geo_input, buffer):
            # When the input is a buffer (WKB).
            g = io.wkb_r.read(geo_input)
        elif isinstance(geo_input, GEOSGeometry):
            g = capi.geom_clone(geo_input.ptr)
        else:
            # Invalid geometry type.
            raise TypeError('Improper geometry input type: %s' %
                            str(type(geo_input)))

        if bool(g):
            # Setting the pointer object with a valid pointer.
            self.ptr = g
        else:
            raise GEOSException(
                'Could not initialize GEOS Geometry with given input.')

        # Post-initialization setup.
        self._post_init(srid)
示例#7
0
    def transform(self, ct, clone=False):
        """
        Requires GDAL. Transforms the geometry according to the given
        transformation object, which may be an integer SRID, and WKT or
        PROJ.4 string. By default, the geometry is transformed in-place and
        nothing is returned. However if the `clone` keyword is set, then this
        geometry will not be modified and a transformed clone will be returned
        instead.
        """
        srid = self.srid

        if ct == srid:
            # short-circuit where source & dest SRIDs match
            if clone:
                return self.clone()
            else:
                return

        if (srid is None) or (srid < 0):
            warnings.warn("Calling transform() with no SRID set does no transformation!",
                          stacklevel=2)
            warnings.warn("Calling transform() with no SRID will raise GEOSException in v1.5",
                          FutureWarning, stacklevel=2)
            return

        if not gdal.HAS_GDAL:
            raise GEOSException("GDAL library is not available to transform() geometry.")

        # Creating an OGR Geometry, which is then transformed.
        g = gdal.OGRGeometry(self.wkb, srid)
        g.transform(ct)
        # Getting a new GEOS pointer
        ptr = wkb_r().read(g.wkb)
        if clone:
            # User wants a cloned transformed geometry returned.
            return GEOSGeometry(ptr, srid=g.srid)
        if ptr:
            # Reassigning pointer, and performing post-initialization setup
            # again due to the reassignment.
            capi.destroy_geom(self.ptr)
            self.ptr = ptr
            self._post_init(g.srid)
        else:
            raise GEOSException('Transformed WKB was invalid.')