Commit 67619431 authored by Cresson Remi's avatar Cresson Remi
Browse files

Merge branch '11-enh_lazy_dates' into 'develop'

ENH: lazy dates formats

Closes #11

See merge request !26
1 merge request!26ENH: lazy dates formats
Showing with 68 additions and 48 deletions
+68 -48
......@@ -69,3 +69,7 @@ indexation:
script:
- pytest -o log_cli=true --log-cli-level=INFO --junitxml=report_indexation_test.xml test/indexation_test.py
dates:
extends: .test_base
script:
- pytest -o log_cli=true --log-cli-level=INFO --junitxml=report_dates_test.xml test/dates_test.py
File moved
......@@ -5,6 +5,19 @@ This module aims to deal with dates
import datetime
def get_timestamp(dt):
"""Converts datetime.datetime into a timestamp (in seconds)
Args:
dt: datetime.datetime instance
Returns:
timestamp (in seconds)
"""
return dt.replace(tzinfo=datetime.timezone.utc).timestamp()
def str2datetime(datestr):
"""
Converts an input date as string into a datetime instance.
......
......@@ -3,52 +3,23 @@ This module contains stuff for the spatio-temporal indexation of scenes.
"""
import datetime
import rtree
from scenes.dates import get_timestamp, any2datetime
def get_timestamp(dt):
"""Converts datetime.datetime into a timestamp (in seconds)
Args:
dt: datetime.datetime instance
Returns:
timestamp (in seconds)
"""
return dt.replace(tzinfo=datetime.timezone.utc).timestamp()
def new_bbox(bbox_wgs84, dt):
"""Return a bounding box in the domain (lat, lon, time)
Args:
bbox_wgs84: Bounding box (in WGS84)
dt: date datetime.datetime
Returns:
item for rtree
"""
dt_min = dt - datetime.timedelta(days=1)
dt_max = dt + datetime.timedelta(days=1)
return bbox(bbox_wgs84=bbox_wgs84, dt_min=dt_min, dt_max=dt_max)
def bbox(bbox_wgs84, dt_min, dt_max):
def _bbox(bbox_wgs84, date_min, date_max):
"""Return a bounding box in the domain (lat, lon, time)
Args:
bbox_wgs84: The bounding box in WGS84 (BoundingBox instance)
dt_min: date min (datetime.datetime)
dt_max: date max (datetime.datetime)
date_min: date min (datetime.datetime or str)
date_max: date max (datetime.datetime or str)
Returns:
item for rtree
"""
return (bbox_wgs84.xmin, bbox_wgs84.ymin, get_timestamp(dt_min),
bbox_wgs84.xmax, bbox_wgs84.ymax, get_timestamp(dt_max))
return (bbox_wgs84.xmin, bbox_wgs84.ymin, get_timestamp(any2datetime(date_min)),
bbox_wgs84.xmax, bbox_wgs84.ymax, get_timestamp(any2datetime(date_max)))
class Index:
......@@ -66,38 +37,42 @@ class Index:
properties.dimension = 3
self.index = rtree.index.Index(properties=properties)
for scene_idx, scene in enumerate(scenes_list):
self.index.insert(scene_idx, new_bbox(bbox_wgs84=scene.bbox_wgs84, dt=scene.acquisition_date))
dt = scene.acquisition_date
dt_min = dt - datetime.timedelta(days=1)
dt_max = dt + datetime.timedelta(days=1)
new_bbox = _bbox(bbox_wgs84=scene.bbox_wgs84, date_min=dt_min, date_max=dt_max)
self.index.insert(scene_idx, new_bbox)
def find_indices(self, bbox_wgs84, dt_min=None, dt_max=None):
def find_indices(self, bbox_wgs84, date_min=None, date_max=None):
"""Search the intersecting elements, and return their indices
Args:
bbox_wgs84: The bounding box in WGS84 (BoundingBox instance)
dt_min: date min (datetime.datetime) (Default value = None)
dt_max: date max (datetime.datetime) (Default value = None)
date_min: date min (datetime.datetime or str) (Default value = None)
date_max: date max (datetime.datetime or str) (Default value = None)
Returns:
list of indices
"""
if not dt_min:
dt_min = datetime.datetime.strptime("2000-01-01", "%Y-%m-%d")
if not dt_max:
dt_max = datetime.datetime.strptime("3000-01-01", "%Y-%m-%d")
bbox_search = bbox(bbox_wgs84=bbox_wgs84, dt_min=dt_min, dt_max=dt_max)
if not date_min:
date_min = datetime.datetime.strptime("2000-01-01", "%Y-%m-%d")
if not date_max:
date_max = datetime.datetime.strptime("3000-01-01", "%Y-%m-%d")
bbox_search = _bbox(bbox_wgs84=bbox_wgs84, date_min=date_min, date_max=date_max)
return self.index.intersection(bbox_search)
def find(self, bbox_wgs84, dt_min=None, dt_max=None):
def find(self, bbox_wgs84, date_min=None, date_max=None):
"""Search the intersecting elements, and return them
Args:
bbox_wgs84: The bounding box in WGS84 (BoundingBox instance)
dt_min: date min (datetime.datetime) (Default value = None)
dt_max: date max (datetime.datetime) (Default value = None)
date_min: date min (datetime.datetime or str) (Default value = None)
date_max: date max (datetime.datetime or str) (Default value = None)
Returns:
list of indices
"""
indices = self.find_indices(bbox_wgs84=bbox_wgs84, dt_min=dt_min, dt_max=dt_max)
indices = self.find_indices(bbox_wgs84=bbox_wgs84, date_min=date_min, date_max=date_max)
return [self.scenes_list[i] for i in indices]
# -*- coding: utf-8 -*-
from scenes_test_base import ScenesTestBase
import datetime
from scenes import Index, BoundingBox, dates
class DatesTest(ScenesTestBase):
def test_input_dates_formats(self):
datestr1 = "04/10/1986"
datestr2 = "04-10-1986"
datestr3 = "1986-10-04"
dt = datetime.datetime(year=1986, month=10, day=4)
assert dates.any2datetime(datestr1) == dt
assert dates.any2datetime(datestr2) == dt
assert dates.any2datetime(datestr3) == dt
# Index test
idx = Index([])
dummy_bbox = BoundingBox(0, 0, 0, 0)
_ = idx.find(dummy_bbox, date_min=datestr1)
_ = idx.find(dummy_bbox, date_min=datestr2)
_ = idx.find(dummy_bbox, date_min=datestr3)
if __name__ == '__main__':
unittest.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment