Skip to content

Commit

Permalink
revise county labels
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudLun committed Dec 14, 2023
1 parent a4beda3 commit af16786
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
49 changes: 40 additions & 9 deletions components/Geopanel/Geopanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as turf from "@turf/turf";

import assembly from "../../public/assembly.geo.json"
import senate from "../../public/senate.geo.json"
import counties from "../../public/nys_counties.geo.json"

import assemblyOverlapped from "../../public/assembly_overlapping_boundaries.json"
import senateOverlapped from "../../public/senate_overlapping_boundaries.json"
Expand All @@ -20,6 +21,10 @@ import { EventData, MapMouseEvent } from 'mapbox-gl';
const Geopanel = () => {

const { map, districts, setDistricts, legislations, mapClickHandler, panelShown, defaultMapHandler, selectedDistrictFeatures, setSelectedDistrictFeatures, selectedDistrictOverlappedData, setSelectedDistrictOverlappedData } = useContext(MapContext) as MapContextType

/* @ts-ignore */
const countiesFeatures = (counties as GeoJson).features

const districtBtnClickHandler = (e: MouseEvent<HTMLElement>, district: Districts) => {
const selectedDistrict = (e.target as HTMLElement).innerText
/* @ts-ignore */
Expand Down Expand Up @@ -104,25 +109,51 @@ const Geopanel = () => {
map?.moveLayer('districts_outline', "zipcodes")
map?.moveLayer('districts_clicked_outline', "zipcodes")

}

4
const countyMouseEnterHandler = (e: MouseEvent<HTMLElement>) => {
const selectedCounty = (e.target as HTMLElement).innerText

const hoveredCountyData = {
/* @ts-ignore */
features: (counties as GeoJson).features.filter((d, i) => d.properties.name === selectedCounty + " County")
}


}
let coordinatesArray = hoveredCountyData.features[0].geometry.coordinates[0]
while (coordinatesArray.length === 1) coordinatesArray = coordinatesArray[0]
const targetPolygon = turf.polygon([coordinatesArray])
/* @ts-ignore */
const targetCentroid = turf.center(targetPolygon).geometry.coordinates
const labelData = {
'type': 'FeatureCollection',
'features': [
{
"type": "Feature",
"properties": {
"label": hoveredCountyData.features[0].properties.name,
},
"geometry": {
'type': 'Point',
'coordinates': targetCentroid
}
}
]
}

/* @ts-ignore */
map?.getSource("counties_label").setData({
type: "FeatureCollection",
features: labelData.features as GeoJson["features"]
})

const countyMouseEnterHandler = (e: MouseEvent<HTMLElement>) => {
const selectedCounty = (e.target as HTMLElement).innerText
map?.setPaintProperty("counties_borders", "fill-opacity", [
"case",
['all', ['==', ['get', "name"], selectedCounty + " County"]],
.7, 0
])
map?.setPaintProperty("counties_labels", "text-opacity", [
"case",
['all', ['==', ['get', "name"], selectedCounty + " County"]],
1, 0
])
map?.setPaintProperty("counties_labels", "text-opacity", 1)

map?.moveLayer("background", "counties_borders")
map?.moveLayer("districts", "counties_borders")
map?.moveLayer('pattern', "counties_borders")
Expand Down
34 changes: 34 additions & 0 deletions components/Map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { MapContext, MapContextType } from "../../context/MapContext";
import mapboxgl, { EventData, MapMouseEvent } from 'mapbox-gl';
import { GeoJSONSource } from 'mapbox-gl';



import assembly from "../../public/assembly.geo.json"
import senate from "../../public/senate.geo.json"
import member from "../../public/rtc_members.geo.json"



import Legend from "./Legend";
import MapLayers from "./MapLayers";
import Geopanel from "../Geopanel/Geopanel";
Expand All @@ -34,6 +37,7 @@ const Map = () => {
/* @ts-ignore */
const memberFeatures = (member as GeoJson).features


const [lng, setLng] = useState(-78.5);
const [lat, setLat] = useState(43.05);
const [zoom, setZoom] = useState(-6.25);
Expand Down Expand Up @@ -123,6 +127,14 @@ const Map = () => {
},
})

m.addSource("counties_label", {
type:"geojson",
data: {
type:"FeatureCollection",
features:[]
}
})

m.addSource("members", {
type: "geojson",
data: {
Expand Down Expand Up @@ -219,6 +231,8 @@ const Map = () => {
}
});



m.addLayer({
'id': 'members',
'type': 'circle',
Expand Down Expand Up @@ -313,6 +327,26 @@ const Map = () => {
},
});

m.addLayer({
id: "counties_labels",
type: 'symbol',
source: 'counties_label',
layout: {
'text-field': ['get', 'label'],
'text-justify': 'auto',
'text-size': 12,
'text-variable-anchor': ['top', 'bottom', 'left', 'right'],
'text-font': ["Arial Unicode MS Bold"],
"text-offset": [0, -1.5]
},
paint: {
'text-opacity': 0,
"text-color": "white",
'text-halo-color': 'black',
"text-halo-width": .6
}
})

m.addLayer({
id: "districts_hovered_label",
type: 'symbol',
Expand Down

0 comments on commit af16786

Please sign in to comment.