[Tilecache] OpenLayers + Tilecache + Mapserver - limiting the maxExtent

Adrian Popa adrian_gh.popa at romtelecom.ro
Thu Jul 15 09:45:40 EDT 2010


Hello everyone,

I've spent the better part of the day trying to get things to play 
nicely together, but I still failed miserably.
Here's what works now:

Mapserver:
EXTENT 20.26 43.16 29.70 48.46

OpenLayers:
var res = 
[2445.9849046875,1222.99245234375,611.496226171875,305.7481130859375,152.87405654296876,76.43702827148438,38.21851413574219,19.109257067871095,9.554628533935547,4.777314266967774,2.388657133483887,1.1943285667419434,0.597164283];
var lonlat = new OpenLayers.Projection("EPSG:4326");
var sphericalMercator = new OpenLayers.Projection("EPSG:900913");
function initStage1(){
     var options = { projection: sphericalMercator, displayProjection: 
lonlat, units: "m", numZoomLevels: 12, maxResolution: 2445.9849046875, 
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 
20037508.34, 20037508.34), resolutions: res };
...
var rtc_base = new OpenLayers.Layer.WMS('RTC TileCache', 
['http://server.romtelecom.ro/tilecache/tilecache.py'],{map: 
'/var/www/html/map/rtc_base.map',transparent: 'false',format: 
'image/png2', map_imagetype: 'agg', layers: 'rtc_base', maxExtent: new 
OpenLayers.Bounds(20.26, 43.16, 29.70, 48.46),maxResolution: 156543, 
units: 'm'}, {isBaseLayer:true, projection: sphericalMercator});

TileCache:
[rtc_base]
type=WMS
debug=no
url=http://server.romtelecom.ro/fcgi-bin/mapserv?map=/var/www/html/map/rtc_base.map
layers=Judete,RuralSate,Rural,Urban,highway,sechighway,streets,buildings,GranitaJudete 

bbox=20.26,43.16,29.70,48.46
spherical_mercator=true
levels=13
srs=EPSG:900913
size=256,256
extension=png
mime_type=image/png2
maxResolution=2445.9849046875
map_imagetype=agg

In this setup, everything works as expected - but the problem I have is 
that my Openlayers map has too permissive bounds - almost the whole world:
> >> new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 
20037508.34).transform(sphericalMercator, lonlat);
left-bottom=(-180,-85.051128779807) right-top=(180,85.051128779807)

I would like to restrict these bounds to overlap with my original bbox: 
20.26,43.16,29.70,48.46.

