Skip to content
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

Use turf-buffer without units and projections #2656

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/turf-buffer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## buffer

Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, degrees, and none.

When using a negative radius, the resulting geometry may be invalid if
it's too small compared to the radius magnitude. If the input is a
Expand Down
17 changes: 13 additions & 4 deletions packages/turf-buffer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
const { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;

/**
* Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
* Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, degrees, and none.
*
* When using a negative radius, the resulting geometry may be invalid if
* it's too small compared to the radius magnitude. If the input is a
Expand Down Expand Up @@ -87,6 +87,7 @@ function buffer(geojson, radius, options) {
function bufferFeature(geojson, radius, units, steps) {
var properties = geojson.properties || {};
var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
var isUnitless = units === "none";

// Geometry Types faster than jsts
if (geometry.type === "GeometryCollection") {
Expand All @@ -100,15 +101,20 @@ function bufferFeature(geojson, radius, units, steps) {

// Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)
var projection = defineProjection(geometry);
var projectedCoords = isUnitless
? geometry.coordinates
: projectCoords(geometry.coordinates, projection);
var projected = {
type: geometry.type,
coordinates: projectCoords(geometry.coordinates, projection),
coordinates: projectedCoords,
};

// JSTS buffer operation
var reader = new GeoJSONReader();
var geom = reader.read(projected);
var distance = radiansToLength(lengthToRadians(radius, units), "meters");
var distance = isUnitless
? radius
: radiansToLength(lengthToRadians(radius, units), "meters");
var buffered = BufferOp.bufferOp(geom, distance, steps);
var writer = new GeoJSONWriter();
buffered = writer.write(buffered);
Expand All @@ -117,9 +123,12 @@ function bufferFeature(geojson, radius, units, steps) {
if (coordsIsNaN(buffered.coordinates)) return undefined;

// Unproject coordinates (convert to Degrees)
var unprojectedCoords = isUnitless
? buffered.coordinates
: unprojectCoords(buffered.coordinates, projection);
var result = {
type: buffered.type,
coordinates: unprojectCoords(buffered.coordinates, projection),
coordinates: unprojectedCoords,
};

return feature(result, properties);
Expand Down
35 changes: 35 additions & 0 deletions packages/turf-buffer/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ test("turf-buffer - undefined return", (t) => {
t.end();
});

test("turf-buffer - units none", (t) => {
const point: GeoJSON.Feature<GeoJSON.Point> = {
type: "Feature",
properties: {},
geometry: {
type: "Point",
coordinates: [0, 0],
},
};

const polygon: GeoJSON.Feature<GeoJSON.Polygon> = {
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates: [
[
[+1, +0],
[+0, -1],
[-1, -0],
[-0, +1],
[+1, +0],
],
],
},
};

t.deepEqual(
truncate(buffer(point, 1, { units: "none", steps: 1 })),
polygon,
"unitless geometry should not be projected"
);
t.end();
});

function colorize(feature, color) {
color = color || "#F00";
if (feature.properties) {
Expand Down
6 changes: 4 additions & 2 deletions packages/turf-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ export type Units =
| "yards"
| "feet"
| "radians"
| "degrees";
| "degrees"
| "none";
export type AreaUnits =
| Exclude<Units, "radians" | "degrees">
| Exclude<Units, "radians" | "degrees" | "none">
| "acres"
| "hectares";
export type Grid = "point" | "square" | "hex" | "triangle";
Expand Down Expand Up @@ -87,6 +88,7 @@ export const factors: Record<Units, number> = {
nauticalmiles: earthRadius / 1852,
radians: 1,
yards: earthRadius * 1.0936,
none: NaN,
};

/**
Expand Down