Created
August 29, 2021 16:42
-
-
Save nazmulidris/b2723dafff880701c1cbdccdd5a212a2 to your computer and use it in GitHub Desktop.
working w/ turf.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * turf docs: | |
| * - https://turfjs.org/docs/#nearestPoint | |
| * - https://turfjs.org/docs/#feature | |
| * | |
| * mapbox sample w/ turf: | |
| * - https://docs.mapbox.com/help/tutorials/analysis-with-turf/ | |
| */ | |
| import { MapboxGeoJSONFeature } from "mapbox-gl" | |
| import { | |
| point, | |
| featureCollection, | |
| nearestPoint, | |
| FeatureCollection, | |
| feature, | |
| Feature, | |
| Point, | |
| Geometry, | |
| GeometryCollection, | |
| } from "@turf/turf" | |
| export const getAllCoordinates = (mpxFeature: MapboxGeoJSONFeature): FeatureCollection<Point> => { | |
| // https://lodash.com/docs/#get | |
| // _.get(object, 'a[0].b.c'); | |
| // [3].value.features[0].geometry.coordinates | |
| // [3].value.features[1].geometry.coordinates | |
| } | |
| export const convert = (features: MapboxGeoJSONFeature[]): FeatureCollection => { | |
| // TODO convert a mapbox geojsonfeature array into a turn featurecollection | |
| const feature1: Feature<Geometry> = { | |
| type: "Feature", | |
| geometry: { | |
| type: "Point", | |
| coordinates: [22.489013671875, 58.43048192568003], | |
| }, | |
| properties: { | |
| name: "Name", | |
| }, | |
| id: 1, | |
| } | |
| const point1: Feature<Point> = point([28.948459, 41.024204]) | |
| const coordinates = [ | |
| [22.840576171875, 58.63121664342478], | |
| [23.1591796875, 58.551060622877486], | |
| [23.345947265625, 58.43623315794525], | |
| [22.554931640625, 58.14172026743037], | |
| [22.291259765625, 58.11851749586461], | |
| ] | |
| console.log( | |
| ">>>map:", | |
| coordinates.map((coordinate) => point(coordinate)) | |
| ) | |
| const myFeatureCollection: FeatureCollection<Point> = featureCollection([feature1, point1]) | |
| nearestPoint(point1, myFeatureCollection) | |
| return myFeatureCollection | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment