index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. import bbox from "@turf/bbox";
  2. import { point, } from "@turf/helpers";
  3. /**
  4. * Takes a {@link Feature} or {@link FeatureCollection} and returns the absolute center point of all features.
  5. *
  6. * @name center
  7. * @param {GeoJSON} geojson GeoJSON to be centered
  8. * @param {Object} [options={}] Optional parameters
  9. * @param {Object} [options.properties={}] Translate GeoJSON Properties to Point
  10. * @param {Object} [options.bbox={}] Translate GeoJSON BBox to Point
  11. * @param {Object} [options.id={}] Translate GeoJSON Id to Point
  12. * @returns {Feature<Point>} a Point feature at the absolute center point of all input features
  13. * @example
  14. * var features = turf.points([
  15. * [-97.522259, 35.4691],
  16. * [-97.502754, 35.463455],
  17. * [-97.508269, 35.463245]
  18. * ]);
  19. *
  20. * var center = turf.center(features);
  21. *
  22. * //addToMap
  23. * var addToMap = [features, center]
  24. * center.properties['marker-size'] = 'large';
  25. * center.properties['marker-color'] = '#000';
  26. */
  27. function center(geojson, options) {
  28. if (options === void 0) { options = {}; }
  29. var ext = bbox(geojson);
  30. var x = (ext[0] + ext[2]) / 2;
  31. var y = (ext[1] + ext[3]) / 2;
  32. return point([x, y], options.properties, options);
  33. }
  34. export default center;