forked from huttj/high-ground-alert
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
269 lines (227 loc) · 8.2 KB
/
Copy pathapp.js
File metadata and controls
269 lines (227 loc) · 8.2 KB
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
(function() {
L.Icon.Default.imagePath = '/node_modules/leaflet/dist/images';
// The map
var leaf = null;
// The mudflow polygon
var mudflow;
// The official evacuation points and routes
var evacPointsJson;
var evacRoutesJson;
// Geocoder to turn address into coords
var geocoder = new google.maps.Geocoder();
// For icon clicks and hovers
var clicked, timeout;
var markers = new L.FeatureGroup();
function setPosition(position) {
var coords = new L.LatLng(position.latitude, position.longitude);
try {
if (leaf) {
// Remove previous markers
leaf.removeLayer(markers);
// Make new markers
markers = new L.FeatureGroup();
// Make and append the marker
var marker = L.marker(coords);
markers.addLayer(marker);
leaf.addLayer(markers);
leaf.setView(coords, leaf.getZoom());
if (inADangerZone(position)) {
marker
.bindPopup('You are in a danger zone!')
.openPopup();
} else {
marker
.bindPopup('You are safe.')
.openPopup();
}
}
} catch(e) {
console.warn(e);
}
}
function inADangerZone(position) {
var point = {"type":"Point","coordinates":[position.longitude, position.latitude]};
var polygons = mudflow.features;
for (var i = 0; i < polygons.length; i++) {
console.log('checking polygon', i+1);
if (gju.pointInPolygon(point, polygons[i].geometry)) {
findNearestEvacPoint(point);
return true;
}
}
return false;
}
function findNearestEvacPoint(point) {
var userLocation = {"type":"Point","coordinates":[position.longitude, position.latitude]};
var evacPoints = evacPointsJson.features;
var nearestPoint = null;
var nearestDistance = Number.POSITIVE_INFINITY;
for (var mp = 0; mp < multiPoints.length; mp++) {
console.log('checking multiPoint: %d', mp+1);
var coordinates = evacPoints[mp].geometry.coordinates;
for (var c = 0; c < coordinates.length; c++) {
var currentPoint = {type: 'Point', coordinates:coordinates[c]};
var distance = gju.pointDistance(userLocation, currentPoint);
console.log('distance: %d calculated to point: %d', distance, c+1);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestPoint = currentPoint;
}
}
}
return nearestPoint;
}
var tiles = {
standard : 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
humanitarian : 'http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
watercolor : 'http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg',
sketchy : 'https://{s}.tiles.mapbox.com/v3/aj.Sketchy2/{z}/{x}/{y}.png',
cartodb : 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'
};
var Map = React.createClass({
getInitialState: function() {
return {
tile: tiles.cartodb
};
},
componentDidMount: function() {
// leaf = L.map('map').setView([1,1], 16);
// this.setState({
// map: leaf
// });
//
leaf = L.map('map', {
center: [46.823873, -121.693061],
zoom: 9
});
leaf.locate({
setView: true
});
L.tileLayer(this.state.tile, {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(leaf);
$.getJSON('/mudflow.json').then(function (json) {
mudflow = json;
L.geoJson(json, {
style: function (feature) {
return {
color: 'red'
};
}
}).addTo(leaf);
});
$.getJSON('/full-evac-routes.json').then(function (json) {
evacRoutesJson = json;
L.geoJson(json, {
style: function (feature) {
return {
color: 'blue'
};
}
}).addTo(leaf);
});
$.getJSON('/evac_rtes_case1_intersect_pts_wgs84.json').then(function (json) {
evacPointsJson = json;
L.geoJson(json, {
style: function (feature) {
return {
color: 'green'
};
}
}).addTo(leaf);
});
},
render: function() {
return (
// Keep opened user selected
<div id="map"></div>
);
}
});
function attachListenersToLayer(featureLayer) {
featureLayer.on('mouseover', function(e){
if (!clicked) {
clearTimeout(timeout);
e.layer.openPopup();
}
});
featureLayer.on('mouseout', function (e) {
if (!clicked) {
//timeout = setTimeout(function() {
e.layer.closePopup();
//}, 500);
}
});
featureLayer.on('click', function (e) {
if (!clicked) {
//clearTimeout(timeout);
clicked = true;
e.layer.openPopup();
} else {
clicked = false;
e.layer.closePopup();
}
});
featureLayer.on('popupclose', function(e) {
clicked = false;
});
}
var Search = React.createClass({
getInitialState: function() {
return {
address: ''
};
},
searchAddress: function(e) {
e.preventDefault();
if (!this.state.address || this.state.address.length < 3) return alert('Please enter a valid address!');
geocoder.geocode({ 'address': this.state.address }, handleGeocode);
function handleGeocode(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var position = {
latitude: results[0].geometry.location.lat(),
longitude: results[0].geometry.location.lng()
};
setPosition(position);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
}
},
detectLocation: function(e) {
e.preventDefault();
var watch = navigator.geolocation.watchPosition(success, failure);
function success(position) {
setPosition(position.coords);
navigator.geolocation.clearWatch(watch);
}
function failure(error) {
alert(error);
}
},
updateAddress: function(e, val) {
this.setState({ address: e.target.value });
},
render: function() {
return (
<form className="search" onSubmit={this.searchAddress}>
<input placeholder="Street Address" onChange={this.updateAddress} value={this.state.address} />
<button onClick={this.searchAddress}>Search</button>
<button onClick={this.detectLocation}>Detect</button>
</form>
);
}
});
var Page = React.createClass({
render: function() {
return (
<div>
<Map />
<Search />
</div>
);
}
});
React.render(<Page />, document.getElementById('main'));
})();