-
Notifications
You must be signed in to change notification settings - Fork 219
A transform that uses @mapbox/polylabel to derive “pois” #2098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {geoCentroid as GeoCentroid} from "d3"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {geoCentroid as GeoCentroid, geoPath, greatest, polygonArea, polygonContains} from "d3"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {memoize1} from "../memoize.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {identity, valueof} from "../options.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {initializer} from "./basic.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import polylabel from "polylabel"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function centroid({geometry = identity, ...options} = {}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getG = memoize1((data) => valueof(data, geometry)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,6 +26,55 @@ export function centroid({geometry = identity, ...options} = {}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function poi({geometry = identity, ...options} = {}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getG = memoize1((data) => valueof(data, geometry)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return initializer( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {...options, x: null, y: null, geometry: {transform: getG}}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (data, facets, channels, scales, dimensions, {projection}) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const G = getG(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const n = G.length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const X = new Float64Array(n); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const Y = new Float64Array(n); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let polygons, holes, ring; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const alpha = 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const context = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arc() {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| moveTo(x, y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ring = [[x, -alpha * y]]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lineTo(x, y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ring.push([x, -alpha * y]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| closePath() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ring.push(ring[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (polygonArea(ring) > 0) polygons.push([ring]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else holes.push(ring); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = geoPath(projection, context); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+54
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested to work with #2252:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = 0; i < n; ++i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| polygons = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| holes = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path(G[i]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const h of holes) polygons.find(([ring]) => polygonContains(ring, h[0]))?.push(h); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const a = greatest( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| polygons.map((d) => polylabel(d)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (d) => d.distance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [X[i], Y[i]] = a ? [a[0], -a[1] / alpha] : path.centroid(G[i]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| facets, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| channels: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x: {value: X, scale: projection == null ? "x" : null, source: null}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y: {value: Y, scale: projection == null ? "y" : null, source: null} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+69
to
+72
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
also after #2252 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function geoCentroid({geometry = identity, ...options} = {}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getG = memoize1((data) => valueof(data, geometry)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getC = memoize1((data) => valueof(getG(data), GeoCentroid)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After #2252