-
Notifications
You must be signed in to change notification settings - Fork 447
/
Voronoi diagram analysis.html
203 lines (167 loc) · 8.78 KB
/
Voronoi diagram analysis.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Voronoi diagram analysis - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to add calculate a Voronoi diagram from a set of points, and then spatially join the Voronoi polygons with a secondary set of points to calculate aggregates. " />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, spatial analysis, spatial join, Voronoi, Voronoi diagram, within, intersects, intersection" />
<meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.jpg" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
<!-- Add reference to the Azure Maps Spatial IO module. -->
<script src="https://atlas.microsoft.com/sdk/javascript/spatial/0/atlas-spatial.min.js"></script>
<!-- Load turf.js a spatial math library. https://turfjs.org/ -->
<script src='/lib/turf.min.js'></script>
<script>
var map, datasource, voronoiLayer;
var policeStationUrl = '/data/SpatialCSV/Chicago_Police_Stations.csv';
var crimeDataUrl = '/data/SpatialCSV/Chicago_Narcotics.csv';
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
style: 'grayscale_light',
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Microsoft Entra ID authentication.
authType: 'anonymous',
clientId: 'e6b6ab59-eb5d-4d25-aa57-581135b927f0', //Your Azure Maps client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Microsoft Entra ID Token.
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsToken';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup();
//Create a data source ad add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Add a simple data layer for rendering the data.
voronoiLayer = new atlas.layer.PolygonLayer(datasource, null, {
fillColor: [
'step',
['get', 'totalCrimes'],
'transparent',
1, '#f0f9e8',
1000, '#bae4bc',
2000, '#7bccc4',
3000, '#43a2ca',
4000, '#0868ac'
],
fillOpacity: 0.8
});
map.layers.add(voronoiLayer, 'labels');
//Add a click event to the layers.
map.events.add('click', voronoiLayer, showPopup);
map.layers.add(new atlas.layer.LineLayer(datasource, null, {
strokeColor: 'black'
}));
//Add a bubble layer for displaying police stations.
map.layers.add(new atlas.layer.BubbleLayer(datasource, null, {
createIndicators: true, // to enable bubble layer a11y feature
filter: ['==', ['geometry-type'], 'Point']
}));
document.getElementById('statusPanel').innerHTML = '<br/>Downloading data...';
//Download the police station and crime data in parrallel.
Promise.all([
atlas.io.read(policeStationUrl),
atlas.io.read(crimeDataUrl)
]).then(function (values) {
var stations = values[0];
var crimes = values[1];
//Update map view to display the area of crime data.
map.setCamera({
bounds: crimes.bbox
});
//Add the police station data to the map.
datasource.add(stations);
//Calculate the voronoi polygons from the station data. Pass in the bounding box to clip the voronoi diagram.
var voronoi = turf.voronoi(stations, { bbox: crimes.bbox });
//Loop through the voronoi polygons and aggregate the crime data within them.
for (var i = 0, len = voronoi.features.length; i < len; i++) {
//Copy the properties of the police station to the voronoi polygon.
var s = turf.pointsWithinPolygon(stations, voronoi.features[i]);
voronoi.features[i].properties = s.features[0].properties;
//Spatially join the crime data with each voronoi polygon, then use the total number of crimes as the aggregate.
var ptsWithin = turf.pointsWithinPolygon(crimes, voronoi.features[i]);
voronoi.features[i].properties.totalCrimes = ptsWithin.features.length;
}
//Add the voronoi polygon data to the map.
datasource.add(voronoi);
document.getElementById('statusPanel').innerHTML = '';
});
});
}
function showPopup(e) {
if (e.shapes && e.shapes.length > 0) {
var properties = e.shapes[0].getProperties();
popup.setOptions({
//Update the content of the popup.
content: atlas.PopupTemplate.applyTemplate(properties, {
title: '{totalCrimes} crimes'
}),
//Update the position of the popup.
position: (e.shapes[0].getType() === 'Point') ? e.shapes[0].getCoordinates() : e.position
});
//Open the popup.
popup.open(map);
}
}
</script>
<style>
#myMap {
position: relative;
width: 100%;
min-width: 290px;
height: 600px;
}
.legend {
font-family: Arial;
font-size: 12px;
position: absolute;
top: 480px;
background-color: #fff;
padding: 5px;
border-radius: 5px;
}
.legend i {
width: 12px;
height: 12px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
</style>
</head>
<body onload="getMap()">
<div id="myMap"></div>
<div class="legend" style="left: 15px;">
<b>Number of Crimes</b>
<table>
<tr><td><i style='background:#0868ac' /></td><td>2000+</td></tr>
<tr><td><i style='background:#43a2ca' /></td><td>1500 - 2000</td></tr>
<tr><td><i style='background:#7bccc4' /></td><td>1000 - 1500</td></tr>
<tr><td><i style='background:#bae4bc' /></td><td>500 - 1000</td></tr>
<tr><td><i style='background:#f0f9e8' /></td><td>1 - 500</td></tr>
</table>
<div id="statusPanel"></div>
</div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Voronoi diagram analysis</legend>
This sample shows how to add calculate a Voronoi diagram from a set of points, and then spatially join the Voronoi polygons with a secondary set of points to calculate aggregates.
This sample generates a Voronoi diagram from police stations in Chicago, and then joins it with the location of crimes.
This sample uses the open source <a href="https://turfjs.org/" target="_blank">Turf.js</a> library to for some of the spatial calculation.
</fieldset>
</body>
</html>