Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty geometries in Source #1610

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cartoframes/viz/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ 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.')

# Checking empty geometries
has_empty_geoms = self.gdf.geometry.is_empty.any()
if has_empty_geoms:
raise ValueError('Empty geometries found. Please remove the empty geometries first: ' +
'gdf = gdf[~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:
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/viz/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
}
}

EMPTY = {
"type": "Feature",
"geometry": {
"type": "GeometryCollection",
"coordinates": None
},
"properties": {}
}


def setup_mocks(mocker):
mocker.patch.object(ContextManager, 'compute_query')
Expand Down Expand Up @@ -171,3 +180,16 @@ 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": [POINT, EMPTY]
}
gdf = gpd.GeoDataFrame.from_features(geojson)

with pytest.raises(ValueError) as e:
Source(gdf)

assert str(e.value).startswith(
'Empty geometries found. Please remove the empty geometries first')