Skip to content

Commit

Permalink
Fixing extraction method bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysrevans3 committed Jan 9, 2024
1 parent e7150f6 commit 2ba1e0c
Show file tree
Hide file tree
Showing 23 changed files with 605 additions and 128 deletions.
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"header = stac_generator.plugins.extraction_methods.header.header:HeaderExtract",
"iso19115 = stac_generator.plugins.extraction_methods.iso19115:ISO19115Extract",
"xml = stac_generator.plugins.extraction_methods.xml:XMLExtract",
"open_zip = stac_generator.plugins.extraction_methods.open_zip:ZipExtract",
"datetime_bound_to_centroid = stac_generator.plugins.extraction_methods.datetime_bound_to_centroid:DatetimeBoundToCentroidExtract",
"elasticsearch_aggregation = stac_generator.plugins.extraction_methods.elasticsearch_aggregation:ElasticsearchAggregationExtract",
"json_file = stac_generator.plugins.extraction_methods.json_file:JsonFileExtract",
"path_parts = stac_generator.plugins.extraction_methods.path_parts:PathPartsExtract",
Expand All @@ -84,6 +86,7 @@
"geometry_line = stac_generator.plugins.extraction_methods.geometry_line:GeometryLineExtract",
"geometry_point = stac_generator.plugins.extraction_methods.geometry_point:GeometryPointExtract",
"geometry_polygon = stac_generator.plugins.extraction_methods.geometry_polygon:GeometryPolygonExtract",
"geometry_to_bbox = stac_generator.plugins.extraction_methods.geometry_to_bbox:GeometryToBboxExtract",
"string_join = stac_generator.plugins.extraction_methods.string_join:StringJoinExtract",
"string_template = stac_generator.plugins.extraction_methods.string_template:StringTemplateExtract",
"facet_prefix = stac_generator.plugins.extraction_methods.facet_prefix:FacetPrefixExtract",
Expand Down
8 changes: 4 additions & 4 deletions stac_generator/core/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def __init__(self, root_path: str):
for file_path in Path(root_path).rglob("*.y*ml"):
_ = self._load_data(file_path)

# only used during the
del self.location_map

def _load_data(self, file: Path) -> Recipe:
"""
Loads the yaml files from the root path and builds the recipe dictionary and map of
Expand Down Expand Up @@ -159,7 +156,7 @@ def get(self, path: str, stac_type: str) -> Recipe:
:param path: Path for which to retrieve the recipe
"""
if path in self.recipes[stac_type]:
return self.load_recipe(path)
return self.load_recipe(path, stac_type)

for parent in chain([path], Path(path).parents):
if parent in self.paths_map[stac_type]:
Expand All @@ -168,3 +165,6 @@ def get(self, path: str, stac_type: str) -> Recipe:
return self.load_recipe(key, stac_type)

raise ValueError(f"No Recipe found for path: {path}")

def get_maps(self):
return self.paths_map, self.location_map
3 changes: 3 additions & 0 deletions stac_generator/core/extraction_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(self, **kwargs):
# Override with specific processor settings
self._set_attrs(kwargs)

if not hasattr(self, "exists_key"):
self.exists_key = "$"

