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");
85 function customDataLoader(request) {
86 var doc = request.responseXML;
88 if (!doc || !doc.documentElement) {
89 doc = request.responseText;
94 OpenLayers.Util.extend(options, this.formatOptions);
95 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
96 options.externalProjection = this.projection;
97 options.internalProjection = this.map.getProjectionObject();
100 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
101 var features = gml.read(doc);
102 if (!this.maxFeatures || features.length <= this.maxFeatures) {
103 this.addFeatures(features);
104 this.events.triggerEvent("loadend");
107 featureList = features;
108 displayFeatureWarning();
112 function getData(bounds) {
114 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
115 var size = bounds.getWidth() * bounds.getHeight();
117 $("status").innerHTML = "Unable to load: Bounding box size of " + size + " is too large. (Must be smaller than 0.25)";
121 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
123 $("status").innerHTML = "Loading...";
125 var def = OpenLayers.Feature.Vector.style['default'];
126 var style = new OpenLayers.Style();
127 style.addRules([new OpenLayers.Rule(
129 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
130 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
131 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
134 gml = new OpenLayers.Layer.GML("Data",url,
135 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
136 maxFeatures: 100, requestSuccess: customDataLoader,
137 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff'}})
140 gml.events.register("loadend", gml, dataLoaded );
143 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
144 sf.handler.stopDown = false;
145 sf.handler.stopUp = false;
153 currentFeature = null;
156 function dataLoaded() {
157 $("status").innerHTML = "Loaded " + this.features.length + " features. (<a href='"+ this.url+"'>API</a>)";
159 objList = document.createElement("ul");
160 for (var i = 0; i < this.features.length; i++) {
161 var feature = this.features[i];
165 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
168 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
169 var li = document.createElement("li");
170 li.appendChild(document.createTextNode(nice_name + " "));
172 // Link, for viewing in the tab
173 var link = document.createElement("a");
174 link.href = "/browse/" + type + "/" + feature.osm_id;
175 var name = feature.attributes.name || feature.osm_id;
176 link.appendChild(document.createTextNode(name));
177 link.feature = feature;
178 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
179 li.appendChild(link);
181 objList.appendChild(li);
183 $("object").innerHTML = "";
184 $("object").appendChild(objList);
187 function viewFeatureLink() {
188 var layer = this.feature.layer;
189 for (var i = 0; i < layer.selectedFeatures.length; i++) {
190 var f = layer.selectedFeatures[i];
191 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
193 onFeatureSelect(this.feature);
194 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
198 function loadObjList() {
199 $("object").innerHTML="";
200 $("object").appendChild(objList);
204 function onFeatureSelect(feature) {
205 // Unselect previously selected feature
206 if (currentFeature) {
207 currentFeature.layer.drawFeature(
208 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
212 // Redraw in selected style
213 feature.layer.drawFeature(
214 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
217 // If the current object is the list, don't innerHTML="", since that could clar it.
218 if ($("object").firstChild == objList) {
219 $("object").removeChild(objList);
221 $("object").innerHTML = "";
224 // Create a link back to the object list
225 var div = document.createElement("div");
226 var link = document.createElement("a");
228 link.onclick = loadObjList;
229 link.appendChild(document.createTextNode("Back to Object List"));
230 div.appendChild(link)
231 $("object").appendChild(div);
233 // Now the list of attributes
234 var ul = document.createElement("ul");
236 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
239 var li = document.createElement("li");
240 var link = document.createElement("a");
241 link.href = "/browse/"+type+"/"+feature.osm_id;
242 link.appendChild(document.createTextNode(feature.osm_id));
243 li.appendChild(link);
245 for (var key in feature.attributes) {
246 var li = document.createElement("li");
247 var b = document.createElement("b");
248 b.appendChild(document.createTextNode(key));
250 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
253 $("object").appendChild(ul);
255 // Stash the currently drawn feature
256 currentFeature = feature;
259 function setBounds(bounds) {
260 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
261 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
263 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
265 $("minlon").innerHTML = Math.round(bounds.left * decimals) / decimals;
266 $("minlat").innerHTML = Math.round(bounds.bottom * decimals) / decimals;
267 $("maxlon").innerHTML = Math.round(bounds.right * decimals) / decimals;
268 $("maxlat").innerHTML = Math.round(bounds.top * decimals) / decimals;
270 function validateLinks() {
271 var bounds = this.getExtent();
272 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
274 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
275 $("use_map").style.display = "none";
277 $("use_map").style.display = "inline";