def filter_by_bbox(self, bbox): '''Filter a querystring with a given bbox This function adds a bbox filter to the queryset. Args: queryset: A django queryset (https://docs.djangoproject.com/en/3.0/ref/models/querysets/) bbox: A string defining a spatial bbox (f.ex. 5.96, 45.82, 10.49, 47.81) Returns: The queryset with the added spatial filter Raises: ValidationError: When the bbox does not contain 4 values. Or when the polygon build from the bbox string is invalid. ''' try: logger.debug('Query parameter bbox = %s', bbox) bbox_geometry = geometry_from_bbox(bbox) validate_geometry(bbox_geometry) except (ValueError, serializers.ValidationError, IndexError) as error: logger.error( 'Invalid bbox query parameter: ' 'Could not transform bbox "%s" to a polygon; %s' 'f.ex. bbox=5.96, 45.82, 10.49, 47.81', bbox, error) raise serializers.ValidationError( _('Invalid bbox query parameter, ' ' has to contain 4 values. f.ex. bbox=5.96,45.82,10.49,47.81' ), ) from None return self.filter(geometry__intersects=bbox_geometry)
def validate_bbox(self, bbox): ''' Validation of the bbox. If the creation of a geometry (point or polygon) would not work, the function adds a entry to the self.errors dict. Args: bbox: string The bbox is a string that has to be composed of 4 comma-seperated float values. F. ex.: 5.96,45.82,10.49,47.81 ''' try: list_bbox_values = bbox.split(',') if (Decimal(list_bbox_values[0]) == Decimal(list_bbox_values[2]) and Decimal(list_bbox_values[1]) == Decimal( list_bbox_values[3])): bbox_geometry = Point(float(list_bbox_values[0]), float(list_bbox_values[1])) else: bbox_geometry = Polygon.from_bbox(list_bbox_values) validate_geometry(bbox_geometry) except (ValueError, ValidationError, IndexError, GDALException) as error: message = f"Invalid bbox query parameter: " \ f"f.ex. bbox=5.96,45.82,10.49,47.81, {bbox} ({error})" self.errors['bbox'] = _(message)
def validate_intersects(self, geojson): '''Validates the geojson in the intersects parameter. To test, if the string is valid, a geometry is being build out of it. If it is not possible, the dict self.errors is being widened with the corresponding information. Args: geojson: string The geojson string to be validated ''' try: intersects_geometry = GEOSGeometry(geojson) validate_geometry(intersects_geometry) except (ValueError, ValidationError, GDALException) as error: message = f"Invalid query: " \ f"Could not transform {geojson} to a geometry; {error}" self.errors['intersects'] = _(message)
def validate_bbox(self, bbox): ''' Validation of the bbox. If the creation of a geometry (point or polygon) would not work, the function adds a entry to the self.errors dict. Args: bbox: string The bbox is a string that has to be composed of 4 comma-seperated float values. F. ex.: 5.96,45.82,10.49,47.81 ''' try: validate_geometry(geometry_from_bbox(bbox)) except (ValueError, serializers.ValidationError, IndexError, GDALException) as error: message = f"Invalid bbox query parameter: " \ f"f.ex. bbox=5.96,45.82,10.49,47.81, {bbox} ({error})" self.errors['bbox'] = _(message)
def filter_by_bbox(self, bbox): '''Filter a querystring with a given bbox This function adds a bbox filter to the queryset. Args: queryset: A django queryset (https://docs.djangoproject.com/en/3.0/ref/models/querysets/) bbox: A string defining a spatial bbox (f.ex. 5.96, 45.82, 10.49, 47.81) Returns: The queryset with the added spatial filter Raises: ValidationError: When the bbox does not contain 4 values. Or when the polygon build from the bbox string is invalid. ''' try: logger.debug('Query parameter bbox = %s', bbox) list_bbox_values = bbox.split(',') if (Decimal(list_bbox_values[0]) == Decimal(list_bbox_values[2]) and Decimal(list_bbox_values[1]) == Decimal( list_bbox_values[3])): bbox_geometry = Point(float(list_bbox_values[0]), float(list_bbox_values[1])) else: bbox_geometry = Polygon.from_bbox(list_bbox_values) validate_geometry(bbox_geometry) except (ValueError, ValidationError, IndexError) as error: logger.error( 'Invalid bbox query parameter: ' 'Could not transform bbox "%s" to a polygon; %s' 'f.ex. bbox=5.96, 45.82, 10.49, 47.81', bbox, error) raise ValidationError(_( 'Invalid bbox query parameter, ' ' has to contain 4 values. f.ex. bbox=5.96,45.82,10.49,47.81'), code='bbox-invalid') return self.filter(geometry__intersects=bbox_geometry)