Skip to content

Commit

Permalink
Merge branch 'expire-tiles'
Browse files Browse the repository at this point in the history
  • Loading branch information
olt committed Feb 16, 2017
2 parents 07552da + 5764817 commit 4f7372c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
10 changes: 10 additions & 0 deletions doc/coverages.rst
Expand Up @@ -75,6 +75,16 @@ Any polygon datasource that is supported by OGR (e.g. Shapefile, GeoJSON, PostGI
option is unset, the first layer from the datasource will be used.


Expire tiles
""""""""""""

Text file with webmercator tile coordinates. The tiles should be in ``z/x/y`` format (e.g. ``14/1283/6201``),
with one tile coordinate per line. Only tiles in the webmercator grid are supported (origin is always `nw`).

``expire_tiles``:
File or directory with expire tile files. Directories are loaded recursive.


Examples
--------

Expand Down
7 changes: 7 additions & 0 deletions mapproxy/config/coverage.py
Expand Up @@ -21,6 +21,7 @@
load_datasource,
load_ogr_datasource,
load_polygons,
load_expire_tiles,
require_geom_support,
build_multipolygon,
)
Expand Down Expand Up @@ -70,6 +71,12 @@ def load_coverage(conf, base_path=None):
where = conf.get('where', None)
geom = load_datasource(datasource, where)
bbox, geom = build_multipolygon(geom, simplify=True)
elif 'expire_tiles' in conf:
require_geom_support()
filename = abspath(conf['expire_tiles'])
geom = load_expire_tiles(filename)
_, geom = build_multipolygon(geom, simplify=False)
return coverage(geom, SRS(3857))
else:
return None
return coverage(geom or bbox, SRS(srs))
1 change: 1 addition & 0 deletions mapproxy/config/spec.py
Expand Up @@ -47,6 +47,7 @@ def validate_options(conf_dict):
'datasource': one_of(str(), [number()]),
'where': str(),
'srs': str(),
'expire_tiles': str(),
}
image_opts = {
'mode': str(),
Expand Down
28 changes: 28 additions & 0 deletions mapproxy/test/unit/test_geom.py
Expand Up @@ -16,11 +16,14 @@
from __future__ import division, with_statement

import os
import tempfile
import shutil

from mapproxy.srs import SRS, bbox_equals
from mapproxy.util.geom import (
load_polygons,
load_datasource,
load_expire_tiles,
transform_geometry,
geom_support,
bbox_polygon,
Expand Down Expand Up @@ -364,3 +367,28 @@ def test_wkt(self):

geoms = load_datasource(fname)
eq_(len(geoms), 2)

def test_expire_tiles_dir(self):
dirname = tempfile.mkdtemp()
try:
fname = os.path.join(dirname, 'tiles')
with open(fname, 'wb') as f:
f.write(b"4/2/5\n")
f.write(b"4/2/6\n")
f.write(b"4/4/3\n")

geoms = load_expire_tiles(dirname)
eq_(len(geoms), 3)
finally:
shutil.rmtree(dirname)

def test_expire_tiles_file(self):
with TempFile() as fname:
with open(fname, 'wb') as f:
f.write(b"4/2/5\n")
f.write(b"4/2/6\n")
f.write(b"error\n")
f.write(b"4/2/1\n") # rest of file is ignored

geoms = load_expire_tiles(fname)
eq_(len(geoms), 2)
34 changes: 33 additions & 1 deletion mapproxy/util/geom.py
Expand Up @@ -16,10 +16,13 @@
from __future__ import division, with_statement

import os
import json
import re
import codecs
from functools import partial
from contextlib import closing

from mapproxy.grid import tile_grid
from mapproxy.compat import string_type

import logging
Expand Down Expand Up @@ -216,4 +219,33 @@ def flatten_to_polygons(geometry):

return []


def load_expire_tiles(expire_dir, grid=None):
if grid is None:
grid = tile_grid(3857, origin='nw')
tiles = set()

def parse(filename):
with open(filename) as f:
try:
for line in f:
if not line:
continue
tile = tuple(map(int, line.split('/')))
tiles.add(tile)
except:
log_config.warn('found error in %s, skipping rest of file', filename)

if os.path.isdir(expire_dir):
for root, dirs, files in os.walk(expire_dir):
for name in files:
filename = os.path.join(root, name)
parse(filename)
else:
parse(expire_dir)

boxes = []
for tile in tiles:
z, x, y = tile
boxes.append(shapely.geometry.box(*grid.tile_bbox((x, y, z))))

return boxes

0 comments on commit 4f7372c

Please sign in to comment.