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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cartoframes/viz/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 19 additions & 1 deletion 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 @@ -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']
Expand Down Expand Up @@ -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