Repository: CartoDB/cartoframes
Pull request #1694 (2020-10-05 13:02:20)

Remove nan raw geometries


Patch:
diff --git a/cartoframes/data/observatory/catalog/repository/geography_repo.py b/cartoframes/data/observatory/catalog/repository/geography_repo.py
index 353ac0e1..d23fc922 100644
--- a/cartoframes/data/observatory/catalog/repository/geography_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/geography_repo.py
@@ -61,7 +61,7 @@ class GeographyRepository(EntityRepository):
     def get_geographies_gdf(self):
         data = self.client.get_geographies({'get_geoms_coverage': True})
         gdf = GeoDataFrame(data, crs='epsg:4326')
-        set_geometry(gdf, col='geom_coverage', inplace=True)
+        set_geometry(gdf, 'geom_coverage', inplace=True)
         return gdf
 
 
diff --git a/cartoframes/utils/geom_utils.py b/cartoframes/utils/geom_utils.py
index 1f8221c9..1ec140f9 100644
--- a/cartoframes/utils/geom_utils.py
+++ b/cartoframes/utils/geom_utils.py
@@ -49,9 +49,9 @@ def set_geometry(gdf, col, drop=False, inplace=False, crs=None):
     if isinstance(col, str):
         if col not in frame:
             raise Exception('Column "{0}" does not exist.'.format(col))
-        frame[col] = decode_geometry(frame[col])
+        frame[col] = decode_geometry(frame[col].dropna())
     else:
-        col = decode_geometry(col)
+        col = decode_geometry(col.dropna())
 
     # Call set_geometry with decoded column
     frame.set_geometry(col, drop=drop, inplace=True, crs=crs)
diff --git a/cartoframes/viz/source.py b/cartoframes/viz/source.py
index 07a8ccc3..884fb9c6 100644
--- a/cartoframes/viz/source.py
+++ b/cartoframes/viz/source.py
@@ -91,6 +91,9 @@ class Source:
                 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 nan geometries
+            self.gdf.dropna(subset=[self.gdf.geometry.name], inplace=True)
+
             # Remove empty geometries
             self.gdf = self.gdf[~self.gdf.geometry.is_empty]
 


Test patch:
diff --git a/tests/unit/viz/test_source.py b/tests/unit/viz/test_source.py
index b8389a09..8aeec39c 100644
--- a/tests/unit/viz/test_source.py
+++ b/tests/unit/viz/test_source.py
@@ -167,6 +167,7 @@ class TestSource(object):
         gdf = gpd.GeoDataFrame.from_features(geojson)
         source = Source(gdf)
 
+        assert len(source.gdf) == len(features)
         assert source.gdf.equals(gdf)
 
     @pytest.mark.parametrize('features', [
@@ -187,7 +188,7 @@ class TestSource(object):
         gdf = gpd.GeoDataFrame.from_features(geojson)
         source = Source(gdf)
 
-        assert source.gdf.equals(gdf)
+        assert len(source.gdf) == len(features) - 1
 
     @pytest.mark.parametrize('features', [
         [POINT, LINESTRING],
@@ -218,3 +219,12 @@ class TestSource(object):
         source = Source(gdf)
 
         assert len(source.gdf) == 2
+
+    def test_nan_geometries(self):
+        df = pd.DataFrame({
+            'geom': ['POINT(0 0)', np.nan, np.nan, 'POINT(0 0)', None]
+        })
+
+        source = Source(df, geom_col='geom')
+
+        assert len(source.gdf) == 2