Skip to content

Commit

Permalink
Fix srs handling
Browse files Browse the repository at this point in the history
  • Loading branch information
azvoleff committed Mar 9, 2025
1 parent 657e046 commit 64de899
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
20 changes: 15 additions & 5 deletions te_schemas/aoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _geojson_to_ds(geojson):
with tempfile.NamedTemporaryFile(delete=False, suffix=".geojson") as temp_file:
temp_file.write(json.dumps(geojson).encode("utf-8"))
temp_file_path = temp_file.name
logger.debug(f"Wrote temporary file with geojsons to {temp_file.name}")
logger.debug(f"In _geojson_to_ds wrote temporary file to {temp_file.name}")

return ogr.Open(temp_file_path)

Expand All @@ -43,11 +43,14 @@ def _ds_to_geojson(ds):
temp_file = _make_temp_name()
driver = ogr.GetDriverByName("GeoJSON")
temp_ds = driver.CreateDataSource(temp_file)
temp_layer = temp_ds.CreateLayer("layer_name", geom_type=ogr.wkbPolygon)
temp_layer = temp_ds.CreateLayer(
"layer_name", srs=ds.GetLayer(0).GetSpatialRef(), geom_type=ogr.wkbPolygon
)
for aoi_layer in ds:
for feature in aoi_layer:
temp_layer.CreateFeature(feature)
temp_ds = None
logger.debug(f"In _ds_to_geojson wrote temporary file to {temp_file}")

with open(temp_file, "r") as file:
return json.load(file)
Expand Down Expand Up @@ -123,7 +126,14 @@ def is_valid(self):

@property
def crs(self):
return self.get_ds().GetSpatialReference().ExportToWkt()
return self.get_srs().ExportToWkt()

def get_crs_wkt(self):
return self.crs

def get_srs(self):
aoi = self.get_ds()
return aoi.GetLayer(0).GetSpatialRef()

def meridian_split(self, as_extent=False, out_format="geojson"):
"""
Expand Down Expand Up @@ -279,7 +289,7 @@ def bounding_box_gee_geojson(self):
"""

aoi = self.get_ds()
datatype = aoi.GetLayer()[0].GetGeomType()
datatype = aoi.GetLayer(0).GetGeomType()
if datatype == "polygon":
return self.meridian_split()
elif datatype == "point":
Expand Down Expand Up @@ -308,7 +318,7 @@ def bounding_box_gee_geojson(self):
else:
raise RuntimeError(
f"Failed to process area of interest - unknown geometry "
f"type: {aoi.GetLayer()[0].GetGeomType()}"
f"type: {aoi.GetLayer(0).GetGeomType()}"
)

def calc_frac_overlap(self, in_geom):
Expand Down
53 changes: 53 additions & 0 deletions tests/test_aoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,56 @@ def test_meridian_split():

assert AOI(geom).meridian_split() == geoms_split
assert AOI(geom).get_geojson(split=True) == AOI(geoms_split).get_geojson()


def test_crs_not_specified():
aoi = AOI(
{
"type": "Polygon",
"coordinates": [
[
[125.6, 10.1],
[125.7, 10.1],
[125.7, 10.2],
[125.6, 10.2],
[125.6, 10.1],
]
],
}
)
assert aoi.get_srs().GetAuthorityCode(None) == "4326"
assert aoi.get_srs().GetAuthorityName(None) == "EPSG"


def test_crs_3857():
aoi = AOI(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [125.6, 10.1]},
"properties": {"name": "Dinagat Islands"},
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[125.6, 10.1],
[125.7, 10.1],
[125.7, 10.2],
[125.6, 10.2],
[125.6, 10.1],
]
],
},
"properties": {"name": "Polygon Feature"},
},
],
"crs": {"type": "name", "properties": {"name": "EPSG:3857"}},
}
)
assert aoi.get_srs().GetAuthorityCode(None) == "3857"
assert aoi.get_srs().GetAuthorityName(None) == "EPSG"

0 comments on commit 64de899

Please sign in to comment.