def test_multipoint(self): # From coordinate tuples geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) assert len(geom.geoms) == 2 assert dump_coords(geom) == [[(1.0, 2.0)], [(3.0, 4.0)]] # From points geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) assert len(geom.geoms) == 2 assert dump_coords(geom) == [[(1.0, 2.0)], [(3.0, 4.0)]] # From another multi-point geom2 = MultiPoint(geom) assert len(geom2.geoms) == 2 assert dump_coords(geom2) == [[(1.0, 2.0)], [(3.0, 4.0)]] # Sub-geometry Access assert isinstance(geom.geoms[0], Point) assert geom.geoms[0].x == 1.0 assert geom.geoms[0].y == 2.0 with pytest.raises(IndexError): # index out of range geom.geoms[2] # Geo interface assert geom.__geo_interface__ == { "type": "MultiPoint", "coordinates": ((1.0, 2.0), (3.0, 4.0)), }
def test_from_points(): # From Points line = LineString((Point(1.0, 2.0), Point(3.0, 4.0))) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)] line = LineString([Point(1.0, 2.0), Point(3.0, 4.0)]) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
def test_empty_subgeoms(): geom = GeometryCollection([Point(), LineString()]) assert geom.type == "GeometryCollection" assert geom.type == geom.geom_type assert geom.is_empty assert len(geom.geoms) == 2 assert list(geom.geoms) == [Point(), LineString()]
def test_point(self): # Test 2D points p = Point(1.0, 2.0) assert p.x == 1.0 assert p.y == 2.0 assert p.coords[:] == [(1.0, 2.0)] assert str(p) == p.wkt assert p.has_z is False with pytest.raises(DimensionError): p.z # Check 3D p = Point(1.0, 2.0, 3.0) assert p.coords[:] == [(1.0, 2.0, 3.0)] assert str(p) == p.wkt assert p.has_z is True assert p.z == 3.0 # Coordinate access p = Point((3.0, 4.0)) assert p.x == 3.0 assert p.y == 4.0 assert tuple(p.coords) == ((3.0, 4.0),) assert p.coords[0] == (3.0, 4.0) with pytest.raises(IndexError): # index out of range p.coords[1] # Bounds assert p.bounds == (3.0, 4.0, 3.0, 4.0) # Geo interface assert p.__geo_interface__ == {"type": "Point", "coordinates": (3.0, 4.0)}
def test_from_numpy(): # Construct from a numpy array p = Point(np.array([1.0, 2.0])) assert p.coords[:] == [(1.0, 2.0)] p = Point(np.array([1.0, 2.0, 3.0])) assert p.coords[:] == [(1.0, 2.0, 3.0)]
def test_GeometryType_deprecated(): geom = Point(1, 1) with pytest.warns(ShapelyDeprecationWarning): geom_type = geom.geometryType() assert geom_type == geom.geom_type
def test_collection(): g = GeometryCollection([Point(1, 2), LineString([(1, 2), (3, 4)])]) assert hash(g) == hash( GeometryCollection([Point(1, 2), LineString([(1, 2), (3, 4)])])) assert hash(g) != hash( GeometryCollection([Point(1, 2), LineString([(1, 2), (3, 3)])]))
def test_point_immutable(): p = Point(3.0, 4.0) with pytest.raises(AttributeError): p.coords = (2.0, 1.0) with pytest.raises(TypeError): p.coords[0] = (2.0, 1.0)
def test_from_point(): # From another point p = Point(3.0, 4.0) q = Point(p) assert q.coords[:] == [(3.0, 4.0)] p = Point(3.0, 4.0, 5.0) q = Point(p) assert q.coords[:] == [(3.0, 4.0, 5.0)]
def test_from_coordinates(): # 2D points p = Point(1.0, 2.0) assert p.coords[:] == [(1.0, 2.0)] assert p.has_z is False # 3D Point p = Point(1.0, 2.0, 3.0) assert p.coords[:] == [(1.0, 2.0, 3.0)] assert p.has_z # empty p = Point() assert p.is_empty assert isinstance(p.coords, CoordinateSequence) assert p.coords[:] == []
def test_point_array_coercion(): # don't convert to array of coordinates, keep objects p = Point(3.0, 4.0) arr = np.array(p) assert arr.ndim == 0 assert arr.size == 1 assert arr.dtype == np.dtype("object") assert arr.item() == p
def test_coords(self): # From Array.txt p = Point(0.0, 0.0, 1.0) coords = p.coords[0] assert coords == (0.0, 0.0, 1.0) # Convert to Numpy array, passing through Python sequence a = np.asarray(coords) assert a.ndim == 1 assert a.size == 3 assert a.shape == (3, )
def test_empty_equality(self): # Test equals operator, including empty geometries # see issue #338 point1 = Point(0, 0) polygon1 = Polygon(((0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0))) polygon2 = Polygon(((0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0))) polygon_empty1 = Polygon() polygon_empty2 = Polygon() assert point1 != polygon1 assert polygon_empty1 == polygon_empty2 assert polygon1 != polygon_empty1 assert polygon1 == polygon2 assert polygon_empty1 is not None
def test_attribute_chains(self): # Attribute Chaining # See also ticket #151. p = Polygon(((0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0))) assert list(p.boundary.coords) == [ (0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0), (0.0, 0.0), ] ec = list(Point(0.0, 0.0).buffer(1.0, 1).exterior.coords) assert isinstance(ec, list) # TODO: this is a poor test # Test chained access to interiors p = Polygon( ((0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)), [((-0.25, 0.25), (-0.25, 0.75), (-0.75, 0.75), (-0.75, 0.25))], ) assert p.area == 0.75 """Not so much testing the exact values here, which are the responsibility of the geometry engine (GEOS), but that we can get chain functions and properties using anonymous references. """ assert list(p.interiors[0].coords) == [ (-0.25, 0.25), (-0.25, 0.75), (-0.75, 0.75), (-0.75, 0.25), (-0.25, 0.25), ] xy = list(p.interiors[0].buffer(1).exterior.coords)[0] assert len(xy) == 2 # Test multiple operators, boundary of a buffer ec = list(p.buffer(1).boundary.coords) assert isinstance(ec, list) # TODO: this is a poor test
def test_from_sequence(): # From single coordinate pair p = Point((3.0, 4.0)) assert p.coords[:] == [(3.0, 4.0)] p = Point([3.0, 4.0]) assert p.coords[:] == [(3.0, 4.0)] # From coordinate sequence p = Point([(3.0, 4.0)]) assert p.coords[:] == [(3.0, 4.0)] # 3D p = Point((3.0, 4.0, 5.0)) assert p.coords[:] == [(3.0, 4.0, 5.0)] p = Point([3.0, 4.0, 5.0]) assert p.coords[:] == [(3.0, 4.0, 5.0)] p = Point([(3.0, 4.0, 5.0)]) assert p.coords[:] == [(3.0, 4.0, 5.0)]
def kml(name, lon, lat, code=None, nc=None): from simplekml import Kml, Style from shapely import Polygon, Point if nc is not None: x = nc.variables['XLONG_M'][0, :, :] y = nc.variables['XLAT_M'][0, :, :] xc = nc.variables['XLONG_C'][0, :, :] yc = nc.variables['XLAT_C'][0, :, :] k = Kml() z = zip(name, lon, lat) if code is None else zip(name, lon, lat, code) for s in z: p = k.newpoint(name=s[3] if len(s) == 4 else s[0], coords=[s[1:3]]) p.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/kml/paddle/red-circle.png" p.style.balloonstyle.text = s[0] if nc is not None: i, j, d = nearest(x, y, s[1], s[2]) coords = [(xc[i, j], yc[i, j]), (xc[i, j + 1], yc[i, j]), (xc[i, j + 1], yc[i + 1, j]), (xc[i, j], yc[i + 1, j]), (xc[i, j], yc[i, j])] if Polygon(coords).contains(Point(*s[1:3])): l = k.newlinestring(coords=[s[1:3], (x[i, j], y[i, j])]) r = k.newlinestring(coords=coords) return k
def test_create_multi_with_empty_component(self): msg = "Can't create MultiPoint with empty component" with pytest.raises(EmptyPartError, match=msg): MultiPoint([Point(0, 0), Point()]).wkt
def test_subgeom_access(self): p0 = Point(1.0, 2.0) p1 = Point(3.0, 4.0) self.subgeom_access_test(MultiPoint, [p0, p1])
def test_from_mix(): # From mix of tuples and Points line = LineString((Point(1.0, 2.0), (2.0, 3.0), Point(3.0, 4.0))) assert line.coords[:] == [(1.0, 2.0), (2.0, 3.0), (3.0, 4.0)]
def test_numpy_object_array(): geom = Point(3.0, 4.0) ar = np.empty(1, object) ar[:] = [geom] assert ar[0] == geom
def test_empty_point(self): assert Point().is_empty
def test_empty_point_bounds(): """The bounds of an empty point is an empty tuple""" p = Point() # TODO keep this empty tuple or change to (nan, nan, nan, nan)? assert p.bounds == ()
def test_numpy_empty_point_coords(): pe = Point() # Access the coords a = np.asarray(pe.coords) assert a.shape == (0, 2)
def test_from_invalid(): with pytest.raises(TypeError, match="takes at most 3 arguments"): Point(1, 2, 3, 4)
def test_from_generator(): gen = (coord for coord in [(1.0, 2.0)]) p = Point(gen) assert p.coords[:] == [(1.0, 2.0)]
def test_point_empty(self): # Test Non-operability of Null geometry p_null = Point() assert p_null.wkt == "POINT EMPTY" assert p_null.coords[:] == [] assert p_null.area == 0.0
def test_polygon(): g = Point(0, 0).buffer(1.0) assert hash(g) == hash(Point(0, 0).buffer(1.0)) assert hash(g) != hash(Point(0, 0).buffer(1.1))
def test_numpy_object_array(): geoms = [Point(), GeometryCollection()] arr = np.empty(2, object) arr[:] = geoms
def test_point(): g = Point(1, 2) assert hash(g) == hash(Point(1, 2)) assert hash(g) != hash(Point(1, 3))
def test_linearring_from_points(): # From Points expected_coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)] ring = LinearRing([Point(0.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0)]) assert ring.coords[:] == expected_coords