def row_to_country(row): return Country( name=row["ADMIN"], type=row["TYPE"], geometry=Feature(geometry=row['geometry']), code=row["ADM0_A3"], )
async def create_upload_file( collection_name: str = Form(...), is_public: bool = Form(...), file: UploadFile = File(...), current_user: models.User = Depends(deps.get_current_user), ) -> schemas.Collection: print("collection_name", collection_name) print("is_public", is_public) print("file name:", file.filename) print("mime:", file.content_type) collection = schemas.Collection.from_dto( services.collection.create_collection( current_user, CollectionDTO(**{ "name": collection_name, "is_public": is_public }), )) geojson = services.shapefile.convert_zip_to_feature_collection(file) item_dtos = map_features_to_item_dtos( [Feature(**f) for f in geojson["features"]]) for item in item_dtos: item.collection_uuid = collection.uuid services.item.add_collection_items(current_user, collection.uuid, item_dtos) return collection
def test_generic_properties_should_raise_for_string(): with pytest.raises(ValidationError): Feature(**({ "type": "Feature", "geometry": polygon, "properties": "should raise" }))
def test_geometry_collection_iteration(coordinates): """test if feature collection is iterable""" print(coordinates, type(coordinates)) polygon = Point(coordinates=coordinates) feature = Feature(geometry=polygon) gc = FeatureCollection(features=[feature, feature]) iter(gc)
def map_item_dto_to_feature(item: ItemDTO) -> Optional[Feature]: feature = None if item.geometry is not None: if isinstance(item.geometry, str): item.geometry = WKTElement(item.geometry) feature = Feature( geometry=to_shape(item.geometry), properties=item.properties, id=str(item.uuid), ) return feature
def bbox_to_feature( bbox: Tuple[float, float, float, float], properties: Optional[Dict] = None, ) -> Feature: """Create a GeoJSON feature polygon from a bounding box.""" return Feature( **{ "geometry": { "type": "Polygon", "coordinates": [[ [bbox[0], bbox[3]], [bbox[0], bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]], ]], }, "properties": {} or properties, "type": "Feature", })
async def geojson_part( geom: Feature, format: Optional[RasterFormat] = Query( None, description="Output image type." ), layer_params=Depends(self.layer_dependency), img_params: ImageParams = Depends(), dataset_params: DatasetParams = Depends(), render_params: ImageRenderingParams = Depends(), postprocess_params: PostProcessParams = Depends(), colormap: ColorMapParams = Depends(), ): """Handle /feature requests.""" async with self.reader(self.src_path) as src_dst: # type: ignore if self.nodata is not None and dataset_params.nodata is not None: dataset_params.nodata = self.nodata # Adapt options for each reader type self._update_params(src_dst, layer_params) data = await src_dst.feature( geom.dict(exclude_none=True), **layer_params, **dataset_params ) dst_colormap = getattr(src_dst, "colormap", None) if not format: format = RasterFormat.jpeg if data.mask.all() else RasterFormat.png image = data.post_process(**postprocess_params) content = image.render( img_format=format.driver, colormap=colormap or dst_colormap, **format.profile, **render_params, ) return Response(content, media_type=format.mediatype)
def test_debug(): """Should work as expected (create app object).""" app = MosaicDebug(src_path=mosaic) assert app.port == 8080 assert app.endpoint == "http://127.0.0.1:8080" assert app.template_url == "http://127.0.0.1:8080" client = TestClient(app.app) response = client.get("/info.geojson") assert response.status_code == 200 assert response.headers["content-type"] == "application/geo+json" info = Feature(**response.json()) print(info) assert info.properties["quadkeys"] assert info.bbox qk = info.properties["quadkeys"][0] response = client.get(f"/assets?qk={qk}") assert response.status_code == 200 assert response.headers["content-type"] == "application/json" body = response.json() assert isinstance(body, list)
def test_generic_properties_is_dict(): feature = Feature(**test_feature) assert feature.properties["id"] == test_feature["properties"]["id"] assert type(feature.properties) == dict assert not hasattr(feature.properties, "id")
def test_feature_with_null_geometry(): feature = Feature(**test_feature_geom_null) assert feature.geometry is None
def test_geo_interface_protocol(): class Pointy: __geo_interface__ = {"type": "Point", "coordinates": (0.0, 0.0)} feat = Feature(geometry=Pointy()) assert feat.geometry.dict() == Pointy.__geo_interface__