mirror of
https://github.com/Turfjs/turf.git
synced 2025-12-08 20:26:16 +00:00
* Added JSDoc for internal types and constants e.g. Unts and earthRadius. Minimal other changes to bring JSDoc types into line with code types. Added GeoJsonProperties to documentation.yml for type linking. * Generated README.md files based on updated source that now includes JSDoc for internal types and constants e.g. Units. Also synced up some out of data JSDoc types with what is in the code. * Switched the structure of documentation.yml to be more traditionally nested. We'll use this new structure from the turf-www repo to generate the website documentation in a more robust manner. * For some reason leaving this function documented as the default (geojsonRbush) causes @turf/turf last-checks to fail. Specifically defining it as rbush like it used to be, except with the @function tag rather than @name.
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { BBox, Feature, GeoJsonProperties, Point } from "geojson";
|
|
import { bbox } from "@turf/bbox";
|
|
import { point, Id, AllGeoJSON } from "@turf/helpers";
|
|
|
|
/**
|
|
* Takes a {@link Feature} or {@link FeatureCollection} and returns the absolute center point of all features.
|
|
*
|
|
* @function
|
|
* @param {GeoJSON} geojson GeoJSON to be centered
|
|
* @param {Object} [options={}] Optional parameters
|
|
* @param {Object} [options.properties={}] Translate GeoJSON Properties to Point
|
|
* @param {Object} [options.bbox={}] Translate GeoJSON BBox to Point
|
|
* @param {Object} [options.id={}] Translate GeoJSON Id to Point
|
|
* @returns {Feature<Point>} a Point feature at the absolute center point of all input features
|
|
* @example
|
|
* var features = turf.points([
|
|
* [-97.522259, 35.4691],
|
|
* [-97.502754, 35.463455],
|
|
* [-97.508269, 35.463245]
|
|
* ]);
|
|
*
|
|
* var center = turf.center(features);
|
|
*
|
|
* //addToMap
|
|
* var addToMap = [features, center]
|
|
* center.properties['marker-size'] = 'large';
|
|
* center.properties['marker-color'] = '#000';
|
|
*/
|
|
function center<P extends GeoJsonProperties = GeoJsonProperties>(
|
|
geojson: AllGeoJSON,
|
|
options: { properties?: P; bbox?: BBox; id?: Id } = {}
|
|
): Feature<Point, P> {
|
|
const ext = bbox(geojson);
|
|
const x = (ext[0] + ext[2]) / 2;
|
|
const y = (ext[1] + ext[3]) / 2;
|
|
return point([x, y], options.properties, options);
|
|
}
|
|
|
|
export { center };
|
|
export default center;
|