-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectaoi.html
352 lines (308 loc) · 11.1 KB
/
selectaoi.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Selection AOI Demo</title>
<link rel="stylesheet" href="https://jsdev.arcgis.com/4.14/esri/css/main.css">
<script src="https://jsdev.arcgis.com/4.14/"></script>
<link rel="stylesheet" href="https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.1.0/css/calcite-web.min.css">
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
.hide{
display: none;
}
</style>
<script>
require([
// "https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.1.0/js/calcite-web.min.js",
"esri/identity/OAuthInfo",
"esri/identity/IdentityManager",
"esri/Map",
"esri/WebMap",
"esri/layers/FeatureLayer",
"esri/views/MapView",
"esri/widgets/Sketch/SketchViewModel",
"esri/Graphic",
"esri/layers/GraphicsLayer",
"esri/widgets/LayerList",
"esri/widgets/Expand",
"esri/geometry/geometryEngine",
"esri/geometry/support/webMercatorUtils",
"dojo/dom-attr",
"esri/tasks/Locator",
"esri/widgets/Search"
// ], function(calcite, Map, MapView, dom) {
], function(OAuthInfo, esriId, Map, WebMap, FeatureLayer, MapView,
SketchViewModel, Graphic, GraphicsLayer,LayerList, Expand,
geometryEngine, webMercatorUtils, domAttr, Locator, Search) {
let noFlyPoly;
/****************************************************
* Initialize OAuth / Login
****************************************************/
const portalUrl = "https://www.arcgis.com/sharing";
var info = new OAuthInfo({
appId: "WwNy6bnHK2YboEpE"
});
esriId.registerOAuthInfos([info]);
const signInBtn = document.getElementById("sign-in");
signInBtn.onclick = function(){
esriId.getCredential(portalUrl);
}
esriId.checkSignInStatus(portalUrl).then(cred => {
if(cred && cred.userId){
signInBtn.innerText = cred.userId;
}
});
/****************************************************
* Set up the map view
****************************************************/
let lyr;
let map = new Map({
basemap: "topo-vector"
});
let view = new MapView({
container: "viewDiv",
map: map,
center: [-29.83059, 35.87093],
zoom: 3
});
// add a layer to display the sketched polygons
let drawGraphic;
const polygonGraphicsLayer = new GraphicsLayer({
listMode: 'hide'
});
map.add(polygonGraphicsLayer);
// add the input form as a map view element
const aoiElem = document.getElementById('aoi-form');
view.ui.add([aoiElem], 'top-right');
view.when(() => aoiElem.classList.remove('hide'));
// create a collapsable layer list widget
const layerList = new LayerList({
container: document.createElement("div"),
view: view
});
const layerListExpand = new Expand({
expandIconClass: "esri-icon-layer-list",
view: view,
content: layerList.domNode,
expanded: true
});
// create a collapsable search widget
const searchWidget = new Search({
container: document.createElement("div"),
view: view
});
const searchExpand = new Expand({
expandIconClass: "esri-icon-search",
view: view,
content: searchWidget.domNode
});
view.ui.add([searchExpand, layerListExpand], {position: "top-left", index: 0});
/****************************************************
* Listeners for adding custom data
****************************************************/
// When a user adds a webmap by id
// Load it then zoom to its extent
document.getElementById("map-add").onclick = function(){
const v = document.getElementById("map-id").value;
const id = v ? v : 'f30543457dc444c2ad191fb906fc1cbb';
map = new WebMap({
portalItem: {id: id}
});
view.map = map;
map.when(() => {
view.viewpoint = map.initialViewProperties.viewpoint;
map.remove(polygonGraphicsLayer);
map.add(polygonGraphicsLayer);
});
}
// When a user adds a no-fly feature layer
// Render it and set the no fly zones
document.getElementById("layer-add").onclick = function(){
const v = document.getElementById("lyr-url").value;
const id = v ? v : 'ea9bb8c7cd39460289778f42585a3784';
lyr = new FeatureLayer({
portalItem: {id: id}
});
map.add(lyr);
lyr.queryFeatures()
.then(res => {
if(res.features.length < 1) return;
const noFlyPolyWGS = res.features[0].geometry;
noFlyPoly = webMercatorUtils.geographicToWebMercator(noFlyPolyWGS);
})
.catch(er => console.log(er))
}
/****************************************************
* Initialize the drawing objects
****************************************************/
// create the sketch view model
let sketchViewModel = new SketchViewModel({
view: view,
layer: polygonGraphicsLayer
});
// add a button to initiate drawing
view.ui.add("select-by-polygon", "top-left");
// initiate drawing when the button is clicked
document.getElementById("select-by-polygon").onclick = function(){
polygonGraphicsLayer.removeAll();
drawGraphic = null;
view.popup.close();
sketchViewModel.create("polygon");
}
/****************************************************
* Geometry validation against no fly zones
****************************************************/
// Geometry symbols
createSymbol = (color, style, width, outlineColor) => {
return {
type: "simple-fill",
style: style,
color: color,
outline: {
color: outlineColor,
width: width
}
}
}
const invalidSymbol = createSymbol([255, 0, 0], "diagonal-cross", 4, [255, 0, 0]);
const validSymbol = createSymbol([0, 170, 255, 0.8], "solid", 2, [255, 255, 255]);
onSketchUpdate = drawGraphic => {
setCoords(drawGraphic.geometry.centroid);
const intersects = noFlyPoly && geometryEngine.intersects(noFlyPoly, drawGraphic.geometry);
drawGraphic.symbol = intersects ? invalidSymbol : validSymbol;
}
sketchViewModel.on("create", event => {
if(event.state === "complete") onSketchUpdate(event.graphic)
});
sketchViewModel.on("update", event =>
onSketchUpdate(event.graphics[0])
)
/****************************************************
* Selection logic and functions
****************************************************/
let selectGraphic;
// Set user form values based on geometries
const setText = (name, note) => {
document.getElementById("name-text").value = name;
document.getElementById("note-text").value = note;
}
const coordTextEl = document.getElementById("coord-text");
const setCoords = centroid => {
if(centroid){
lat = centroid.latitude.toFixed(5);
lon = centroid.longitude.toFixed(5);
coordTextEl.innerText = `Lat/Lon: ${lat}/${lon}`;
} else {
coordTextEl.innerText = `Lat/Lon: --`;
}
}
const addrTextEl = document.getElementById("addr-text");
const setAddress = address => {
if(address){
addrTextEl.innerText = `Address: ${address}`;
} else {
addrTextEl.innerText = `Address: --`;
}
}
// Allow user to select displayed polygons through a popup (vs drawing)
view.popup.actions.add({
title: "Select",
id: "select",
className: "esri-icon-map-pin"
});
view.popup.on("trigger-action", event => {
if(event.action.id === 'select'){
selectGraphic = event.target.selectedFeature;
const attrs = selectGraphic.attributes;
const geom = selectGraphic.geometry;
setText(attrs.id, attrs.notes);
setCoords(geom.centroid);
}
});
// Clear the stored data
const clrBtn = document.getElementById('clear-btn');
clrBtn.addEventListener("click", () => {
polygonGraphicsLayer.removeAll();
drawGraphic = null;
selectGraphic = null;
view.popup.close();
setText('', '');
setCoords(null);
setAddress('');
});
// When OK is clicked, geocode and display the location's address
const locator = new Locator({
url: "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"
});
document.getElementById("ok-btn").onclick = function(){
let sendGraphic = drawGraphic ? drawGraphic : selectGraphic;
if(!sendGraphic) return;
const centroid = sendGraphic.geometry.centroid;
locator.locationToAddress({location: centroid})
.then(res => {
setAddress(res.address);
})
.catch(er => console.log(er));
}
});
</script>
</head>
<body>
<header class="top-nav">
<nav class="class-top-nav-list" role="navigation" aria-labelledby="usernav">
<a class="top-nav-title margin-left-1">Selection Demo</a>
<a id="sign-in" class="top-nav-link icon-ui-user margin-right-1 right" href="#">Sign In</a>
</nav>
</header>
<div id="viewDiv"></div>
<div class="esri-widget hide" style="padding: 10px; width: 250px" id="aoi-form">
<h4>Data</h4>
<label>
WebMap ID
<div class="input-group">
<input class="input-group-input" type="text" placeholder="add default: f30543457dc444c2ad191fb906fc1cbb" id="map-id"/>
<span class="input-group-button">
<button class="btn" id="map-add">+</button>
</span>
</div>
</label>
<label>
Restricted Area (Feature Service) ID
<div class="input-group">
<input id="lyr-url" class="input-group-input" type="text" placeholder="add default: ea9bb8c7cd39460289778f42585a3784"/>
<span class="input-group-button">
<button class="btn" id="layer-add">+</button>
</span>
</div>
</label>
<h4 class="clearfix">Task Area</h4>
<label>
Name
<input id="name-text" type="text" placeholder="Location name">
</label>
<label>
Notes
<input id="note-text" type="text" placeholder="Location notes">
</label>
<p id="coord-text">Lat/Lon: --</p>
<p id="addr-text">Address: --</p>
<nav>
<button id="ok-btn" class="btn btn-grouped">Okay</button>
<button id="clear-btn" class="btn btn-grouped btn-clear">Clear</button>
</nav>
</div>
<div id="select-by-polygon" class="esri-widget esri-widget--button esri-widget esri-interactive"
title="Select features by polygon">
<span class="esri-icon-polygon"></span>
</div>
</body>
</html>