244930866ab07a46c834304eb3b3c40b2050ec30
[rails.git] / public / openlayers / OpenStreetMap.js
1 /**
2  * Namespace: Util.OSM
3  */
4 OpenLayers.Util.OSM = {};
5
6 /**
7  * Constant: MISSING_TILE_URL
8  * {String} URL of image to display for missing tiles
9  */
10 OpenLayers.Util.OSM.MISSING_TILE_URL = "http://openstreetmap.org/openlayers/img/404.png";
11
12 /**
13  * Property: originalOnImageLoadError
14  * {Function} Original onImageLoadError function.
15  */
16 OpenLayers.Util.OSM.originalOnImageLoadError = OpenLayers.Util.onImageLoadError;
17
18 /**
19  * Function: onImageLoadError
20  */
21 OpenLayers.Util.onImageLoadError = function() {
22     if (this.src.match(/^http:\/\/[abc]\.[a-z]+\.openstreetmap\.org\//)) {
23         this.src = OpenLayers.Util.OSM.MISSING_TILE_URL;
24     } else if (this.src.match(/^http:\/\/[def]\.tah\.openstreetmap\.org\//)) {
25         // do nothing - this layer is transparent
26     } else {
27         OpenLayers.Util.OSM.originalOnImageLoadError;
28     }
29 };
30
31 /**
32  * @requires OpenLayers/Layer/TMS.js
33  *
34  * Class: OpenLayers.Layer.OSM
35  *
36  * Inherits from:
37  *  - <OpenLayers.Layer.TMS>
38  */
39 OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.TMS, {
40     /**
41      * Constructor: OpenLayers.Layer.OSM
42      *
43      * Parameters:
44      * name - {String}
45      * url - {String}
46      * options - {Object} Hashtable of extra options to tag onto the layer
47      */
48     initialize: function(name, url, options) {
49         options = OpenLayers.Util.extend({
50             attribution: "Data by <a href='http://openstreetmap.org/'>OpenStreetMap</a>",
51             maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
52             maxResolution: 156543.0339,
53             units: "m",
54             projection: "EPSG:900913",
55             transitionEffect: "resize"
56         }, options);
57         var newArguments = [name, url, options];
58         OpenLayers.Layer.TMS.prototype.initialize.apply(this, newArguments);
59     },
60
61     /**
62      * Method: getUrl
63      *
64      * Parameters:
65      * bounds - {<OpenLayers.Bounds>}
66      *
67      * Returns:
68      * {String} A string with the layer's url and parameters and also the
69      *          passed-in bounds and appropriate tile size specified as
70      *          parameters
71      */
72     getURL: function (bounds) {
73         var res = this.map.getResolution();
74         var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
75         var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
76         var z = this.map.getZoom();
77         var limit = Math.pow(2, z);
78
79         if (y < 0 || y >= limit)
80         {
81             return OpenLayers.Util.OSM.MISSING_TILE_URL;
82         }
83         else
84         {
85             x = ((x % limit) + limit) % limit;
86
87             var url = this.url;
88             var path = z + "/" + x + "/" + y + ".png";
89
90             if (url instanceof Array)
91             {
92                 url = this.selectUrl(path, url);
93             }
94
95             return url + path;
96         }
97     },
98
99     CLASS_NAME: "OpenLayers.Layer.OSM"
100 });
101
102 /**
103  * Class: OpenLayers.Layer.OSM.Mapnik
104  *
105  * Inherits from:
106  *  - <OpenLayers.Layer.OSM>
107  */
108 OpenLayers.Layer.OSM.Mapnik = OpenLayers.Class(OpenLayers.Layer.OSM, {
109     /**
110      * Constructor: OpenLayers.Layer.OSM.Mapnik
111      *
112      * Parameters:
113      * name - {String}
114      * options - {Object} Hashtable of extra options to tag onto the layer
115      */
116     initialize: function(name, options) {
117         var url = [
118             "http://a.tile.openstreetmap.org/",
119             "http://b.tile.openstreetmap.org/",
120             "http://c.tile.openstreetmap.org/"
121         ];
122         options = OpenLayers.Util.extend({ numZoomLevels: 19 }, options);
123         var newArguments = [name, url, options];
124         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
125     },
126
127     CLASS_NAME: "OpenLayers.Layer.OSM.Mapnik"
128 });
129
130 /**
131  * Class: OpenLayers.Layer.OSM.Osmarender
132  *
133  * Inherits from:
134  *  - <OpenLayers.Layer.OSM>
135  */
136 OpenLayers.Layer.OSM.Osmarender = OpenLayers.Class(OpenLayers.Layer.OSM, {
137     /**
138      * Constructor: OpenLayers.Layer.OSM.Osmarender
139      *
140      * Parameters:
141      * name - {String}
142      * options - {Object} Hashtable of extra options to tag onto the layer
143      */
144     initialize: function(name, options) {
145         var url = [
146             "http://a.tah.openstreetmap.org/Tiles/tile.php/",
147             "http://b.tah.openstreetmap.org/Tiles/tile.php/",
148             "http://c.tah.openstreetmap.org/Tiles/tile.php/"
149         ];
150         options = OpenLayers.Util.extend({ numZoomLevels: 18 }, options);
151         var newArguments = [name, url, options];
152         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
153     },
154
155     CLASS_NAME: "OpenLayers.Layer.OSM.Osmarender"
156 });
157
158 /**
159  * Class: OpenLayers.Layer.OSM.Maplint
160  *
161  * Inherits from:
162  *  - <OpenLayers.Layer.OSM>
163  */
164 OpenLayers.Layer.OSM.Maplint = OpenLayers.Class(OpenLayers.Layer.OSM, {
165     /**
166      * Constructor: OpenLayers.Layer.OSM.Maplint
167      *
168      * Parameters:
169      * name - {String}
170      * options - {Object} Hashtable of extra options to tag onto the layer
171      */
172     initialize: function(name, options) {
173         var url = [
174             "http://d.tah.openstreetmap.org/Tiles/maplint.php/",
175             "http://e.tah.openstreetmap.org/Tiles/maplint.php/",
176             "http://f.tah.openstreetmap.org/Tiles/maplint.php/"
177         ];
178         options = OpenLayers.Util.extend({ numZoomLevels: 18, isBaseLayer: false, visibility: false }, options);
179         var newArguments = [name, url, options];
180         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
181     },
182
183     CLASS_NAME: "OpenLayers.Layer.OSM.Maplint"
184 });
185
186 OpenLayers.Layer.Data = OpenLayers.Class(OpenLayers.Layer, { 
187     setVisibility: function(vis) {
188         OpenLayers.Layer.prototype.setVisibility.apply(this, arguments);
189         if (!this.map) { return; }
190         if (vis) {
191             new Ajax.Request('/browse/start', {asynchronous:true, evalScripts:true}); 
192         } else {
193             if (this.stopBrowse) {
194                 this.stopBrowse();
195                 closeSidebar(); 
196             }    
197         }    
198     }
199 });