Python django.contrib.gis.geos.GEOSException() Examples

The following are 13 code examples of django.contrib.gis.geos.GEOSException(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.gis.geos , or try the search function .
Example #1
Source File: fields.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def to_python(self, value):
        """
        Transforms the value to a Geometry object.
        """
        if value in self.empty_values:
            return None

        if not isinstance(value, GEOSGeometry):
            try:
                value = GEOSGeometry(value)
            except (GEOSException, ValueError, TypeError):
                raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')

        # Try to set the srid
        if not value.srid:
            try:
                value.srid = self.widget.map_srid
            except AttributeError:
                if self.srid:
                    value.srid = self.srid
        return value 
Example #2
Source File: fields.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def clean(self, value):
        """
        Validates that the input value can be converted to a Geometry
        object (which is returned).  A ValidationError is raised if
        the value cannot be instantiated as a Geometry.
        """
        geom = super(GeometryField, self).clean(value)
        if geom is None:
            return geom

        # Ensuring that the geometry is of the correct type (indicated
        # using the OGC string label).
        if str(geom.geom_type).upper() != self.geom_type and not self.geom_type == 'GEOMETRY':
            raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')

        # Transforming the geometry if the SRID was set.
        if self.srid and self.srid != -1 and self.srid != geom.srid:
            try:
                geom.transform(self.srid)
            except GEOSException:
                raise forms.ValidationError(
                    self.error_messages['transform_error'], code='transform_error')

        return geom 
Example #3
Source File: fields.py    From bioforum with MIT License 6 votes vote down vote up
def to_python(self, value):
        """Transform the value to a Geometry object."""
        if value in self.empty_values:
            return None

        if not isinstance(value, GEOSGeometry):
            try:
                value = GEOSGeometry(value)
            except (GEOSException, ValueError, TypeError):
                raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')

        # Try to set the srid
        if not value.srid:
            try:
                value.srid = self.widget.map_srid
            except AttributeError:
                if self.srid:
                    value.srid = self.srid
        return value 
Example #4
Source File: fields.py    From bioforum with MIT License 6 votes vote down vote up
def clean(self, value):
        """
        Validate that the input value can be converted to a Geometry object
        and return it. Raise a ValidationError if the value cannot be
        instantiated as a Geometry.
        """
        geom = super().clean(value)
        if geom is None:
            return geom

        # Ensuring that the geometry is of the correct type (indicated
        # using the OGC string label).
        if str(geom.geom_type).upper() != self.geom_type and not self.geom_type == 'GEOMETRY':
            raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')

        # Transforming the geometry if the SRID was set.
        if self.srid and self.srid != -1 and self.srid != geom.srid:
            try:
                geom.transform(self.srid)
            except GEOSException:
                raise forms.ValidationError(
                    self.error_messages['transform_error'], code='transform_error')

        return geom 
Example #5
Source File: fields.py    From openhgsenti with Apache License 2.0 6 votes vote down vote up
def to_python(self, value):
        """
        Transforms the value to a Geometry object.
        """
        if value in self.empty_values:
            return None

        if not isinstance(value, GEOSGeometry):
            try:
                value = GEOSGeometry(value)
            except (GEOSException, ValueError, TypeError):
                raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')

        # Try to set the srid
        if not value.srid:
            try:
                value.srid = self.widget.map_srid
            except AttributeError:
                if self.srid:
                    value.srid = self.srid
        return value 
Example #6
Source File: fields.py    From openhgsenti with Apache License 2.0 6 votes vote down vote up
def clean(self, value):
        """
        Validates that the input value can be converted to a Geometry
        object (which is returned).  A ValidationError is raised if
        the value cannot be instantiated as a Geometry.
        """
        geom = super(GeometryField, self).clean(value)
        if geom is None:
            return geom

        # Ensuring that the geometry is of the correct type (indicated
        # using the OGC string label).
        if str(geom.geom_type).upper() != self.geom_type and not self.geom_type == 'GEOMETRY':
            raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')

        # Transforming the geometry if the SRID was set.
        if self.srid and self.srid != -1 and self.srid != geom.srid:
            try:
                geom.transform(self.srid)
            except GEOSException:
                raise forms.ValidationError(
                    self.error_messages['transform_error'], code='transform_error')

        return geom 
Example #7
Source File: widgets.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def deserialize(self, value):
        try:
            return GEOSGeometry(value, self.map_srid)
        except (GEOSException, ValueError) as err:
            logger.error(
                "Error creating geometry from value '%s' (%s)" % (
                    value, err)
            )
        return None 
Example #8
Source File: widgets.py    From bioforum with MIT License 5 votes vote down vote up
def deserialize(self, value):
        try:
            return GEOSGeometry(value)
        except (GEOSException, ValueError) as err:
            logger.error("Error creating geometry from value '%s' (%s)", value, err)
        return None 
Example #9
Source File: geospatial_utils.py    From scale with Apache License 2.0 5 votes vote down vote up
def parse_geo_json(geo_json):
    """Parses GeoJSON and returns a geometry object and metadata.

    :param geo_json: The geo json to parse
    :type geo_json: dict
    :rtype: GEOSGeometry, dict
    :returns: the geometry and metadata
    """

    geom = None
    geom_json = None
    props = None
    if geo_json['type'] == 'Feature':
        geom_json = geo_json['geometry']
        if 'properties' in geo_json:
            props = geo_json['properties']
    elif geo_json['type'] == 'FeatureCollection':
        # Currently handles collections by just grabbing first entry
        geom_json = geo_json['features'][0]['geometry']
        if 'properties' in geo_json['features'][0]:
            props = geo_json['features'][0]['properties']
    else:
        # The GeoJSON is just a geometry
        geom_json = geo_json

    # Parse geometry
    if geom_json:
        try:
            geom = geos.GEOSGeometry(json.dumps(geom_json), srid=4326)
        except geos.GEOSException as geos_error:
            raise InvalidResultsManifest(str(geos_error))

    return geom, props 
Example #10
Source File: serializers.py    From django-spillway with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def data(self):
        if not hasattr(self, '_data'):
            self._data = super(FeatureSerializer, self).data
            if 'crs' not in self._data:
                try:
                    field = self.fields[self.Meta.geom_field]
                    srid = getattr(self.instance, field.source).srid
                except (AttributeError, geos.GEOSException):
                    pass
                else:
                    self._data['crs'] = sc.NamedCRS(srid)
        return self._data 
Example #11
Source File: fields.py    From django-spillway with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_attribute(self, instance):
        # SpatiaLite returns empty/invalid geometries in WKT or GeoJSON with
        # exceedingly high simplification tolerances.
        try:
            return super(GeometryField, self).get_attribute(instance)
        except geos.GEOSException:
            return None 
Example #12
Source File: widgets.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def deserialize(self, value):
        try:
            return GEOSGeometry(value, self.map_srid)
        except (GEOSException, ValueError) as err:
            logger.error(
                "Error creating geometry from value '%s' (%s)" % (
                    value, err)
            )
        return None 
Example #13
Source File: validators.py    From cadasta-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
def validate_row(headers, row, config):
    party_name, party_type, geometry, tenure_type, location_type = (
        None, None, None, None, None)

    (party_name_field, party_type_field, location_type_field, type,
        geometry_field, tenure_type_field) = get_fields_from_config(config)

    if len(headers) != len(row):
        raise ValidationError(
            _("Number of headers and columns do not match.")
        )

    _get_field_value = partial(get_field_value, headers, row)

    if party_name_field and party_type_field:
        party_name = _get_field_value(party_name_field, "party_name")
        party_type = _get_field_value(party_type_field, "party_type")

    if geometry_field:
        coords = _get_field_value(geometry_field, "geometry_field")
        if coords == '':
            geometry = None
        else:
            try:
                geometry = GEOSGeometry(coords)
            except (ValueError, GEOSException):
                try:
                    geometry = GEOSGeometry(odk_geom_to_wkt(coords))
                except InvalidODKGeometryError:
                    raise ValidationError(_("Invalid geometry."))

    if location_type_field:
        location_type = _get_field_value(location_type_field, "location_type")
        type_choices = config['allowed_location_types']
        if location_type and location_type not in type_choices:
            raise ValidationError(
                _("Invalid location_type: '%s'.") % location_type
            )

    if party_name_field and geometry_field:
        tenure_type = _get_field_value(tenure_type_field, 'tenure_type')

        if tenure_type and tenure_type not in config['allowed_tenure_types']:
            raise ValidationError(
                _("Invalid tenure_type: '%s'.") % tenure_type
            )

    values = (party_name, party_type, geometry, location_type, tenure_type)

    if not all(sanitize_string(val) for val in values):
        raise ValidationError(SANITIZE_ERROR)

    return values