Skip to content

Commit

Permalink
blacken
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Dec 13, 2021
1 parent 9b9fcbb commit a8e5980
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 112 deletions.
41 changes: 26 additions & 15 deletions shapely/tests/geometry/test_collection.py
Expand Up @@ -11,21 +11,32 @@

@pytest.fixture()
def geometrycollection_geojson():
return {"type": "GeometryCollection", "geometries": [
{"type": "Point", "coordinates": (0, 3, 0)},
{"type": "LineString", "coordinates": ((2, 0), (1, 0))}
]}


@pytest.mark.parametrize('geom', [
GeometryCollection(),
shape({"type": "GeometryCollection", "geometries": []}),
shape({"type": "GeometryCollection", "geometries": [
{"type": "Point", "coordinates": ()},
{"type": "LineString", "coordinates": (())}
]}),
wkt.loads('GEOMETRYCOLLECTION EMPTY'),
])
return {
"type": "GeometryCollection",
"geometries": [
{"type": "Point", "coordinates": (0, 3, 0)},
{"type": "LineString", "coordinates": ((2, 0), (1, 0))},
],
}


@pytest.mark.parametrize(
"geom",
[
GeometryCollection(),
shape({"type": "GeometryCollection", "geometries": []}),
shape(
{
"type": "GeometryCollection",
"geometries": [
{"type": "Point", "coordinates": ()},
{"type": "LineString", "coordinates": (())},
],
}
),
wkt.loads("GEOMETRYCOLLECTION EMPTY"),
],
)
def test_empty(geom):
assert geom.type == "GeometryCollection"
assert geom.type == geom.geom_type
Expand Down
24 changes: 14 additions & 10 deletions shapely/tests/geometry/test_geometry_base.py
Expand Up @@ -2,6 +2,7 @@

import pytest


def test_polygon():
assert bool(geometry.Polygon()) is False

Expand All @@ -18,16 +19,19 @@ def test_geometry_collection():
assert bool(geometry.GeometryCollection()) is False


@pytest.mark.parametrize("geom", [
geometry.Point(1, 1),
geometry.LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]),
geometry.LineString([(0, 0), (1, 1), (0, 1), (0, 0)]),
geometry.Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
geometry.MultiPoint([(1, 1)]),
geometry.MultiLineString([[(0, 0), (1, 1), (0, 1), (0, 0)]]),
geometry.MultiPolygon([geometry.Polygon([(0, 0), (1, 1), (0, 1), (0, 0)])]),
geometry.GeometryCollection([geometry.Point(1, 1)]),
])
@pytest.mark.parametrize(
"geom",
[
geometry.Point(1, 1),
geometry.LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]),
geometry.LineString([(0, 0), (1, 1), (0, 1), (0, 0)]),
geometry.Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
geometry.MultiPoint([(1, 1)]),
geometry.MultiLineString([[(0, 0), (1, 1), (0, 1), (0, 0)]]),
geometry.MultiPolygon([geometry.Polygon([(0, 0), (1, 1), (0, 1), (0, 0)])]),
geometry.GeometryCollection([geometry.Point(1, 1)]),
],
)
def test_setattr_disallowed(geom):
with pytest.raises(AttributeError):
geom.name = "test"
25 changes: 16 additions & 9 deletions shapely/tests/geometry/test_linestring.py
Expand Up @@ -43,23 +43,23 @@ def test_from_linestring():
line = LineString(((1.0, 2.0), (3.0, 4.0)))
copy = LineString(line)
assert copy.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
assert copy.geom_type == 'LineString'
assert copy.geom_type == "LineString"


def test_from_linearring():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing(coords)
copy = LineString(ring)
assert copy.coords[:] == coords
assert copy.geom_type == 'LineString'
assert copy.geom_type == "LineString"


def test_from_linestring_z():
coords = [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)]
line = LineString(coords)
copy = LineString(line)
assert copy.coords[:] == coords
assert copy.geom_type == 'LineString'
assert copy.geom_type == "LineString"


