This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tools:rat-map [2022/03/23 12:40] – created juraschek | tools:rat-map [2022/03/23 14:43] (current) – juraschek | ||
---|---|---|---|
Line 1: | Line 1: | ||
Show uploaded csv in openstreetmap layer via javascript | Show uploaded csv in openstreetmap layer via javascript | ||
+ | |||
+ | |||
+ | < | ||
+ | <label for=" | ||
+ | <input type=" | ||
+ | <div id=" | ||
+ | <script src="/ | ||
+ | < | ||
+ | var markers = 0; | ||
+ | |||
+ | document.querySelector("# | ||
+ | console.log(" | ||
+ | // files that user has chosen | ||
+ | var all_files = this.files; | ||
+ | if(all_files.length == 0) { | ||
+ | alert(' | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // first file selected by user | ||
+ | var file = all_files[0]; | ||
+ | |||
+ | |||
+ | var reader = new FileReader(); | ||
+ | |||
+ | |||
+ | // file reading finished successfully | ||
+ | reader.addEventListener(' | ||
+ | var text = e.target.result; | ||
+ | arr = text.split(' | ||
+ | for (i = 0; i < arr.length; i++) { | ||
+ | arr[i] = arr[i].split(',' | ||
+ | } | ||
+ | console.log(arr); | ||
+ | markers = arr; | ||
+ | |||
+ | |||
+ | // MAP STUFF STARTS HERE | ||
+ | // Adapted from: harrywood.co.uk | ||
+ | epsg4326 = new OpenLayers.Projection(" | ||
+ | |||
+ | map = new OpenLayers.Map({ | ||
+ | div: " | ||
+ | displayProjection: | ||
+ | }); | ||
+ | |||
+ | // map = new OpenLayers.Map(" | ||
+ | map.addLayer(new OpenLayers.Layer.OSM()); | ||
+ | map.addLayer(new OpenLayers.Layer.OSM(" | ||
+ | [" | ||
+ | { | ||
+ | attribution: | ||
+ | " | ||
+ | }) | ||
+ | ); | ||
+ | // See https:// | ||
+ | |||
+ | map.addControls([ | ||
+ | new OpenLayers.Control.MousePosition(), | ||
+ | new OpenLayers.Control.ScaleLine(), | ||
+ | new OpenLayers.Control.LayerSwitcher(), | ||
+ | new OpenLayers.Control.Permalink({ anchor: true }) | ||
+ | ]); | ||
+ | |||
+ | // Put your point-definitions here | ||
+ | |||
+ | |||
+ | projectTo = map.getProjectionObject(); | ||
+ | var lonLat = new OpenLayers.LonLat(markers[0][15], | ||
+ | var zoom = 12; | ||
+ | if (!map.getCenter()) { | ||
+ | map.setCenter(lonLat, | ||
+ | } | ||
+ | |||
+ | |||
+ | var layerName = [markers[0][7]]; | ||
+ | switch (markers[0][7]) { | ||
+ | case ' | ||
+ | color = " | ||
+ | break; | ||
+ | case ' | ||
+ | color = " | ||
+ | break; | ||
+ | default: | ||
+ | color = " | ||
+ | } | ||
+ | var styleArray = [new OpenLayers.StyleMap({ pointRadius: | ||
+ | var vectorLayer = [new OpenLayers.Layer.Vector(layerName[0], | ||
+ | |||
+ | var j = 0; | ||
+ | for (var i = 1; i < markers.length; | ||
+ | if (!layerName.includes(markers[i][7])) { | ||
+ | j++; | ||
+ | layerName.push(markers[i][7]); | ||
+ | switch (markers[i][7]) { | ||
+ | case ' | ||
+ | color = " | ||
+ | break; | ||
+ | case ' | ||
+ | color = " | ||
+ | break; | ||
+ | default: | ||
+ | color = " | ||
+ | } | ||
+ | styleArray.push(new OpenLayers.StyleMap({ pointRadius: | ||
+ | vectorLayer.push(new OpenLayers.Layer.Vector(layerName[j], | ||
+ | } | ||
+ | } | ||
+ | |||
+ | //Loop through the markers array | ||
+ | for (var i = 0; i < markers.length; | ||
+ | var lon = markers[i][15]; | ||
+ | var lat = markers[i][14]; | ||
+ | var feature = new OpenLayers.Feature.Vector( | ||
+ | new OpenLayers.Geometry.Point(lon, | ||
+ | { description: | ||
+ | // see http:// | ||
+ | ); | ||
+ | vectorLayer[layerName.indexOf(markers[i][7])].addFeatures(feature); | ||
+ | } | ||
+ | |||
+ | for (var i = 0; i < layerName.length; | ||
+ | map.addLayer(vectorLayer[i]); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | }); | ||
+ | // read as text file | ||
+ | reader.readAsText(file); | ||
+ | |||
+ | }); | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ |