]> git.openstreetmap.org Git - rails.git/blob - public/openlayers/OpenStreetMap.js
Edgemaster's patch to colour routes depending on network
[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,-20037508,20037508,20037508),
52             maxResolution: 156543,
53             units: "m",
54             projection: "EPSG:900913"
55         }, options);
56         var newArguments = [name, url, options];
57         OpenLayers.Layer.TMS.prototype.initialize.apply(this, newArguments);
58     },
59
60     /**
61      * Method: getUrl
62      *
63      * Parameters:
64      * bounds - {<OpenLayers.Bounds>}
65      *
66      * Returns:
67      * {String} A string with the layer's url and parameters and also the
68      *          passed-in bounds and appropriate tile size specified as
69      *          parameters
70      */
71     getURL: function (bounds) {
72         var res = this.map.getResolution();
73         var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
74         var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
75         var z = this.map.getZoom();
76         var limit = Math.pow(2, z);
77
78         if (y < 0 || y >= limit)
79         {
80             return OpenLayers.Util.OSM.MISSING_TILE_URL;
81         }
82         else
83         {
84             x = ((x % limit) + limit) % limit;
85
86             var url = this.url;
87             var path = z + "/" + x + "/" + y + ".png";
88
89             if (url instanceof Array)
90             {
91                 url = this.selectUrl(path, url);
92             }
93
94             return url + path;
95         }
96     },
97
98     CLASS_NAME: "OpenLayers.Layer.OSM"
99 });
100
101 /**
102  * Class: OpenLayers.Layer.OSM.Mapnik
103  *
104  * Inherits from:
105  *  - <OpenLayers.Layer.OSM>
106  */
107 OpenLayers.Layer.OSM.Mapnik = OpenLayers.Class(OpenLayers.Layer.OSM, {
108     /**
109      * Constructor: OpenLayers.Layer.OSM.Mapnik
110      *
111      * Parameters:
112      * name - {String}
113      * options - {Object} Hashtable of extra options to tag onto the layer
114      */
115     initialize: function(name, options) {
116         var url = [
117             "http://a.tile.openstreetmap.org/",
118             "http://b.tile.openstreetmap.org/",
119             "http://c.tile.openstreetmap.org/"
120         ];
121         options = OpenLayers.Util.extend({ numZoomLevels: 19 }, options);
122         var newArguments = [name, url, options];
123         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
124     },
125
126     CLASS_NAME: "OpenLayers.Layer.OSM.Mapnik"
127 });
128
129 /**
130  * Class: OpenLayers.Layer.OSM.Osmarender
131  *
132  * Inherits from:
133  *  - <OpenLayers.Layer.OSM>
134  */
135 OpenLayers.Layer.OSM.Osmarender = OpenLayers.Class(OpenLayers.Layer.OSM, {
136     /**
137      * Constructor: OpenLayers.Layer.OSM.Osmarender
138      *
139      * Parameters:
140      * name - {String}
141      * options - {Object} Hashtable of extra options to tag onto the layer
142      */
143     initialize: function(name, options) {
144         var url = [
145             "http://a.tah.openstreetmap.org/Tiles/tile.php/",
146             "http://b.tah.openstreetmap.org/Tiles/tile.php/",
147             "http://c.tah.openstreetmap.org/Tiles/tile.php/"
148         ];
149         options = OpenLayers.Util.extend({ numZoomLevels: 18 }, options);
150         var newArguments = [name, url, options];
151         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
152     },
153
154     CLASS_NAME: "OpenLayers.Layer.OSM.Osmarender"
155 });
156
157 /**
158  * Class: OpenLayers.Layer.OSM.Maplint
159  *
160  * Inherits from:
161  *  - <OpenLayers.Layer.OSM>
162  */
163 OpenLayers.Layer.OSM.Maplint = OpenLayers.Class(OpenLayers.Layer.OSM, {
164     /**
165      * Constructor: OpenLayers.Layer.OSM.Maplint
166      *
167      * Parameters:
168      * name - {String}
169      * options - {Object} Hashtable of extra options to tag onto the layer
170      */
171     initialize: function(name, options) {
172         var url = [
173             "http://d.tah.openstreetmap.org/Tiles/maplint.php/",
174             "http://e.tah.openstreetmap.org/Tiles/maplint.php/",
175             "http://f.tah.openstreetmap.org/Tiles/maplint.php/"
176         ];
177         options = OpenLayers.Util.extend({ numZoomLevels: 18, isBaseLayer: false, visibility: false }, options);
178         var newArguments = [name, url, options];
179         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
180     },
181
182     CLASS_NAME: "OpenLayers.Layer.OSM.Maplint"
183 });