1 page.replace_html :sidebar_title, 'Browse'
2 page.replace_html :sidebar_content, :partial => 'start'
5 var gml, sf, objList, currentFeature, featureList;
6 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
7 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
10 openSidebar({ onclose: stopBrowse });
11 var vectors = new OpenLayers.Layer.Vector();
13 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
19 callbacks: { done: endDrag }
23 map.events.register("moveend", map, validateLinks);
24 map.events.triggerEvent("moveend");
27 function stopBrowse() {
37 currentFeature.destroy();
38 currentFeature = null;
40 map.events.unregister("moveend", map, validateLinks);
43 function startDrag() {
44 $("drag_box").innerHTML='Drag a box on the map to select an area';
48 $("drag_box").onclick = startDrag;
51 var bounds = map.getExtent();
56 $("use_map").onclick = useMap;
58 function endDrag(bbox) {
59 var bounds = bbox.getBounds();
63 $("drag_box").innerHTML = "Manually select a different area";
66 function displayFeatureWarning() {
67 var div = document.createElement("div");
68 var p = document.createElement("p");
69 p.appendChild(document.createTextNode("You have loaded an area which contains " + featureList.length + " features. In general, some browsers may not cope well with displaying this quantity of data. Generally, browsers work best at displaying less than 100 features at a time: doing anything else may make your browser slow/unresponsive. If you are sure you want to display this data, you may do so by clicking the button below."));
71 var input = document.createElement("input");
72 input.type = "submit";
73 input.value = "Load Data";
74 input.onclick = loadFeatureList;
75 div.appendChild(input);
76 $("object").innerHTML="";
77 $("object").appendChild(div);
80 function loadFeatureList() {
81 gml.addFeatures(featureList);
82 gml.events.triggerEvent("loadend");
86 function customDataLoader(request) {
87 var doc = request.responseXML;
89 if (!doc || !doc.documentElement) {
90 doc = request.responseText;
95 OpenLayers.Util.extend(options, this.formatOptions);
96 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
97 options.externalProjection = this.projection;
98 options.internalProjection = this.map.getProjectionObject();
101 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
102 var features = gml.read(doc);
103 if (!this.maxFeatures || features.length <= this.maxFeatures) {
104 this.addFeatures(features);
105 this.events.triggerEvent("loadend");
108 featureList = features;
109 displayFeatureWarning();
113 function getData(bounds) {
115 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
116 var size = bounds.getWidth() * bounds.getHeight();
118 $("status").innerHTML = "Unable to load: Bounding box size of " + size + " is too large. (Must be smaller than 0.25)<br />";
122 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
124 $("status").innerHTML = "Loading...";
126 var def = OpenLayers.Feature.Vector.style['default'];
127 var style = new OpenLayers.Style();
128 style.addRules([new OpenLayers.Rule(
130 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
131 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
132 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
135 gml = new OpenLayers.Layer.GML("Data",url,
136 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
137 maxFeatures: 100, requestSuccess: customDataLoader,
138 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff'}})
141 gml.events.register("loadend", gml, dataLoaded );
144 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
145 sf.handler.stopDown = false;
146 sf.handler.stopUp = false;
154 currentFeature = null;
157 function dataLoaded() {
158 $("status").innerHTML = "Loaded " + this.features.length + " features. (<a href='"+ this.url+"'>API</a>)";
160 objList = document.createElement("ul");
161 for (var i = 0; i < this.features.length; i++) {
162 var feature = this.features[i];
166 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
169 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
170 var li = document.createElement("li");
171 li.appendChild(document.createTextNode(nice_name + " "));
173 // Link, for viewing in the tab
174 var link = document.createElement("a");
175 link.href = "/browse/" + type + "/" + feature.osm_id;
176 var name = feature.attributes.name || feature.osm_id;
177 link.appendChild(document.createTextNode(name));
178 link.feature = feature;
179 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
180 li.appendChild(link);
182 objList.appendChild(li);
184 $("object").innerHTML = "";
185 $("object").appendChild(objList);
188 function viewFeatureLink() {
189 var layer = this.feature.layer;
190 for (var i = 0; i < layer.selectedFeatures.length; i++) {
191 var f = layer.selectedFeatures[i];
192 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
194 onFeatureSelect(this.feature);
195 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
199 function loadObjList() {
200 $("object").innerHTML="";
201 $("object").appendChild(objList);
205 function onFeatureSelect(feature) {
206 // Unselect previously selected feature
207 if (currentFeature) {
208 currentFeature.layer.drawFeature(
209 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
213 // Redraw in selected style
214 feature.layer.drawFeature(
215 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
218 // If the current object is the list, don't innerHTML="", since that could clar it.
219 if ($("object").firstChild == objList) {
220 $("object").removeChild(objList);
222 $("object").innerHTML = "";
225 // Create a link back to the object list
226 var div = document.createElement("div");
227 var link = document.createElement("a");
229 link.onclick = loadObjList;
230 link.appendChild(document.createTextNode("Back to Object List"));
231 div.appendChild(link)
232 $("object").appendChild(div);
234 // Now the list of attributes
235 var ul = document.createElement("ul");
237 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
240 var li = document.createElement("li");
241 var link = document.createElement("a");
242 link.href = "/browse/"+type+"/"+feature.osm_id;
243 link.appendChild(document.createTextNode(feature.osm_id));
244 li.appendChild(link);
246 for (var key in feature.attributes) {
247 var li = document.createElement("li");
248 var b = document.createElement("b");
249 b.appendChild(document.createTextNode(key));
251 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
254 $("object").appendChild(ul);
256 // Stash the currently drawn feature
257 currentFeature = feature;
260 function setBounds(bounds) {
261 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
262 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
264 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
266 $("minlon").innerHTML = Math.round(bounds.left * decimals) / decimals;
267 $("minlat").innerHTML = Math.round(bounds.bottom * decimals) / decimals;
268 $("maxlon").innerHTML = Math.round(bounds.right * decimals) / decimals;
269 $("maxlat").innerHTML = Math.round(bounds.top * decimals) / decimals;
271 function validateLinks() {
272 var bounds = this.getExtent();
273 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
275 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
276 $("use_map").style.display = "none";
278 $("use_map").style.display = "inline";