def _create_square(x, y, zoom) -> geojson.MultiPolygon: """ Function for creating a geojson.MultiPolygon square representing a single OSM tile grid square :param x: osm tile grid x :param y: osm tile grid y :param zoom: osm tile grid zoom level :return: geojson.MultiPolygon in EPSG:4326 """ # Maximum resolution MAXRESOLUTION = 156543.0339 # X/Y axis limit max = MAXRESOLUTION * 256 / 2 # calculate extents step = max / (2**(zoom - 1)) xmin = x * step - max ymin = y * step - max xmax = (x + 1) * step - max ymax = (y + 1) * step - max # make a shapely multipolygon multipolygon = MultiPolygon([ Polygon([(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]) ]) # use the database to transform the geometry from 3857 to 4326 transformed_geometry = ST_Transform( shape.from_shape(multipolygon, 3857), 4326) # use DB to get the geometry as geojson return geojson.loads( db.engine.execute(transformed_geometry.ST_AsGeoJSON()).scalar())
def _make_4326_polygon_from_bbox(bbox: list, srid: int) -> Polygon: """ make a shapely Polygon in SRID 4326 from bbox and srid""" try: polygon = box(bbox[0], bbox[1], bbox[2], bbox[3]) if not srid == 4326: geometry = shape.from_shape(polygon, srid) geom_4326 = db.engine.execute(ST_Transform(geometry, 4326)).scalar() polygon = shape.to_shape(geom_4326) except Exception as e: raise ProjectSearchServiceError(f"error making polygon: {e}") return polygon
def _get_area_sqm(polygon: Polygon) -> float: """ get the area of the polygon in square metres """ return db.engine.execute( ST_Area(ST_Transform(shape.from_shape(polygon, 4326), 3857))).scalar()