diff --git a/te_schemas/aoi.py b/te_schemas/aoi.py index 311811a..0619a0c 100644 --- a/te_schemas/aoi.py +++ b/te_schemas/aoi.py @@ -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) @@ -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) @@ -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"): """ @@ -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": @@ -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): diff --git a/tests/test_aoi.py b/tests/test_aoi.py index be9ad8a..bec15e1 100644 --- a/tests/test_aoi.py +++ b/tests/test_aoi.py @@ -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"