def test_from_generator():
Expand Down Expand Up @@ -111,10 +111,16 @@ def test_from_invalid_dim():
with pytest.raises(shapely.GEOSException):
LineString([(1, 2)])

with pytest.raises(ValueError, match="Inconsistent coordinate dimensionality|Input operand 0 does not have enough dimensions"):
with pytest.raises(
ValueError,
match="Inconsistent coordinate dimensionality|Input operand 0 does not have enough dimensions",
):
LineString([(1, 2, 3), (4, 5)])

with pytest.raises(ValueError, match="Inconsistent coordinate dimensionality|Input operand 0 does not have enough dimensions"):
with pytest.raises(
ValueError,
match="Inconsistent coordinate dimensionality|Input operand 0 does not have enough dimensions",
):
LineString([(1, 2), (3, 4, 5)])

# TODO better error, right now raises AssertionError
Expand All @@ -131,7 +137,6 @@ def test_from_single_coordinate():


class TestLineString:

def test_linestring(self):

# From coordinate tuples
Expand All @@ -150,13 +155,15 @@ def test_linestring(self):
line.coords[2] # index out of range

# Geo interface
assert line.__geo_interface__ == {'type': 'LineString',
'coordinates': ((1.0, 2.0), (3.0, 4.0))}
assert line.__geo_interface__ == {
"type": "LineString",
"coordinates": ((1.0, 2.0), (3.0, 4.0)),
}

def test_linestring_empty(self):
# Test Non-operability of Null geometry
l_null = LineString()
assert l_null.wkt == 'LINESTRING EMPTY'
assert l_null.wkt == "LINESTRING EMPTY"
assert l_null.length == 0.0

def test_equals_argument_order(self):
Expand Down
1 change: 1 addition & 0 deletions shapely/tests/geometry/test_multi.py
Expand Up @@ -2,6 +2,7 @@

test_int_types = [int, np.int16, np.int32, np.int64]


class MultiGeometryTestCase:
def subgeom_access_test(self, cls, geoms):
geom = cls(geoms)
Expand Down
17 changes: 8 additions & 9 deletions shapely/tests/geometry/test_multilinestring.py
Expand Up @@ -10,7 +10,6 @@


class TestMultiLineString(MultiGeometryTestCase):

def test_multilinestring(self):

# From coordinate tuples
Expand Down Expand Up @@ -38,8 +37,10 @@ def test_multilinestring(self):
geom.geoms[1]

# Geo interface
assert geom.__geo_interface__ == {'type': 'MultiLineString',
'coordinates': (((0.0, 0.0), (1.0, 2.0)),)}
assert geom.__geo_interface__ == {
"type": "MultiLineString",
"coordinates": (((0.0, 0.0), (1.0, 2.0)),),
}

def test_from_multilinestring_z(self):
coords1 = [(0.0, 1.0, 2.0), (3.0, 4.0, 5.0)]
Expand All @@ -49,7 +50,7 @@ def test_from_multilinestring_z(self):
ml = MultiLineString([coords1, coords2])
copy = MultiLineString(ml)
assert isinstance(copy, MultiLineString)
assert copy.geom_type == 'MultiLineString'
assert copy.geom_type == "MultiLineString"
assert len(copy.geoms) == 2
assert dump_coords(copy.geoms[0]) == coords1
assert dump_coords(copy.geoms[1]) == coords2
Expand All @@ -67,11 +68,9 @@ def test_subgeom_access(self):
self.subgeom_access_test(MultiLineString, [line0, line1])

def test_create_multi_with_empty_component(self):
with pytest.raises(EmptyPartError, match="Can't create MultiLineString with empty component"):
wkt = MultiLineString([
LineString([(0, 0), (1, 1), (2, 2)]),
LineString()
]).wkt
msg = "Can't create MultiLineString with empty component"
with pytest.raises(EmptyPartError, match=msg):
MultiLineString([LineString([(0, 0), (1, 1), (2, 2)]), LineString()]).wkt


@pytest.mark.filterwarnings("error:An exception was ignored") # NumPy 1.21
Expand Down
13 changes: 7 additions & 6 deletions shapely/tests/geometry/test_multipoint.py
Expand Up @@ -9,7 +9,6 @@


