Show labels in a single layer
This commit is contained in:
1 parent
f60b1802d5
commit
e1a1ff4af3
2 files changed
+40
-13
No files matched your search
Vendored
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
\****************/
|
\****************/
|
||||||
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
||||||
|
|
||||||
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var polylabel__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! polylabel */ \"./node_modules/polylabel/polylabel.js\");\n\r\n\r\nfunction normaliseCouncilName(str) {\r\n const regex = /(.*?)(?:(?: Rural)?(?: City| Shire) Council)/g;\r\n const matches = str.matchAll(regex);\r\n\r\n // If we get a match, convert to slug format\r\n for (const match of matches) {\r\n return match[1].toLowerCase().replace(\" \", \"-\");\r\n }\r\n\r\n // If we didn't find any matches, try convert input to slug format\r\n return str.toLowerCase().replace(\" \", \"-\");\r\n};\r\n\r\nconst searchParams = new URLSearchParams(window.location.search);\r\nconst councilName = normaliseCouncilName(searchParams.get(\"council\"));\r\nconsole.log(councilName);\r\n\r\nmapboxgl.accessToken = 'pk.eyJ1IjoibWF0dHl3YXkiLCJhIjoiY2x6eG9vMzZyMHY2cDJqb3M1ODZnNjF4cyJ9.IX8CfYQZUaQhSjWgMXmsEA';\r\nconst map = new mapboxgl.Map({\r\n container: 'map',\r\n zoom: 10,\r\n style: 'mapbox://styles/mattyway/clzy2ozzf004k01pn840h9xdb',\r\n center: [145.00724,-37.79011]\r\n});\r\n\r\nmap.addControl(new mapboxgl.NavigationControl());\r\n\r\nfetch(\"wards_withboundaries.json\")\r\n .then(response => {\r\n response.json()\r\n .then((wardData) => {\r\n const filteredWardData = wardData.filter((ward) => normaliseCouncilName(ward.parentElectorateName) == councilName);\r\n\r\n var bounds = {\r\n \"west\": undefined,\r\n \"south\": undefined,\r\n \"east\": undefined,\r\n \"north\": undefined\r\n }\r\n\r\n filteredWardData.forEach(wardData => {\r\n const featureCollection = {\r\n 'type': 'FeatureCollection',\r\n 'features': [\r\n {\r\n 'type': 'Feature',\r\n 'geometry': JSON.parse(wardData.boundaryJson)\r\n }\r\n ]\r\n };\r\n\r\n featureCollection.features[0].geometry.coordinates[0].forEach(coordinate => {\r\n if (bounds.west == undefined || coordinate[0] < bounds.west) {\r\n bounds.west = coordinate[0];\r\n }\r\n\r\n if (bounds.south == undefined || coordinate[1] < bounds.south) {\r\n bounds.south = coordinate[1];\r\n }\r\n\r\n if (bounds.east == undefined || coordinate[0] > bounds.east) {\r\n bounds.east = coordinate[0];\r\n }\r\n\r\n if (bounds.north == undefined || coordinate[1] > bounds.north) {\r\n bounds.north = coordinate[1];\r\n }\r\n });\r\n\r\n // Add data\r\n map.addSource(\"data_\"+wardData.electorateId, {\r\n 'type': 'geojson',\r\n 'data': featureCollection\r\n });\r\n\r\n // Add a line along the data\r\n map.addLayer({\r\n 'id': \"outline_\"+wardData.electorateId,\r\n 'type': 'line',\r\n 'source': \"data_\"+wardData.electorateId,\r\n 'layout': {},\r\n 'paint': {\r\n 'line-color': '#0899fe',\r\n 'line-width': 3\r\n }\r\n });\r\n\r\n const centrePoint = (0,polylabel__WEBPACK_IMPORTED_MODULE_0__[\"default\"])(featureCollection.features[0].geometry.coordinates, 0.000001);\r\n\r\n map.addSource('center_'+wardData.electorateId, {\r\n 'type': 'geojson',\r\n 'data': {\r\n 'type': 'FeatureCollection',\r\n 'features': [\r\n {\r\n 'type': 'Feature',\r\n 'properties': {\r\n 'description': wardData.electorateName\r\n },\r\n 'geometry': {\r\n 'type': 'Point',\r\n 'coordinates': centrePoint\r\n }\r\n }\r\n ]\r\n }\r\n });\r\n\r\n map.addLayer({\r\n 'id': 'label_'+wardData.electorateId,\r\n 'type': 'syLine truncated
|
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var polylabel__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! polylabel */ \"./node_modules/polylabel/polylabel.js\");\n\r\n\r\nfunction normaliseCouncilName(str) {\r\n const regex = /(.*?)(?:(?: Rural)?(?: City| Shire) Council)/g;\r\n const matches = str.matchAll(regex);\r\n\r\n // If we get a match, convert to slug format\r\n for (const match of matches) {\r\n return match[1].toLowerCase().replace(\" \", \"-\");\r\n }\r\n\r\n // If we didn't find any matches, try convert input to slug format\r\n return str.toLowerCase().replace(\" \", \"-\");\r\n};\r\n\r\nconst searchParams = new URLSearchParams(window.location.search);\r\nconst councilName = normaliseCouncilName(searchParams.get(\"council\"));\r\nconsole.log(councilName);\r\n\r\nmapboxgl.accessToken = 'pk.eyJ1IjoibWF0dHl3YXkiLCJhIjoiY2x6eG9vMzZyMHY2cDJqb3M1ODZnNjF4cyJ9.IX8CfYQZUaQhSjWgMXmsEA';\r\nconst map = new mapboxgl.Map({\r\n container: 'map',\r\n zoom: 10,\r\n style: 'mapbox://styles/mattyway/clzy2ozzf004k01pn840h9xdb',\r\n center: [145.00724,-37.79011]\r\n});\r\n\r\nmap.addControl(new mapboxgl.NavigationControl());\r\n\r\nfetch(\"wards_withboundaries.json\")\r\n .then(response => {\r\n response.json()\r\n .then((wardData) => {\r\n const filteredWardData = wardData.filter((ward) => normaliseCouncilName(ward.parentElectorateName) == councilName);\r\n\r\n var bounds = {\r\n \"west\": undefined,\r\n \"south\": undefined,\r\n \"east\": undefined,\r\n \"north\": undefined\r\n }\r\n\r\n var labelFeatures = [];\r\n\r\n filteredWardData.forEach(wardData => {\r\n const featureCollection = {\r\n 'type': 'FeatureCollection',\r\n 'features': [\r\n {\r\n 'type': 'Feature',\r\n 'geometry': JSON.parse(wardData.boundaryJson)\r\n }\r\n ]\r\n };\r\n\r\n featureCollection.features[0].geometry.coordinates[0].forEach(coordinate => {\r\n if (bounds.west == undefined || coordinate[0] < bounds.west) {\r\n bounds.west = coordinate[0];\r\n }\r\n\r\n if (bounds.south == undefined || coordinate[1] < bounds.south) {\r\n bounds.south = coordinate[1];\r\n }\r\n\r\n if (bounds.east == undefined || coordinate[0] > bounds.east) {\r\n bounds.east = coordinate[0];\r\n }\r\n\r\n if (bounds.north == undefined || coordinate[1] > bounds.north) {\r\n bounds.north = coordinate[1];\r\n }\r\n });\r\n\r\n // Add data\r\n map.addSource(\"data_\"+wardData.electorateId, {\r\n 'type': 'geojson',\r\n 'data': featureCollection\r\n });\r\n\r\n // Add a line along the data\r\n map.addLayer({\r\n 'id': \"outline_\"+wardData.electorateId,\r\n 'type': 'line',\r\n 'source': \"data_\"+wardData.electorateId,\r\n 'layout': {},\r\n 'paint': {\r\n 'line-color': '#0899fe',\r\n 'line-width': 3\r\n }\r\n });\r\n\r\n const centrePoint = (0,polylabel__WEBPACK_IMPORTED_MODULE_0__[\"default\"])(featureCollection.features[0].geometry.coordinates, 0.000001);\r\n\r\n if (wardData.electorateName.includes(' ')) {\r\n // Breaking long names into newlines looks better\r\n const parts = wardData.electorateName.split(' ');\r\n // Special case if a ward starts with \"St\" (like \"St Albans East\")\r\n // Join the first two parts\r\n if (parts[0] == \"St\") {\r\n parts[0] = parts[0] + ' ' + parts[1];\r\n parts.splice(1, 1);\r\n }\r\n const wardNameNewLines = parts.join('\\n');\r\n labelFeatures.push({\r\n 'type': 'Feature',\r\n 'properties': {\r\n 'description': wardNameNewLines\r\n },\r\n 'geometry': {\r\n 'type':
Line truncated
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
|
|
||||||
|
|||||||
+39
-12
@@ -40,6 +40,8 @@ fetch("wards_withboundaries.json")
|
|||||||
"north": undefined
|
"north": undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var labelFeatures = [];
|
||||||
|
|
||||||
filteredWardData.forEach(wardData => {
|
filteredWardData.forEach(wardData => {
|
||||||
const featureCollection = {
|
const featureCollection = {
|
||||||
'type': 'FeatureCollection',
|
'type': 'FeatureCollection',
|
||||||
@@ -89,12 +91,28 @@ fetch("wards_withboundaries.json")
|
|||||||
|
|
||||||
const centrePoint = polylabel(featureCollection.features[0].geometry.coordinates, 0.000001);
|
const centrePoint = polylabel(featureCollection.features[0].geometry.coordinates, 0.000001);
|
||||||
|
|
||||||
map.addSource('center_'+wardData.electorateId, {
|
if (wardData.electorateName.includes(' ')) {
|
||||||
'type': 'geojson',
|
// Breaking long names into newlines looks better
|
||||||
'data': {
|
const parts = wardData.electorateName.split(' ');
|
||||||
'type': 'FeatureCollection',
|
// Special case if a ward starts with "St" (like "St Albans East")
|
||||||
'features': [
|
// Join the first two parts
|
||||||
{
|
if (parts[0] == "St") {
|
||||||
|
parts[0] = parts[0] + ' ' + parts[1];
|
||||||
|
parts.splice(1, 1);
|
||||||
|
}
|
||||||
|
const wardNameNewLines = parts.join('\n');
|
||||||
|
labelFeatures.push({
|
||||||
|
'type': 'Feature',
|
||||||
|
'properties': {
|
||||||
|
'description': wardNameNewLines
|
||||||
|
},
|
||||||
|
'geometry': {
|
||||||
|
'type': 'Point',
|
||||||
|
'coordinates': centrePoint
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
labelFeatures.push({
|
||||||
'type': 'Feature',
|
'type': 'Feature',
|
||||||
'properties': {
|
'properties': {
|
||||||
'description': wardData.electorateName
|
'description': wardData.electorateName
|
||||||
@@ -103,23 +121,32 @@ fetch("wards_withboundaries.json")
|
|||||||
'type': 'Point',
|
'type': 'Point',
|
||||||
'coordinates': centrePoint
|
'coordinates': centrePoint
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
]
|
});
|
||||||
|
|
||||||
|
map.addSource('labels', {
|
||||||
|
'type': 'geojson',
|
||||||
|
'data': {
|
||||||
|
'type': 'FeatureCollection',
|
||||||
|
'features': labelFeatures
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
map.addLayer({
|
map.addLayer({
|
||||||
'id': 'label_'+wardData.electorateId,
|
'id': 'labels',
|
||||||
'type': 'symbol',
|
'type': 'symbol',
|
||||||
'source': 'center_'+wardData.electorateId,
|
'source': 'labels',
|
||||||
'layout': {
|
'layout': {
|
||||||
'text-field': ['get', 'description'],
|
'text-field': ['get', 'description'],
|
||||||
'text-variable-anchor': ['center', 'top', 'bottom'],
|
'text-variable-anchor': ['center', 'top', 'bottom'],
|
||||||
|
'text-radial-offset': 0.5,
|
||||||
|
'text-padding': 0,
|
||||||
'text-justify': 'auto',
|
'text-justify': 'auto',
|
||||||
'text-allow-overlap': true
|
'text-allow-overlap': false,
|
||||||
|
'text-ignore-placement': false,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
map.fitBounds([
|
map.fitBounds([
|
||||||
[bounds.west, bounds.south],
|
[bounds.west, bounds.south],
|
||||||
@@ -132,5 +159,5 @@ fetch("wards_withboundaries.json")
|
|||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
Reference in new issue
Block a user