Skip to content

Commit

Permalink
Merge pull request #180 from trailbehind/feature/wms-provider-projection
Browse files Browse the repository at this point in the history
Add "source projection" parameter to url template provider to support WMS servers that want WGS84 coordinates
  • Loading branch information
rburhum committed May 20, 2014
2 parents d2b82c8 + e9b725f commit 63b9dfb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions API.html
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,10 @@ <h4><a id="url-template-provider" name="url-template-provider">URL Template</a>
Some WMS servers use the Referer request header to authenticate requests;
this parameter provides one.
</dd>
<dt>source projection</dt>
Names a geographic projection, explained in <a href="#projections">Projections</a>, that
coordinates should be transformed to for requests.
</dd>
</dl>

<p>
Expand Down
20 changes: 19 additions & 1 deletion TileStache/Providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,22 @@ class UrlTemplate:
- referer (optional)
String to use in the "Referer" header when making HTTP requests.
- source projection (optional)
Projection to transform coordinates into before making request
More on string substitutions:
- http://docs.python.org/library/string.html#template-strings
"""

def __init__(self, layer, template, referer=None):
def __init__(self, layer, template, referer=None, source_projection=None):
""" Initialize a UrlTemplate provider with layer and template string.
http://docs.python.org/library/string.html#template-strings
"""
self.layer = layer
self.template = Template(template)
self.referer = referer
self.source_projection = source_projection

@staticmethod
def prepareKeywordArgs(config_dict):
Expand All @@ -300,13 +304,27 @@ def prepareKeywordArgs(config_dict):
if 'referer' in config_dict:
kwargs['referer'] = config_dict['referer']

if 'source projection' in config_dict:
kwargs['source_projection'] = Geography.getProjectionByName(config_dict['source projection'])

return kwargs

def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
""" Return an image for an area.
Each argument (width, height, etc.) is substituted into the template.
"""
if self.source_projection is not None:
ne_location = self.layer.projection.projLocation(Point(xmax, ymax))
ne_point = self.source_projection.locationProj(ne_location)
ymax = ne_point.y
xmax = ne_point.x
sw_location = self.layer.projection.projLocation(Point(xmin, ymin))
sw_point = self.source_projection.locationProj(sw_location)
ymin = sw_point.y
xmin = sw_point.x
srs = self.source_projection.srs

mapping = {'width': width, 'height': height, 'srs': srs, 'zoom': zoom}
mapping.update({'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax})

Expand Down

0 comments on commit 63b9dfb

Please sign in to comment.