diff --git a/cartoframes/viz/source.py b/cartoframes/viz/source.py index 11fd9b6a2..b24ff1d5d 100644 --- a/cartoframes/viz/source.py +++ b/cartoframes/viz/source.py @@ -88,6 +88,9 @@ def __init__(self, source, credentials=None, geom_col=None, encode_data=True): raise ValueError('No valid geometry found. Please provide an input source with ' + 'a valid geometry or specify the "geom_col" param with a geometry column.') + # Remove empty geometries + self.gdf = self.gdf[~self.gdf.geometry.is_empty] + # Checking the uniqueness of the geometry type geometry_types = set(self.gdf.geom_type.unique()) if geometry_types not in VALID_GEOMETRY_TYPES: diff --git a/tests/unit/viz/test_source.py b/tests/unit/viz/test_source.py index febc7a125..b164a454b 100644 --- a/tests/unit/viz/test_source.py +++ b/tests/unit/viz/test_source.py @@ -73,6 +73,15 @@ } } +EMPTY = { + "type": "Feature", + "geometry": { + "type": "GeometryCollection", + "coordinates": None + }, + "properties": {} +} + def setup_mocks(mocker): mocker.patch.object(ContextManager, 'compute_query') @@ -126,7 +135,6 @@ def test_dates_in_source(self): gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.lon, df.lat)) assert df.dtypes['date_column'] == np.dtype('datetime64[ns]') - source = Source(gdf) assert source.datetime_column_names == ['date_column'] @@ -171,3 +179,13 @@ def test_different_geometry_types_source_fail(self, features): Source(gdf) assert str(e.value).startswith('No valid geometry column types') + + def test_empty_geometries(self): + geojson = { + "type": "FeatureCollection", + "features": [EMPTY, POINT, EMPTY, EMPTY, POINT, EMPTY] + } + gdf = gpd.GeoDataFrame.from_features(geojson) + source = Source(gdf) + + assert len(source.gdf) == 2