author Angelo Gladding
name Districts
published 2025-12-25T15:29:04.136115-08:00
type entry
updated 2025-12-25T16:29:51.432839-08:00
url /south-pasadena/districts, /2025/12/25/xq
visibility public
Content
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 | <div id="map" style="width: 100%; height: 70vh"></div> <link href="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css" rel="stylesheet" /> <script src="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js"></script> <script> mapboxgl.accessToken = 'pk.eyJ1IjoicmFndC1hZyIsImEiOiJjbWR3ZjZ5cjkxMmRhMmtwbHVlcDBrN240In0.nCEdq0_XYbjl8jq6YV4p5g' const map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/satellite-streets-v12', center: [-118.15, 34.113], zoom: 13 }) map.addControl(new mapboxgl.NavigationControl()) const DISTRICT_COLORS = { 1: '#b58900', 2: '#cb4b16', 3: '#d33682', 4: '#6c71c4', 5: '#2aa198' } function mercatorToLngLat(x, y) { const originShift = 20037508.342789244 const lon = (x / originShift) * 180.0 let lat = (y / originShift) * 180.0 lat = 180.0 / Math.PI * (2.0 * Math.atan(Math.exp(lat * Math.PI / 180.0)) - Math.PI / 2.0) return [lon, lat] } function esriRingsToGeoJSONPolygon(rings) { const coords = rings.map(function (ring) { return ring.map(function (pt) { return mercatorToLngLat(pt[0], pt[1]) }) }) return { type: 'Polygon', coordinates: coords } } function arcgisFeatureCollectionToGeoJSON(fc) { const layer = fc.layers[0] const features = layer.featureSet && layer.featureSet.features ? layer.featureSet.features : [] return { type: 'FeatureCollection', features: features.map(function (f) { const attrs = f.attributes || {} const district = attrs.District const color = DISTRICT_COLORS[district] || '#6c71c4' return { type: 'Feature', properties: { District: district, _color: color }, geometry: esriRingsToGeoJSONPolygon(f.geometry && f.geometry.rings ? f.geometry.rings : []) } }) } } function bboxOfFeatureCollection(gj) { let minX = 180, minY = 90, maxX = -180, maxY = -90 gj.features.forEach(function (feat) { const geom = feat.geometry if (!geom || geom.type !== 'Polygon') return geom.coordinates.forEach(function (ring) { ring.forEach(function (pt) { const x = pt[0] const y = pt[1] if (x < minX) minX = x if (y < minY) minY = y if (x > maxX) maxX = x if (y > maxY) maxY = y }) }) }) return [[minX, minY], [maxX, maxY]] } async function loadDistricts() { const r = await fetch('/media/kYMm.json') if (!r.ok) throw new Error('Failed to fetch /media/kYMm.json') return await r.json() } map.on('load', async function () { const raw = await loadDistricts() // Your file is an object with a "featureCollection" property const gj = arcgisFeatureCollectionToGeoJSON(raw.featureCollection) map.addSource('districts', { type: 'geojson', data: gj }) map.addLayer({ id: 'districts-fill', type: 'fill', source: 'districts', paint: { 'fill-color': ['get', '_color'], 'fill-opacity': 0.18 } }) map.addLayer({ id: 'districts-outline', type: 'line', source: 'districts', paint: { 'line-color': ['get', '_color'], 'line-width': 6 } }) map.fitBounds(bboxOfFeatureCollection(gj), { padding: 30, duration: 0 }) }) </script> |