torch-geopooling 1.3.0__cp310-cp310-manylinux_2_28_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ # SPDX-License-Identifier: GPL-3.0-or-later
2
+ # SPDX-FileCopyrightText: 2025 Yakau Bubnou
3
+ # SPDX-FileType: SOURCE
4
+
5
+ from __future__ import annotations
6
+
7
+ from typing import Iterator, Tuple
8
+
9
+ from torch import Tensor
10
+
11
+ from torch_geopooling.tiling import Tile
12
+
13
+ __all__ = ["TileWKT"]
14
+
15
+
16
+ class _TileSet(set):
17
+ def __init__(self, tiles: Tensor) -> None:
18
+ super().__init__(Tile(*tile.detach().tolist()) for tile in tiles)
19
+
20
+ def is_terminal(self, tile: Tile) -> bool:
21
+ return (
22
+ (tile.child(0, 0) not in self)
23
+ and (tile.child(0, 1) not in self)
24
+ and (tile.child(1, 0) not in self)
25
+ and (tile.child(1, 1) not in self)
26
+ )
27
+
28
+
29
+ class TileWKT:
30
+ """Convert a Tile to a WKT polygon given the exterior of the whole geometry.
31
+
32
+ Module returns a tile geometry in WKT format, which comprises a polygon.
33
+
34
+ Args:
35
+ exterior: Exterior coordinates in (X, Y, W, H) format. The exterior is used to calculate
36
+ boundaries of a tile to produce a final WKT.
37
+ precision: A precision of the resulting geometry, digits after the decimal point.
38
+ internal: When `True`, output includes internal nodes of the quadtree tiles.
39
+ Otherwise (default) returns only geometry of terminal nodes.
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ exterior: Tuple[float, float, float, float],
45
+ precision: int = 7,
46
+ internal: bool = False,
47
+ ) -> None:
48
+ self.exterior = tuple(map(float, exterior))
49
+ self.precision = precision
50
+ self.internal = internal
51
+
52
+ self._xmin, self._ymin, self._width, self._height = exterior
53
+ if self._width <= 0:
54
+ raise ValueError(f"exterior width should be >0, got {self._width}")
55
+ if self._height <= 0:
56
+ raise ValueError(f"exterior height should be >0, got {self._height}")
57
+
58
+ def __call__(self, tiles: Tensor) -> Iterator[str]:
59
+ if len(tiles.size()) != 2:
60
+ raise ValueError(f"tiles tensor must be a 2D tensor, got {tiles.size()} shape")
61
+
62
+ if tiles.size(1) != 3:
63
+ raise ValueError(
64
+ f"tiles should be triplets of (z, x, y), got tensor of shape {tiles.size()}"
65
+ )
66
+
67
+ tileset = _TileSet(tiles)
68
+
69
+ for tile in tiles:
70
+ z, x, y = tile.detach().tolist()
71
+ width = self._width / (1 << z)
72
+ height = self._height / (1 << z)
73
+
74
+ if (not self.internal) and (not tileset.is_terminal(Tile(z, x, y))):
75
+ continue
76
+
77
+ xmin = self._xmin + width * x
78
+ xmax = round(xmin + width, self.precision)
79
+ xmin = round(xmin, self.precision)
80
+
81
+ ymin = self._ymin + height * y
82
+ ymax = round(ymin + height, self.precision)
83
+ ymin = round(ymin, self.precision)
84
+
85
+ yield (
86
+ "POLYGON (("
87
+ f"{xmin} {ymin}, {xmax} {ymin}, {xmax} {ymax}, {xmin} {ymax}, {xmin} {ymin}"
88
+ "))"
89
+ )
90
+
91
+ def __repr__(self) -> str:
92
+ return f"{self.__class__.__name__}(exterior={self.exterior})"
@@ -0,0 +1,26 @@
1
+ # SPDX-License-Identifier: GPL-3.0-or-later
2
+ # SPDX-FileCopyrightText: 2025 Yakau Bubnou
3
+ # SPDX-FileType: SOURCE
4
+
5
+ import torch
6
+
7
+ from torch_geopooling.transforms import TileWKT
8
+
9
+
10
+ def test_tile_wkt() -> None:
11
+ tile_wkt4 = TileWKT(exterior=(0.0, 0.0, 10.0, 10.0), internal=False)
12
+ tile_wkt5 = TileWKT(exterior=(0.0, 0.0, 10.0, 10.0), internal=True)
13
+
14
+ tiles = torch.tensor(
15
+ [
16
+ [0, 0, 0],
17
+ [1, 0, 0],
18
+ [1, 0, 1],
19
+ [1, 1, 0],
20
+ [1, 1, 1],
21
+ ],
22
+ dtype=torch.int32,
23
+ )
24
+
25
+ assert len(list(tile_wkt4(tiles))) == 4
26
+ assert len(list(tile_wkt5(tiles))) == 5