Skip to content

Commit

Permalink
Merge 294a7e4 into bb718c5
Browse files Browse the repository at this point in the history
  • Loading branch information
ungarj committed Mar 26, 2019
2 parents bb718c5 + 294a7e4 commit cbbb804
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
12 changes: 12 additions & 0 deletions test/test_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import math
import pytest
from shapely.geometry import box
from tilematrix import TilePyramid, GridDefinition, PYRAMID_PARAMS


Expand Down Expand Up @@ -76,3 +77,14 @@ def test_irregular_grids(grid_definition_irregular):
assert tile.pixel_x_size == tile.pixel_y_size
assert tile.pixel_x_size == 10.
assert tile.shape(10) != tile.shape()


def test_tiles_from_bounds(grid_definition_irregular):
bounds = (755336.179, 300068.615, 791558.022, 319499.955)
bbox = box(*bounds)
tp = TilePyramid(grid_definition_irregular, metatiling=4)
tiles_bounds = list(tp.tiles_from_bounds(bounds, 0))
tiles_bbox = list(tp.tiles_from_bbox(bbox, 0))
tiles_geom = list(tp.tiles_from_geom(bbox, 0))

assert set(tiles_bounds) == set(tiles_bbox) == set(tiles_geom)
7 changes: 4 additions & 3 deletions tilematrix/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,17 @@ def _tiles_from_cleaned_bounds(tp, bounds, zoom):

def _tile_from_xy(tp, x, y, zoom, on_edge_use="rb"):
# determine row
tile_y_size = round(tp.y_size / tp.matrix_height(zoom), ROUND)
tile_y_size = round(tp.pixel_y_size(zoom) * tp.tile_size * tp.metatiling, ROUND)
row = int((tp.top - y) / tile_y_size)
if on_edge_use in ["rt", "lt"] and (tp.top - y) % tile_y_size == 0.:
row -= 1

# determine column
tile_x_size = round(tp.x_size / tp.matrix_width(zoom), ROUND)
tile_x_size = round(tp.pixel_x_size(zoom) * tp.tile_size * tp.metatiling, ROUND)
col = int((x - tp.left) / tile_x_size)
if on_edge_use in ["lb", "lt"] and (x - tp.left) % tile_x_size == 0.:
col -= 1

# handle Antimeridian wrapping
if tp.is_global:
# left side
Expand All @@ -234,5 +235,5 @@ def _tile_from_xy(tp, x, y, zoom, on_edge_use="rb"):
return tp.tile(zoom, row, col)
except ValueError as e:
raise ValueError(
"on_edge_use '%s' results in an invalid tile here: %s" % (on_edge_use, e)
"on_edge_use '%s' results in an invalid tile: %s" % (on_edge_use, e)
)
20 changes: 12 additions & 8 deletions tilematrix/_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def __init__(self, tile_pyramid, zoom, row, col):
self.zoom = zoom
self.row = row
self.col = col
if not self.is_valid():
raise ValueError(
"invalid tile index given: %s %s %s" % (zoom, row, col))
self.is_valid()
self.index = TileIndex(zoom, row, col)
self.id = TileIndex(zoom, row, col)
self.pixel_x_size = self.tile_pyramid.pixel_x_size(self.zoom)
Expand Down Expand Up @@ -146,16 +144,22 @@ def shape(self, pixelbuffer=0):

def is_valid(self):
"""Return True if tile is available in tile pyramid."""
return all([
if not all([
isinstance(self.zoom, int),
self.zoom >= 0,
isinstance(self.row, int),
self.row >= 0,
isinstance(self.col, int),
self.col >= 0,
self.col < self.tile_pyramid.matrix_width(self.zoom),
self.row < self.tile_pyramid.matrix_height(self.zoom)
])
self.col >= 0
]):
raise ValueError("zoom, col and row must be integers >= 0")
cols = self.tile_pyramid.matrix_width(self.zoom)
rows = self.tile_pyramid.matrix_height(self.zoom)
if self.col >= cols:
raise ValueError("col (%s) exceeds matrix width (%s)" % (self.col, cols))
if self.row >= rows:
raise ValueError("row (%s) exceeds matrix height (%s)" % (self.row, rows))
return True

def get_parent(self):
"""Return tile from previous zoom level."""
Expand Down
3 changes: 1 addition & 2 deletions tilematrix/_tilepyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ def tiles_from_geom(self, geometry, zoom):
"LineString", "MultiLineString", "Polygon", "MultiPolygon",
"GeometryCollection"
):
prepared_geometry = prep(
clip_geometry_to_srs_bounds(geometry, self))
prepared_geometry = prep(clip_geometry_to_srs_bounds(geometry, self))
for tile in self.tiles_from_bbox(geometry, zoom):
if prepared_geometry.intersects(tile.bbox()):
yield tile
Expand Down

0 comments on commit cbbb804

Please sign in to comment.