Skip to content

Commit

Permalink
Added POST endpoint for image download
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienJaussaudGeosys committed Jun 4, 2024
1 parent f3a5cac commit 1190c95
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
8 changes: 4 additions & 4 deletions geosyspy/geosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def get_satellite_coverage_image_references_post(self,

return df, images_references

def download_image(self, image_ref, indicator: str = "", path: str = ""):
def download_image(self, polygon, image_id, indicator: str = "", path: str = ""):
"""Downloads a satellite image locally
Args:
Expand All @@ -339,11 +339,11 @@ def download_image(self, image_ref, indicator: str = "", path: str = ""):
path (str): the path to download the image to
"""

response_zipped_tiff = self.__map_product_service.get_zipped_tiff(
image_ref.season_field_id, image_ref.image_id, indicator = indicator
response_zipped_tiff = self.__map_product_service.get_tiff_by_geometry(
polygon, image_id, indicator
)
if path == "":
file_name = image_ref.image_id.replace("|", "_")
file_name = image_id.replace("|", "_")
path = Path.cwd() / f"image_{file_name}_tiff.zip"
with open(path, "wb") as f:
self.logger.info("writing to %s", path)
Expand Down
33 changes: 32 additions & 1 deletion geosyspy/services/map_product_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def get_satellite_coverage_post(self, polygon: str,
end_date: str = end_date.strftime("%Y-%m-%d")
sensors: list[str] = [elem.value for elem in sensors_collection]

parameters = f"?Image.Sensor=$in:{'|'.join(sensors)}&coveragePercent=$gte:20&$filter=Image.Date >= '{start_date}' and Image.Date <= '{end_date}'&$limit=None"
parameters = f"?Image.Sensor=$in:{'|'.join(sensors)}&coveragePercent=$gte:20&$filter=Image.Date >= '{start_date}' and Image.Date <= '{end_date}'&$limit=None&mask=Auto"
payload = {
"seasonFields": [
{
Expand Down Expand Up @@ -196,6 +196,37 @@ def get_zipped_tiff(self, field_id: str,
)
return response_zipped_tiff

def get_tiff_by_geometry(self, field_geometry: str,
image_id: str,
indicator: str):
parameters = f"/INSEASON_{indicator.upper()}/image.tiff.zip?resolution=Sensor"


download_tiff_url: str = urljoin(
self.base_url,
GeosysApiEndpoints.FLM_BASE_REFERENCE_MAP_POST.value + parameters,
)
payload = {
"image": {
"id": image_id
},
"seasonField": {
"geometry": field_geometry
}
}

response_zipped_tiff = self.http_client.post(
download_tiff_url,
payload,
{"X-Geosys-Task-Code": PRIORITY_HEADERS[self.priority_queue]},
)
if response_zipped_tiff.status_code != 200:
raise HTTPError(
"Unable to download tiff.zip file. Server error: "
+ str(response_zipped_tiff.status_code)
)
return response_zipped_tiff

def get_product(self, field_id: str, image_id: str, indicator: str, image: str = None):
"""
Retrieves image product for a given season field and image reference from MP API.
Expand Down
2 changes: 2 additions & 0 deletions geosyspy/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ class GeosysApiEndpoints(Enum):
VTS_BY_PIXEL_ENDPOINT = "vegetation-time-series/v1/season-fields/pixels"
FLM_CATALOG_IMAGERY = "field-level-maps/v5/season-fields/{}/catalog-imagery"
FLM_BASE_REFERENCE_MAP = "field-level-maps/v5/season-fields/{}/coverage"
FLM_BASE_REFERENCE_MAP_POST = "field-level-maps/v5/maps/base-reference-map"
FLM_CATALOG_IMAGERY_POST = "field-level-maps/v5/season-fields/catalog-imagery"


WEATHER_ENDPOINT = "Weather/v1/weather"
ANALYTICS_FABRIC_ENDPOINT = "analytics/metrics"
ANALYTICS_FABRIC_LATEST_ENDPOINT = "analytics/metrics-latest"
Expand Down

0 comments on commit 1190c95

Please sign in to comment.