From 76425cddf8c3965c58df918242c82b02558f9c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Arroyo=20Torrens?= Date: Fri, 3 Apr 2020 14:17:19 +0200 Subject: [PATCH 1/2] Add empty geometries check --- cartoframes/viz/source.py | 6 ++++++ tests/unit/viz/test_source.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cartoframes/viz/source.py b/cartoframes/viz/source.py index 11fd9b6a2..04414364d 100644 --- a/cartoframes/viz/source.py +++ b/cartoframes/viz/source.py @@ -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: diff --git a/tests/unit/viz/test_source.py b/tests/unit/viz/test_source.py index febc7a125..0038888d4 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') @@ -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') From 5f4a6dec098c189bdc578f64a3d2c0adaeaed375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Arroyo=20Torrens?= Date: Fri, 3 Apr 2020 15:59:19 +0200 Subject: [PATCH 2/2] Remove empty geometries in Source --- cartoframes/viz/source.py | 7 ++----- tests/unit/viz/test_source.py | 10 +++------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/cartoframes/viz/source.py b/cartoframes/viz/source.py index 04414364d..b24ff1d5d 100644 --- a/cartoframes/viz/source.py +++ b/cartoframes/viz/source.py @@ -88,11 +88,8 @@ 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]') + # 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()) diff --git a/tests/unit/viz/test_source.py b/tests/unit/viz/test_source.py index 0038888d4..b164a454b 100644 --- a/tests/unit/viz/test_source.py +++ b/tests/unit/viz/test_source.py @@ -135,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'] @@ -184,12 +183,9 @@ def test_different_geometry_types_source_fail(self, features): def test_empty_geometries(self): geojson = { "type": "FeatureCollection", - "features": [POINT, EMPTY] + "features": [EMPTY, POINT, EMPTY, EMPTY, POINT, EMPTY] } gdf = gpd.GeoDataFrame.from_features(geojson) + source = Source(gdf) - with pytest.raises(ValueError) as e: - Source(gdf) - - assert str(e.value).startswith( - 'Empty geometries found. Please remove the empty geometries first') + assert len(source.gdf) == 2