-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
358 lines (293 loc) · 12.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.6.2/proj4.min.js" integrity="sha512-EKjCCRjU5ClBwaRb6dGbElFNWJTE7Ek7+PlXelkum5uofPwlf6u2VRch1ty3csFCQn9XdyX89Te8jVg61qtm3Q==" crossorigin="anonymous"></script>
<script async src="https://cdn.jsdelivr.net/npm/geotiff"></script>
<script>
// the markers are stored in a global array
var speciesMarkers = Array();
var sepChar;
var tiff;
var canvas;
var global_rgb, global_canvas;
var layerControl;
// I am too cheap to load them via ajax...
proj4.defs("EPSG:31466","+proj=tmerc +lat_0=0 +lon_0=6 +k=1 +x_0=2500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs");
proj4.defs("EPSG:31467","+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs");
proj4.defs("EPSG:31468","+proj=tmerc +lat_0=0 +lon_0=12 +k=1 +x_0=4500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs");
proj4.defs("EPSG:25832","+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");
proj4.defs("EPSG:4647","+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=32500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");
var map;
$(function() {
map = L.map("map", {maxZoom: 25}).setView([51.52, 11], 8);
// OSM layer
var osmLayer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',
{attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'}
).addTo(map);
layercontrol = L.control.layers({"OpenStreetMap": osmLayer}, []).addTo(map);
map.on('click', function(e) {
// create marker
var m = new L.marker(e.latlng).addTo(map);
// add a species attribute
m.species = undefined;
// create and show empty popup, will be filled on popupopen
m.bindPopup("<div/>").openPopup();
// store in global speciesMarkers array
speciesMarkers.push(m);
});
map.on('popupopen', function(evt) {
// create a select dropdown menu
var selectElement = $(constructSelectElementText(evt.popup._source.species));
// attach event handler
selectElement.on('change', function() {
// popup._source is the marker, store the selected list value on the marker
evt.popup._source.species = selectElement.val();
// trigger marker change function
markerChange(evt.popup._source, evt.popup._source.species);
});
// set the select element as the contents of the popup. The [0] gets the native DOM element, which is necessary for setContent.
evt.popup.setContent(selectElement[0]);
});
// dataset id is first column in csv; write CSV if changed
$(datasetid).on('change', writeCSV);
// CSV separator character; store and write CSV if changed
$(sc).on('change',function() {
sepChar = $(sc).val();
writeCSV();
});
sepChar = $(sc).val();
$(helpcard).hide();
$(loadingindicator).hide();
$(openhelp).on('click', function(e) {
$(helpcard).toggle();
$(helpcard).css({
"position": "absolute",
"background-color":"white",
"left": e.pageX + 'px',
"top": e.pageY + 'px'
})
});
$(closehelp).on('click',function() {
$(helpcard).hide();
});
// TIFF loading handler
document.getElementById('fileInput').onchange = async function() {
tiff = await GeoTIFF.fromBlob(document.getElementById('fileInput').files[0]);
const image = await tiff.getImage();
const width = image.getWidth();
const height = image.getHeight();
$(log).append("Width: " + width+"<br/>");
$(log).append("Height: " + height+"<br/>");
try {
$(log).append("BBOX: " + image.getBoundingBox().join(",")+"<br/>");
$(log).append("EPSG (projected): " + image.geoKeys.ProjectedCSTypeGeoKey+"<br/>");
$(log).append("EPSG (geographic): " + image.geoKeys.GeographicTypeGeoKey+"<br/>");
} catch(err) {
$(log).append("<p class='w3-red'>Error: "+err.message+"</p>");
return;
}
canvas = document.createElement('canvas');
//canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Set width and height
canvas.width = width;
canvas.height = height;
// this async function is executed immediately ;-)
(async function() {
// show loading indicator
$(loadingindicator).show();
// read TIFF as RGB
const rgb = await image.readRGB();
// get ImageData object from RGB, set the color from the text box as transparent
const img = rgbArray2ImageData(rgb,width,height,$(transpColor).val().split(","));
// draw image on the offscreen canvas
ctx.putImageData(img, 0, 0);
// get the base64 encoded image from the canvas
dataURL = canvas.toDataURL("image/png")
// for debugging show a preview of the image
var im = $("<img src='"+dataURL+"' width='100' heigth='100'/>");
$(log).append(im);
const b = image.getBoundingBox();
var bounds;
if (image.geoKeys.GeographicTypeGeoKey == 4326) {
// latLngBounds is the other way round...
bounds = L.latLngBounds( [[b[1],b[0]],[b[3],b[2]]] );
} else if (image.geoKeys.ProjectedCSTypeGeoKey) {
const crs = "EPSG:"+image.geoKeys.ProjectedCSTypeGeoKey;
console.log(crs);
const transform = proj4(crs,"EPSG:4326")
const first = transform.forward([b[0],b[1]]);
const second = transform.forward([b[2],b[3]]);
console.log([first , second])
bounds = L.latLngBounds( [ [first[1],first[0]],[second[1],second[0]]] );
}
$(log).append("Bounds: " + bounds.toBBoxString()+"\n");
// create leaflet layer from data url (which is the base64 encoded image)
const imgLayer = L.imageOverlay(dataURL, bounds);
// add to map and layercontrol, layer description is the filename
layercontrol.addOverlay(imgLayer,document.getElementById("fileInput").files[0].name);
// set dataset id to the filename, makes distinguishing the CSV from multiple users easy
$(datasetid).val(document.getElementById("fileInput").files[0].name);
// hide loading indicator
$(loadingindicator).hide();
// zoom and pan to image
map.fitBounds(bounds);
imgLayer.addTo(map);
})(); // <--- that mess of characters executes the just-defined async function
}
}); // end $(), which is the jquery init function
/**
* called after a marker has changed
*
* @param marker the leaflet marker
* @param species the new species value
*/
var markerChange = function(marker,species) {
// don't do anything fancy, just redraw the CSV
writeCSV();
}
/**
* write the CSV textbox from the speciesMarkers array
*/
var writeCSV = function() {
// csv header
var res = "datasetid"+sepChar+"lat"+sepChar+"lon"+sepChar+"species\n";
const dsid = $(datasetid).val();
for (var i=0;i<speciesMarkers.length;i++) {
res += dsid + sepChar
+ speciesMarkers[i].getLatLng().lat.toFixed(6) + sepChar
+ speciesMarkers[i].getLatLng().lng.toFixed(6) + sepChar
+ speciesMarkers[i].species
+ "\n";
}
// write CSV to the textbox
$(outcsv).val(res);
}
/**
* construct a select element with values from the form element "#possiblespecies".
*
* @param selectedText the string that should be selected, defaults to undefined
*
* @return A string like '<select><option value="...">...</option></select>'
*/
var constructSelectElementText = function(selectedText="undefined") {
var r = "<select>";
var sp = $(possiblespecies).val().split(",");
// add undefined as selectable value
sp.push("undefined");
for (var i=0;i<sp.length;i++) {
// create option, mark selected if its equal to the function parameter
r += "<option value='"+sp[i]+"'"+(sp[i]==selectedText?" selected":"")+">"+sp[i]+"</option>";
}
r += "</select>";
return r;
}
/**
* create a ImageData object soitable for drawing on a canvas from an array of RGB values
*
* adapted from https://stackoverflow.com/a/45992175/2168905
*
* @param in_rgb the array of RGB values, e.g. [255,255,255,0,0,0,12,23,36,...] . No nesting.
* @param w width of image
* @param h height of image
* @param transpColor an array of one color RGB values that should be set fully transparent in the returned image. Defaults to [255,255,255] (white). Set undefined for none.
*
* @return an ImageData object from the in_rgb image of dimension w,h and with transpColor transparent
*/
var rgbArray2ImageData = function(in_rgb,w,h,transpColor=[255,255,255]) {
var srcBmp = in_rgb, width = w, height = h;
var dstBmp = new Uint8ClampedArray(width * height * 4);
var ptrSrc = 0; // source pointer
var ptrDst = 0; // destination pointer
var dstLen = dstBmp.length;
var alpha; // alpha value
while(ptrDst < dstLen) {
if (transpColor && srcBmp[ptrSrc] == transpColor[0] && srcBmp[ptrSrc+1] == transpColor[1] && srcBmp[ptrSrc+2] == transpColor[2]) {
alpha = 0;
} else {
alpha = 255;
}
dstBmp[ptrDst++] = srcBmp[ptrSrc++]; // copy R
dstBmp[ptrDst++] = srcBmp[ptrSrc++]; // copy G
dstBmp[ptrDst++] = srcBmp[ptrSrc++]; // copy B
dstBmp[ptrDst++] = alpha; // set alpha to full (255 => no transparency, 0 => fully transparent)
}
// finally create a ImageData object using the typed array
var idata = new ImageData(dstBmp, width, height);
return idata;
}
</script>
</script>
<style type="text/css">
#map {
height: 500px;
width: 500px;
}
h2 {
margin-top:0.1em;
margin-left:0.1em;
font-family: Arial, Helvetica, sans-serif;
}
#helpcard {
width: 30%;
height: 500px;
scrolling: auto;
z-index: 1000;
}
#loadingindicator {
position: relative,
display: none,
background-color: white,
left: 200px,
top: 80px,
width: 30%,
z-index: 1000
}
</style>
</head>
<body>
<div class="w3-container w3-right">
<a href="#" id="openhelp">about/help</a>
<ul class="w3-ul w3-border">
<li>Choose file overlay<input type="file" id="fileInput"/> <span id="loadingindicator" class="w3-yellow">Loading...please wait</span> <br/>
Display color as transparent (RGB): <input type="text" id="transpColor" value="255,255,255"/>
</li>
<li>
Possible observations, separated by comma: <input type="text" id="possiblespecies" value="sheep,cow,velociraptor,trex,elefant,another_cow" size="70"/><br/>
Dataset id: <input type="text" id="datasetid" name="datasetid" value="s1"/><br/>
Separator character: <input type="text" id="sc" name="sc" value=";" size="1"/><br/>
</li>
<li>
<textarea id="outcsv" rows="10" cols="50">
</textarea>
</li>
<li>
<div id="log" class="w3-yellow">
</div>
</li>
</ul>
</div>
<div id="map" class="w3-left w3-margin"></div>
<div class="w3-container" id="helpcard">
<div class="w3-card-4">
<header class="w3-container w3-blue">
<h1>About this tool / help <span id="closehelp" class="w3-button w3-display-topright">×</span></h1>
</header>
<div class="w3-container">
<p>This tool was created as an exercise inspired by the GIS.SE question <a href="https://gis.stackexchange.com/q/375072/15183">How to digitise animal positions in aerial photos?</a>. A user should be able to digitize animal positions from aerial imagery and set the species.<br/>
I learned loads of cool techniques and Javascript libraries while writing this answer.</p>
<p>The basis is <a href="//leafletjs.com">leaflet</a>. On click of the map a marker is created at the click position. The marker popup is created on the fly and consists only of a dropdown select element. The possible values for the select list are created from the text boxes on the right, so fully customizable. On change of the select element its value is written to the (custom) <code>species</code> attribute of the marker. Markers are stored in a global array.
</p>
<p>Each change of a marker dropdown triggers a full rewrite of the output CSV. This is the main output and can be mailed to someone, used as input for a spreadsheet etc. It would also be possible to trigger AJAX calls on marker value change, so one could store the markers at some remote net location, if needed.
</p>
<p>Images can be added via the file overlay input. Only GeoTIFF files with photometric interpretation RGB, 3 pixels per sample and data type UInt8 are supported. Hint: These images are created by QGIS when exporting a map view to an image.
The TIF reading is done with <a href="https://geotiffjs.github.io/">geotiff.js</a>. Images with coordinates in EPSG:4326 (WGS84) are displayed right away, the corner coordinates of images in other CRS are reprojected to wgs84 using <a href="http://proj4js.org/">proj4js</a>.
</p>
</div>
</div>
</div>
</body>
</html>