class TestMultiPoint(MultiGeometryTestCase):

def test_multipoint(self):

# From coordinate tuples
Expand All @@ -35,8 +34,10 @@ def test_multipoint(self):
geom.geoms[2]

# Geo interface
assert geom.__geo_interface__ == {'type': 'MultiPoint',
'coordinates': ((1.0, 2.0), (3.0, 4.0))}
assert geom.__geo_interface__ == {
"type": "MultiPoint",
"coordinates": ((1.0, 2.0), (3.0, 4.0)),
}

def test_multipoint_from_numpy(self):
# Construct from a numpy array
Expand All @@ -51,9 +52,9 @@ def test_subgeom_access(self):
self.subgeom_access_test(MultiPoint, [p0, p1])

def test_create_multi_with_empty_component(self):
with pytest.raises(EmptyPartError, match="Can't create MultiPoint with empty component"):
wkt = MultiPoint([Point(0, 0), Point()]).wkt

msg = "Can't create MultiPoint with empty component"
with pytest.raises(EmptyPartError, match=msg):
MultiPoint([Point(0, 0), Point()]).wkt


def test_multipoint_array_coercion():
Expand Down
99 changes: 73 additions & 26 deletions shapely/tests/geometry/test_multipolygon.py
Expand Up @@ -9,49 +9,84 @@


class TestMultiPolygon(MultiGeometryTestCase):

def test_multipolygon(self):

# From coordinate tuples
geom = MultiPolygon(
[(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])])
coords = [
(
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))],
)
]
geom = MultiPolygon(coords)
assert isinstance(geom, MultiPolygon)
assert len(geom.geoms) == 1
assert dump_coords(geom) == [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25),
(0.25, 0.25)]]]
assert dump_coords(geom) == [
[
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)],
]
]

# Or from polygons
p = Polygon(((0, 0), (0, 1), (1, 1), (1, 0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])
p = Polygon(
((0, 0), (0, 1), (1, 1), (1, 0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))],
)
geom = MultiPolygon([p])
assert len(geom.geoms) == 1
assert dump_coords(geom) == [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25),
(0.25, 0.25)]]]
assert dump_coords(geom) == [
[
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)],
]
]

# Or from another multi-polygon
geom2 = MultiPolygon(geom)
assert len(geom2.geoms) == 1
assert dump_coords(geom2) == [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25),
(0.25, 0.25)]]]
assert dump_coords(geom2) == [
[
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)],
]
]

# Sub-geometry Access
assert isinstance(geom.geoms[0], Polygon)
assert dump_coords(geom.geoms[0]) == [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25),
(0.25, 0.25)]]
assert dump_coords(geom.geoms[0]) == [
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)],
]
with pytest.raises(IndexError): # index out of range
geom.geoms[1]

# Geo interface
assert geom.__geo_interface__ == {'type': 'MultiPolygon',
'coordinates': [(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0),
(1.0, 0.0), (0.0, 0.0)),
((0.25, 0.25), (0.25, 0.5), (0.5, 0.5),
(0.5, 0.25), (0.25, 0.25)))]}
assert geom.__geo_interface__ == {
"type": "MultiPolygon",
"coordinates": [
(
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)),
)
],
}

def test_subgeom_access(self):
poly0 = Polygon([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)])
Expand All @@ -61,16 +96,28 @@ def test_subgeom_access(self):

def test_fail_list_of_multipolygons():
"""A list of multipolygons is not a valid multipolygon ctor argument"""
multi = MultiPolygon([(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])])
multi = MultiPolygon(
[
(
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))],
)
]
)
with pytest.raises(ValueError):
MultiPolygon([multi])


@pytest.mark.filterwarnings("error:An exception was ignored") # NumPy 1.21
def test_numpy_object_array():
geom = MultiPolygon(
[(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])])
[
(
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
[((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))],
)
]
)
ar = np.empty(1, object)
ar[:] = [geom]
assert ar[0] == geom

0 comments on commit a8e5980

Please sign in to comment.