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