I've tried every trick in the book, but I still haven't managed. Here's 
what I did (and didn't work):
  - set the map (and all layers) maxExtent to the new value:
new OpenLayers.Bounds(20.26, 43.16, 29.70, 48.46).transform(lonlat, 
sphericalMercator);
left-bottom=(2255332.8831578,5336357.2739115) 
right-top=(3306188.8761,6183726.4985628)
  - calculate the new resolution array using this formula:
maxExtent = (3306188.8761 - 2255332.8831578)/512 = 1050855.9929422/512 = 
2052.453111215
subsequent resolutions are maxExtent/2, maxExtent/4, maxExtent/8...
so, res becomes: var res = 
[2052.453111215,1026.226555608,513.113277804,256.556638902,128.278319451,64.139159726,32.069579863,16.034789931,8.017394966,4.008697483,2.004348741,1.002174371,0.501087185];

My current config is now:
Mapserver:
EXTENT 20.26 43.16 29.70 48.46

OpenLayers:
var res = 
[2052.453111215,1026.226555608,513.113277804,256.556638902,128.278319451,64.139159726,32.069579863,16.034789931,8.017394966,4.008697483,2.004348741,1.002174371,0.501087185];
var maxRes = 2052.453111215;

var lonlat = new OpenLayers.Projection("EPSG:4326");
var sphericalMercator = new OpenLayers.Projection("EPSG:900913");

var romaniaBounds = new OpenLayers.Bounds();
romaniaBounds.extend(new OpenLayers.LonLat(20.26, 43.16));
romaniaBounds.extend(new OpenLayers.LonLat(29.70, 48.46));
romaniaBounds = romaniaBounds.transform(lonlat, sphericalMercator);

function initStage1(){
     var options = { projection: sphericalMercator, displayProjection: 
lonlat, units: "m", numZoomLevels: 13, maxResolution: maxRes, maxExtent: 
romaniaBounds, panMethod: null, restrictedExtent: romaniaBounds, 
resolutions: res};
...
var rtc_base = new OpenLayers.Layer.WMS('RTC TileCache', 
['http://server.romtelecom.ro/tilecache/tilecache.py'],{map: 
'/var/www/html/map/rtc_base.map',transparent: 'false',format: 
'image/png2', map_imagetype: 'agg', layers: 'rtc_base1', maxExtent: new 
OpenLayers.Bounds(20.26, 43.16, 29.70, 48.46),maxResolution: maxRes, 
units: 'm'}, {isBaseLayer:true, projection: sphericalMercator, 
resolutions: res});

TileCache:
[rtc_base1]
type=WMS
debug=yes
url=http://terra.romtelecom.ro/fcgi-bin/mapserv?map=/var/www/html/map/rtc_base.map
layers=Judete,RuralSate,Rural,Urban,highway,sechighway,streets,buildings,GranitaJudete
bbox=20.26,43.16,29.70,48.46
spherical_mercator=true
levels=13
srs=EPSG:900913
size=256,256
extension=png
mime_type=image/png2
maxResolution=2052.453111215
map_imagetype=agg
resolutions=2052.453111215,1026.226555608,513.113277804,256.556638902,128.278319451,64.139159726,32.069579863,16.034789931,8.017394966,4.008697483,2.004348741,1.002174371,0.501087185
extent_type=loose

With these settings, my request looks like this: 
http://server.romtelecom.ro/tilecache/tilecache.py?MAP=%2Fvar%2Fwww%2Fhtml%2Fmap%2Frtc_base.map&TRANSPARENT=false&FORMAT=image%2Fpng2&MAP_IMAGETYPE=agg&LAYERS=rtc_base1&MAXEXTENT=left-bottom%3D(20.26%2C43.16)%20right-top%3D(29.7%2C48.46)&MAXRESOLUTION=2052.453111215&UNITS=m&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A900913&BBOX=2255332.8831578,5861785.2703828,2518046.8813934,6124499.2686184&WIDTH=256&HEIGHT=256

... and the output is:

An error occurred: Current x value 2255332.883158 is too far from tile corner x 2293181.510030
   File "/usr/lib/python2.4/site-packages/TileCache-2.10-py2.4.egg/TileCache/Service.py", line 256, in modPythonHandler
     host )
   File "/usr/lib/python2.4/site-packages/TileCache-2.10-py2.4.egg/TileCache/Service.py", line 181, in dispatchRequest
     tile = WMS(self).parse(params, path_info, host)
   File "/usr/lib/python2.4/site-packages/TileCache-2.10-py2.4.egg/TileCache/Services/WMS.py", line 19, in parse
     return self.getMap(param)
   File "/usr/lib/python2.4/site-packages/TileCache-2.10-py2.4.egg/TileCache/Services/WMS.py", line 27, in getMap
     tile  = self.getLayer(name).getTile(bbox)
   File "/usr/lib/python2.4/site-packages/TileCache-2.10-py2.4.egg/TileCache/Layer.py", line 311, in getTile
     coord = self.getCell(bbox)
   File "/usr/lib/python2.4/site-packages/TileCache-2.10-py2.4.egg/TileCache/Layer.py", line 286, in getCell
     raise TileCacheException("Current x value %f is too far from tile corner x %f" % (minx, tilex))


My guess is the problem appears because I mix lonlat coordinates with 
sphericalMercator coordinates, but it seems I do the same thing in the 
first case, and there I don't have any issues.

By the way, I've tried it also the other way around - using bounds in 
WGS84 format and recalculating everything, but I get the same general 
result.

Can anyone show me what I'm doing wrong or what else I can try to debug 
this problem?
Thank you for your time,
Adrian

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/tilecache/attachments/20100715/7a929ab8/attachment.html


More information about the Tilecache mailing list