-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
443 lines (300 loc) · 12 KB
/
script.js
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
let geoData = {};
var modal = $("#myModal")[0];
var span = $(".close")[0];
// Expandable section taken from example
var isExpanded = false;
var upArrow = 'https://s3.amazonaws.com/drone-deploy-plugins/templates/login-example-imgs/arrow-up.svg';
var downArrow = 'https://s3.amazonaws.com/drone-deploy-plugins/templates/login-example-imgs/arrow-down.svg';
var expandArrow = $('.expand-arrow')[0];
var expandBody = $('.expand-section')[0];
var expandRow = $('.expand-row')[0];
expandRow.addEventListener('click', function(){
isExpanded = !isExpanded
if (isExpanded){
expandArrow.src = upArrow;
expandBody.style.display = 'block';
} else{
expandArrow.src = downArrow;
expandBody.style.display = 'none';
}
});
// jQuery event handler functions
// display selected file in input text box and check acceptability of filetype
$("#input")[0].onchange = function() {
$("#upload")[0].value = this.value.substring(12);
var file = $("#input")[0].files[0];
checkFile(file.name);
};
// close modal if opened
span.onclick = function() {
modal.style.display = "none";
};
window.onclick = function(event) {
if (event.target === modal) {
modal.style.display = "none";
}
};
// verify that the file has useable data when Submit button is clicked
$("#verify")[0].onclick = function() {
var file = $("#input")[0].files[0];
channelFile(file)
};
// create plan when Create Plan button is clicked
$("#plan")[0].onclick = function() {
var file = $("#input")[0].files[0];
createPlan(file);
};
// Open modal with error message
function modalOpen(message){
modal.style.display="block";
$(".modal-paragraph")[0].innerHTML = message;
}
// helper functions:
// check filetype and display error message if not an accepted type
function checkFile(file) {
var extension = file.substr((file.lastIndexOf('.')+1));
if (!/(shp|zip|kml)$/ig.test(extension)) {
modalOpen('Invalid file type: '+extension+'. Please use a .shp, .kml, or .zip file.');
}
};
// based on file type create geoJSON and verify that file is useable
function channelFile(file) {
let fileName = file.name;
let extension = fileName.substr((fileName.lastIndexOf('.') +1));
if (/(shp)$/ig.test(extension)) {
readFile(file).then(shpToGeo).then(checkShpData).then(displayCreatePlan);
}
if(/(kml)$/ig.test(extension)) {
readKMLFile(file).then(fromKML).then(getKMLGeo).then(checkData).then(displayCreatePlan);
}
if (/(zip)$/ig.test(extension)) {
readFile(file).then(zipToGeo).then(checkData).then(displayCreatePlan);
}
};
// create drone flight plan
function createPlan(file) {
let fileName = file.name;
geoData.name = fileName.substring(0, fileName.length-4);
let extension = fileName.substr((fileName.lastIndexOf('.') +1));
if (/(shp)$/ig.test(extension)) {
readFile(file).then(shpToGeo).then(modifySHPGeoJson).then(transformCoordinates).then(droneDeployApi);
}
if(/(kml)$/ig.test(extension)) {
readKMLFile(file).then(fromKML).then(getKMLGeo).then(modifyKMLGeoJson).then(transformCoordinates).then(droneDeployApi);
}
if (/(zip)$/ig.test(extension)) {
readFile(file).then(zipToGeo).then(modifyZIPGeoJson).then(transformCoordinates).then(droneDeployApi);
}
};
// read file after submitting, one for KML and one for SHP and ZIP due to different type required per the separate libraries; return Promise
function readKMLFile(file) {
return new Promise(function(succeed, fail) {
var reader = new FileReader();
reader.addEventListener("load", function() {
succeed(reader.result);
});
reader.addEventListener("error", function() {
fail(reader.error);
});
reader.readAsDataURL(file);
})
};
function readFile(file) {
return new Promise(function(succeed, fail) {
var reader = new FileReader();
reader.addEventListener("load", function() {
succeed(reader.result);
});
reader.addEventListener("error", function() {
fail(reader.error);
});
reader.readAsArrayBuffer(file);
});
};
// KML to geoJSON per library syntax - Tom MacWright's (with error message if fail to read)
function getKMLGeo(xml) {
return toGeoJSON.kml(xml);
};
function fromKML(kml) {
return $.ajax(kml).done(getKMLGeo).fail(() => {modalOpen('Unable to read file. Please submit a different file.')});
};
// SHP to geoJSON per library syntax - Mike Bostock's
let geoObject = {};
function shpToGeo(shape) {
let geoArr = [];
return shapefile.openShp(shape)
.then(source => {
return new Promise(function(resolve, reject) {
source.read()
.then(function log(result) {
if (result.done) return resolve(geoArr);
geoArr.push(result.value);
source.read().then(log);
})
})
})
.then(createGeoObject)
.catch(error => console.error(error.stack));
};
function createGeoObject(array) {
geoObject.geojson = array;
return geoObject;
};
// ZIP to geoJSON per library syntax - Calvin Metcalf's
function zipToGeo(zip) {
return shp(zip).then(function(geojson) {
return geojson;
})
};
// Check data:
//
// 2 functions, checkData for .zip & .kml since geojson is value of the features key while checkShpData since geojson is pushed to an array
function checkData(data) {
if(data.features.length===0){
modalOpen('GeoJSON has no coordinates. Please submit a file with at least one feature.');
return false;
}
let geojson = data.features[0];
if (!geojson.geometry.coordinates[0][0].length) {
if ( (geojson.geometry.coordinates[0][0]>180 || geojson.geometry.coordinates[0][0]<-180) ||
(geojson.geometry.coordinates[0][1]>90 || geojson.geometry.coordinates[0][1]<-90) ) {
modalOpen('Coordinates are not in Longitude/Latitude. Please submit file with coordinates in Longitude/Latitude.');
return false;
}
} else if ( (geojson.geometry.coordinates[0][0][0]>180 || geojson.geometry.coordinates[0][0][0]<-180) ||
(geojson.geometry.coordinates[0][0][1]>90 || geojson.geometry.coordinates[0][0][1]<-90) ) {
modalOpen('Coordinates are not in Longitude/Latitude. Please submit file with coordinates in Longitude/Latitude.');
return false;
}
return true;
};
function checkShpData(data) {
let geojson = data.geojson[0];
if(geojson.coordinates.length===0){
modalOpen('GeoJSON has no coordinates. Please submit a file with at least one feature.');
return false;
}
if (!geojson.coordinates[0][0].length) {
if ( (geojson.coordinates[0][0]>180 || geojson.coordinates[0][0]<-180) ||
(geojson.coordinates[0][1]>90 || geojson.coordinates[0][1]<-90) ) {
modalOpen('Coordinates are not in Longitude/Latitude. Please submit file with coordinates in Longitude/Latitude.');
return false;
}
} else if ( (geojson.coordinates[0][0][0]>180 || geojson.coordinates[0][0][0]<-180) ||
(geojson.coordinates[0][0][1]>90 || geojson.coordinates[0][0][1]<-90) ) {
modalOpen('Coordinates are not in Longitude/Latitude. Please submit file with coordinates in Longitude/Latitude.');
return false;
}
return true;
};
// reveal Create Plan option if file has passed checks upon Submit
function displayCreatePlan(boolean){
var checkGo = $(".check-go")[0];
if(boolean){
checkGo.style.display = "block";
}
};
// create geoData object with bbox, coordinates, and cleaned up geojson with data returned from KML to GeoJson converter
function modifyKMLGeoJson(data) {
geoData.coordinates = [];
if (data.features.length > 1 && data.features[0].geometry.coordinates[0].length <= 3) {
data.features.forEach((el) => el.geometry.coordinates.forEach((el) => geoData.coordinates.push(el)));
} else if (data.features.length === 1 && data.features[0].geometry.coordinates[0][0].length <= 3) {
data.features[0].geometry.coordinates[0].forEach((el)=> geoData.coordinates.push(el) );
}
else if (data.features.length === 1 && data.features[0].geometry.coordinates[0].length <= 3) {
data.features[0].geometry.coordinates.forEach((el) => geoData.coordinates.push(el))
}
geoData.coordinates.forEach((coordinate) => coordinate.splice(2));
geoData.coordinates = [geoData.coordinates];
let data1 = {polygon: geoData.coordinates};
geoData.geojson = GeoJSON.parse(data1, {'Polygon': 'polygon'});
let shape1 = {type: 'Polygon', coordinates: geoData.coordinates}
geoData.bbox = turf.bbox(shape1);
return geoData;
};
// create geoData object with bbox, coordinates, and cleaned up geojson with data returned from SHP to GeoJson converter
function modifySHPGeoJson(data) {
if (data.geojson.length === 1 && data.geojson[0].coordinates.length === 1) {
createGeoData(data.geojson[0], geoData);
}
if (data.geojson.length === 1 && data.geojson[0].coordinates.length > 1) {
let tempGeoDataArr = [];
data.geojson[0].coordinates.forEach((coordsArr)=>tempGeoDataArr.push({polygon: [coordsArr]}));
let tempGeoJson = tempGeoDataArr.map((geoObj)=>GeoJSON.parse(geoObj, {'Polygon':'polygon'}));
let finalArr = [];
for (let i=0; i<tempGeoJson.length; i++) {
for (let j=0; j<tempGeoJson.length; j++) {
if (i!==j) {
if ( turf.booleanContains(tempGeoJson[i].geometry,tempGeoJson[j].geometry) ) {
finalArr.push(tempGeoJson[i].geometry)
}
}
}
}
createGeoData(finalArr[0], geoData);
}
if (data.geojson.length > 1) {
let tempGeoDataArr = [];
data.geojson.forEach((geojson)=>tempGeoDataArr.push({polygon: [geojson.coordinates]}));
let tempGeoJson = tempGeoDataArr.map((geoObj)=>GeoJSON.parse(geoObj, {'Polygon':'polygon'}));
let finalArr = [];
let max = tempGeoJson[0].geometry;
for (let i=1; i<tempGeoJson.length; i++) {
if ( turf.booleanContains(tempGeoJson[i].geometry, max) ) {
max = tempGeoJson[i].geometry;
}
}
finalArr.push(max);
createGeoData(finalArr[0], geoData);
}
return geoData;
};
// helper function to create bbox, coordinates, and geojson properties on geoData object
function createGeoData(geoJsonObj, geoDataObj) {
let geojsonPrelim = {polygon:geoJsonObj.coordinates};
geoDataObj.geojson = GeoJSON.parse(geojsonPrelim, {'Polygon':'polygon'});
geoDataObj.bbox = turf.bbox(geoJsonObj);
geoDataObj.coordinates = geoJsonObj.coordinates;
return geoDataObj;
};
// create geoData object with bbox, coordinates, and cleaned up geojson with data returned from ZIP to GeoJson converter
function modifyZIPGeoJson(data) {
geoData.bbox = data.features[0].geometry.bbox;
if (data.features[0].geometry.coordinates.length > 1) {
geoData.coordinates = [data.features[0].geometry.coordinates];
} else {
geoData.coordinates = data.features[0].geometry.coordinates;
}
let geojsonPrelim = {polygon:geoData.coordinates};
geoData.geojson = GeoJSON.parse(geojsonPrelim, {'Polygon':'polygon'});
return geoData;
};
// transform array of coordinates into {lat: ,lng: } so it is readable by DroneDeploy API, add centroid in {lat: ,lng: } format so map can pan to vicinity of flight plan
function transformCoordinates(geoData) {
let centroid = turf.centroid(geoData.geojson);
let centroidLatLng = {};
centroidLatLng.lat = centroid.geometry.coordinates[1];
centroidLatLng.lng = centroid.geometry.coordinates[0];
let coordinatesTransformed = [];
for (let j=0; j<geoData.coordinates[0].length; j++) {
let latlng = {};
latlng.lat = geoData.coordinates[0][j][1];
latlng.lng = geoData.coordinates[0][j][0];
coordinatesTransformed.push(latlng);
}
geoData.coordinatesTransformed = coordinatesTransformed;
geoData.centroid = centroidLatLng;
return geoData;
};
// make call to Drone Deploy API, send geoData name of flight, geometry of shape, call Plans.update to update currently viewed flight path, and call Track.successCondition
function droneDeployApi(geoData) {
const dd = new DroneDeploy({version:1});
let options = {geometry: geoData.coordinatesTransformed};
dd.then(function(droneDeploy) {
droneDeploy.Plans.getCurrentlyViewed().then(function(plan) {
droneDeploy.Plans.update(plan.id, options);
droneDeploy.Track.successCondition();
});
});
};