Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions geostructures/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 43 additions & 20 deletions geostructures/multistructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
64 changes: 44 additions & 20 deletions geostructures/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
23 changes: 0 additions & 23 deletions tests/utils/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down