test_collection.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import numpy as np
  2. import pytest
  3. import shapely
  4. from shapely import GeometryCollection, LineString, Point, wkt
  5. from shapely.geometry import shape
  6. @pytest.fixture()
  7. def geometrycollection_geojson():
  8. return {
  9. "type": "GeometryCollection",
  10. "geometries": [
  11. {"type": "Point", "coordinates": (0, 3, 0)},
  12. {"type": "LineString", "coordinates": ((2, 0), (1, 0))},
  13. ],
  14. }
  15. @pytest.mark.parametrize(
  16. "geom",
  17. [
  18. GeometryCollection(),
  19. GeometryCollection([]),
  20. shape({"type": "GeometryCollection", "geometries": []}),
  21. wkt.loads("GEOMETRYCOLLECTION EMPTY"),
  22. ],
  23. )
  24. def test_empty(geom):
  25. assert geom.geom_type == "GeometryCollection"
  26. assert geom.is_empty
  27. assert len(geom.geoms) == 0
  28. assert list(geom.geoms) == []
  29. def test_empty_subgeoms():
  30. geom = GeometryCollection([Point(), LineString()])
  31. assert geom.geom_type == "GeometryCollection"
  32. assert geom.is_empty
  33. assert len(geom.geoms) == 2
  34. parts = list(geom.geoms)
  35. if shapely.geos_version < (3, 9, 0):
  36. # the accessed empty 2D point has a 3D coordseq on GEOS 3.8
  37. parts[0] = shapely.force_2d(parts[0])
  38. assert parts == [Point(), LineString()]
  39. def test_child_with_deleted_parent():
  40. # test that we can remove a collection while keeping
  41. # children around
  42. a = LineString([(0, 0), (1, 1), (1, 2), (2, 2)])
  43. b = LineString([(0, 0), (1, 1), (2, 1), (2, 2)])
  44. collection = a.intersection(b)
  45. child = collection.geoms[0]
  46. # delete parent of child
  47. del collection
  48. # access geometry, this should not seg fault as 1.2.15 did
  49. assert child.wkt is not None
  50. def test_from_numpy_array():
  51. geoms = np.array([Point(0, 0), LineString([(1, 1), (2, 2)])])
  52. geom = GeometryCollection(geoms)
  53. assert len(geom.geoms) == 2
  54. np.testing.assert_array_equal(geoms, geom.geoms)
  55. def test_from_geojson(geometrycollection_geojson):
  56. geom = shape(geometrycollection_geojson)
  57. assert geom.geom_type == "GeometryCollection"
  58. assert len(geom.geoms) == 2
  59. geom_types = [g.geom_type for g in geom.geoms]
  60. assert "Point" in geom_types
  61. assert "LineString" in geom_types
  62. def test_geointerface(geometrycollection_geojson):
  63. geom = shape(geometrycollection_geojson)
  64. assert geom.__geo_interface__ == geometrycollection_geojson
  65. def test_len_raises(geometrycollection_geojson):
  66. geom = shape(geometrycollection_geojson)
  67. with pytest.raises(TypeError):
  68. len(geom)
  69. def test_numpy_object_array():
  70. geom = GeometryCollection([LineString([(0, 0), (1, 1)])])
  71. ar = np.empty(1, object)
  72. ar[:] = [geom]
  73. assert ar[0] == geom