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

Errors method for validating geometry collections. #102

Merged
merged 5 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions geojson/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def __init__(self, geometries=None, **extra):
super(GeometryCollection, self).__init__(**extra)
self["geometries"] = geometries or []

def errors(self):
return [geom.errors() for geom in self['geometries']]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I promise this is my last comment! 🙈

I noticed that for MultiPoint, MultiLineString, and MultiPolygon, we call check_list_errors which:

  1. iterates over all the sub-geometries
  2. checks for errors for each subgeometry
  3. if there's no error, no item gets added to the resulting list

This behavior is different from your GeometryCollection behavior where the result here be something like [[], [], []].

What do you think? Would it be good to make the implementations more similar or do you think we should keep them how they are right now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I think it is a slightly different case than the Multi geometries because a geometry collection could contain any of those objects. However I do like the idea of consistency around errors array, so i'll change the method to only add the errors if they exist. Then we can leave the baseclass is_valid property.


@property
def is_valid(self):
return not any(self.errors())


# Marker classes.

Expand Down
23 changes: 23 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,26 @@ def test_valid_multipolygon(self):
(22, -18.11), (3.14, 23.17)]]
multipoly = geojson.MultiPolygon([poly1, poly2, poly3])
self.assertEqual(multipoly.is_valid, True)


class TestValidationGeometryCollection(unittest.TestCase):

def test_invalid_geometrycollection(self):
point = geojson.Point((10, 20))
bad_poly = geojson.Polygon([[(2.38, 57.322), (23.194, -20.28),
(-120.43, 19.15), (25.44, -17.91)]])

geom_collection = geojson.GeometryCollection(
geometries=[point, bad_poly]
)
self.assertFalse(geom_collection.is_valid)

def test_valid_geometrycollection(self):
point = geojson.Point((10, 20))
poly = geojson.Polygon([[(2.38, 57.322), (23.194, -20.28),
(-120.43, 19.15), (2.38, 57.322)]])

geom_collection = geojson.GeometryCollection(
geometries=[point, poly]
)
self.assertTrue(geom_collection.is_valid)