-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle1.js
279 lines (252 loc) · 9.48 KB
/
bundle1.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
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/**
* This javascript file creates a route from point A to point B using a MultiPolygon GeoJSON object.
* The MultiPolygon GeoJSON object contains the coordinates of each polygon that represents a buffered region
* around a single point of crime.
* After making changes to this file, run 'browserify main.js -o bundle1.js' in the command line after to see
* the changes on the website.
*/
var circleToPolygon = require('circle-to-polygon');
// The free version of Crimeometer API contains limited data points
// Generated JSON in a similar format to Crimeomter to test the app in the Urbana-Champaign area with police reports
var sampleCrimeData = {
"incidents": [
{
"location": " Motor Vehicle Theft 1600 BLOCK OF N MATTIS AVE",
"incident_latitude": 40.132970,
"incident_longitude": -88.277000
},
{
"location": "Burglary 700 BLOCK OF HAMILTON Dr",
"incident_latitude": 40.100430,
"incident_longitude": -88.253530
},
{
"location": "Mob Action 1000 Block RACE ST S",
"incident_latitude": 40.104110,
"incident_longitude": -88.209590
},
{
"location": "Disorderly Conduct 400 Block VINE ST S",
"incident_latitude": 40.110200,
"incident_longitude": -88.204600
},
{
"location": "Disorderly Conduct 800 Block GREEN ST W",
"incident_latitude": 40.110680,
"incident_longitude": -88.217770
},
{
"location": "Theft 200 Block HARVEY ST",
"incident_latitude": 40.113180,
"incident_longitude": -88.222270
},
{
"location": "RECKLESS DISCHARGE OF A FIREARM COLER AV & FAIRVIEW AV",
"incident_latitude":40.120110,
"incident_longitude": -88.215740
},
{
"location": "Burglary 4000 BLOCK OF TURNBERRY DR",
"incident_latitude": 40.103130,
"incident_longitude": -88.310780
},
{
"location":"Burglary 1200 BLOCK OF S MATTIS AVE",
"incident_latitude": 40.103730,
"incident_longitude":-88.276490
},
{
"location": "Engineering Hall",
"incident_latitude": 40.106499574,
"incident_longitude": -88.222832442
},
{
"location": "Alma Mater",
"incident_latitude": 40.1099,
"incident_longitude": -88.2284
},
{
"location": "Bell Tower",
"incident_latitude": 40.1028,
"incident_longitude": -88.2272
},
{
"location": "ARC",
"incident_latitude": 40.1008,
"incident_longitude": -88.2356
}
]
}
/**
* Uses the Node.js package circle-to-polygon to create a GeoJSON that roughly approximates a circle
* centered at a specific latitude and longitude
* @param circleToPolygon the module loaded by require
* @param coordinates represented by array [longitude, latitude]
* @param radius in meters
* @param numberofEdges polygon resembles more of a circle as this value increases
*/
function createBufferPolygon(latitude, longitude) {
let coordinates = [longitude, latitude];
let radius = 20;
let numberOfEdges = 20;
return circleToPolygon(coordinates, radius, numberOfEdges);
}
// Plots and creates 20 meter buffer around each sampleCrimeData entry and stores in the bufferPolygons MultiPolygon GeoJSON object
var bufferPolygons = {
"type": "MultiPolygon",
"coordinates": []
}
var incidents = sampleCrimeData.incidents;
var bufferPolygons = {
"type": "MultiPolygon",
"coordinates": []
}
for (i = 0; i < incidents.length; i++) {
let latitude = incidents[i].incident_latitude;
let longitude = incidents[i].incident_longitude;
L.circle([latitude, longitude], 20, {
color:'red'
}).addTo(map);
let buffPolyCoordinates = createBufferPolygon(latitude, longitude).coordinates;
bufferPolygons.coordinates.push(buffPolyCoordinates);
}
/**
* Creates 20 meter buffer around each crime point and stores in the bufferPolygons MultiPolygon GeoJSON object
* @param latitudes array of crime point latitudes
* @param longitudes array of crime point longitudes
*/
window.crimePointsToPolygons = function(latitudes, longitudes) {
for(i = 0; i < latitudes.length; i++) {
let latitude = latitudes[i];
let longitude = longitudes[i];
let buffPolyCoordinates = createBufferPolygon(latitude, longitude).coordinates;
bufferPolygons.coordinates.push(buffPolyCoordinates);
}
}
/**
* Creates openrouteservice POST request to construct an FeatureCollection GeoJSON object that represents
* the path from the user-inputed start point to destination that also avoids polygons that represent areas with
* past crime
* @param startLat start point latitude
* @param startLng start point longitude
* @param endLat destination latitude
* @param endLng destination longitude
*/
window.createRoute = function(startLat, startLng, endLat, endLng) {
let request = new XMLHttpRequest();
var url = "https://api.openrouteservice.org/v2/directions/foot-walking/geojson";
request.open('POST', url);
request.setRequestHeader('Accept', 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', '5b3ce3597851110001cf6248b365d96ecb99476a8e3a4ca7d24de70f');
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
//Plot route on map
var route = JSON.parse(this.responseText);
L.geoJSON(route).addTo(map);
}
};
let pathing_restrictions = {
"coordinates": [[startLng, startLat],[endLng, endLat]],
"options":{
"avoid_polygons": bufferPolygons
},
format: 'geojson'
};
const body = JSON.stringify(pathing_restrictions);
request.send(body);
}
},{"circle-to-polygon":2}],2:[function(require,module,exports){
"use strict";
function toRadians(angleInDegrees) {
return (angleInDegrees * Math.PI) / 180;
}
function toDegrees(angleInRadians) {
return (angleInRadians * 180) / Math.PI;
}
function offset(c1, distance, bearing) {
var lat1 = toRadians(c1[1]);
var lon1 = toRadians(c1[0]);
var dByR = distance / 6378137; // distance divided by 6378137 (radius of the earth) wgs84
var lat = Math.asin(
Math.sin(lat1) * Math.cos(dByR) +
Math.cos(lat1) * Math.sin(dByR) * Math.cos(bearing)
);
var lon =
lon1 +
Math.atan2(
Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1),
Math.cos(dByR) - Math.sin(lat1) * Math.sin(lat)
);
return [toDegrees(lon), toDegrees(lat)];
}
function validateCenter(center) {
const validCenterLengths = [2, 3]
if (!Array.isArray(center) || !validCenterLengths.includes(center.length)) {
throw new Error("ERROR! Center has to be an array of length two or three");
}
const [lng, lat] = center;
if (typeof lng !== "number" || typeof lat !== "number") {
throw new Error(
`ERROR! Longitude and Latitude has to be numbers but where ${typeof lng} and ${typeof lat}`
);
}
if (lng > 180 || lng < -180) {
throw new Error(
`ERROR! Longitude has to be between -180 and 180 but was ${lng}`
);
}
if (lat > 90 || lat < -90) {
throw new Error(
`ERROR! Latitude has to be between -90 and 90 but was ${lat}`
);
}
}
function validateRadius(radius) {
if (typeof radius !== "number") {
throw new Error(
`ERROR! Radius has to be a positive number but was: ${typeof radius}`
);
}
if (radius <= 0) {
throw new Error(
`ERROR! Radius has to be a positive number but was: ${radius}`
);
}
}
function validateNumberOfSegments(numberOfSegments) {
if (typeof numberOfSegments !== "number" && numberOfSegments !== undefined) {
throw new Error(
`ERROR! Number of segments has to be a number but was: ${typeof numberOfSegments}`
);
}
if (numberOfSegments < 3) {
throw new Error(
`ERROR! Number of segments has to be at least 3 but was: ${numberOfSegments}`
);
}
}
function validateInput({ center, radius, numberOfSegments }) {
validateCenter(center);
validateRadius(radius);
validateNumberOfSegments(numberOfSegments);
}
module.exports = function circleToPolygon(center, radius, numberOfSegments) {
var n = numberOfSegments ? numberOfSegments : 32;
// validateInput() throws error on invalid input and do nothing on valid input
validateInput({ center, radius, numberOfSegments });
var coordinates = [];
for (var i = 0; i < n; ++i) {
coordinates.push(offset(center, radius, (2 * Math.PI * -i) / n));
}
coordinates.push(coordinates[0]);
return {
type: "Polygon",
coordinates: [coordinates]
};
};
},{}]},{},[1]);