test_geointerface.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import unittest
  2. from shapely import wkt
  3. from shapely.geometry import shape
  4. from shapely.geometry.linestring import LineString
  5. from shapely.geometry.multilinestring import MultiLineString
  6. from shapely.geometry.multipoint import MultiPoint
  7. from shapely.geometry.multipolygon import MultiPolygon
  8. from shapely.geometry.polygon import LinearRing, Polygon
  9. class GeoThing:
  10. def __init__(self, d):
  11. self.__geo_interface__ = d
  12. class GeoInterfaceTestCase(unittest.TestCase):
  13. def test_geointerface(self):
  14. # Convert a dictionary
  15. d = {"type": "Point", "coordinates": (0.0, 0.0)}
  16. geom = shape(d)
  17. assert geom.geom_type == "Point"
  18. assert tuple(geom.coords) == ((0.0, 0.0),)
  19. # Convert an object that implements the geo protocol
  20. geom = None
  21. thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
  22. geom = shape(thing)
  23. assert geom.geom_type == "Point"
  24. assert tuple(geom.coords) == ((0.0, 0.0),)
  25. # Check line string
  26. geom = shape({"type": "LineString", "coordinates": ((-1.0, -1.0), (1.0, 1.0))})
  27. assert isinstance(geom, LineString)
  28. assert tuple(geom.coords) == ((-1.0, -1.0), (1.0, 1.0))
  29. # Check linearring
  30. geom = shape(
  31. {
  32. "type": "LinearRing",
  33. "coordinates": (
  34. (0.0, 0.0),
  35. (0.0, 1.0),
  36. (1.0, 1.0),
  37. (2.0, -1.0),
  38. (0.0, 0.0),
  39. ),
  40. }
  41. )
  42. assert isinstance(geom, LinearRing)
  43. assert tuple(geom.coords) == (
  44. (0.0, 0.0),
  45. (0.0, 1.0),
  46. (1.0, 1.0),
  47. (2.0, -1.0),
  48. (0.0, 0.0),
  49. )
  50. # polygon
  51. geom = shape(
  52. {
  53. "type": "Polygon",
  54. "coordinates": (
  55. ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)),
  56. ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1), (0.1, 0.1)),
  57. ),
  58. }
  59. )
  60. assert isinstance(geom, Polygon)
  61. assert tuple(geom.exterior.coords) == (
  62. (0.0, 0.0),
  63. (0.0, 1.0),
  64. (1.0, 1.0),
  65. (2.0, -1.0),
  66. (0.0, 0.0),
  67. )
  68. assert len(geom.interiors) == 1
  69. # multi point
  70. geom = shape({"type": "MultiPoint", "coordinates": ((1.0, 2.0), (3.0, 4.0))})
  71. assert isinstance(geom, MultiPoint)
  72. assert len(geom.geoms) == 2
  73. # multi line string
  74. geom = shape(
  75. {"type": "MultiLineString", "coordinates": (((0.0, 0.0), (1.0, 2.0)),)}
  76. )
  77. assert isinstance(geom, MultiLineString)
  78. assert len(geom.geoms) == 1
  79. # multi polygon
  80. geom = shape(
  81. {
  82. "type": "MultiPolygon",
  83. "coordinates": [
  84. (
  85. ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
  86. ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1), (0.1, 0.1)),
  87. )
  88. ],
  89. }
  90. )
  91. assert isinstance(geom, MultiPolygon)
  92. assert len(geom.geoms) == 1
  93. def test_empty_wkt_polygon():
  94. """Confirm fix for issue #450"""
  95. g = wkt.loads("POLYGON EMPTY")
  96. assert g.__geo_interface__["type"] == "Polygon"
  97. assert g.__geo_interface__["coordinates"] == ()
  98. def test_empty_polygon():
  99. """Confirm fix for issue #450"""
  100. g = Polygon()
  101. assert g.__geo_interface__["type"] == "Polygon"
  102. assert g.__geo_interface__["coordinates"] == ()