def get_bounding_box(self) -> BoundingBox: """Determine bounding box of all items.""" self.fetch_results() # Avoid querying results twice # Start with an obviously invalid bbox, # which corrects at the first extend_to_geometry call. bbox = BoundingBox(math.inf, math.inf, -math.inf, -math.inf) geometry_field = self.feature_type.resolve_element( self.feature_type.geometry_field.name).child for instance in self: geomery_value = geometry_field.get_value(instance) if geomery_value is None: continue bbox.extend_to_geometry(geomery_value) return bbox
def get_bounding_box(self) -> Optional[BoundingBox]: """Returns a WGS84 BoundingBox for the complete feature. This is used by the GetCapabilities request. It may return ``None`` when the database table is empty, or the custom queryset doesn't return any results. """ geo_expression = conditional_transform(self.geometry_field.name, self.geometry_field.srid, WGS84.srid) bbox = self.get_queryset().aggregate(a=Extent(geo_expression))["a"] return BoundingBox(*bbox, crs=WGS84) if bbox else None
def get_envelope(self, instance, crs: Optional[CRS] = None) -> Optional[BoundingBox]: """Get the bounding box for a single instance. This is only used for native Python rendering. When the database rendering is enabled (GISSERVER_USE_DB_RENDERING=True), the calculation is entirely performed within the query. """ geometries = [ geom for geom in (getattr(instance, f.name) for f in self.geometry_fields) if geom is not None ] if not geometries: return None # Perform the combining of geometries inside libgeos geometry = (geometries[0] if len(geometries) == 1 else reduce( operator.or_, geometries)) if crs is not None and geometry.srid != crs.srid: crs.apply_to(geometry) # avoid clone return BoundingBox.from_geometry(geometry, crs=crs)
def test_parse_5(self): bbox = BoundingBox.from_string("-1,-2,3,4,urn:ogc:def:crs:EPSG::28992") assert bbox.lower_corner == [-1, -2] assert bbox.upper_corner == [3, 4] assert bbox.crs.srid == 28992
def test_parse_4(self): bbox = BoundingBox.from_string("-1,-2,3,4") assert bbox.lower_corner == [-1, -2] assert bbox.upper_corner == [3, 4]
def test_extend(self): bbox = BoundingBox(1, 2, 3, 4) bbox.extend_to(-4, -3, -2, -1) assert bbox.lower_corner == [-4, -3] assert bbox.upper_corner == [3, 4]