I’m writing some backend scripts in python that have to be coupled with a web interface that uses django.
The web application allows to view layers that comes from different web services. As part of my tasks I developed a tool that request elevation data from a geoserver (a WCS service) and then, based on the a previous line drew by a user in the front end, is able to generate a cross section of the underlying data.
So, in short: when a user draws a line over a layer and then clicks draw-profile, my function request the data, intersects the line when the associated raster/layer and returns a figure to the front-end.
The problem with that approach is that each time the user clicks draw-profile, the request is made again, which takes some time.
When the request is made, the data is stored in a DTM object
(class that I made) in a self.raster
property.
A simplified version of the function that requests the data is shown below.
def get_dtm_from_wcs100(
self,
raster_source: str = "YY",
layer: str = "XX",
format: str = "image/tiff",
version: str = "1.0.0",
resx: int = 1,
resy: int = 1,
) -> None:
"""
Get raster and metadata from a WCS service
Parameters
----------
raster_source : str, optional
URL of WCS service. The default is "YY" which is the company geoserver
layer : str, optional
CoverageID. The default is 'XX'.
format : str, optional
File type of the request. The default is 'image/tiff'.
version : str, optional
Version of the WCS protocol. The default is "1.0.0".
resx : int, optional
Pixel resolution in x-coordinate. The default is 1.
resy : int, optional
Pixel resolution in y-coordinate. The default is 1.
Returns
-------
None.
"""
try:
# Connect to WCS and try getting a coverage
wcs = WebCoverageService(raster_source, version=version)
response = wcs.getCoverage(
identifier=layer,
bbox=self.bbox,
crs=self.epsg,
format=format,
resx=resx,
resy=resy,
)
except Exception as e:
raise Exception("An unexpected error has ocurred:", e)
else:
self._read_response(response)
self._set_masked_raster()
When self._read_response(response)
runs the self.raster
(numpy array
) property is filled.
Do you know what could I do in order to store somehow this response and not have to call it each time I want to draw a profile?