diff --git a/geostructures/collections.py b/geostructures/collections.py index f3a4f99..1a9b6d6 100644 --- a/geostructures/collections.py +++ b/geostructures/collections.py @@ -229,6 +229,9 @@ def _get_dt(rec): ): return None + if isinstance(dt_start, str) and isinstance(dt_start, str): + return TimeInterval.from_str(dt_start, dt_end) + if not (dt_start and dt_end) or dt_start == dt_end: return dt_start or dt_end diff --git a/geostructures/multistructures.py b/geostructures/multistructures.py index fd3620e..336ecbc 100644 --- a/geostructures/multistructures.py +++ b/geostructures/multistructures.py @@ -15,12 +15,11 @@ PolygonLikeMixin, MultiShapeBase, PointLikeMixin, LineLikeMixin, SimpleShapeMixin ) -from geostructures.time import GEOTIME_TYPE +from geostructures.time import GEOTIME_TYPE, TimeInterval from geostructures._geometry import convex_hull from geostructures.calc import haversine_distance_meters from geostructures.coordinates import Coordinate from geostructures.structures import GeoCircle, GeoLineString, GeoPoint, GeoPolygon, PolygonBase -from geostructures.utils.functions import get_dt_from_geojson_props class MultiGeoLineString(MultiShapeBase, LineLikeMixin, SimpleShapeMixin): @@ -108,12 +107,20 @@ def from_geojson( ) for line in geom.get('coordinates', []) ] properties = gjson.get('properties', {}) - dt = get_dt_from_geojson_props( - properties, - time_start_property, - time_end_property, - time_format - ) + dt = None + if time_start_property in properties or time_end_property in properties: + # Pop time field so it doesn't remain in properties + dt_start = properties.pop(time_start_property, None) + dt_end = properties.pop(time_end_property, None) + + if dt_start and not dt_end: + dt = TimeInterval.from_str(dt_start, dt_start, time_format) + + elif dt_end and not dt_start: + dt = TimeInterval.from_str(dt_end, dt_end, time_format) + + else: + dt = TimeInterval.from_str(dt_start, dt_end, time_format) return MultiGeoLineString( lines, dt=dt, @@ -305,12 +312,20 @@ def from_geojson( for coord in geom.get('coordinates', []) ] properties = gjson.get('properties', {}) - dt = get_dt_from_geojson_props( - properties, - time_start_property, - time_end_property, - time_format - ) + dt = None + if time_start_property in properties or time_end_property in properties: + # Pop time field so it doesn't remain in properties + dt_start = properties.pop(time_start_property, None) + dt_end = properties.pop(time_end_property, None) + + if dt_start and not dt_end: + dt = TimeInterval.from_str(dt_start, dt_start, time_format) + + elif dt_end and not dt_start: + dt = TimeInterval.from_str(dt_end, dt_end, time_format) + + else: + dt = TimeInterval.from_str(dt_start, dt_end, time_format) return MultiGeoPoint( points, dt=dt, @@ -545,12 +560,20 @@ def from_geojson( shapes.append(GeoPolygon(shell, holes=holes)) properties = gjson.get('properties', {}) - dt = get_dt_from_geojson_props( - properties, - time_start_property, - time_end_property, - time_format - ) + dt = None + if time_start_property in properties or time_end_property in properties: + # Pop time field so it doesn't remain in properties + dt_start = properties.pop(time_start_property, None) + dt_end = properties.pop(time_end_property, None) + + if dt_start and not dt_end: + dt = TimeInterval.from_str(dt_start, dt_start, time_format) + + elif dt_end and not dt_start: + dt = TimeInterval.from_str(dt_end, dt_end, time_format) + + else: + dt = TimeInterval.from_str(dt_start, dt_end, time_format) return MultiGeoPolygon( shapes, dt=dt, diff --git a/geostructures/structures.py b/geostructures/structures.py index 5c2d678..522f7e7 100644 --- a/geostructures/structures.py +++ b/geostructures/structures.py @@ -23,7 +23,7 @@ _RE_LINESTRING_WKT, LineLikeMixin, PointLikeMixin, PolygonLikeMixin, SingleShapeBase, SimpleShapeMixin ) -from geostructures.time import GEOTIME_TYPE +from geostructures.time import GEOTIME_TYPE, TimeInterval from geostructures.coordinates import Coordinate from geostructures.calc import ( inverse_haversine_radians, @@ -35,7 +35,7 @@ circumscribing_circle_for_polygon, do_edges_intersect, find_line_intersection, is_counter_clockwise ) -from geostructures.utils.functions import round_half_up, get_dt_from_geojson_props, is_sub_list +from geostructures.utils.functions import round_half_up, is_sub_list from geostructures.utils.logging import warn_once if TYPE_CHECKING: # pragma: no cover @@ -489,12 +489,20 @@ def from_geojson( holes = [GeoPolygon(ring) for ring in rings[1:]] properties = gjson.get('properties', {}) - dt = get_dt_from_geojson_props( - properties, - time_start_property, - time_end_property, - time_format - ) + dt = None + if time_start_property in properties or time_end_property in properties: + # Pop time field so it doesn't remain in properties + dt_start = properties.pop(time_start_property, None) + dt_end = properties.pop(time_end_property, None) + + if dt_start and not dt_end: + dt = TimeInterval.from_str(dt_start, dt_start, time_format) + + elif dt_end and not dt_start: + dt = TimeInterval.from_str(dt_end, dt_end, time_format) + + else: + dt = TimeInterval.from_str(dt_start, dt_end, time_format) return GeoPolygon(rings[0], holes=holes, dt=dt, properties=properties) @@ -1394,12 +1402,20 @@ def from_geojson( for x in geom.get('coordinates', []) ] properties = gjson.get('properties', {}) - dt = get_dt_from_geojson_props( - properties, - time_start_property, - time_end_property, - time_format - ) + dt = None + if time_start_property in properties or time_end_property in properties: + # Pop time field so it doesn't remain in properties + dt_start = properties.pop(time_start_property, None) + dt_end = properties.pop(time_end_property, None) + + if dt_start and not dt_end: + dt = TimeInterval.from_str(dt_start, dt_start, time_format) + + elif dt_end and not dt_start: + dt = TimeInterval.from_str(dt_end, dt_end, time_format) + + else: + dt = TimeInterval.from_str(dt_start, dt_end, time_format) return GeoLineString(coords, dt=dt, properties=properties) @classmethod @@ -1635,12 +1651,20 @@ def from_geojson( coord = Coordinate(**dict(zip(('longitude', 'latitude', 'z'), geom['coordinates']))) properties = gjson.get('properties', {}) - dt = get_dt_from_geojson_props( - properties, - time_start_property, - time_end_property, - time_format - ) + dt = None + if time_start_property in properties or time_end_property in properties: + # Pop time field so it doesn't remain in properties + dt_start = properties.pop(time_start_property, None) + dt_end = properties.pop(time_end_property, None) + + if dt_start and not dt_end: + dt = TimeInterval.from_str(dt_start, dt_start, time_format) + + elif dt_end and not dt_start: + dt = TimeInterval.from_str(dt_end, dt_end, time_format) + + else: + dt = TimeInterval.from_str(dt_start, dt_end, time_format) return GeoPoint(coord, dt=dt, properties=properties) diff --git a/tests/utils/test_functions.py b/tests/utils/test_functions.py index 8100561..aa1c282 100644 --- a/tests/utils/test_functions.py +++ b/tests/utils/test_functions.py @@ -2,7 +2,6 @@ from datetime import datetime, timezone from geostructures.utils.functions import * -from geostructures.time import TimeInterval def test_default_to_zulu(caplog): @@ -13,28 +12,6 @@ def test_default_to_zulu(caplog): assert default_to_zulu(dt).tzinfo == timezone.utc -def test_get_dt_from_geojson_props(): - props = { - 'datetime_start': '2020-01-01 00:00:00', - 'datetime_end': '2020-01-01 00:00:00', - } - assert get_dt_from_geojson_props(props) == TimeInterval(datetime(2020, 1, 1), datetime(2020, 1, 1)) - - props = {} - assert get_dt_from_geojson_props(props) is None - - props = { - 'datetime_start': '2020-01-01 00:00:00', - } - assert get_dt_from_geojson_props(props) == datetime(2020, 1, 1) - - props = { - 'start': '2020-01-01', - 'end': '2020-01-01', - } - assert get_dt_from_geojson_props(props, time_start_field='start', time_end_field='end', time_format='%Y-%m-%d') == TimeInterval(datetime(2020, 1, 1), datetime(2020, 1, 1)) - - def test_round_half_up(): assert round_half_up(1.59, 1) == 1.6 assert round_half_up(1.51, 1) == 1.5