Skip to content

Commit

Permalink
add exact intersection check (#49)
Browse files Browse the repository at this point in the history
* add flag to make exact intersection check when selecting tiles from geometry

* also test batch option

* bump version; update changelog
  • Loading branch information
ungarj committed Mar 15, 2022
1 parent f5797cf commit 881e36b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,6 +2,11 @@
Changelog
#########

2022.3.0 - 2022-03-15
---------------------
* add option to exactly get intersection tiles using `TilePyramid.tiles_from_geom(exact=True)`


2021.11.0 - 2021-11-12
----------------------
* enable yielding tiles in batches by either row or column for following methods:
Expand Down
12 changes: 12 additions & 0 deletions test/conftest.py
Expand Up @@ -285,3 +285,15 @@ def multipolygon():
],
}
)


@pytest.fixture
def tile_bounds_polygon():
return shape(
{
"type": "Polygon",
"coordinates": [
[(0, 0), (0, 45), (22.5, 45), (22.5, 22.5), (45, 22.5), (45, 0), (0, 0)]
],
}
)
24 changes: 23 additions & 1 deletion test/test_tilepyramid.py
@@ -1,7 +1,7 @@
"""TilePyramid creation."""

import pytest
from shapely.geometry import Point
from shapely.geometry import Point, box
from shapely.ops import unary_union
from types import GeneratorType

Expand Down Expand Up @@ -323,6 +323,28 @@ def test_tiles_from_bounds_batch_by_row_both_antimeridian_bounds():
assert tiles == len(list(tp.tiles_from_bounds(bounds, zoom)))


def test_tiles_from_geom_exact(tile_bounds_polygon):

tp = TilePyramid("geodetic")
zoom = 3

tiles = len(list(tp.tiles_from_geom(tile_bounds_polygon, zoom)))
assert tiles == 4
tiles = 0
for batch in tp.tiles_from_geom(tile_bounds_polygon, zoom, batch_by="row"):
tiles += len(list(batch))
assert tiles == 4

exact_tiles = len(list(tp.tiles_from_geom(tile_bounds_polygon, zoom, exact=True)))
assert exact_tiles == 3
tiles = 0
for batch in tp.tiles_from_geom(
tile_bounds_polygon, zoom, batch_by="row", exact=True
):
tiles += len(list(batch))
assert tiles == 3


def test_snap_bounds():
bounds = (0, 1, 2, 3)
tp = TilePyramid("geodetic")
Expand Down
2 changes: 1 addition & 1 deletion tilematrix/__init__.py
Expand Up @@ -21,4 +21,4 @@
]


__version__ = "2021.11.0"
__version__ = "2022.3.0"
3 changes: 2 additions & 1 deletion tilematrix/_funcs.py
@@ -1,6 +1,7 @@
"""Helper functions."""

from itertools import product, chain
from itertools import product

from rasterio.crs import CRS
from shapely.affinity import translate
from shapely.geometry import Polygon, GeometryCollection, box
Expand Down
43 changes: 31 additions & 12 deletions tilematrix/_tilepyramid.py
Expand Up @@ -185,6 +185,7 @@ def tiles_from_bounds(self, bounds=None, zoom=None, batch_by=None):
- bounds: tuple of (left, bottom, right, top) bounding values in tile
pyramid CRS
- zoom: zoom level
- batch_by: yield tiles in row or column batches if activated
"""
validate_zoom(zoom)
if not isinstance(bounds, tuple) or len(bounds) != 4:
Expand All @@ -208,7 +209,7 @@ def tiles_from_bbox(self, geometry=None, zoom=None, batch_by=None):
validate_zoom(zoom)
yield from self.tiles_from_bounds(geometry.bounds, zoom, batch_by=batch_by)

def tiles_from_geom(self, geometry=None, zoom=None, batch_by=None):
def tiles_from_geom(self, geometry=None, zoom=None, batch_by=None, exact=False):
"""
Return all tiles intersecting with input geometry.
Expand All @@ -235,18 +236,36 @@ def tiles_from_geom(self, geometry=None, zoom=None, batch_by=None):
"MultiPolygon",
"GeometryCollection",
):
prepared_geometry = prep(clip_geometry_to_srs_bounds(geometry, self))
if batch_by:
for batch in self.tiles_from_bbox(geometry, zoom, batch_by=batch_by):
yield (
tile
for tile in batch
if prepared_geometry.intersects(tile.bbox())
)
if exact:
geometry = clip_geometry_to_srs_bounds(geometry, self)
if batch_by:
for batch in self.tiles_from_bbox(
geometry, zoom, batch_by=batch_by
):
yield (
tile
for tile in batch
if geometry.intersection(tile.bbox()).area
)
else:
for tile in self.tiles_from_bbox(geometry, zoom, batch_by=batch_by):
if geometry.intersection(tile.bbox()).area:
yield tile
else:
for tile in self.tiles_from_bbox(geometry, zoom, batch_by=batch_by):
if prepared_geometry.intersects(tile.bbox()):
yield tile
prepared_geometry = prep(clip_geometry_to_srs_bounds(geometry, self))
if batch_by:
for batch in self.tiles_from_bbox(
geometry, zoom, batch_by=batch_by
):
yield (
tile
for tile in batch
if prepared_geometry.intersects(tile.bbox())
)
else:
for tile in self.tiles_from_bbox(geometry, zoom, batch_by=batch_by):
if prepared_geometry.intersects(tile.bbox()):
yield tile

def tile_from_xy(self, x=None, y=None, zoom=None, on_edge_use="rb"):
"""
Expand Down

0 comments on commit 881e36b

Please sign in to comment.