Skip to content

Commit

Permalink
Added clipped=padded configuration option to Vector provider for bett…
Browse files Browse the repository at this point in the history
…er polygon display in Polymaps
  • Loading branch information
Michal Migurski committed Jul 23, 2011
1 parent fcfcf4c commit 16916e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
7 changes: 4 additions & 3 deletions API.html
Expand Up @@ -1000,9 +1000,10 @@ <h4><a id="vector-provider" name="vector-provider">Vector</a></h4>
Default is <samp>true</samp>.
<br>
Boolean flag for optionally clipping the output geometries to the
bounds of the enclosing tile. This results in incomplete geometries,
dramatically smaller file sizes, and improves performance and
compatibility with Polymaps (http://polymaps.org).
bounds of the enclosing tile, or the string value <samp>"padded"</samp>
for clipping to the bounds of the tile plus 5%. This results in incomplete
geometries, dramatically smaller file sizes, and improves performance and
compatibility with <a href="http://polymaps.org">Polymaps</a>.
</dd>
<dt>projected</dt>
<dd>
Expand Down
6 changes: 5 additions & 1 deletion TileStache/Config.py
Expand Up @@ -286,8 +286,12 @@ def _parseConfigfileLayer(layer_dict, config, dirpath):
provider_kwargs['parameters'] = provider_dict['parameters']
provider_kwargs['properties'] = provider_dict.get('properties', None)
provider_kwargs['projected'] = bool(provider_dict.get('projected', False))
provider_kwargs['clipped'] = bool(provider_dict.get('clipped', True))
provider_kwargs['verbose'] = bool(provider_dict.get('verbose', False))

if provider_dict.get('clipped', None) == 'padded':
provider_kwargs['clipped'] = 'padded'
else:
provider_kwargs['clipped'] = bool(provider_dict.get('clipped', True))

elif _class is Providers.MBTiles.Provider:
provider_kwargs['tileset'] = provider_dict['tileset']
Expand Down
21 changes: 14 additions & 7 deletions TileStache/Vector/__init__.py
Expand Up @@ -64,7 +64,8 @@
clipped:
Default is true.
Boolean flag for optionally clipping the output geometries to the
bounds of the enclosing tile. This results in incomplete geometries,
bounds of the enclosing tile, or the string value "padded" for clipping
to the bounds of the tile plus 5%. This results in incomplete geometries,
dramatically smaller file sizes, and improves performance and
compatibility with Polymaps (http://polymaps.org).
Expand Down Expand Up @@ -234,15 +235,21 @@ def _sref_4326():

return sref

def _tile_perimeter(coord, projection):
def _tile_perimeter(coord, projection, padded):
""" Get a tile's outer edge for a coordinate and a projection.
Returns a list of 17 (x, y) coordinates corresponding to a clockwise
circumambulation of a tile boundary in a given projection. Projection
is like those found in TileStache.Geography, used for tile output.
If padded argument is True, pad bbox by 5% on all sides.
"""
ul = projection.coordinateProj(coord)
lr = projection.coordinateProj(coord.right().down())
if padded:
ul = projection.coordinateProj(coord.left(0.05).up(0.05))
lr = projection.coordinateProj(coord.down(1.05).right(1.05))
else:
ul = projection.coordinateProj(coord)
lr = projection.coordinateProj(coord.right().down())

xmin, ymin, xmax, ymax = ul.x, ul.y, lr.x, lr.y
xspan, yspan = xmax - xmin, ymax - ymin
Expand All @@ -269,12 +276,12 @@ def _tile_perimeter(coord, projection):

return perimeter

def _tile_perimeter_geom(coord, projection):
def _tile_perimeter_geom(coord, projection, padded):
""" Get an OGR Geometry object for a coordinate tile polygon.
Uses _tile_perimeter().
"""
perimeter = _tile_perimeter(coord, projection)
perimeter = _tile_perimeter(coord, projection, padded)
wkt = 'POLYGON((%s))' % ', '.join(['%.3f %.3f' % xy for xy in perimeter])
geom = ogr.CreateGeometryFromWkt(wkt)

Expand Down Expand Up @@ -419,7 +426,7 @@ def _get_features(coord, properties, projection, layer, clipped, projected):
#
# Spatially filter the layer
#
bbox = _tile_perimeter_geom(coord, projection)
bbox = _tile_perimeter_geom(coord, projection, clipped == 'padded')
bbox.TransformTo(layer_sref)
layer.SetSpatialFilter(bbox)

Expand Down

0 comments on commit 16916e2

Please sign in to comment.