-
Notifications
You must be signed in to change notification settings - Fork 33
add ogc maps #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add ogc maps #236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The `world.tif` was created using https://www.naturalearthdata.com/downloads/10m-raster-data/10m-natural-earth-2/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ | |
| from psycopg.rows import class_row, dict_row | ||
| from pydantic import Field | ||
| from rio_tiler.constants import MAX_THREADS, WGS84_CRS | ||
| from rio_tiler.utils import CRS_to_urn | ||
| from rio_tiler.utils import CRS_to_uri, CRS_to_urn | ||
| from starlette.datastructures import QueryParams | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
|
|
@@ -45,6 +45,7 @@ | |
| DefaultDependency, | ||
| DstCRSParams, | ||
| HistogramParams, | ||
| OGCMapsParams, | ||
| PartFeatureParams, | ||
| StatisticsParams, | ||
| ) | ||
|
|
@@ -113,6 +114,7 @@ class MosaicTilerFactory(BaseFactory): | |
| add_viewer: bool = False | ||
| add_statistics: bool = False | ||
| add_part: bool = False | ||
| add_ogc_maps: bool = False | ||
|
|
||
| conforms_to: Set[str] = field( | ||
| factory=lambda: { | ||
|
|
@@ -142,6 +144,9 @@ def register_routes(self) -> None: | |
| if self.add_part: | ||
| self.part() | ||
|
|
||
| if self.add_ogc_maps: | ||
| self.ogc_maps() | ||
|
|
||
| if self.add_statistics: | ||
| self.statistics() | ||
|
|
||
|
|
@@ -694,6 +699,107 @@ def feature_image( | |
|
|
||
| return Response(content, media_type=media_type, headers=headers) | ||
|
|
||
| ############################################################################ | ||
| # OGC Maps (Optional) | ||
| ############################################################################ | ||
| def ogc_maps(self): # noqa: C901 | ||
| """Register OGC Maps /map` endpoint.""" | ||
|
|
||
| self.conforms_to.update( | ||
| { | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/core", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/crs", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/scaling", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/scaling/width-definition", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/scaling/height-definition", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/spatial-subsetting", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/spatial-subsetting/bbox-definition", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/spatial-subsetting/bbox-crs", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/spatial-subsetting/crs-curie", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/png", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/jpeg", | ||
| "https://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/tiff", | ||
| } | ||
| ) | ||
|
|
||
| # GET endpoints | ||
| @self.router.get( | ||
| "/map", | ||
| operation_id=f"{self.operation_prefix}getMap", | ||
| **img_endpoint_params, | ||
| ) | ||
| def get_map( | ||
| request: Request, | ||
| search_id=Depends(self.path_dependency), | ||
| backend_params=Depends(self.backend_dependency), | ||
| assets_accessor_params=Depends(self.assets_accessor_dependency), | ||
| reader_params=Depends(self.reader_dependency), | ||
| ogc_params=Depends(OGCMapsParams), | ||
| layer_params=Depends(self.layer_dependency), | ||
| dataset_params=Depends(self.dataset_dependency), | ||
| pixel_selection=Depends(self.pixel_selection_dependency), | ||
| post_process=Depends(self.process_dependency), | ||
| colormap=Depends(self.colormap_dependency), | ||
| render_params=Depends(self.render_dependency), | ||
| env=Depends(self.environment_dependency), | ||
| ): | ||
| """OGC Maps API.""" | ||
| with rasterio.Env(**env): | ||
| logger.info( | ||
| f"opening data with backend: {self.backend} and reader {self.dataset_reader}" | ||
| ) | ||
| with self.backend( | ||
| search_id, | ||
| reader=self.dataset_reader, | ||
| reader_options=reader_params.as_dict(), | ||
| **backend_params.as_dict(), | ||
| ) as src_dst: | ||
| if ogc_params.bbox is not None: | ||
| image, assets = src_dst.part( | ||
| ogc_params.bbox, | ||
| dst_crs=ogc_params.crs or src_dst.crs, | ||
| bounds_crs=ogc_params.bbox_crs or WGS84_CRS, | ||
| pixel_selection=pixel_selection, | ||
| width=ogc_params.width, | ||
| height=ogc_params.height, | ||
| max_size=ogc_params.max_size, | ||
| **layer_params.as_dict(), | ||
| **dataset_params.as_dict(), | ||
| **assets_accessor_params.as_dict(), | ||
| ) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should use
when returning the |
||
|
|
||
| else: | ||
| image, assets = src_dst.preview( | ||
| width=ogc_params.width, | ||
| height=ogc_params.height, | ||
| max_size=ogc_params.max_size, | ||
| dst_crs=ogc_params.crs, | ||
| **assets_accessor_params.as_dict(), | ||
| ) | ||
|
|
||
| dst_colormap = getattr(src_dst, "colormap", None) | ||
|
|
||
| if post_process: | ||
| logger.info("post processing image") | ||
| image = post_process(image) | ||
|
|
||
| content, media_type = self.render_func( | ||
| image, | ||
| output_format=ogc_params.format, | ||
| colormap=colormap or dst_colormap, | ||
| **render_params.as_dict(), | ||
| ) | ||
|
|
||
| headers: Dict[str, str] = {} | ||
| if image.bounds is not None: | ||
| headers["Content-Bbox"] = ",".join(map(str, image.bounds)) | ||
| if uri := CRS_to_uri(image.crs): | ||
| headers["Content-Crs"] = f"<{uri}>" | ||
| if OptionalHeader.x_assets in self.optional_headers: | ||
| headers["X-Assets"] = ",".join(assets) | ||
|
|
||
| return Response(content, media_type=media_type, headers=headers) | ||
|
|
||
|
|
||
| def add_search_register_route( # noqa: C901 | ||
| app: FastAPI, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,6 +213,7 @@ def pgstac_info(request: Request) -> Dict: | |
| add_statistics=True, | ||
| add_viewer=True, | ||
| add_part=True, | ||
| add_ogc_maps=True, | ||
| extensions=[ | ||
| searchInfoExtension(), | ||
| ], | ||
|
|
@@ -251,6 +252,7 @@ def pgstac_info(request: Request) -> Dict: | |
| add_statistics=True, | ||
| add_viewer=True, | ||
| add_part=True, | ||
| add_ogc_maps=True, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory we could add |
||
| extensions=[ | ||
| searchInfoExtension(), | ||
| ], | ||
|
|
@@ -268,6 +270,7 @@ def pgstac_info(request: Request) -> Dict: | |
| path_dependency=ItemIdParams, | ||
| router_prefix="/collections/{collection_id}/items/{item_id}", | ||
| add_viewer=True, | ||
| add_ogc_maps=True, | ||
| templates=templates, | ||
| ) | ||
| app.include_router( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we were burning the first 100/1000 items returned by the
/search?