spatial_extent.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. """
  2. Spatial extents classes for map layer and space time datasets
  3. Usage:
  4. .. code-block:: python
  5. >>> import grass.temporal as tgis
  6. >>> tgis.init()
  7. >>> extent = tgis.RasterSpatialExtent(
  8. ... ident="raster@PERMANENT", north=90, south=90, east=180, west=180,
  9. ... top=100, bottom=-20)
  10. >>> extent = tgis.Raster3DSpatialExtent(
  11. ... ident="raster3d@PERMANENT", north=90, south=90, east=180, west=180,
  12. ... top=100, bottom=-20)
  13. >>> extent = tgis.VectorSpatialExtent(
  14. ... ident="vector@PERMANENT", north=90, south=90, east=180, west=180,
  15. ... top=100, bottom=-20)
  16. >>> extent = tgis.STRDSSpatialExtent(
  17. ... ident="strds@PERMANENT", north=90, south=90, east=180, west=180,
  18. ... top=100, bottom=-20)
  19. >>> extent = tgis.STR3DSSpatialExtent(
  20. ... ident="str3ds@PERMANENT", north=90, south=90, east=180, west=180,
  21. ... top=100, bottom=-20)
  22. >>> extent = tgis.STVDSSpatialExtent(
  23. ... ident="stvds@PERMANENT", north=90, south=90, east=180, west=180,
  24. ... top=100, bottom=-20)
  25. (C) 2012-2013 by the GRASS Development Team
  26. This program is free software under the GNU General Public
  27. License (>=v2). Read the file COPYING that comes with GRASS
  28. for details.
  29. :authors: Soeren Gebbert
  30. """
  31. from __future__ import print_function
  32. from .base import SQLDatabaseInterface
  33. class SpatialExtent(SQLDatabaseInterface):
  34. """This is the spatial extent base class for all maps and space time datasets
  35. This class implements a three dimensional axis aligned bounding box
  36. and functions to compute topological relationships
  37. Usage:
  38. .. code-block:: python
  39. >>> init()
  40. >>> extent = SpatialExtent(table="raster_spatial_extent",
  41. ... ident="soil@PERMANENT", north=90, south=90, east=180, west=180,
  42. ... top=100, bottom=-20)
  43. >>> extent.id
  44. 'soil@PERMANENT'
  45. >>> extent.north
  46. 90.0
  47. >>> extent.south
  48. 90.0
  49. >>> extent.east
  50. 180.0
  51. >>> extent.west
  52. 180.0
  53. >>> extent.top
  54. 100.0
  55. >>> extent.bottom
  56. -20.0
  57. >>> extent.print_info()
  58. +-------------------- Spatial extent ----------------------------------------+
  59. | North:...................... 90.0
  60. | South:...................... 90.0
  61. | East:.. .................... 180.0
  62. | West:....................... 180.0
  63. | Top:........................ 100.0
  64. | Bottom:..................... -20.0
  65. >>> extent.print_shell_info()
  66. north=90.0
  67. south=90.0
  68. east=180.0
  69. west=180.0
  70. top=100.0
  71. bottom=-20.0
  72. """
  73. def __init__(
  74. self,
  75. table=None,
  76. ident=None,
  77. north=None,
  78. south=None,
  79. east=None,
  80. west=None,
  81. top=None,
  82. bottom=None,
  83. proj="XY",
  84. ):
  85. SQLDatabaseInterface.__init__(self, table, ident)
  86. self.set_id(ident)
  87. self.set_spatial_extent_from_values(north, south, east, west, top, bottom)
  88. self.set_projection(proj)
  89. def overlapping_2d(self, extent):
  90. """Return True if this (A) and the provided spatial extent (B) overlaps
  91. in two dimensional space.
  92. Code is lend from wind_overlap.c in lib/gis
  93. Overlapping includes the spatial relations:
  94. - contain
  95. - in
  96. - cover
  97. - covered
  98. - equivalent
  99. .. code-block:: python
  100. >>> A = SpatialExtent(north=80, south=20, east=60, west=10)
  101. >>> B = SpatialExtent(north=80, south=20, east=60, west=10)
  102. >>> A.overlapping_2d(B)
  103. True
  104. :param extent: The spatial extent to check overlapping with
  105. :return: True or False
  106. """
  107. if self.get_projection() != extent.get_projection():
  108. self.msgr.error(
  109. _(
  110. "Projections are different. Unable to compute "
  111. "overlapping_2d for spatial extents"
  112. )
  113. )
  114. return False
  115. N = extent.get_north()
  116. S = extent.get_south()
  117. E = extent.get_east()
  118. W = extent.get_west()
  119. # Adjust the east and west in case of LL projection
  120. if self.get_projection() == "LL":
  121. while E < self.get_west():
  122. E += 360.0
  123. W += 360.0
  124. while W > self.get_east():
  125. E -= 360.0
  126. W -= 360.0
  127. if self.get_north() <= S:
  128. return False
  129. if self.get_south() >= N:
  130. return False
  131. if self.get_east() <= W:
  132. return False
  133. if self.get_west() >= E:
  134. return False
  135. return True
  136. def overlapping(self, extent):
  137. """Return True if this (A) and the provided spatial
  138. extent (B) overlaps in three dimensional space.
  139. Overlapping includes the spatial relations:
  140. - contain
  141. - in
  142. - cover
  143. - covered
  144. - equivalent
  145. Usage:
  146. .. code-block:: python
  147. >>> A = SpatialExtent(north=80, south=20, east=60, west=10, bottom=-50, top=50)
  148. >>> B = SpatialExtent(north=80, south=20, east=60, west=10, bottom=-50, top=50)
  149. >>> A.overlapping(B)
  150. True
  151. :param extent: The spatial extent to check overlapping with
  152. :return: True or False
  153. """
  154. if not self.overlapping_2d(extent):
  155. return False
  156. T = extent.get_top()
  157. B = extent.get_bottom()
  158. if self.get_top() <= B:
  159. return False
  160. if self.get_bottom() >= T:
  161. return False
  162. return True
  163. def intersect_2d(self, extent):
  164. """Return the two dimensional intersection as spatial_extent
  165. object or None in case no intersection was found.
  166. :param extent: The spatial extent to intersect with
  167. :return: The intersection spatial extent
  168. """
  169. if not self.overlapping_2d(extent):
  170. return None
  171. eN = extent.get_north()
  172. eS = extent.get_south()
  173. eE = extent.get_east()
  174. eW = extent.get_west()
  175. N = self.get_north()
  176. S = self.get_south()
  177. E = self.get_east()
  178. W = self.get_west()
  179. # Adjust the east and west in case of LL projection
  180. if self.get_projection() == "LL":
  181. while eE < W:
  182. eE += 360.0
  183. eW += 360.0
  184. while eW > E:
  185. eE -= 360.0
  186. eW -= 360.0
  187. # Compute the extent
  188. nN = N
  189. nS = S
  190. nE = E
  191. nW = W
  192. if W < eW:
  193. nW = eW
  194. if E > eE:
  195. nE = eE
  196. if N > eN:
  197. nN = eN
  198. if S < eS:
  199. nS = eS
  200. new = SpatialExtent(
  201. north=nN,
  202. south=nS,
  203. east=nE,
  204. west=nW,
  205. top=0,
  206. bottom=0,
  207. proj=self.get_projection(),
  208. )
  209. return new
  210. def intersect(self, extent):
  211. """Return the three dimensional intersection as spatial_extent
  212. object or None in case no intersection was found.
  213. Usage:
  214. .. code-block:: python
  215. >>> A = SpatialExtent(north=80, south=20, east=60, west=10,
  216. ... bottom=-50, top=50)
  217. >>> B = SpatialExtent(north=80, south=20, east=60, west=10,
  218. ... bottom=-50, top=50)
  219. >>> C = A.intersect(B)
  220. >>> C.print_info()
  221. +-------------------- Spatial extent ----------------------------------------+
  222. | North:...................... 80.0
  223. | South:...................... 20.0
  224. | East:.. .................... 60.0
  225. | West:....................... 10.0
  226. | Top:........................ 50.0
  227. | Bottom:..................... -50.0
  228. >>> B = SpatialExtent(north=40, south=30, east=60, west=10,
  229. ... bottom=-50, top=50)
  230. >>> C = A.intersect(B)
  231. >>> C.print_info()
  232. +-------------------- Spatial extent ----------------------------------------+
  233. | North:...................... 40.0
  234. | South:...................... 30.0
  235. | East:.. .................... 60.0
  236. | West:....................... 10.0
  237. | Top:........................ 50.0
  238. | Bottom:..................... -50.0
  239. >>> B = SpatialExtent(north=40, south=30, east=60, west=30,
  240. ... bottom=-50, top=50)
  241. >>> C = A.intersect(B)
  242. >>> C.print_info()
  243. +-------------------- Spatial extent ----------------------------------------+
  244. | North:...................... 40.0
  245. | South:...................... 30.0
  246. | East:.. .................... 60.0
  247. | West:....................... 30.0
  248. | Top:........................ 50.0
  249. | Bottom:..................... -50.0
  250. >>> B = SpatialExtent(north=40, south=30, east=60, west=30,
  251. ... bottom=-30, top=50)
  252. >>> C = A.intersect(B)
  253. >>> C.print_info()
  254. +-------------------- Spatial extent ----------------------------------------+
  255. | North:...................... 40.0
  256. | South:...................... 30.0
  257. | East:.. .................... 60.0
  258. | West:....................... 30.0
  259. | Top:........................ 50.0
  260. | Bottom:..................... -30.0
  261. >>> B = SpatialExtent(north=40, south=30, east=60, west=30,
  262. ... bottom=-30, top=30)
  263. >>> C = A.intersect(B)
  264. >>> C.print_info()
  265. +-------------------- Spatial extent ----------------------------------------+
  266. | North:...................... 40.0
  267. | South:...................... 30.0
  268. | East:.. .................... 60.0
  269. | West:....................... 30.0
  270. | Top:........................ 30.0
  271. | Bottom:..................... -30.0
  272. :param extent: The spatial extent to intersect with
  273. :return: The intersection spatial extent
  274. """
  275. if not self.overlapping(extent):
  276. return None
  277. new = self.intersect_2d(extent)
  278. eT = extent.get_top()
  279. eB = extent.get_bottom()
  280. T = self.get_top()
  281. B = self.get_bottom()
  282. nT = T
  283. nB = B
  284. if B < eB:
  285. nB = eB
  286. if T > eT:
  287. nT = eT
  288. new.set_top(nT)
  289. new.set_bottom(nB)
  290. return new
  291. def union_2d(self, extent):
  292. """Return the two dimensional union as spatial_extent
  293. object or None in case the extents does not overlap or meet.
  294. :param extent: The spatial extent to create a union with
  295. :return: The union spatial extent
  296. """
  297. if not self.overlapping_2d(extent) and not self.meet_2d(extent):
  298. return None
  299. return self.disjoint_union_2d(extent)
  300. def disjoint_union_2d(self, extent):
  301. """Return the two dimensional union as spatial_extent.
  302. :param extent: The spatial extent to create a union with
  303. :return: The union spatial extent
  304. """
  305. eN = extent.get_north()
  306. eS = extent.get_south()
  307. eE = extent.get_east()
  308. eW = extent.get_west()
  309. N = self.get_north()
  310. S = self.get_south()
  311. E = self.get_east()
  312. W = self.get_west()
  313. # Adjust the east and west in case of LL projection
  314. if self.get_projection() == "LL":
  315. while eE < W:
  316. eE += 360.0
  317. eW += 360.0
  318. while eW > E:
  319. eE -= 360.0
  320. eW -= 360.0
  321. # Compute the extent
  322. nN = N
  323. nS = S
  324. nE = E
  325. nW = W
  326. if W > eW:
  327. nW = eW
  328. if E < eE:
  329. nE = eE
  330. if N < eN:
  331. nN = eN
  332. if S > eS:
  333. nS = eS
  334. new = SpatialExtent(
  335. north=nN,
  336. south=nS,
  337. east=nE,
  338. west=nW,
  339. top=0,
  340. bottom=0,
  341. proj=self.get_projection(),
  342. )
  343. return new
  344. def union(self, extent):
  345. """Return the three dimensional union as spatial_extent
  346. object or None in case the extents does not overlap or meet.
  347. :param extent: The spatial extent to create a union with
  348. :return: The union spatial extent
  349. """
  350. if not self.overlapping(extent) and not self.meet(extent):
  351. return None
  352. return self.disjoint_union(extent)
  353. def disjoint_union(self, extent):
  354. """Return the three dimensional union as spatial_extent .
  355. Usage:
  356. .. code-block:: python
  357. >>> A = SpatialExtent(north=80, south=20, east=60, west=10,
  358. ... bottom=-50, top=50)
  359. >>> B = SpatialExtent(north=80, south=20, east=60, west=10,
  360. ... bottom=-50, top=50)
  361. >>> C = A.disjoint_union(B)
  362. >>> C.print_info()
  363. +-------------------- Spatial extent ----------------------------------------+
  364. | North:...................... 80.0
  365. | South:...................... 20.0
  366. | East:.. .................... 60.0
  367. | West:....................... 10.0
  368. | Top:........................ 50.0
  369. | Bottom:..................... -50.0
  370. >>> B = SpatialExtent(north=40, south=30, east=60, west=10,
  371. ... bottom=-50, top=50)
  372. >>> C = A.disjoint_union(B)
  373. >>> C.print_info()
  374. +-------------------- Spatial extent ----------------------------------------+
  375. | North:...................... 80.0
  376. | South:...................... 20.0
  377. | East:.. .................... 60.0
  378. | West:....................... 10.0
  379. | Top:........................ 50.0
  380. | Bottom:..................... -50.0
  381. >>> B = SpatialExtent(north=40, south=30, east=60, west=30,
  382. ... bottom=-50, top=50)
  383. >>> C = A.disjoint_union(B)
  384. >>> C.print_info()
  385. +-------------------- Spatial extent ----------------------------------------+
  386. | North:...................... 80.0
  387. | South:...................... 20.0
  388. | East:.. .................... 60.0
  389. | West:....................... 10.0
  390. | Top:........................ 50.0
  391. | Bottom:..................... -50.0
  392. >>> B = SpatialExtent(north=40, south=30, east=60, west=30,
  393. ... bottom=-30, top=50)
  394. >>> C = A.disjoint_union(B)
  395. >>> C.print_info()
  396. +-------------------- Spatial extent ----------------------------------------+
  397. | North:...................... 80.0
  398. | South:...................... 20.0
  399. | East:.. .................... 60.0
  400. | West:....................... 10.0
  401. | Top:........................ 50.0
  402. | Bottom:..................... -50.0
  403. >>> B = SpatialExtent(north=40, south=30, east=60, west=30,
  404. ... bottom=-30, top=30)
  405. >>> C = A.disjoint_union(B)
  406. >>> C.print_info()
  407. +-------------------- Spatial extent ----------------------------------------+
  408. | North:...................... 80.0
  409. | South:...................... 20.0
  410. | East:.. .................... 60.0
  411. | West:....................... 10.0
  412. | Top:........................ 50.0
  413. | Bottom:..................... -50.0
  414. >>> A = SpatialExtent(north=80, south=20, east=60, west=10,
  415. ... bottom=-50, top=50)
  416. >>> B = SpatialExtent(north=90, south=80, east=70, west=20,
  417. ... bottom=-30, top=60)
  418. >>> C = A.disjoint_union(B)
  419. >>> C.print_info()
  420. +-------------------- Spatial extent ----------------------------------------+
  421. | North:...................... 90.0
  422. | South:...................... 20.0
  423. | East:.. .................... 70.0
  424. | West:....................... 10.0
  425. | Top:........................ 60.0
  426. | Bottom:..................... -50.0
  427. :param extent: The spatial extent to create a disjoint union with
  428. :return: The union spatial extent
  429. """
  430. new = self.disjoint_union_2d(extent)
  431. eT = extent.get_top()
  432. eB = extent.get_bottom()
  433. T = self.get_top()
  434. B = self.get_bottom()
  435. nT = T
  436. nB = B
  437. if B > eB:
  438. nB = eB
  439. if T < eT:
  440. nT = eT
  441. new.set_top(nT)
  442. new.set_bottom(nB)
  443. return new
  444. def is_in_2d(self, extent):
  445. """Return True if this extent (A) is located in the provided spatial
  446. extent (B) in two dimensions.
  447. ::
  448. _____
  449. |A _ |
  450. | |_| |
  451. |_____|B
  452. :param extent: The spatial extent
  453. :return: True or False
  454. """
  455. if self.get_projection() != extent.get_projection():
  456. self.msgr.error(
  457. _(
  458. "Projections are different. Unable to compute "
  459. "is_in_2d for spatial extents"
  460. )
  461. )
  462. return False
  463. eN = extent.get_north()
  464. eS = extent.get_south()
  465. eE = extent.get_east()
  466. eW = extent.get_west()
  467. N = self.get_north()
  468. S = self.get_south()
  469. E = self.get_east()
  470. W = self.get_west()
  471. # Adjust the east and west in case of LL projection
  472. if self.get_projection() == "LL":
  473. while eE < W:
  474. eE += 360.0
  475. eW += 360.0
  476. while eW > E:
  477. eE -= 360.0
  478. eW -= 360.0
  479. if W <= eW:
  480. return False
  481. if E >= eE:
  482. return False
  483. if N >= eN:
  484. return False
  485. if S <= eS:
  486. return False
  487. return True
  488. def is_in(self, extent):
  489. """Return True if this extent (A) is located in the provided spatial
  490. extent (B) in three dimensions.
  491. Usage:
  492. .. code-block:: python
  493. >>> A = SpatialExtent(north=79, south=21, east=59, west=11,
  494. ... bottom=-49, top=49)
  495. >>> B = SpatialExtent(north=80, south=20, east=60, west=10,
  496. ... bottom=-50, top=50)
  497. >>> A.is_in(B)
  498. True
  499. >>> B.is_in(A)
  500. False
  501. :param extent: The spatial extent
  502. :return: True or False
  503. """
  504. if not self.is_in_2d(extent):
  505. return False
  506. eT = extent.get_top()
  507. eB = extent.get_bottom()
  508. T = self.get_top()
  509. B = self.get_bottom()
  510. if B <= eB:
  511. return False
  512. if T >= eT:
  513. return False
  514. return True
  515. def contain_2d(self, extent):
  516. """Return True if this extent (A) contains the provided spatial
  517. extent (B) in two dimensions.
  518. Usage:
  519. .. code-block:: python
  520. >>> A = SpatialExtent(north=80, south=20, east=60, west=10)
  521. >>> B = SpatialExtent(north=79, south=21, east=59, west=11)
  522. >>> A.contain_2d(B)
  523. True
  524. >>> B.contain_2d(A)
  525. False
  526. :param extent: The spatial extent
  527. :return: True or False
  528. """
  529. return extent.is_in_2d(self)
  530. def contain(self, extent):
  531. """Return True if this extent (A) contains the provided spatial
  532. extent (B) in three dimensions.
  533. Usage:
  534. .. code-block:: python
  535. >>> A = SpatialExtent(north=80, south=20, east=60, west=10,
  536. ... bottom=-50, top=50)
  537. >>> B = SpatialExtent(north=79, south=21, east=59, west=11,
  538. ... bottom=-49, top=49)
  539. >>> A.contain(B)
  540. True
  541. >>> B.contain(A)
  542. False
  543. :param extent: The spatial extent
  544. :return: True or False
  545. """
  546. return extent.is_in(self)
  547. def equivalent_2d(self, extent):
  548. """Return True if this extent (A) is equal to the provided spatial
  549. extent (B) in two dimensions.
  550. Usage:
  551. .. code-block:: python
  552. >>> A = SpatialExtent(north=80, south=20, east=60, west=10)
  553. >>> B = SpatialExtent(north=80, south=20, east=60, west=10)
  554. >>> A.equivalent_2d(B)
  555. True
  556. >>> B.equivalent_2d(A)
  557. True
  558. :param extent: The spatial extent
  559. :return: True or False
  560. """
  561. if self.get_projection() != extent.get_projection():
  562. self.msgr.error(
  563. _(
  564. "Projections are different. Unable to compute "
  565. "equivalent_2d for spatial extents"
  566. )
  567. )
  568. return False
  569. eN = extent.get_north()
  570. eS = extent.get_south()
  571. eE = extent.get_east()
  572. eW = extent.get_west()
  573. N = self.get_north()
  574. S = self.get_south()
  575. E = self.get_east()
  576. W = self.get_west()
  577. # Adjust the east and west in case of LL projection
  578. if self.get_projection() == "LL":
  579. while eE < W:
  580. eE += 360.0
  581. eW += 360.0
  582. while eW > E:
  583. eE -= 360.0
  584. eW -= 360.0
  585. if W != eW:
  586. return False
  587. if E != eE:
  588. return False
  589. if N != eN:
  590. return False
  591. if S != eS:
  592. return False
  593. return True
  594. def equivalent(self, extent):
  595. """Return True if this extent (A) is equal to the provided spatial
  596. extent (B) in three dimensions.
  597. Usage:
  598. .. code-block:: python
  599. >>> A = SpatialExtent(north=80, south=20, east=60, west=10,
  600. ... bottom=-50, top=50)
  601. >>> B = SpatialExtent(north=80, south=20, east=60, west=10,
  602. ... bottom=-50, top=50)
  603. >>> A.equivalent(B)
  604. True
  605. >>> B.equivalent(A)
  606. True
  607. :param extent: The spatial extent
  608. :return: True or False
  609. """
  610. if not self.equivalent_2d(extent):
  611. return False
  612. eT = extent.get_top()
  613. eB = extent.get_bottom()
  614. T = self.get_top()
  615. B = self.get_bottom()
  616. if B != eB:
  617. return False
  618. if T != eT:
  619. return False
  620. return True
  621. def cover_2d(self, extent):
  622. """Return True if this extent (A) covers the provided spatial
  623. extent (B) in two dimensions.
  624. ::
  625. _____ _____ _____ _____
  626. |A __| |__ A| |A | B| |B | A|
  627. | |B | | B| | | |__| |__| |
  628. |__|__| |__|__| |_____| |_____|
  629. _____ _____ _____ _____
  630. |A|B| | |A __| |A _ | |__ A|
  631. | |_| | | |__|B | |B| | B|__| |
  632. |_____| |_____| |_|_|_| |_____|
  633. _____ _____ _____ _____
  634. |A|B | |_____|A |A|B|A| |_____|A
  635. | | | |B | | | | | |_____|B
  636. |_|___| |_____| |_|_|_| |_____|A
  637. The following cases are excluded:
  638. - contain
  639. - in
  640. - equivalent
  641. :param extent: The spatial extent
  642. :return: True or False
  643. """
  644. if self.get_projection() != extent.get_projection():
  645. self.msgr.error(
  646. _(
  647. "Projections are different. Unable to compute"
  648. " cover_2d for spatial extents"
  649. )
  650. )
  651. return False
  652. # Exclude equivalent_2d
  653. if self.equivalent_2d(extent):
  654. return False
  655. eN = extent.get_north()
  656. eS = extent.get_south()
  657. eE = extent.get_east()
  658. eW = extent.get_west()
  659. N = self.get_north()
  660. S = self.get_south()
  661. E = self.get_east()
  662. W = self.get_west()
  663. # Adjust the east and west in case of LL projection
  664. if self.get_projection() == "LL":
  665. while eE < W:
  666. eE += 360.0
  667. eW += 360.0
  668. while eW > E:
  669. eE -= 360.0
  670. eW -= 360.0
  671. # Edges of extent located outside of self are not allowed
  672. if E <= eW:
  673. return False
  674. if W >= eE:
  675. return False
  676. if N <= eS:
  677. return False
  678. if S >= eN:
  679. return False
  680. # First we check that at least one edge of extent meets an edge of self
  681. if W != eW and E != eE and N != eN and S != eS:
  682. return False
  683. # We check that at least one edge of extent is located in self
  684. edge_count = 0
  685. if W < eW and E > eW:
  686. edge_count += 1
  687. if E > eE and W < eE:
  688. edge_count += 1
  689. if N > eN and S < eN:
  690. edge_count += 1
  691. if S < eS and N > eS:
  692. edge_count += 1
  693. if edge_count == 0:
  694. return False
  695. return True
  696. def cover(self, extent):
  697. """Return True if this extent covers the provided spatial
  698. extent in three dimensions.
  699. The following cases are excluded:
  700. - contain
  701. - in
  702. - equivalent
  703. :param extent: The spatial extent
  704. :return: True or False
  705. """
  706. if self.get_projection() != extent.get_projection():
  707. self.msgr.error(
  708. _(
  709. "Projections are different. Unable to compute "
  710. "cover for spatial extents"
  711. )
  712. )
  713. return False
  714. # Exclude equivalent_2d
  715. if self.equivalent_2d(extent):
  716. return False
  717. eN = extent.get_north()
  718. eS = extent.get_south()
  719. eE = extent.get_east()
  720. eW = extent.get_west()
  721. eT = extent.get_top()
  722. eB = extent.get_bottom()
  723. N = self.get_north()
  724. S = self.get_south()
  725. E = self.get_east()
  726. W = self.get_west()
  727. T = self.get_top()
  728. B = self.get_bottom()
  729. # Adjust the east and west in case of LL projection
  730. if self.get_projection() == "LL":
  731. while eE < W:
  732. eE += 360.0
  733. eW += 360.0
  734. while eW > E:
  735. eE -= 360.0
  736. eW -= 360.0
  737. # Edges of extent located outside of self are not allowed
  738. if E <= eW:
  739. return False
  740. if W >= eE:
  741. return False
  742. if N <= eS:
  743. return False
  744. if S >= eN:
  745. return False
  746. if T <= eB:
  747. return False
  748. if B >= eT:
  749. return False
  750. # First we check that at least one edge of extent meets an edge of self
  751. if W != eW and E != eE and N != eN and S != eS and B != eB and T != eT:
  752. return False
  753. # We check that at least one edge of extent is located in self
  754. edge_count = 0
  755. if W < eW and E > eW:
  756. edge_count += 1
  757. if E > eE and W < eE:
  758. edge_count += 1
  759. if N > eN and S < eN:
  760. edge_count += 1
  761. if S < eS and N > eS:
  762. edge_count += 1
  763. if N > eN and S < eN:
  764. edge_count += 1
  765. if S < eS and N > eS:
  766. edge_count += 1
  767. if T > eT and B < eT:
  768. edge_count += 1
  769. if B < eB and T > eB:
  770. edge_count += 1
  771. if edge_count == 0:
  772. return False
  773. return True
  774. def covered_2d(self, extent):
  775. """Return True if this extent is covered by the provided spatial
  776. extent in two dimensions.
  777. The following cases are excluded:
  778. - contain
  779. - in
  780. - equivalent
  781. :param extent: The spatial extent
  782. :return: True or False
  783. """
  784. return extent.cover_2d(self)
  785. def covered(self, extent):
  786. """Return True if this extent is covered by the provided spatial
  787. extent in three dimensions.
  788. The following cases are excluded:
  789. - contain
  790. - in
  791. - equivalent
  792. :param extent: The spatial extent
  793. :return: True or False
  794. """
  795. return extent.cover(self)
  796. def overlap_2d(self, extent):
  797. """Return True if this extent (A) overlaps with the provided spatial
  798. extent (B) in two dimensions.
  799. Code is lend from wind_overlap.c in lib/gis
  800. ::
  801. _____
  802. |A __|__
  803. | | | B|
  804. |__|__| |
  805. |_____|
  806. The following cases are excluded:
  807. - contain
  808. - in
  809. - cover
  810. - covered
  811. - equivalent
  812. :param extent: The spatial extent
  813. :return: True or False
  814. """
  815. if self.contain_2d(extent):
  816. return False
  817. if self.is_in_2d(extent):
  818. return False
  819. if self.cover_2d(extent):
  820. return False
  821. if self.covered_2d(extent):
  822. return False
  823. if self.equivalent_2d(extent):
  824. return False
  825. N = extent.get_north()
  826. S = extent.get_south()
  827. E = extent.get_east()
  828. W = extent.get_west()
  829. # Adjust the east and west in case of LL projection
  830. if self.get_projection() == "LL":
  831. while E < self.get_west():
  832. E += 360.0
  833. W += 360.0
  834. while W > self.get_east():
  835. E -= 360.0
  836. W -= 360.0
  837. if self.get_north() <= S:
  838. return False
  839. if self.get_south() >= N:
  840. return False
  841. if self.get_east() <= W:
  842. return False
  843. if self.get_west() >= E:
  844. return False
  845. return True
  846. def overlap(self, extent):
  847. """Return True if this extent overlaps with the provided spatial
  848. extent in three dimensions.
  849. The following cases are excluded:
  850. - contain
  851. - in
  852. - cover
  853. - covered
  854. - equivalent
  855. :param extent: The spatial extent
  856. :return: True or False
  857. """
  858. if self.is_in(extent):
  859. return False
  860. if self.contain(extent):
  861. return False
  862. if self.cover(extent):
  863. return False
  864. if self.covered(extent):
  865. return False
  866. if self.equivalent(extent):
  867. return False
  868. N = extent.get_north()
  869. S = extent.get_south()
  870. E = extent.get_east()
  871. W = extent.get_west()
  872. T = extent.get_top()
  873. B = extent.get_bottom()
  874. # Adjust the east and west in case of LL projection
  875. if self.get_projection() == "LL":
  876. while E < self.get_west():
  877. E += 360.0
  878. W += 360.0
  879. while W > self.get_east():
  880. E -= 360.0
  881. W -= 360.0
  882. if self.get_north() <= S:
  883. return False
  884. if self.get_south() >= N:
  885. return False
  886. if self.get_east() <= W:
  887. return False
  888. if self.get_west() >= E:
  889. return False
  890. if self.get_top() <= B:
  891. return False
  892. if self.get_bottom() >= T:
  893. return False
  894. return True
  895. def meet_2d(self, extent):
  896. """Return True if this extent (A) meets with the provided spatial
  897. extent (B) in two dimensions.
  898. ::
  899. _____ _____
  900. | A | B |
  901. |_____| |
  902. |_____|
  903. _____ _____
  904. | B | A |
  905. | | |
  906. |_____|_____|
  907. ___
  908. | A |
  909. | |
  910. |___|
  911. | B |
  912. | |
  913. |_____|
  914. _____
  915. | B |
  916. | |
  917. |_____|_
  918. | A |
  919. | |
  920. |_____|
  921. :param extent: The spatial extent
  922. :return: True or False
  923. """
  924. eN = extent.get_north()
  925. eS = extent.get_south()
  926. eE = extent.get_east()
  927. eW = extent.get_west()
  928. N = self.get_north()
  929. S = self.get_south()
  930. E = self.get_east()
  931. W = self.get_west()
  932. # Adjust the east and west in case of LL projection
  933. if self.get_projection() == "LL":
  934. while eE < W:
  935. eE += 360.0
  936. eW += 360.0
  937. while eW > E:
  938. eE -= 360.0
  939. eW -= 360.0
  940. edge = None
  941. edge_count = 0
  942. if E == eW:
  943. edge = "E"
  944. edge_count += 1
  945. if W == eE:
  946. edge = "W"
  947. edge_count += 1
  948. if N == eS:
  949. edge = "N"
  950. edge_count += 1
  951. if S == eN:
  952. edge = "S"
  953. edge_count += 1
  954. # Meet a a single edge only
  955. if edge_count != 1:
  956. return False
  957. # Check boundaries of the faces
  958. if edge == "E" or edge == "W":
  959. if N < eS or S > eN:
  960. return False
  961. if edge == "N" or edge == "S":
  962. if E < eW or W > eE:
  963. return False
  964. return True
  965. def meet(self, extent):
  966. """Return True if this extent meets with the provided spatial
  967. extent in three dimensions.
  968. :param extent: The spatial extent
  969. :return: True or False
  970. """
  971. eN = extent.get_north()
  972. eS = extent.get_south()
  973. eE = extent.get_east()
  974. eW = extent.get_west()
  975. eT = extent.get_top()
  976. eB = extent.get_bottom()
  977. N = self.get_north()
  978. S = self.get_south()
  979. E = self.get_east()
  980. W = self.get_west()
  981. T = self.get_top()
  982. B = self.get_bottom()
  983. # Adjust the east and west in case of LL projection
  984. if self.get_projection() == "LL":
  985. while eE < W:
  986. eE += 360.0
  987. eW += 360.0
  988. while eW > E:
  989. eE -= 360.0
  990. eW -= 360.0
  991. edge = None
  992. edge_count = 0
  993. if E == eW:
  994. edge = "E"
  995. edge_count += 1
  996. if W == eE:
  997. edge = "W"
  998. edge_count += 1
  999. if N == eS:
  1000. edge = "N"
  1001. edge_count += 1
  1002. if S == eN:
  1003. edge = "S"
  1004. edge_count += 1
  1005. if T == eB:
  1006. edge = "T"
  1007. edge_count += 1
  1008. if B == eT:
  1009. edge = "B"
  1010. edge_count += 1
  1011. # Meet a single edge only
  1012. if edge_count != 1:
  1013. return False
  1014. # Check boundaries of the faces
  1015. if edge == "E" or edge == "W":
  1016. if N < eS or S > eN:
  1017. return False
  1018. if T < eB or B > eT:
  1019. return False
  1020. if edge == "N" or edge == "S":
  1021. if E < eW or W > eE:
  1022. return False
  1023. if T < eB or B > eT:
  1024. return False
  1025. if edge == "T" or edge == "B":
  1026. if E < eW or W > eE:
  1027. return False
  1028. if N < eS or S > eN:
  1029. return False
  1030. return True
  1031. def disjoint_2d(self, extent):
  1032. """Return True if this extent (A) is disjoint with the provided spatial
  1033. extent (B) in three dimensions.
  1034. ::
  1035. _____
  1036. | A |
  1037. |_____|
  1038. _______
  1039. | B |
  1040. |_______|
  1041. :param extent: The spatial extent
  1042. :return: True or False
  1043. """
  1044. if self.is_in_2d(extent):
  1045. return False
  1046. if self.contain_2d(extent):
  1047. return False
  1048. if self.cover_2d(extent):
  1049. return False
  1050. if self.covered_2d(extent):
  1051. return False
  1052. if self.equivalent_2d(extent):
  1053. return False
  1054. if self.overlapping_2d(extent):
  1055. return False
  1056. if self.meet_2d(extent):
  1057. return False
  1058. return True
  1059. def disjoint(self, extent):
  1060. """Return True if this extent is disjoint with the provided spatial
  1061. extent in three dimensions.
  1062. :param extent: The spatial extent
  1063. :return: True or False
  1064. """
  1065. if self.is_in(extent):
  1066. return False
  1067. if self.contain(extent):
  1068. return False
  1069. if self.cover(extent):
  1070. return False
  1071. if self.covered(extent):
  1072. return False
  1073. if self.equivalent(extent):
  1074. return False
  1075. if self.overlapping(extent):
  1076. return False
  1077. if self.meet(extent):
  1078. return False
  1079. return True
  1080. def spatial_relation_2d(self, extent):
  1081. """Returns the two dimensional spatial relation between this
  1082. extent and the provided spatial extent in two dimensions.
  1083. Spatial relations are:
  1084. - disjoint
  1085. - meet
  1086. - overlap
  1087. - cover
  1088. - covered
  1089. - in
  1090. - contain
  1091. - equivalent
  1092. Usage: see self.spatial_relation()
  1093. """
  1094. if self.equivalent_2d(extent):
  1095. return "equivalent"
  1096. if self.contain_2d(extent):
  1097. return "contain"
  1098. if self.is_in_2d(extent):
  1099. return "in"
  1100. if self.cover_2d(extent):
  1101. return "cover"
  1102. if self.covered_2d(extent):
  1103. return "covered"
  1104. if self.overlap_2d(extent):
  1105. return "overlap"
  1106. if self.meet_2d(extent):
  1107. return "meet"
  1108. if self.disjoint_2d(extent):
  1109. return "disjoint"
  1110. return "unknown"
  1111. def spatial_relation(self, extent):
  1112. """Returns the two dimensional spatial relation between this
  1113. extent and the provided spatial extent in three dimensions.
  1114. Spatial relations are:
  1115. - disjoint
  1116. - meet
  1117. - overlap
  1118. - cover
  1119. - covered
  1120. - in
  1121. - contain
  1122. - equivalent
  1123. Usage:
  1124. .. code-block:: python
  1125. >>> A = SpatialExtent(north=80, south=20, east=60, west=10, bottom=-50, top=50)
  1126. >>> B = SpatialExtent(north=80, south=20, east=60, west=10, bottom=-50, top=50)
  1127. >>> A.spatial_relation(B)
  1128. 'equivalent'
  1129. >>> B.spatial_relation(A)
  1130. 'equivalent'
  1131. >>> B = SpatialExtent(north=70, south=20, east=60, west=10, bottom=-50, top=50)
  1132. >>> A.spatial_relation_2d(B)
  1133. 'cover'
  1134. >>> A.spatial_relation(B)
  1135. 'cover'
  1136. >>> B = SpatialExtent(north=70, south=30, east=60, west=10, bottom=-50, top=50)
  1137. >>> A.spatial_relation_2d(B)
  1138. 'cover'
  1139. >>> A.spatial_relation(B)
  1140. 'cover'
  1141. >>> B.spatial_relation_2d(A)
  1142. 'covered'
  1143. >>> B.spatial_relation(A)
  1144. 'covered'
  1145. >>> B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=-50, top=50)
  1146. >>> A.spatial_relation_2d(B)
  1147. 'cover'
  1148. >>> B.spatial_relation_2d(A)
  1149. 'covered'
  1150. >>> A.spatial_relation(B)
  1151. 'cover'
  1152. >>> B = SpatialExtent(north=70, south=30, east=50, west=20, bottom=-50, top=50)
  1153. >>> B.spatial_relation(A)
  1154. 'covered'
  1155. >>> B = SpatialExtent(north=70, south=30, east=50, west=20, bottom=-50, top=50)
  1156. >>> A.spatial_relation_2d(B)
  1157. 'contain'
  1158. >>> A.spatial_relation(B)
  1159. 'cover'
  1160. >>> B = SpatialExtent(north=70, south=30, east=50, west=20, bottom=-40, top=50)
  1161. >>> A.spatial_relation(B)
  1162. 'cover'
  1163. >>> B = SpatialExtent(north=70, south=30, east=50, west=20, bottom=-40, top=40)
  1164. >>> A.spatial_relation(B)
  1165. 'contain'
  1166. >>> B.spatial_relation(A)
  1167. 'in'
  1168. >>> B = SpatialExtent(north=90, south=30, east=50, west=20, bottom=-40, top=40)
  1169. >>> A.spatial_relation_2d(B)
  1170. 'overlap'
  1171. >>> A.spatial_relation(B)
  1172. 'overlap'
  1173. >>> B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=40)
  1174. >>> A.spatial_relation_2d(B)
  1175. 'in'
  1176. >>> A.spatial_relation(B)
  1177. 'overlap'
  1178. >>> B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=60)
  1179. >>> A.spatial_relation(B)
  1180. 'overlap'
  1181. >>> B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-60, top=60)
  1182. >>> A.spatial_relation(B)
  1183. 'in'
  1184. >>> A = SpatialExtent(north=80, south=60, east=60, west=10, bottom=-50, top=50)
  1185. >>> B = SpatialExtent(north=60, south=20, east=60, west=10, bottom=-50, top=50)
  1186. >>> A.spatial_relation_2d(B)
  1187. 'meet'
  1188. >>> A.spatial_relation(B)
  1189. 'meet'
  1190. >>> A = SpatialExtent(north=60, south=40, east=60, west=10, bottom=-50, top=50)
  1191. >>> B = SpatialExtent(north=80, south=60, east=60, west=10, bottom=-50, top=50)
  1192. >>> A.spatial_relation_2d(B)
  1193. 'meet'
  1194. >>> A.spatial_relation(B)
  1195. 'meet'
  1196. >>> A = SpatialExtent(north=80, south=40, east=60, west=40, bottom=-50, top=50)
  1197. >>> B = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1198. >>> A.spatial_relation_2d(B)
  1199. 'meet'
  1200. >>> A.spatial_relation(B)
  1201. 'meet'
  1202. >>> A = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1203. >>> B = SpatialExtent(north=90, south=30, east=60, west=40, bottom=-50, top=50)
  1204. >>> A.spatial_relation_2d(B)
  1205. 'meet'
  1206. >>> A.spatial_relation(B)
  1207. 'meet'
  1208. >>> A = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1209. >>> B = SpatialExtent(north=70, south=50, east=60, west=40, bottom=-50, top=50)
  1210. >>> A.spatial_relation_2d(B)
  1211. 'meet'
  1212. >>> A.spatial_relation(B)
  1213. 'meet'
  1214. >>> A = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1215. >>> B = SpatialExtent(north=60, south=20, east=60, west=40, bottom=-50, top=50)
  1216. >>> A.spatial_relation_2d(B)
  1217. 'meet'
  1218. >>> A.spatial_relation(B)
  1219. 'meet'
  1220. >>> A = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1221. >>> B = SpatialExtent(north=40, south=20, east=60, west=40, bottom=-50, top=50)
  1222. >>> A.spatial_relation_2d(B)
  1223. 'disjoint'
  1224. >>> A.spatial_relation(B)
  1225. 'disjoint'
  1226. >>> A = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1227. >>> B = SpatialExtent(north=60, south=20, east=60, west=40, bottom=-60, top=60)
  1228. >>> A.spatial_relation(B)
  1229. 'meet'
  1230. >>> A = SpatialExtent(north=80, south=40, east=40, west=20, bottom=-50, top=50)
  1231. >>> B = SpatialExtent(north=90, south=30, east=60, west=40, bottom=-40, top=40)
  1232. >>> A.spatial_relation(B)
  1233. 'meet'
  1234. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1235. >>> B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1236. >>> A.spatial_relation(B)
  1237. 'meet'
  1238. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1239. >>> B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=-50, top=0)
  1240. >>> A.spatial_relation(B)
  1241. 'meet'
  1242. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1243. >>> B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=-50, top=0)
  1244. >>> A.spatial_relation(B)
  1245. 'meet'
  1246. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1247. >>> B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=-50, top=0)
  1248. >>> A.spatial_relation(B)
  1249. 'meet'
  1250. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1251. >>> B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=-50, top=0)
  1252. >>> A.spatial_relation(B)
  1253. 'meet'
  1254. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1255. >>> B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1256. >>> A.spatial_relation(B)
  1257. 'meet'
  1258. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1259. >>> B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=0, top=50)
  1260. >>> A.spatial_relation(B)
  1261. 'meet'
  1262. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1263. >>> B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=0, top=50)
  1264. >>> A.spatial_relation(B)
  1265. 'meet'
  1266. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1267. >>> B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=0, top=50)
  1268. >>> A.spatial_relation(B)
  1269. 'meet'
  1270. >>> A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1271. >>> B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=0, top=50)
  1272. >>> A.spatial_relation(B)
  1273. 'meet'
  1274. >>> A = SpatialExtent(north=80, south=20, east=60, west=10, bottom=-50, top=50)
  1275. >>> B = SpatialExtent(north=90, south=81, east=60, west=10, bottom=-50, top=50)
  1276. >>> A.spatial_relation(B)
  1277. 'disjoint'
  1278. >>> A = SpatialExtent(north=80, south=20, east=60, west=10, bottom=-50, top=50)
  1279. >>> B = SpatialExtent(north=90, south=80, east=60, west=10, bottom=-50, top=50)
  1280. >>> A.spatial_relation(B)
  1281. 'meet'
  1282. """
  1283. if self.equivalent(extent):
  1284. return "equivalent"
  1285. if self.contain(extent):
  1286. return "contain"
  1287. if self.is_in(extent):
  1288. return "in"
  1289. if self.cover(extent):
  1290. return "cover"
  1291. if self.covered(extent):
  1292. return "covered"
  1293. if self.overlap(extent):
  1294. return "overlap"
  1295. if self.meet(extent):
  1296. return "meet"
  1297. if self.disjoint(extent):
  1298. return "disjoint"
  1299. return "unknown"
  1300. def set_spatial_extent_from_values(self, north, south, east, west, top, bottom):
  1301. """Set the three dimensional spatial extent
  1302. :param north: The northern edge
  1303. :param south: The southern edge
  1304. :param east: The eastern edge
  1305. :param west: The western edge
  1306. :param top: The top edge
  1307. :param bottom: The bottom edge
  1308. """
  1309. self.set_north(north)
  1310. self.set_south(south)
  1311. self.set_east(east)
  1312. self.set_west(west)
  1313. self.set_top(top)
  1314. self.set_bottom(bottom)
  1315. def set_spatial_extent(self, spatial_extent):
  1316. """Set the three dimensional spatial extent
  1317. :param spatial_extent: An object of type SpatialExtent or its
  1318. subclasses
  1319. """
  1320. self.set_north(spatial_extent.get_north())
  1321. self.set_south(spatial_extent.get_south())
  1322. self.set_east(spatial_extent.get_east())
  1323. self.set_west(spatial_extent.get_west())
  1324. self.set_top(spatial_extent.get_top())
  1325. self.set_bottom(spatial_extent.get_bottom())
  1326. def set_projection(self, proj):
  1327. """Set the projection of the spatial extent it should be XY or LL.
  1328. As default the projection is XY
  1329. """
  1330. if proj is None or (proj != "XY" and proj != "LL"):
  1331. self.D["proj"] = "XY"
  1332. else:
  1333. self.D["proj"] = proj
  1334. def set_spatial_extent_from_values_2d(self, north, south, east, west):
  1335. """Set the two dimensional spatial extent from values
  1336. :param north: The northern edge
  1337. :param south: The southern edge
  1338. :param east: The eastern edge
  1339. :param west: The western edge
  1340. """
  1341. self.set_north(north)
  1342. self.set_south(south)
  1343. self.set_east(east)
  1344. self.set_west(west)
  1345. def set_spatial_extent_2d(self, spatial_extent):
  1346. """Set the three dimensional spatial extent
  1347. :param spatial_extent: An object of type SpatialExtent or its
  1348. subclasses
  1349. """
  1350. self.set_north(spatial_extent.north)
  1351. self.set_south(spatial_extent.south)
  1352. self.set_east(spatial_extent.east)
  1353. self.set_west(spatial_extent.west)
  1354. def set_id(self, ident):
  1355. """Convenient method to set the unique identifier (primary key)"""
  1356. self.ident = ident
  1357. self.D["id"] = ident
  1358. def set_north(self, north):
  1359. """Set the northern edge of the map"""
  1360. if north is not None:
  1361. self.D["north"] = float(north)
  1362. else:
  1363. self.D["north"] = None
  1364. def set_south(self, south):
  1365. """Set the southern edge of the map"""
  1366. if south is not None:
  1367. self.D["south"] = float(south)
  1368. else:
  1369. self.D["south"] = None
  1370. def set_west(self, west):
  1371. """Set the western edge of the map"""
  1372. if west is not None:
  1373. self.D["west"] = float(west)
  1374. else:
  1375. self.D["west"] = None
  1376. def set_east(self, east):
  1377. """Set the eastern edge of the map"""
  1378. if east is not None:
  1379. self.D["east"] = float(east)
  1380. else:
  1381. self.D["east"] = None
  1382. def set_top(self, top):
  1383. """Set the top edge of the map"""
  1384. if top is not None:
  1385. self.D["top"] = float(top)
  1386. else:
  1387. self.D["top"] = None
  1388. def set_bottom(self, bottom):
  1389. """Set the bottom edge of the map"""
  1390. if bottom is not None:
  1391. self.D["bottom"] = float(bottom)
  1392. else:
  1393. self.D["bottom"] = None
  1394. def get_id(self):
  1395. """Convenient method to get the unique identifier (primary key)
  1396. :return: None if not found
  1397. """
  1398. if "id" in self.D:
  1399. return self.D["id"]
  1400. else:
  1401. return None
  1402. def get_projection(self):
  1403. """Get the projection of the spatial extent"""
  1404. return self.D["proj"]
  1405. def get_volume(self):
  1406. """Compute the volume of the extent, in case z is zero
  1407. (top == bottom or top - bottom = 1) the area is returned"""
  1408. if self.get_projection() == "LL":
  1409. self.msgr.error(
  1410. _("Volume computation is not supported " "for LL projections")
  1411. )
  1412. area = self.get_area()
  1413. bbox = self.get_spatial_extent_as_tuple()
  1414. z = abs(bbox[4] - bbox[5])
  1415. if z == 0:
  1416. z = 1.0
  1417. return area * z
  1418. def get_area(self):
  1419. """Compute the area of the extent, extent in z direction is ignored"""
  1420. if self.get_projection() == "LL":
  1421. self.msgr.error(
  1422. _("Area computation is not supported " "for LL projections")
  1423. )
  1424. bbox = self.get_spatial_extent_as_tuple()
  1425. y = abs(bbox[0] - bbox[1])
  1426. x = abs(bbox[2] - bbox[3])
  1427. return x * y
  1428. def get_spatial_extent_as_tuple(self):
  1429. """Return a tuple (north, south, east, west, top, bottom)
  1430. of the spatial extent"""
  1431. return (self.north, self.south, self.east, self.west, self.top, self.bottom)
  1432. def get_spatial_extent_as_tuple_2d(self):
  1433. """Return a tuple (north, south, east, west,) of the 2d spatial extent"""
  1434. return (self.north, self.south, self.east, self.west)
  1435. def get_north(self):
  1436. """Get the northern edge of the map
  1437. :return: None if not found"""
  1438. if "north" in self.D:
  1439. return self.D["north"]
  1440. else:
  1441. return None
  1442. def get_south(self):
  1443. """Get the southern edge of the map
  1444. :return: None if not found"""
  1445. if "south" in self.D:
  1446. return self.D["south"]
  1447. else:
  1448. return None
  1449. def get_east(self):
  1450. """Get the eastern edge of the map
  1451. :return: None if not found"""
  1452. if "east" in self.D:
  1453. return self.D["east"]
  1454. else:
  1455. return None
  1456. def get_west(self):
  1457. """Get the western edge of the map
  1458. :return: None if not found"""
  1459. if "west" in self.D:
  1460. return self.D["west"]
  1461. else:
  1462. return None
  1463. def get_top(self):
  1464. """Get the top edge of the map
  1465. :return: None if not found"""
  1466. if "top" in self.D:
  1467. return self.D["top"]
  1468. else:
  1469. return None
  1470. def get_bottom(self):
  1471. """Get the bottom edge of the map
  1472. :return: None if not found"""
  1473. if "bottom" in self.D:
  1474. return self.D["bottom"]
  1475. else:
  1476. return None
  1477. id = property(fget=get_id, fset=set_id)
  1478. north = property(fget=get_north, fset=set_north)
  1479. south = property(fget=get_south, fset=set_south)
  1480. east = property(fget=get_east, fset=set_east)
  1481. west = property(fget=get_west, fset=set_west)
  1482. top = property(fget=get_top, fset=set_top)
  1483. bottom = property(fget=get_bottom, fset=set_bottom)
  1484. def print_info(self):
  1485. """Print information about this class in human readable style"""
  1486. # 0123456789012345678901234567890
  1487. print(
  1488. " +-------------------- Spatial extent ----------------------------------------+"
  1489. )
  1490. print(" | North:...................... " + str(self.get_north()))
  1491. print(" | South:...................... " + str(self.get_south()))
  1492. print(" | East:.. .................... " + str(self.get_east()))
  1493. print(" | West:....................... " + str(self.get_west()))
  1494. print(" | Top:........................ " + str(self.get_top()))
  1495. print(" | Bottom:..................... " + str(self.get_bottom()))
  1496. def print_shell_info(self):
  1497. """Print information about this class in shell style"""
  1498. print("north=" + str(self.get_north()))
  1499. print("south=" + str(self.get_south()))
  1500. print("east=" + str(self.get_east()))
  1501. print("west=" + str(self.get_west()))
  1502. print("top=" + str(self.get_top()))
  1503. print("bottom=" + str(self.get_bottom()))
  1504. ###############################################################################
  1505. class RasterSpatialExtent(SpatialExtent):
  1506. def __init__(
  1507. self,
  1508. ident=None,
  1509. north=None,
  1510. south=None,
  1511. east=None,
  1512. west=None,
  1513. top=None,
  1514. bottom=None,
  1515. ):
  1516. SpatialExtent.__init__(
  1517. self, "raster_spatial_extent", ident, north, south, east, west, top, bottom
  1518. )
  1519. class Raster3DSpatialExtent(SpatialExtent):
  1520. def __init__(
  1521. self,
  1522. ident=None,
  1523. north=None,
  1524. south=None,
  1525. east=None,
  1526. west=None,
  1527. top=None,
  1528. bottom=None,
  1529. ):
  1530. SpatialExtent.__init__(
  1531. self,
  1532. "raster3d_spatial_extent",
  1533. ident,
  1534. north,
  1535. south,
  1536. east,
  1537. west,
  1538. top,
  1539. bottom,
  1540. )
  1541. class VectorSpatialExtent(SpatialExtent):
  1542. def __init__(
  1543. self,
  1544. ident=None,
  1545. north=None,
  1546. south=None,
  1547. east=None,
  1548. west=None,
  1549. top=None,
  1550. bottom=None,
  1551. ):
  1552. SpatialExtent.__init__(
  1553. self, "vector_spatial_extent", ident, north, south, east, west, top, bottom
  1554. )
  1555. class STRDSSpatialExtent(SpatialExtent):
  1556. def __init__(
  1557. self,
  1558. ident=None,
  1559. north=None,
  1560. south=None,
  1561. east=None,
  1562. west=None,
  1563. top=None,
  1564. bottom=None,
  1565. ):
  1566. SpatialExtent.__init__(
  1567. self, "strds_spatial_extent", ident, north, south, east, west, top, bottom
  1568. )
  1569. class STR3DSSpatialExtent(SpatialExtent):
  1570. def __init__(
  1571. self,
  1572. ident=None,
  1573. north=None,
  1574. south=None,
  1575. east=None,
  1576. west=None,
  1577. top=None,
  1578. bottom=None,
  1579. ):
  1580. SpatialExtent.__init__(
  1581. self, "str3ds_spatial_extent", ident, north, south, east, west, top, bottom
  1582. )
  1583. class STVDSSpatialExtent(SpatialExtent):
  1584. def __init__(
  1585. self,
  1586. ident=None,
  1587. north=None,
  1588. south=None,
  1589. east=None,
  1590. west=None,
  1591. top=None,
  1592. bottom=None,
  1593. ):
  1594. SpatialExtent.__init__(
  1595. self, "stvds_spatial_extent", ident, north, south, east, west, top, bottom
  1596. )
  1597. ###############################################################################
  1598. if __name__ == "__main__":
  1599. import doctest
  1600. doctest.testmod()