|
21 | 21 | </style> |
22 | 22 | </head> |
23 | 23 | <body> |
24 | | -<h2>Convert lat-long string from ona.io to KML</h2> |
25 | | -<p>Input: <br> |
26 | | -<textarea id="input"></textarea><br> |
27 | | -<button onclick="text2kml()" class="bigbutton">Convert to KML</button> | <button onclick="example()">load example</button></p> |
28 | | - |
29 | | -<p>Output - polygon: <br> |
30 | | -<textarea id="polygon"></textarea><br> |
31 | | -<button onclick="download_file('polygon','shape-polygon.kml')" class="bigbutton">Download Polygon KML</button></p> |
32 | | - |
33 | | - |
34 | | -<p>Output - line: <br> |
35 | | -<textarea id="line"></textarea><br> |
36 | | -<button onclick="download_file('line','shape-line.kml')" class="bigbutton">Download Line KML</button></p> |
37 | | - |
38 | | -<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> |
39 | | - |
40 | | -<p><a href="https://github.com/answerquest/answerquest.github.io/blob/master/kmlcreate.html">See the code</a> | By <a href="http://nikhilvj.co.in">Nikhil VJ</a> from Pune, India | <a href="https://nikhilvj.benow.in/pay">i appreciate this!</a></p> |
41 | | - |
42 | | -<script> |
43 | | - |
44 | | -function text2kml() { |
45 | | -// TO DO : FIX A BUG! sample data from Philip created some "undefined" stuff at end. |
46 | | - |
47 | | -/* |
48 | | -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 |
49 | | -ie, [lat] [long] [alt]; |
50 | | -
|
51 | | -output: -84.687180,38.198642,0 -84.683888,38.197461,0 |
52 | | -ie, [long],[lat],[alt](space) |
53 | | -*/ |
54 | | - |
55 | | -var inputbox = document.getElementById('input').value.split('\n'); |
56 | | -var shapecounter = 1; |
57 | | -var totaloutput = []; |
58 | | -var outputstring = ''; |
59 | | - |
60 | | -for (input in inputbox) { |
61 | | - |
62 | | - if(inputbox[input] == '') continue; |
63 | | - |
64 | | - var inputarray = inputbox[input].split(';'); |
65 | | - var longlatarray = []; |
66 | | - |
67 | | - for (coord in inputarray) { |
68 | | - // data cleaning: zap everything that is not 0-9 (.) (;) or (space). from https://stackoverflow.com/a/4460306/4355695 |
69 | | - var latlonstring = inputarray[coord].replace(/[^0-9.; -]/g, "").trim(); |
70 | | - var container = latlonstring.split(' '); |
71 | | - //console.log(container); |
72 | | - if(checklatlng(container[0], container[1]) ) { |
73 | | - // check for valid lat-long values, push in only if valid. |
74 | | - var rectified = container[1] + ',' + container[0] + ',' + container[2]; |
75 | | - console.log(shapecounter,':',rectified); |
76 | | - longlatarray.push( rectified ); |
77 | | - } |
78 | | - |
79 | | - } |
80 | | - |
81 | | - if(longlatarray.length) { |
82 | | - outputstring = '<Placemark>\n<name>Shape' + shapecounter + '</name>\n<MultiGeometry><Polygon><outerBoundaryIs><LinearRing>\n<coordinates>' + longlatarray.join(' ') + '</coordinates>\n</LinearRing></outerBoundaryIs></Polygon></MultiGeometry>\n</Placemark>'; |
83 | | - shapecounter++; |
84 | | - totaloutput.push(outputstring); |
85 | | - //console.log('added ',outputstring) |
86 | | - } |
87 | | - |
88 | | -} |
89 | | - |
90 | | - |
91 | | -document.getElementById('polygon').value ='<?xml version="1.0" encoding="UTF-8"?>\n<kml xmlns="http://www.opengis.net/kml/2.2">\n<Document>\n' + totaloutput.join('\n') + '\n</Document>\n</kml>'; |
92 | | - |
93 | | -// afterthought: let's make a linestring KML too, so it's just line and not polygon: |
94 | | -document.getElementById('line').value = document.getElementById('polygon').value |
95 | | - .replace(/<MultiGeometry><Polygon><outerBoundaryIs><LinearRing>/gi,'<LineString><tessellate>1</tessellate>') |
96 | | - .replace(/<\/LinearRing><\/outerBoundaryIs><\/Polygon><\/MultiGeometry>/gi,'</LineString>'); |
97 | | -// use replace-all and case-insensitive replace. from https://www.w3schools.com/jsref/jsref_replace.asp . The normal replace only does it once. |
98 | | -} |
99 | | - |
100 | | -function example() { |
101 | | - document.getElementById('input').value = '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'; |
102 | | -} |
103 | | - |
104 | | -/* For saving as kml */ |
105 | | -function download_file(source, name, mime_type) { |
106 | | - // Adapted from https://stackoverflow.com/a/35251739/4355695 |
107 | | - mime_type = mime_type || "text/plain"; |
108 | | - |
109 | | - var checkboxes = document.getElementsByName('option1'); |
110 | | - var vals = ""; |
111 | | - for (var i=0, n=checkboxes.length;i<n;i++) { |
112 | | - if (checkboxes[i].checked) |
113 | | - vals += ","+checkboxes[i].value; |
114 | | - } |
115 | | - |
116 | | -if (vals) vals = vals.substring(1); |
117 | | - |
118 | | - var contentString = document.getElementById(source).value; |
119 | | - |
120 | | - if( !contentString.length || contentString=='[]' ) { alert('No Data! Check the previous step.'); return; } |
121 | | - |
122 | | - var blob = new Blob([contentString], {type: mime_type}); |
123 | | - var dlink = document.createElement('a'); |
124 | | - dlink.download = name; |
125 | | - dlink.href = window.URL.createObjectURL(blob); |
126 | | - dlink.onclick = function(e) { |
127 | | - // revokeObjectURL needs a delay to work properly |
128 | | - var that = this; |
129 | | - setTimeout(function() { |
130 | | - window.URL.revokeObjectURL(that.href); |
131 | | - }, 1500); |
132 | | - }; |
133 | | - |
134 | | - dlink.click(); |
135 | | - dlink.remove(); |
136 | | - /* |
137 | | - setTimeout(function() { |
138 | | - fieldsreset(); |
139 | | - }, 1500); |
140 | | - */ |
141 | | -} |
142 | | - |
143 | | -// brought in from static-gtfs-manager |
144 | | -function checklatlng(lat,lon) { |
145 | | - lat = parseFloat(lat); |
146 | | - lon = parseFloat(lon); |
147 | | - if ( typeof lat == 'number' && typeof lon == 'number' && |
148 | | - !isNaN(lat) && !isNaN(lon) ) { |
149 | | - //console.log(lat,lon,'is valid'); |
150 | | - // again, check if they're in 90 -90 etc |
151 | | - if( (-90<=lat<=90) && (-180<=lon<=180) ) return true; |
152 | | - } |
153 | | - |
154 | | - // no need of else.. if it fails anywhere above, default return should be false |
155 | | - //console.log(lat,lon,'is not valid'); |
156 | | - return false; |
157 | | -} |
158 | | -</script> |
| 24 | + <h2>Moved to <a href="https://answerquest.github.io/ODKgeotrace2shapefile/">https://answerquest.github.io/ODKgeotrace2shapefile/</a></h2> |
| 25 | + <p>Now in a repo of its own: <a href="https://github.com/answerquest/ODKgeotrace2shapefile">https://github.com/answerquest/ODKgeotrace2shapefile</a></p> |
159 | 26 | </body> |
160 | 27 | </html> |
161 | 28 |
|
0 commit comments