test_create_inconsistent_dimensionality.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """
  2. When a "context" passed to shape/asShape has a coordinate
  3. which is missing a dimension we should raise a descriptive error.
  4. When we use mixed dimensions in a WKT geometry, the parser strips
  5. any dimension which is not present in every coordinate.
  6. """
  7. import pytest
  8. import shapely
  9. from shapely import wkt
  10. from shapely.errors import GEOSException
  11. from shapely.geometry import LineString, Polygon, shape
  12. geojson_cases = [
  13. {"type": "LineString", "coordinates": [[1, 1, 1], [2, 2]]},
  14. # Specific test case from #869
  15. {
  16. "type": "Polygon",
  17. "coordinates": [
  18. [
  19. [55.12916764533149, 24.980385694214384, 2.5],
  20. [55.13098248044217, 24.979828079961905],
  21. [55.13966519231666, 24.97801442415322],
  22. [55.13966563924936, 24.97801442415322],
  23. [55.14139286840762, 24.982307444496097],
  24. [55.14169331277646, 24.983717465495562],
  25. [55.14203489144224, 24.985419446276566, 2.5],
  26. [55.14180327151276, 24.98428602667792, 2.5],
  27. [55.14170091915952, 24.984242720177235, 2.5],
  28. [55.14122966992623, 24.984954809433702, 2.5],
  29. [55.14134021791831, 24.985473928648396, 2.5],
  30. [55.141405876161286, 24.986090184809793, 2.5],
  31. [55.141361358941225, 24.986138101357326, 2.5],
  32. [55.14093322994411, 24.986218753894093, 2.5],
  33. [55.140897653420964, 24.986214283545635, 2.5],
  34. [55.14095492976058, 24.9863027591922, 2.5],
  35. [55.140900447388745, 24.98628436557094, 2.5],
  36. [55.140867059473706, 24.98628869622101, 2.5],
  37. [55.14089155325796, 24.986402364143782, 2.5],
  38. [55.14090938808566, 24.986479011993385, 2.5],
  39. [55.140943893587824, 24.986471188883584, 2.5],
  40. [55.1410161176551, 24.9864174050037, 2.5],
  41. [55.140996932409635, 24.986521806266644, 2.5],
  42. [55.14163554031332, 24.986910400619593, 2.5],
  43. [55.14095781686062, 24.987033474900578, 2.5],
  44. [55.14058258698692, 24.98693261266349, 2.5],
  45. [55.14032624044253, 24.98747538747211, 2.5],
  46. [55.14007240846915, 24.988001119077232, 2.5],
  47. [55.14013122149105, 24.98831115636925, 2.5],
  48. [55.13991827457961, 24.98834356639557, 2.5],
  49. [55.139779460946755, 24.988254625087706, 2.5],
  50. [55.13974742344948, 24.988261377176524, 2.5],
  51. [55.139515198160304, 24.98841811876934, 2.5],
  52. [55.13903617238334, 24.98817914139135, 2.5],
  53. [55.1391330764994, 24.988660542040925, 2.5],
  54. [55.13914369357698, 24.989438289540374, 2.5],
  55. [55.136431216517785, 24.98966711550207, 2.0],
  56. [55.13659028641709, 24.99041706302204, 2.0],
  57. [55.1355852030721, 24.990933481401207, 2.5],
  58. [55.13535549235394, 24.99110470506038, 2.5],
  59. [55.13512578163577, 24.99127592871955, 2.5],
  60. [55.129969653784556, 24.991440074326995, 2.5],
  61. [55.130221623112746, 24.988070688875112, 2.5],
  62. [55.130451333830905, 24.98789946521594, 2.5],
  63. [55.13089208224919, 24.98742639990359, 2.5],
  64. [55.132177586827666, 24.989003408454433, 2.5],
  65. [55.13238862452779, 24.988701566801254, 2.5],
  66. [55.132482594977674, 24.988501518707757, 2.5],
  67. [55.132525994610624, 24.988048802794115, 2.5],
  68. [55.13249018525683, 24.987180623870653, 2.5],
  69. [55.13253358488978, 24.986727907957015, 2.5],
  70. [55.1322761673244, 24.985827132742713, 2.5],
  71. [55.13163341503516, 24.98503862846729, 2.5],
  72. [55.131514764536504, 24.984469124700183, 2.5],
  73. [55.131275600894, 24.983796337257242, 2.0],
  74. [55.13066865795855, 24.98387601190528, 2.0],
  75. [55.13026930682963, 24.981537228037503, 2.0],
  76. [55.130260412698846, 24.981495691049748, 2.0],
  77. [55.13025151856806, 24.981454154061993, 2.0],
  78. [55.13022925995803, 24.98096497686874, 2.5],
  79. [55.12984453059386, 24.9804285816199, 2.5],
  80. [55.129998291954365, 24.98021419115843, 2.5],
  81. [55.12916764533149, 24.980385694214384, 2.5],
  82. ]
  83. ],
  84. },
  85. ]
  86. direct_cases = [
  87. (LineString, [[[0, 0, 0], [1, 1]]]),
  88. (Polygon, [[[0, 0, 0], [1, 1, 0], [1, 1], [0, 1, 0], [0, 0, 0]]]),
  89. # Specific test case from #869
  90. (
  91. Polygon,
  92. [
  93. [
  94. [55.12916764533149, 24.980385694214384, 2.5],
  95. [55.13098248044217, 24.979828079961905],
  96. [55.13966519231666, 24.97801442415322],
  97. [55.13966563924936, 24.97801442415322],
  98. [55.14139286840762, 24.982307444496097],
  99. [55.14169331277646, 24.983717465495562],
  100. [55.14203489144224, 24.985419446276566, 2.5],
  101. [55.14180327151276, 24.98428602667792, 2.5],
  102. [55.14170091915952, 24.984242720177235, 2.5],
  103. [55.14122966992623, 24.984954809433702, 2.5],
  104. [55.14134021791831, 24.985473928648396, 2.5],
  105. [55.141405876161286, 24.986090184809793, 2.5],
  106. [55.141361358941225, 24.986138101357326, 2.5],
  107. [55.14093322994411, 24.986218753894093, 2.5],
  108. [55.140897653420964, 24.986214283545635, 2.5],
  109. [55.14095492976058, 24.9863027591922, 2.5],
  110. [55.140900447388745, 24.98628436557094, 2.5],
  111. [55.140867059473706, 24.98628869622101, 2.5],
  112. [55.14089155325796, 24.986402364143782, 2.5],
  113. [55.14090938808566, 24.986479011993385, 2.5],
  114. [55.140943893587824, 24.986471188883584, 2.5],
  115. [55.1410161176551, 24.9864174050037, 2.5],
  116. [55.140996932409635, 24.986521806266644, 2.5],
  117. [55.14163554031332, 24.986910400619593, 2.5],
  118. [55.14095781686062, 24.987033474900578, 2.5],
  119. [55.14058258698692, 24.98693261266349, 2.5],
  120. [55.14032624044253, 24.98747538747211, 2.5],
  121. [55.14007240846915, 24.988001119077232, 2.5],
  122. [55.14013122149105, 24.98831115636925, 2.5],
  123. [55.13991827457961, 24.98834356639557, 2.5],
  124. [55.139779460946755, 24.988254625087706, 2.5],
  125. [55.13974742344948, 24.988261377176524, 2.5],
  126. [55.139515198160304, 24.98841811876934, 2.5],
  127. [55.13903617238334, 24.98817914139135, 2.5],
  128. [55.1391330764994, 24.988660542040925, 2.5],
  129. [55.13914369357698, 24.989438289540374, 2.5],
  130. [55.136431216517785, 24.98966711550207, 2.0],
  131. [55.13659028641709, 24.99041706302204, 2.0],
  132. [55.1355852030721, 24.990933481401207, 2.5],
  133. [55.13535549235394, 24.99110470506038, 2.5],
  134. [55.13512578163577, 24.99127592871955, 2.5],
  135. [55.129969653784556, 24.991440074326995, 2.5],
  136. [55.130221623112746, 24.988070688875112, 2.5],
  137. [55.130451333830905, 24.98789946521594, 2.5],
  138. [55.13089208224919, 24.98742639990359, 2.5],
  139. [55.132177586827666, 24.989003408454433, 2.5],
  140. [55.13238862452779, 24.988701566801254, 2.5],
  141. [55.132482594977674, 24.988501518707757, 2.5],
  142. [55.132525994610624, 24.988048802794115, 2.5],
  143. [55.13249018525683, 24.987180623870653, 2.5],
  144. [55.13253358488978, 24.986727907957015, 2.5],
  145. [55.1322761673244, 24.985827132742713, 2.5],
  146. [55.13163341503516, 24.98503862846729, 2.5],
  147. [55.131514764536504, 24.984469124700183, 2.5],
  148. [55.131275600894, 24.983796337257242, 2.0],
  149. [55.13066865795855, 24.98387601190528, 2.0],
  150. [55.13026930682963, 24.981537228037503, 2.0],
  151. [55.130260412698846, 24.981495691049748, 2.0],
  152. [55.13025151856806, 24.981454154061993, 2.0],
  153. [55.13022925995803, 24.98096497686874, 2.5],
  154. [55.12984453059386, 24.9804285816199, 2.5],
  155. [55.129998291954365, 24.98021419115843, 2.5],
  156. [55.12916764533149, 24.980385694214384, 2.5],
  157. ]
  158. ],
  159. ),
  160. ]
  161. wkt_cases = [
  162. # preserve 3rd dimension
  163. ("MULTIPOINT (1 1 1, 2 2)", "MULTIPOINT Z (1 1 1, 2 2 0)"),
  164. ("MULTIPOINT (1 1, 2 2 2)", "MULTIPOINT Z (1 1 0, 2 2 2)"),
  165. ("LINESTRING (1 1 1, 2 2)", "LINESTRING Z (1 1 1, 2 2 0)"),
  166. (
  167. "POLYGON ((0 0 0, 1 0 0, 1 1, 0 1 0, 0 0 0))",
  168. "POLYGON Z ((0 0 0, 1 0 0, 1 1 0, 0 1 0, 0 0 0))",
  169. ),
  170. # drop 3rd dimension
  171. ("LINESTRING (1 1, 2 2 2)", "LINESTRING (1 1, 2 2)"),
  172. ("POLYGON ((0 0, 1 0 1, 1 1, 0 1, 0 0))", "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))"),
  173. ]
  174. @pytest.mark.filterwarnings("ignore:Creating an ndarray from ragged nested sequences:")
  175. @pytest.mark.parametrize("geojson", geojson_cases)
  176. def test_create_from_geojson(geojson):
  177. # exact error depends on numpy version
  178. with pytest.raises((ValueError, TypeError)) as exc:
  179. shape(geojson).wkt
  180. assert exc.match(
  181. "Inconsistent coordinate dimensionality|Input operand 0 does not have enough "
  182. "dimensions|ufunc 'linestrings' not supported for the input types|setting an "
  183. "array element with a sequence. The requested array has an inhomogeneous shape"
  184. )
  185. @pytest.mark.filterwarnings("ignore:Creating an ndarray from ragged nested sequences:")
  186. @pytest.mark.parametrize("constructor, args", direct_cases)
  187. def test_create_directly(constructor, args):
  188. with pytest.raises((ValueError, TypeError)) as exc:
  189. constructor(*args)
  190. assert exc.match(
  191. "Inconsistent coordinate dimensionality|Input operand 0 does not have enough "
  192. "dimensions|ufunc 'linestrings' not supported for the input types|setting an "
  193. "array element with a sequence. The requested array has an inhomogeneous shape"
  194. )
  195. @pytest.mark.parametrize("wkt_geom,expected", wkt_cases)
  196. def test_create_from_wkt(wkt_geom, expected):
  197. if shapely.geos_version >= (3, 12, 0):
  198. # https://github.com/shapely/shapely/issues/1541
  199. with pytest.raises(GEOSException):
  200. wkt.loads(wkt_geom)
  201. else:
  202. geom = wkt.loads(wkt_geom)
  203. assert geom.wkt == expected