Skip to content

Commit

Permalink
adopt the usual API for clipPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 23, 2024
1 parent b4f3646 commit 4f33498
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ const projection = d3.geoEquirectangular()

## API Reference

<a name="geoClipPolygon" href="#geoClipPolygon">#</a> d3.<b>geoClipPolygon</b>(<i>polygon</i>[, <i>options</i>]) · [Source](https://github.com/d3/d3-geo-polygon/blob/main/src/clip/polygon.js), [Examples](https://observablehq.com/@mbostock/spherical-clipping)
<a name="geoClipPolygon" href="#geoClipPolygon">#</a> d3.<b>geoClipPolygon</b>(<i>polygon</i>) · [Source](https://github.com/d3/d3-geo-polygon/blob/main/src/clip/polygon.js), [Examples](https://observablehq.com/@mbostock/spherical-clipping)

Given a GeoJSON *polygon* or *multipolygon*, returns a clip function suitable for [_projection_.preclip](https://github.com/d3/d3-geo#preclip). The *options* argument is an optional object with (currently) one key: **clipPoint**. Set it to false to avoid clipping points.
Given a GeoJSON *polygon* or *multipolygon*, returns a clip function suitable for [_projection_.preclip](https://github.com/d3/d3-geo#preclip).

<a name="polygon" href="#polygon">#</a> clip.<b>polygon</b>()
<a name="polygon" href="#polygon">#</a> clip.<b>polygon</b>([<i>geometry</i>])

Given a clipPolygon function, returns the GeoJSON polygon.
If <i>geometry</i> is specified, sets the clipping polygon to the geometry and returns a new <i>clip</i> function. Otherwise returns the clipping polygon.

<a name="polygon" href="#clipPoint">#</a> clip.<b>clipPoint</b>([<i>clipPoint</i>])

Whether the projection should clip points. If <i>clipPoint</i> is false, the clip function only clips line and polygon geometries. If <i>clipPoint</i> is true, points outside the clipping polygon are not projected. Typically set to false when the projection covers the whole sphere, to make sure that all points —even those on the edge of the clipping polygon— get projected.

<a name="geoIntersectArc" href="#geoIntersectArc">#</a> d3.<b>geoIntersectArc</b>(<i>arcs</i>) · [Source](https://github.com/d3/d3-geo-polygon/blob/main/src/intersect.js), [Examples](https://observablehq.com/@fil/spherical-intersection)

Expand All @@ -58,7 +62,7 @@ d3-geo-polygon adds polygon clipping to the polyhedral and interrupted projectio

Defines a new polyhedral projection. The *tree* is a spanning tree of polygon face nodes; each *node* is assigned a *node*.transform matrix. The *face* function returns the appropriate *node* for a given *lambda* and *phi* in radians.

Polyhedral projections’ default **clipPoint** option depends on whether the clipping polygon covers the whole sphere. When the polygon’s area is almost complete (larger than 4π minus .1 steradian), clipPoint is set to false, and all point geometries are displayed, even if they (technically) fall outside the clipping polygon. For smaller polygons, clipPoint defaults to true, thus hiding points outside the clipping region.
Polyhedral projections’ default **clipPoint** depends on whether the clipping polygon covers the whole sphere. When the polygon’s area is almost complete (larger than 4π minus .1 steradian), clipPoint is set to false, and all point geometries are displayed, even if they (technically) fall outside the clipping polygon. For smaller polygons, clipPoint defaults to true, thus hiding points outside the clipping region.

<a href="#geoPolyhedral_tree" name="geoPolyhedral_tree">#</a> <i>polyhedral</i>.<b>tree</b>() returns the spanning tree of the polyhedron, from which one can infer the faces’ centers, polygons, shared edges etc.

Expand Down
9 changes: 6 additions & 3 deletions src/clip/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import polygonContains from "../polygonContains.js";
const clipNone = (stream) => stream;

// clipPolygon
export default function (geometry, options) {
export default function (geometry) {
let clipPoint = true;

function clipGeometry(geometry) {
if (geometry.type === "Polygon") geometry = {type: "MultiPolygon", coordinates: [geometry.coordinates]};
if (geometry.type !== "MultiPolygon") return clipNone;
Expand All @@ -21,7 +23,7 @@ export default function (geometry, options) {
interpolate(segments, polygon),
polygon[0][0],
clipPolygonSort,
options
{clipPoint}
);
});

Expand Down Expand Up @@ -49,7 +51,8 @@ export default function (geometry, options) {
};
}

clipPolygon.polygon = (_) => _ ? ((geometry = _), clipGeometry(geometry)) : geometry;
clipPolygon.polygon = (_) => _ !== undefined ? clipGeometry(geometry = _) : geometry;
clipPolygon.clipPoint = (_) => _ !== undefined ? ((clipPoint = !!_), clipGeometry(geometry)) : clipPoint;

return clipPolygon;
}
Expand Down
2 changes: 1 addition & 1 deletion src/polyhedral/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default function(tree, face) {
const geometry = {type: "MultiPolygon", coordinates: [[p]]};
outline({point: (lambda, phi) => p.push([lambda, phi])}, tree);
p.push(p[0]);
proj.preclip(clipPolygon(geometry, {clipPoint: geoArea(geometry) < 4 * Math.PI - 0.1}));
proj.preclip(clipPolygon(geometry).clipPoint(geoArea(geometry) < 4 * Math.PI - 0.1));
proj.tree = function() { return tree; };

return proj;
Expand Down
4 changes: 2 additions & 2 deletions test/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export async function clipPointWorld() {
export async function clipPointTrue() {
const projection = geoRhombic();
const polygon = projection.preclip().polygon();
projection.preclip(geoClipPolygon(polygon, {clipPoint: true}));
projection.preclip(geoClipPolygon(polygon).clipPoint(true));
return renderWorld(projection, {points: [
[0, 0],
[10 - 0.0001, 0],
Expand All @@ -322,7 +322,7 @@ export async function clipPointSmall() {
export async function clipPointFalse() {
const projection = geoRhombic();
projection.preclip(geoClipPolygon(
geoRhombic().parents([-1, 0, 6, 2, 1, 9, 11, 3, 4, 8, 6, 10]).preclip().polygon(), {clipPoint: false})
geoRhombic().parents([-1, 0, 6, 2, 1, 9, 11, 3, 4, 8, 6, 10]).preclip().polygon()).clipPoint(false)
);
return renderWorld(projection, {points: [
[0, 0],
Expand Down

0 comments on commit 4f33498

Please sign in to comment.