|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> |
| 6 | +<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 7 | +<meta name="HandheldFriendly" content="true"> |
| 8 | + <title>form2text</title> |
| 9 | + <style> |
| 10 | + body, textarea { |
| 11 | + font-family: "Open Sans","Arial"; |
| 12 | + |
| 13 | + } |
| 14 | + textarea { |
| 15 | + height: 100px; |
| 16 | + width: 500px; |
| 17 | + } |
| 18 | + </style> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | +<h2>Convert lat-long string from ona.io to KML</h2> |
| 22 | +<p>Input: <br> |
| 23 | +<textarea id="input"></textarea></p> |
| 24 | + |
| 25 | +<p><button onclick="text2kml()" class="bigbutton">Convert to KML</button></p> |
| 26 | + |
| 27 | +<p>Output - polygon: <br> |
| 28 | +<textarea id="polygon"></textarea></p> |
| 29 | +<p><button onclick="download_file('polygon','shape-polygon.kml')" class="bigbutton">Download Polygon KML</button></p> |
| 30 | + |
| 31 | + |
| 32 | +<p>Output - line: <br> |
| 33 | +<textarea id="line"></textarea></p> |
| 34 | +<p><button onclick="download_file('line','shape-line.kml')" class="bigbutton">Download Line KML</button></p> |
| 35 | + |
| 36 | +<p>Preview it in <a href="http://geojson.io/" target="_blank">GeoJSON.io</a>, Upload in <a href="https://mymaps.google.com/" target="_blank">Google My Maps</a></p> |
| 37 | + |
| 38 | +By <a href="http://nikhilvj.cu.cc">Nikhil VJ</a> | <a href="https://www.instamojo.com/@nikhilvj/">i appreciate this!</a> |
| 39 | +</small> |
| 40 | + |
| 41 | +<script> |
| 42 | + |
| 43 | +function text2kml() { |
| 44 | + |
| 45 | +/* |
| 46 | +input : 18.530663 73.875515 0 0;18.534935 73.874657 0 0;18.534925 73.87626 0 0;18.531995 73.876925 0 0;18.530663 73.875515 0 0 |
| 47 | +
|
| 48 | +output: -84.687180,38.198642,0 -84.683888,38.197461,0 |
| 49 | +*/ |
| 50 | + |
| 51 | +var inputbox = document.getElementById('input').value.split('\n'); |
| 52 | +var shapecounter = 1; |
| 53 | +var totaloutput = []; |
| 54 | +var outputstring = ''; |
| 55 | + |
| 56 | +for (input in inputbox) { |
| 57 | + |
| 58 | + if(inputbox[input] == '') continue; |
| 59 | + |
| 60 | + var inputarray = inputbox[input].split(';'); |
| 61 | + var longlatarray = []; |
| 62 | + |
| 63 | + for (coord in inputarray) { |
| 64 | + var container = inputarray[coord].split(' '); |
| 65 | + console.log(container); |
| 66 | + var rectified = container[1] + ',' + container[0] + ',' + container[2]; |
| 67 | + //console.log(rectified); |
| 68 | + longlatarray.push( rectified ); |
| 69 | + } |
| 70 | + |
| 71 | + outputstring = '<Placemark><name>Shape' + shapecounter + '</name><MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>' + longlatarray.join(' ') + '</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry></Placemark>'; |
| 72 | + shapecounter++; |
| 73 | + |
| 74 | + totaloutput.push(outputstring); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +document.getElementById('polygon').value ='<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document>' + totaloutput.join('\n') + '</Document></kml>'; |
| 79 | + |
| 80 | + |
| 81 | +document.getElementById('line').value = document.getElementById('polygon').value.replace('<Polygon><outerBoundaryIs><LinearRing>','<LineString>').replace('</LinearRing></outerBoundaryIs></Polygon>','</LineString>'); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +function download_file(source, name, mime_type) { |
| 86 | + // Adapted from https://stackoverflow.com/a/35251739/4355695 |
| 87 | + mime_type = mime_type || "text/plain"; |
| 88 | + |
| 89 | + var checkboxes = document.getElementsByName('option1'); |
| 90 | + var vals = ""; |
| 91 | + for (var i=0, n=checkboxes.length;i<n;i++) { |
| 92 | + if (checkboxes[i].checked) |
| 93 | + vals += ","+checkboxes[i].value; |
| 94 | + } |
| 95 | + |
| 96 | +if (vals) vals = vals.substring(1); |
| 97 | + |
| 98 | + var contentString = document.getElementById(source).value; |
| 99 | + |
| 100 | + if( !contentString.length || contentString=='[]' ) { alert('No Data! Check the previous step.'); return; } |
| 101 | + |
| 102 | + var blob = new Blob([contentString], {type: mime_type}); |
| 103 | + var dlink = document.createElement('a'); |
| 104 | + dlink.download = name; |
| 105 | + dlink.href = window.URL.createObjectURL(blob); |
| 106 | + dlink.onclick = function(e) { |
| 107 | + // revokeObjectURL needs a delay to work properly |
| 108 | + var that = this; |
| 109 | + setTimeout(function() { |
| 110 | + window.URL.revokeObjectURL(that.href); |
| 111 | + }, 1500); |
| 112 | + }; |
| 113 | + |
| 114 | + dlink.click(); |
| 115 | + dlink.remove(); |
| 116 | + /* |
| 117 | + setTimeout(function() { |
| 118 | + fieldsreset(); |
| 119 | + }, 1500); |
| 120 | + */ |
| 121 | +} |
| 122 | + |
| 123 | +</script> |
| 124 | +</body> |
| 125 | +</html> |
| 126 | + |
0 commit comments