示例#1
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.
        """
        input_srid = None
        if isinstance(geo_input, bytes):
            geo_input = force_text(geo_input)
        if isinstance(geo_input, str):
            wkt_m = wkt_regex.match(geo_input)
            if wkt_m:
                # Handling WKT input.
                if wkt_m.group('srid'):
                    input_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.
                g = wkb_r().read(gdal.OGRGeometry(geo_input).wkb)
            else:
                raise ValueError(
                    'String 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, 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' %
                            type(geo_input))

        if not g:
            raise GEOSException(
                'Could not initialize GEOS Geometry with given input.')

        input_srid = input_srid or capi.geos_get_srid(g) or None
        if input_srid and srid and input_srid != srid:
            raise ValueError('Input geometry already has SRID: %d.' %
                             input_srid)

        # Setting the pointer object with a valid pointer.
        self.ptr = g
        # Post-initialization setup.
        self._post_init(input_srid or srid)
示例#2
0
    def __init__(self, geo_input, srid=None):
        """
        The base constructor for GEOS geometry objects. It may take the
        following inputs:

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

        The `srid` keyword specifies the Source Reference Identifier (SRID)
        number for this Geometry. If not provided, it defaults to None.
        """
        input_srid = None
        if isinstance(geo_input, bytes):
            geo_input = force_text(geo_input)
        if isinstance(geo_input, str):
            wkt_m = wkt_regex.match(geo_input)
            if wkt_m:
                # Handle WKT input.
                if wkt_m.group('srid'):
                    input_srid = int(wkt_m.group('srid'))
                g = self._from_wkt(force_bytes(wkt_m.group('wkt')))
            elif hex_regex.match(geo_input):
                # Handle HEXEWKB input.
                g = wkb_r().read(force_bytes(geo_input))
            elif json_regex.match(geo_input):
                # Handle GeoJSON input.
                ogr = gdal.OGRGeometry.from_json(geo_input)
                g = ogr._geos_ptr()
                input_srid = ogr.srid
            else:
                raise ValueError('String 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, 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:
            raise TypeError('Improper geometry input type: %s' % type(geo_input))

        if not g:
            raise GEOSException('Could not initialize GEOS Geometry with given input.')

        input_srid = input_srid or capi.geos_get_srid(g) or None
        if input_srid and srid and input_srid != srid:
            raise ValueError('Input geometry already has SRID: %d.' % input_srid)

        super().__init__(g, None)
        # Set the SRID, if given.
        srid = input_srid or srid
        if srid and isinstance(srid, int):
            self.srid = srid
示例#3
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 = wkt_r().read(wkt_m.group('wkt'))
            elif hex_regex.match(geo_input):
                # Handling HEXEWKB input.
                g = wkb_r().read(geo_input)
            elif gdal.HAS_GDAL and json_regex.match(geo_input):
                # Handling GeoJSON input.
                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 geomtry (GEOM_PTR).
            g = geo_input
        elif isinstance(geo_input, buffer):
            # 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 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)
示例#4
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)
示例#5
0
    def get_geometry_converter(self, expression):
        read = wkb_r().read
        geom_class = expression.output_field.geom_class

        def converter(value, expression, connection):
            return None if value is None else GEOSGeometryBase(read(value), geom_class)
        return converter
示例#6
0
 def __setstate__(self, state):
     # Instantiating from the tuple state that was pickled.
     wkb, srid = state
     ptr = wkb_r().read(buffer(wkb))
     if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.')
     self.ptr = ptr
     self._post_init(srid)
示例#7
0
 def __setstate__(self, state):
     # Instantiating from the tuple state that was pickled.
     wkb, srid = state
     ptr = wkb_r().read(buffer(wkb))
     if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.')
     self.ptr = ptr
     self._post_init(srid)
 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.')
示例#9
0
    def get_geometry_converter(self, expression):
        read = wkb_r().read
        geom_class = expression.output_field.geom_class

        def converter(value, expression, connection):
            return None if value is None else GEOSGeometryBase(read(value), geom_class)
        return converter
示例#10
0
    def get_geometry_converter(self, expression):
        read = wkb_r().read
        srid = expression.output_field.srid
        if srid == -1:
            srid = None

        def converter(value, expression, connection):
            return None if value is None else GEOSGeometry(
                read(memoryview(value.read())), srid)

        return converter
示例#11
0
 def get_geometry_converter(self, expression):
     if self.spatial_version >= (4, 3, 0):
         read = wkb_r().read
         return lambda value, expression, connection: None if value is None else GEOSGeometry(
             read(value))
     else:
         read = wkt_r().read
         srid = expression.output_field.srid
         if srid == -1:
             srid = None
         return lambda value, expression, connection: None if value is None else GEOSGeometry(
             read(value), srid)
示例#12
0
    def get_geometry_converter(self, expression):
        read = wkb_r().read
        srid = expression.output_field.srid
        if srid == -1:
            srid = None
        geom_class = expression.output_field.geom_class

        def converter(value, expression, connection):
            if value is not None:
                geom = GEOSGeometryBase(read(memoryview(value.read())), geom_class)
                if srid:
                    geom.srid = srid
                return geom
        return converter
示例#13
0
    def get_geometry_converter(self, expression):
        read = wkb_r().read
        srid = expression.output_field.srid
        if srid == -1:
            srid = None
        geom_class = expression.output_field.geom_class

        def converter(value, expression, connection):
            if value is not None:
                geom = GEOSGeometryBase(read(memoryview(value)), geom_class)
                if srid:
                    geom.srid = srid
                return geom
        return converter
示例#14
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 not gdal.HAS_GDAL:
            raise GEOSException(
                "GDAL library is not available to transform() geometry.")

        if isinstance(ct, gdal.CoordTransform):
            # We don't care about SRID because CoordTransform presupposes
            # source SRS.
            srid = None
        elif srid is None or srid < 0:
            raise GEOSException(
                "Calling transform() with no SRID set is not supported")

        # 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.')
示例#15
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 not gdal.HAS_GDAL:
            raise GEOSException("GDAL library is not available to transform() geometry.")

        if isinstance(ct, gdal.CoordTransform):
            # We don't care about SRID because CoordTransform presupposes
            # source SRS.
            srid = None
        elif srid is None or srid < 0:
            raise GEOSException("Calling transform() with no SRID set is not supported")

        # 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.')
示例#16
0
    def get_geometry_converter(self, expression):
        geom_class = expression.output_field.geom_class
        if self.spatial_version >= (4, 3, 0):
            read = wkb_r().read

            def converter(value, expression, connection):
                return None if value is None else GEOSGeometryBase(read(value), geom_class)
        else:
            read = wkt_r().read
            srid = expression.output_field.srid
            if srid == -1:
                srid = None

            def converter(value, expression, connection):
                if value is not None:
                    geom = GEOSGeometryBase(read(value), geom_class)
                    if srid:
                        geom.srid = srid
                    return geom
        return converter
示例#17
0
    def get_geometry_converter(self, expression):
        geom_class = expression.output_field.geom_class
        if self.spatial_version >= (4, 3, 0):
            read = wkb_r().read

            def converter(value, expression, connection):
                return None if value is None else GEOSGeometryBase(read(value), geom_class)
        else:
            read = wkt_r().read
            srid = expression.output_field.srid
            if srid == -1:
                srid = None

            def converter(value, expression, connection):
                if value is not None:
                    geom = GEOSGeometryBase(read(value), geom_class)
                    if srid:
                        geom.srid = srid
                    return geom
        return converter
示例#18
0
 The `srid` keyword specifies the Source Reference Identifier (SRID)
 number for this Geometry. If not provided, it defaults to None.
 """
 input_srid = None
 if isinstance(geo_input, bytes):
     geo_input = force_text(geo_input)
 if isinstance(geo_input, str):
     wkt_m = wkt_regex.match(geo_input)
     if wkt_m:
         # Handle WKT input.
         if wkt_m.group('srid'):
             input_srid = int(wkt_m.group('srid'))
         g = self._from_wkt(force_bytes(wkt_m.group('wkt')))
     elif hex_regex.match(geo_input):
         # Handle HEXEWKB input.
         g = wkb_r().read(force_bytes(geo_input))
     elif json_regex.match(geo_input):
         # Handle GeoJSON input.
         ogr = gdal.OGRGeometry.from_json(geo_input)
         g = ogr._geos_ptr()
         input_srid = ogr.srid
     else:
         raise ValueError('String 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, 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)
示例#19
0
 def _from_wkb(cls, wkb):
     return wkb_r().read(wkb)
示例#20
0
 def get_geometry_converter(self, expression):
     read = wkb_r().read
     return lambda value, expression, connection: None if value is None else GEOSGeometry(
         read(value))
示例#21
0
 def _from_wkb(cls, wkb):
     return wkb_r().read(wkb)
示例#22
0
 def _from_pickle_wkb(self, wkb):
     return wkb_r().read(memoryview(wkb))
示例#23
0
        converters = super(SpatiaLiteOperations, self).get_db_converters(expression)
        if hasattr(expression.output_field, 'geom_type'):
            converters.append(self.convert_geometry)
        return converters

    def convert_geometry(self, value, expression, connection, context):
        if value:
            value = Geometry(value)
            if 'transformed_srid' in context:
                value.srid = context['transformed_srid']
        return value
=======
    def get_geometry_converter(self, expression):
        geom_class = expression.output_field.geom_class
        if self.spatial_version >= (4, 3, 0):
            read = wkb_r().read

            def converter(value, expression, connection):
                return None if value is None else GEOSGeometryBase(read(value), geom_class)
        else:
            read = wkt_r().read
            srid = expression.output_field.srid
            if srid == -1:
                srid = None

            def converter(value, expression, connection):
                if value is not None:
                    geom = GEOSGeometryBase(read(value), geom_class)
                    if srid:
                        geom.srid = srid
                    return geom
示例#24
0
 def _from_pickle_wkb(self, wkb):
     return wkb_r().read(memoryview(wkb))