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

Raise a suitable error when trying to visualise a GeoDataFrame with multiple geometries #1541

Merged
merged 3 commits into from
Feb 14, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions cartoframes/viz/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@

from ..io.managers.context_manager import ContextManager
from ..utils.geom_utils import set_geometry, has_geometry
from ..utils.utils import encode_geodataframe, get_geodataframe_bounds, get_geodataframe_geom_type, \
get_datetime_column_names
from ..utils.utils import encode_geodataframe, get_geodataframe_bounds, \
get_geodataframe_geom_type, get_datetime_column_names

RFC_2822_DATETIME_FORMAT = "%a, %d %b %Y %T %z"

VALID_GEOMETRY_TYPES = [
{'Point'},
{'MultiPoint'},
{'LineString'},
{'MultiLineString'},
{'LineString', 'MultiLineString'},
{'Polygon'},
{'MultiPolygon'},
{'Polygon', 'MultiPolygon'}
]


class SourceType:
QUERY = 'Query'
Expand Down Expand Up @@ -75,6 +86,13 @@ def __init__(self, source, credentials=None, geom_col=None):
else:
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 the uniqueness of the geometry type
geometry_types = set(self.gdf.geom_type.unique())
if geometry_types not in VALID_GEOMETRY_TYPES:
raise ValueError('No valid geometry column types ({}), it has '.format(geometry_types) +
'to be one of the next type sets: {}.'.format(VALID_GEOMETRY_TYPES))

else:
raise ValueError('Wrong source input. Valid values are str and DataFrame.')

Expand Down
106 changes: 106 additions & 0 deletions tests/unit/viz/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,72 @@
from cartoframes.viz.source import Source
from cartoframes.io.managers.context_manager import ContextManager

POINT = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"prop0": "value0"
}
}

MULTIPOINT = {
"type": "Feature",
"geometry": {
"type": "MultiPoint",
"coordinates": [[102.0, 0.0], [103.0, 1.0]]
},
"properties": {
"prop0": "value0"
}
}

LINESTRING = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]]
},
"properties": {
"prop0": "value0"
}
}

MULTILINESTRING = {
"type": "Feature",
"geometry": {
"type": "MultiLineString",
"coordinates": [[[102.0, 0.0], [103.0, 1.0]], [[104.0, 0.0], [105.0, 1.0]]]
},
"properties": {
"prop0": "value0"
}
}

POLYGON = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
},
"properties": {
"prop0": "value0"
}
}

MULTIPOLYGON = {
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0]]], [[[100.0, 1.0], [100.0, 0.0], [100.0, 0.0]]]]
},
"properties": {
"prop0": "value0"
}
}


def setup_mocks(mocker):
mocker.patch.object(ContextManager, 'compute_query')
Expand Down Expand Up @@ -65,3 +131,43 @@ def test_dates_in_source(self):

assert source.datetime_column_names == ['date_column']
assert source.gdf.dtypes['date_column'] == np.object

@pytest.mark.parametrize('features', [
[POINT],
[MULTIPOINT],
[LINESTRING],
[MULTILINESTRING],
[LINESTRING, MULTILINESTRING],
[POLYGON],
[MULTIPOLYGON],
[POLYGON, MULTIPOLYGON]
])
def test_different_geometry_types_source(self, features):
geojson = {
"type": "FeatureCollection",
"features": features
}
gdf = gpd.GeoDataFrame.from_features(geojson)
source = Source(gdf)

assert source.gdf.equals(gdf)

@pytest.mark.parametrize('features', [
[POINT, LINESTRING],
[POINT, POLYGON],
[LINESTRING, POLYGON],
[POINT, LINESTRING, POLYGON],
[MULTIPOINT, MULTILINESTRING, MULTIPOLYGON],
[POINT, MULTIPOINT]
])
def test_different_geometry_types_source_fail(self, features):
geojson = {
"type": "FeatureCollection",
"features": features
}
gdf = gpd.GeoDataFrame.from_features(geojson)

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

assert str(e.value).startswith('No valid geometry column types')