diff --git a/clients/python/sliderule/raster.py b/clients/python/sliderule/raster.py index ca70ae9f..899a5542 100644 --- a/clients/python/sliderule/raster.py +++ b/clients/python/sliderule/raster.py @@ -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 @@ -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'], diff --git a/clients/python/tests/test_worldcover.py b/clients/python/tests/test_worldcover.py index 6a36e0df..01fb5c62 100644 --- a/clients/python/tests/test_worldcover.py +++ b/clients/python/tests/test_worldcover.py @@ -3,6 +3,8 @@ import pytest from pathlib import Path import sliderule +from sliderule import raster +import geopandas as gpd TESTDIR = Path(__file__).parent @@ -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()}" +