def _set_attrs(self, conf: dict) -> None:
"""
Set instance attributes
Expand Down
10 changes: 6 additions & 4 deletions stac_generator/plugins/extraction_methods/asset_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,22 @@ def run(self, body: dict, **kwargs) -> dict:

for asset in body["assets"].values():
for list_term in self.list_terms:
body[list_term["name"]].append(asset[list_term["key"]])
if list_term["key"] in asset:
body[list_term["name"]].append(asset[list_term["key"]])

for sum_term in self.sum_terms:
body[sum_term["name"]] += asset[sum_term["key"]]
if sum_term["key"] in asset:
body[sum_term["name"]] += asset[sum_term["key"]]

for avg_term in self.avg_terms:
body[avg_term["name"]] /= len(body["assets"])

for min_term in self.min_terms:
if asset[min_term["key"]] < body[min_term["name"]]:
if min_term["key"] in asset and asset[min_term["key"]] < body[min_term["name"]]:
body[min_term["name"]] = asset[min_term["key"]]

for max_term in self.max_terms:
if asset[max_term["key"]] < body[max_term["name"]]:
if max_term["key"] in asset and asset[max_term["key"]] < body[max_term["name"]]:
body[max_term["name"]] = asset[max_term["key"]]

return body
16 changes: 12 additions & 4 deletions stac_generator/plugins/extraction_methods/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ class BboxExtract(BaseExtractionMethod):

def run(self, body: dict, **kwargs):
try:
west = body[self.coordinate_keys[0]]
south = body[self.coordinate_keys[1]]
east = body[self.coordinate_keys[2]]
north = body[self.coordinate_keys[3]]

coordinates = [
[
float(body[self.coordinate_keys[0]]),
float(body[self.coordinate_keys[1]]),
float(west) if west is not None else west,
float(south) if south is not None else south,
],
[
float(body[self.coordinate_keys[2]]),
float(body[self.coordinate_keys[3]]),
float(east) if east is not None else east,
float(north) if north is not None else north,
],
]

Expand All @@ -57,6 +62,9 @@ def run(self, body: dict, **kwargs):
"coordinates": coordinates,
}

except TypeError:
LOGGER.warning("Unable to convert bbox.", exc_info=True)

except KeyError:
LOGGER.warning("Unable to convert bbox.", exc_info=True)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
__author__ = "Richard Smith"
__date__ = "28 May 2021"
__copyright__ = "Copyright 2018 United Kingdom Research and Innovation"
__license__ = "BSD - see LICENSE file in top-level package directory"
__contact__ = "richard.d.smith@stfc.ac.uk"


import logging

from datetime import datetime

# Package imports
from stac_generator.core.extraction_method import BaseExtractionMethod

LOGGER = logging.getLogger(__name__)


class DatetimeBoundToCentroidExtract(BaseExtractionMethod):
"""
Processor Name: ``datetime_bound_to_centroid``
Description:
Accepts a dictionary of coordinate values and converts to `RFC 7946, section 5 <https://tools.ietf.org/html/rfc7946#section-5>`_
formatted bbox.
Configuration Options:
- ``coordinate_keys``: ``REQUIRED`` list of keys to convert to bbox array. Ordering is respected.
Example Configuration:
.. code-block:: yaml
- method: stac_bbox
inputs:
output_term: polygon
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)

if not hasattr(self, "start_term"):
self.start_term = {"name": "start_datetime", "format": "%Y-%m-%dT%H:%M:%S"}

if "name" not in self.start_term:
self.start_term["name"] = "start_datetime"

if "format" not in self.start_term:
self.start_term["format"] = "%Y-%m-%dT%H:%M:%S"

if not hasattr(self, "end_term"):
self.end_term = {"name": "end_datetime", "format": "%Y-%m-%dT%H:%M:%S"}

if "name" not in self.end_term:
self.end_term["name"] = "start_datetime"

if "format" not in self.end_term:
self.end_term["format"] = "%Y-%m-%dT%H:%M:%S"

if not hasattr(self, "output_term"):
self.output_term = {"name": "datetime", "format": "%Y-%m-%dT%H:%M:%S"}

if "name" not in self.output_term:
self.output_term["name"] = "datetime"

if "format" not in self.output_term:
self.output_term["format"] = "%Y-%m-%dT%H:%M:%S"

def run(self, body: dict, **kwargs):

start_datetime = datetime.strptime(body[self.start_term["name"]], self.start_term["format"])
end_datetime = datetime.strptime(body[self.end_term["name"]], self.end_term["format"])

centroid_datetime = start_datetime + (end_datetime - start_datetime) / 2

body[self.output_term["name"]] = centroid_datetime.strftime(self.output_term["format"])

return body
6 changes: 0 additions & 6 deletions stac_generator/plugins/extraction_methods/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ class DefaultExtract(BaseExtractionMethod):
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)

if not hasattr(self, "exists_key"):
self.exists_key = "$"

def run(self, body: dict, **kwargs) -> dict:
defaults = {}
for default_key, default_value in self.defaults.items():
Expand Down
Loading

0 comments on commit 2ba1e0c

Please sign in to comment.