Skip to content

Commit

Permalink
Filter by area
Browse files Browse the repository at this point in the history
  • Loading branch information
nofurtherinformation committed Nov 21, 2024
1 parent a4c0dc0 commit e632dca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
16 changes: 16 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@sentry/nextjs": "^8.26.0",
"@stitches/react": "^1.2.8",
"@tanstack/react-query": "^5.51.11",
"@turf/area": "^7.1.0",
"@turf/bbox": "^7.1.0",
"@turf/center-of-mass": "^7.1.0",
"@turf/dissolve": "^7.1.0",
Expand Down
23 changes: 20 additions & 3 deletions app/src/app/constants/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {MapStore, useMapStore} from '../store/mapStore';
import {colorScheme} from './colors';
import {dissolve} from '@turf/dissolve';
import {centerOfMass} from '@turf/center-of-mass';
import {area} from '@turf/area'
import { debounce } from 'lodash';
import { NullableZone } from './types';

export const BLOCK_SOURCE_ID = 'blocks';
export const BLOCK_LAYER_ID = 'blocks';
Expand Down Expand Up @@ -358,6 +360,20 @@ const getDissolved = () => {
propertyName: 'zone',
}
);
let largestDissolvedFeatures: Record<number, {feature: GeoJSON.Feature, area: number}> = {}

dissolved.features.forEach(feature => {
const zone = feature.properties?.zone
if (!zone) return
const featureArea = area(feature)
if (!largestDissolvedFeatures[zone] || featureArea > largestDissolvedFeatures[zone].area){
largestDissolvedFeatures[zone] = {
area: featureArea,
feature
}
}
})
const cleanDissolvedFeautres = Object.values(largestDissolvedFeatures).map(f => f.feature)
// dissolved.features = dissolved.features.map(f => ({
// ...f,
// properties: {
Expand All @@ -367,7 +383,7 @@ const getDissolved = () => {

const centroids: GeoJSON.FeatureCollection = {
type: 'FeatureCollection',
features: dissolved.features.map(f => ({
features: cleanDissolvedFeautres.map(f => ({
type: 'Feature',
properties: {
zone: +f.properties?.zone,
Expand All @@ -378,7 +394,7 @@ const getDissolved = () => {

return {
centroids,
dissolved,
dissolved: cleanDissolvedFeautres
};
};
const removeZoneMetaLayers = () => {
Expand All @@ -400,10 +416,10 @@ const addZoneMetaLayers = ({
centroids?: GeoJSON.FeatureCollection;
dissolved?: GeoJSON.FeatureCollection;
}) => {
const t0 = performance.now()
const geoms = centroids && dissolved ? {
centroids, dissolved
} : getDissolved()

const {getMapRef} = useMapStore.getState();
const mapRef = getMapRef();
if (!mapRef || !geoms) return;
Expand Down Expand Up @@ -462,6 +478,7 @@ const addZoneMetaLayers = ({
'text-color': ZONE_LABEL_STYLE || '#000',
},
});
console.log("!!!ADDED NUMERIC LAYERS IN", performance.now() - t0)
};

const debouncedAddZoneMetaLayers = debounce(addZoneMetaLayers, 250)
Expand Down

0 comments on commit e632dca

Please sign in to comment.