-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathBuildings.html
More file actions
230 lines (198 loc) · 8 KB
/
Buildings.html
File metadata and controls
230 lines (198 loc) · 8 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>PMTiles Buildings - 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 leverage PMTiles as custom protocol to create data layers using Overture building dataset." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, pmtiles, pmtile, Overture, building, polygon extrusions, 3d buildings" />
<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 references to the PMTiles lib. -->
<script src="https://unpkg.com/pmtiles@3.0.5/dist/pmtiles.js"></script>
<style>
#map {
width: 100%;
height: 600px;
}
#legend {
position: absolute;
top: 5px;
left: 5px;
font-family: Arial;
font-size: 12px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 5px;
padding: 5px;
line-height:18px;
}
#legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
#info {
margin-top: 15px;
}
.atlas-map .azure-maps-control-popup-template-content {
overflow-x: hidden;
}
.atlas-map .azure-maps-control-popup-template {
padding: 20px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="legend">Subtype<br/>
<i style="background:wheat"></i>Agricultural<br/>
<i style="background:teal"></i>Civic<br/>
<i style="background:blue"></i>Commercial<br/>
<i style="background:aqua"></i>Education<br/>
<i style="background:pink"></i>Entertainment<br/>
<i style="background:yellow"></i>Industrial<br/>
<i style="background:red"></i>Medical<br/>
<i style="background:darkgreen"></i>Military<br/>
<i style="background:white"></i>Outbuilding<br/>
<i style="background:khaki"></i>Religious<br/>
<i style="background:green"></i>Residential<br/>
<i style="background:gold"></i>Service<br/>
<i style="background:orange"></i>Transportation<br/>
</div>
<fieldset id="info">
<legend>PMTiles - Buildings</legend>
This sample showcases the use of <a href="https://protomaps.com/" target="_blank">Protomaps</a> to display <a href="https://overturemaps.org/" target="_blank">Overture</a> buildings data on a map.
The data is stored in a PMTiles file that contains a vector tileset of buildings data. The data is filtered by the building subtype and displayed as polygons on the map.
Click on a polygon to view more information about the building.
</fieldset>
<script>
var map, layer, popup;
//Initialize the plugin.
const protocol = new pmtiles.Protocol();
atlas.addProtocol("pmtiles", (request) => {
return new Promise((resolve, reject) => {
const callback = (err, data) => {
if (err) {
reject(err);
} else {
resolve({ data });
}
};
protocol.tile(request, callback);
});
});
const PMTILES_URL = "https://overturemaps-tiles-us-west-2-beta.s3.amazonaws.com/2024-07-22/buildings.pmtiles";
protocol.add(new pmtiles.PMTiles(PMTILES_URL));
//Initialize the map.
map = new atlas.Map("map", {
//Add authentication details for connecting to Azure Maps.
authOptions: {
// Use SAS token for authentication
authType: 'sas',
getToken: function (resolve, reject, map) {
// URL to your authentication service that retrieves a SAS Token
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
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]'
},
style: "night",
center: [-73.97721, 40.76982],
bearing: 162,
pitch: 60,
zoom: 14,
});
//Wait until the map resources are ready.
map.events.add("ready", function () {
//Add the source to the map.
map.sources.add(
new atlas.source.VectorTileSource("pmtiles", {
type: "vector",
url: `pmtiles://${PMTILES_URL}`,
})
);
//Create a polygon extrusion layer.
layer = new atlas.layer.PolygonExtrusionLayer(
"pmtiles",
"building",
{
sourceLayer: "building",
height: ["get", "height"],
fillOpacity: 0.5,
fillColor: [
"match",
["get", "subtype"],
"agricultural", "wheat",
"civic", "teal",
"commercial", "blue",
"education", "aqua",
"entertainment", "pink",
"industrial", "yellow",
"medical", "red",
"military", "darkgreen",
"outbuilding", "white",
"religious", "khaki",
"residential", "green",
"service", "gold",
"transportation", "orange",
//Default color.
"grey"
],
filter: ['any', ['==', ['geometry-type'], 'Polygon'], ['==', ['geometry-type'], 'MultiPolygon']]
}
);
//Add a click event to the layer.
map.events.add("click", layer, featureClicked);
//Add the layer to the map.
map.layers.add([layer]);
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup({ position: [0, 0] });
//Add map controls.
map.controls.add([
new atlas.control.ZoomControl(),
new atlas.control.PitchControl(),
new atlas.control.CompassControl(),
new atlas.control.StyleControl({
mapStyles: ['road', 'satellite', 'satellite_road_labels', 'night', 'grayscale_light', 'grayscale_dark', 'road_shaded_relief', 'high_contrast_light', 'high_contrast_dark']
})
], {
position: 'top-right'
});
});
map.events.add("stylechanged", function (e) {
//Hide the default buildings layer.
map._getMap().setLayoutProperty("microsoft.bing.maps.buildings.buildings", "visibility", "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 = e.shapes[0].properties;
//Parse the JSON properties.
properties.names = properties.names ? JSON.parse(properties.names) : undefined;
properties.sources = properties.sources ? JSON.parse(properties.sources) : undefined;
//Update the content and position of the popup.
popup.setOptions({
//Create a table from the properties in the feature.
content: atlas.PopupTemplate.applyTemplate(properties, { sandboxContent: false }),
position: pos,
pixelOffset: offset
});
//Open the popup.
popup.open(map);
}
}
</script>
</body>
</html>