Skip to content

Commit

Permalink
Merge pull request #1610 from CartoDB/jarroyo/ch66629/downloading-a-c…
Browse files Browse the repository at this point in the history
…arto-table-may-return-two

Check empty geometries in Source
  • Loading branch information
Jesus89 committed Apr 3, 2020
2 parents 9f17774 + 5f4a6de commit e59245e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit e59245e

Please sign in to comment.