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

🛠 Extend Circle Options #2516

Open
wants to merge 8 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
32 changes: 21 additions & 11 deletions packages/turf-circle/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GeoJsonProperties, Feature, Point, Polygon } from "geojson";
import { GeoJsonProperties, Feature, Point, Polygon, BBox } from "geojson";
import destination from "@turf/destination";
import { polygon, Units } from "@turf/helpers";
import { Id, polygon, Units } from "@turf/helpers";

/**
* Takes a {@link Point} and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
Expand All @@ -12,6 +12,8 @@ import { polygon, Units } from "@turf/helpers";
* @param {number} [options.steps=64] number of steps
* @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
* @param {Object} [options.properties={}] properties
* @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] to assign to the resulting circle Feature
* @param {string|number} [options.id] Identifier to assign to the resulting circle feature
* @returns {Feature<Polygon>} circle polygon
* @example
* var center = [-75.343, 39.984];
Expand All @@ -29,27 +31,35 @@ function circle<P extends GeoJsonProperties = GeoJsonProperties>(
steps?: number;
units?: Units;
properties?: P;
bbox?: BBox;
id?: Id;
} = {}
): Feature<Polygon, P> {
// default params
const steps = options.steps || 64;
const properties: any = options.properties
? options.properties
: !Array.isArray(center) && center.type === "Feature" && center.properties
? center.properties
: {};
const stepAngle = -360 / steps;

// main
let properties = options.properties;
let bboxValue: BBox | undefined;
let idValue: Id | undefined;

if (!Array.isArray(center) && center.type === "Feature") {
properties = properties || center.properties;
idValue = center.id;
}

bboxValue = bboxValue || options.bbox;
idValue = idValue || options.id;
// Calculate circle coordinates
const coordinates = [];
for (let i = 0; i < steps; i++) {
coordinates.push(
destination(center, radius, (i * -360) / steps, options).geometry
.coordinates
destination(center, radius, i * stepAngle, options).geometry.coordinates
);
}
coordinates.push(coordinates[0]);

return polygon([coordinates], properties);
return polygon([coordinates], properties, { bbox: bboxValue, id: idValue });
}

export default circle;
Empty file modified scripts/add-import-extensions.js
100755 → 100644
Empty file.
Empty file modified scripts/check-dependencies.js
100755 → 100644
Empty file.
Empty file modified scripts/create-new-module
100755 → 100644
Empty file.
Empty file modified scripts/generate-readmes.ts
100755 → 100644
Empty file.
Empty file modified scripts/list-modules-to-markdown
100755 → 100644
Empty file.
Empty file modified scripts/npm-publish-all
100755 → 100644
Empty file.
Empty file modified scripts/organization-make-public
100755 → 100644
Empty file.
Empty file modified scripts/update-dependencies
100755 → 100644
Empty file.