/
Load large Shapefiles using a Web Worker.html
179 lines (149 loc) · 8.95 KB
/
Load large Shapefiles using a Web Worker.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Load large Shapefiles using a Web Worker - 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 load large shapefiles (.shp, .dbf, .prj) onto the map using a web worker so that it doesn't freeze the UI." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, ESRI Shapefiles, shapefiles, shp, dbf, OGC, web worker" />
<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>
<!-- Load in Catiline.js library which makes it easier to work with web workers. -->
<script src="/lib/catiline/dist/catiline.min.js"></script>
<script>
var map, datasource, popup, shpWorker;
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Azure Active Directory 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 Azure Active Directory 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 style control and add it to the map.
map.controls.add(new atlas.control.StyleControl(), {
position: 'top-right'
});
//Create a data source to add your data to.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Create a popup.
popup = new atlas.Popup();
//Add a layers for rendering the data.
var layers = [
new atlas.layer.PolygonLayer(datasource, null, {
filter: ['any', ['==', ['geometry-type'], 'Polygon'], ['==', ['geometry-type'], 'MultiPolygon']] //Only render Polygon or MultiPolygon in this layer.
}),
new atlas.layer.LineLayer(datasource, null, {
strokeColor: 'white',
strokeWidth: 2,
filter: ['any', ['==', ['geometry-type'], 'Polygon'], ['==', ['geometry-type'], 'MultiPolygon']] //Only render Polygon or MultiPolygon in this layer.
}),
new atlas.layer.LineLayer(datasource, null, {
strokeColor: 'red',
filter: ['any', ['==', ['geometry-type'], 'LineString'], ['==', ['geometry-type'], 'MultiLineString']] //Only render LineString or MultiLineString in this layer.
}),
new atlas.layer.BubbleLayer(datasource, null, {
filter: ['any', ['==', ['geometry-type'], 'Point'], ['==', ['geometry-type'], 'MultiPoint']] //Only render Point or MultiPoints in this layer.
})
];
map.layers.add(layers, 'labels');
//Add a click event to the layers to show a popup of what the user clicked on.
map.events.add('click', layers, featureClicked);
//Create a web worker that uses the shapefile-js library.
var wfunc = function (base, cb) {
importScripts('/lib/shp.min.js');
shp(base).then(cb);
};
shpWorker = cw({ data: wfunc }, 2);
});
}
function loadShapeFile(url) {
datasource.clear();
popup.close();
document.getElementById('loadingIcon').style.display = '';
shpWorker.data(cw.makeUrl(url)).then(function (data) {
//Load the shapefile into the data source.
datasource.add(data);
//Bring the data into view on the map.
map.setCamera({
bounds: atlas.data.BoundingBox.fromData(data),
padding: 50
});
document.getElementById('loadingIcon').style.display = 'none';
});
}
function featureClicked(e) {
//Make sure the event occurred on a shape feature.
if (e.shapes && e.shapes.length > 0) {
//By default, show the popup where the mouse event occurred.
var pos = e.position;
var offset = [0, 0];
var properties;
if (e.shapes[0] instanceof atlas.Shape) {
properties = e.shapes[0].getProperties();
//If the shape is a point feature, show the popup at the points coordinate.
if (e.shapes[0].getType() === 'Point') {
pos = e.shapes[0].getCoordinates();
offset = [0, -18];
}
} else {
properties = e.shapes[0].properties;
//If the shape is a point feature, show the popup at the points coordinate.
if (e.shapes[0].type === 'Point') {
pos = e.shapes[0].geometry.coordinates;
offset = [0, -18];
}
}
//Update the content and position of the popup.
popup.setOptions({
//Create a table from the properties in the feature.
content: atlas.PopupTemplate.applyTemplate(properties),
position: pos,
pixelOffset: offset
});
//Open the popup.
popup.open(map);
}
}
</script>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div style="position:absolute;top:10px;left:10px;background-color:white;border-radius:5px;padding:5px;">
Load Shapefile:<br />
<input type="button" onclick="loadShapeFile('/data/shp/Electric_Power_Transmission_Lines.zip')" value="US Electrical Transmission lines (140MB)" /><br />
<input type="button" onclick="loadShapeFile('/data/shp/plzzip5.zip')" value="German Postal Codes (17MB)" />
</div>
<img id="loadingIcon" src="/images/loadingIcon.gif" title="Loading" style="position:absolute;left:calc(50% - 25px);top:250px;display:none;" />
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Load large Shapefiles using a Web Worker</legend>
This sample shows how to load large shapefiles (.shp, .dbf, .prj) onto the map using a web worker so that it doesn't freeze the UI.
This sample uses the open source <a href="https://github.com/calvinmetcalf/shapefile-js" target="_blank">Shapefile.js</a> library which converts Shapefiles into GeoJSON which can easily be consumed by the map control.
It also makes use of the open source <a href="http://catilinejs.com/">Catiline.js</a> library which makes working with web workers easier.
<br />
<br />
<b>Note:</b> Not all file and mime types are enabled in all servers. If using .NET, it is recommended to add the following to the web.config file:
<br />
<br />
<system.webServer><br /> <staticContent><br /> <mimeMap fileExtension=".shp" mimeType="application/octet-stream" /><br /> <mimeMap fileExtension=".dbf" mimeType="application/octet-stream" /><br /> <mimeMap fileExtension=".prj" mimeType="text/plain" /><br /> </staticContent><br /><system.webServer>
</fieldset>
</body>
</html>