Skip to content

Commit

Permalink
fixed int64 time overlfow, fixes #445
Browse files Browse the repository at this point in the history
  • Loading branch information
elidwa committed Nov 18, 2024
1 parent 25125fb commit ad9ed05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clients/python/sliderule/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def sample(asset, coordinates, parms={}):
**columns
}

# GPS Offset between unix epoch and gps epoch in milliseconds
gps_offset_msecs = 315964800.0 * 1000

# Populate Columns
i = 0
per_coord_index = 0
Expand All @@ -142,7 +145,7 @@ def sample(asset, coordinates, parms={}):
latitude = coordinates[per_coord_index][1]
per_coord_index += 1
for raster_sample in input_coord_response:
columns['time'][i] = numpy.int64((raster_sample['time'] + 315964800.0) * 1000000000)
columns['time'][i] = numpy.int64((raster_sample['time'] + gps_offset_msecs) * 1e6) # int64 with gps nanoseconds will overflow in year 2272
columns['longitude'][i] = numpy.double(longitude)
columns['latitude'][i] = numpy.double(latitude)
columns['file'] += raster_sample['file'],
Expand Down
25 changes: 25 additions & 0 deletions clients/python/tests/test_worldcover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest
from pathlib import Path
import sliderule
from sliderule import raster
import geopandas as gpd

TESTDIR = Path(__file__).parent

Expand All @@ -24,3 +26,26 @@ def test_vrt(self, init):
assert abs(rsps["samples"][0][0]["value"] - vrtValue) < sigma
assert rsps["samples"][0][0]["file"] == vrtFile
assert rsps["samples"][0][0]["time"] == vrtFileTime

def test_time_overflow(self, init):
region = [ {"lon": -108.34, "lat": 38.89},
{"lon": -107.76, "lat": 38.90},
{"lon": -107.78, "lat": 39.26},
{"lon": -108.36, "lat": 39.25},
{"lon": -108.34, "lat": 38.89} ]

gfp = gpd.GeoDataFrame(geometry=gpd.points_from_xy([-108.1, -108.3], [39.1, 39.2]), crs='EPSG:4326')
points = [[x,y] for x,y in zip(gfp.geometry.x , gfp.geometry.y)]
gdf = raster.sample("esa-worldcover-10meter", points)

expected_time = "2021-06-30 00:00:18"
expected_value = 10.0

# Check if all timestamps match the expected_time (used as index)
assert all(str(index) == expected_time for index in gdf.index), \
f"Time mismatch. Expected: {expected_time}, Found: {gdf.index.tolist()}"

# Check if all values match the expected value
assert all(gdf["value"] == expected_value), \
f"Value mismatch. Expected: {expected_value}, Found: {gdf['value'].tolist()}"

0 comments on commit ad9ed05

Please sign in to comment.