Skip to content

Commit 4e7f829

Browse files
authored
add CRS and drop Z coordinate + tests (#473)
2 parents 634a991 + 48cb99c commit 4e7f829

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

geetools/ee_feature_collection.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import ee
77
import geopandas as gpd
8+
import shapely
89
from matplotlib import pyplot as plt
910
from matplotlib.axes import Axes
1011

@@ -711,7 +712,11 @@ def plot(
711712
gdf.plot(column=property, ax=ax, cmap=cmap)
712713

713714
@classmethod
714-
def fromGeoInterface(cls, data: dict | GeoInterface) -> ee.FeatureCollection:
715+
def fromGeoInterface(
716+
cls,
717+
data: dict | GeoInterface,
718+
crs: str = "EPSG:4326",
719+
) -> ee.FeatureCollection:
715720
"""Create a :py:class:`ee.FeatureCollection` from a geo interface.
716721
717722
The ``geo_interface`` is a protocol representing a vector collection as a python GeoJSON-like dictionary structure.
@@ -723,7 +728,7 @@ def fromGeoInterface(cls, data: dict | GeoInterface) -> ee.FeatureCollection:
723728
724729
Parameters:
725730
data: The geo_interface to create the :py:class:`ee.FeatureCollection` from.
726-
crs: The CRS to use for the FeatureCollection. Default to ``EPSG:4326``.
731+
crs: The CRS of the input data. Defaults to "EPSG:4326".
727732
728733
Returns:
729734
The created :py:class:`ee.FeatureCollection` from the geo_interface.
@@ -755,5 +760,10 @@ def fromGeoInterface(cls, data: dict | GeoInterface) -> ee.FeatureCollection:
755760
elif not isinstance(data, dict):
756761
raise ValueError("The data must be a geo_interface or a dictionary")
757762

763+
# ensure the geometries are 2D
764+
gdf = gpd.GeoDataFrame.from_features(data["features"], crs=crs)
765+
gdf.geometry = shapely.force_2d(gdf.geometry.values)
766+
data = gdf.__geo_interface__
767+
758768
# create the feature collection
759769
return ee.FeatureCollection(data)

tests/test_FeatureCollection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ def test_error_from_geo_interface_(self):
331331
with pytest.raises(ValueError):
332332
ee.FeatureCollection.geetools.fromGeoInterface("toto")
333333

334+
def test_from_geo_interface_z(self, gdfZ, ee_feature_collection_regression):
335+
fc = ee.FeatureCollection.geetools.fromGeoInterface(gdfZ)
336+
ee_feature_collection_regression.check(fc, prescision=4)
337+
334338
@pytest.fixture
335339
def gdf(self):
336340
data = {
@@ -344,3 +348,17 @@ def gdf(self):
344348
],
345349
}
346350
return gpd.GeoDataFrame.from_features(data["features"])
351+
352+
@pytest.fixture
353+
def gdfZ(self):
354+
data = {
355+
"type": "FeatureCollection",
356+
"features": [
357+
{
358+
"type": "Feature",
359+
"properties": {"name": "Coors Field"},
360+
"geometry": {"type": "Point", "coordinates": [-104.99404, 39.75621, 1000]},
361+
}
362+
],
363+
}
364+
return gpd.GeoDataFrame.from_features(data["features"])
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
result: '0'
2+
values:
3+
'0':
4+
functionInvocationValue:
5+
arguments:
6+
features:
7+
arrayValue:
8+
values:
9+
- functionInvocationValue:
10+
arguments:
11+
geometry:
12+
functionInvocationValue:
13+
arguments:
14+
coordinates:
15+
constantValue:
16+
- -104.99404
17+
- 39.75621
18+
functionName: GeometryConstructors.Point
19+
metadata:
20+
constantValue:
21+
name: Coors Field
22+
system:index: '0'
23+
functionName: Feature
24+
functionName: Collection
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
features:
2+
- geometry:
3+
coordinates:
4+
- -104.994
5+
- 39.7562
6+
type: Point
7+
id: '0'
8+
properties:
9+
name: Coors Field
10+
type: Feature
11+
type: FeatureCollection

0 commit comments

Comments
 (0)