Created
January 14, 2021 17:24
-
-
Save a5e/2916ff5b18fa32f642f71c0cf63af49a to your computer and use it in GitHub Desktop.
turf-sandbox snippet
This file contains 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
// simply return a valid GeoJSON and it will be rendered on the map | |
const poly1 = turf.polygon([[ | |
[49.462728907250906, 32.03540626913309], | |
[49.43353833374158, 31.99601467698813], | |
[49.414854634751144, 32.03060880303383], | |
[49.40926600401516, 31.998867206275463], | |
[49.36541223268354, 32.06928189843893], | |
[49.4083992948085, 32.13303532451391], | |
[49.462728907250906, 32.03540626913309], | |
]], | |
{stroke: 'blue', fill: 'blue', 'fill-opacity': 0.3, 'stroke-width': 2} | |
); | |
const poly2 = turf.polygon([[ | |
[49.24136234816716, 31.863340884447098], | |
[49.22462075975919, 31.833318583667282], | |
[49.20040909648348, 31.86852157115936], | |
[49.203111744950746, 31.9387686252594], | |
[49.23448994162803, 31.94364957511425], | |
[49.24136234816716, 31.863340884447098], | |
]], | |
{stroke: 'blue', fill: 'blue', 'fill-opacity': 0.3, 'stroke-width': 2} | |
); | |
const poly3 = turf.polygon( | |
[[ | |
[48.13998183965103, 33.568177074193954], | |
[48.11256172127216, 33.465478643774986], | |
[48.06998415445941, 33.48885141313076], | |
[48.05255544955855, 33.464987464249134], | |
[48.060517859632476, 33.38402800261974], | |
[47.99290166965593, 33.42316813766956], | |
[47.92982293287458, 33.370756432414055], | |
[47.88709444387379, 33.248226419091225], | |
[47.88424459366558, 33.3445505797863], | |
[47.81532609062384, 33.33251014351845], | |
[47.87616927900448, 33.46778869628906], | |
[47.95971208927601, 33.45645636320114], | |
[48.13998183965103, 33.568177074193954], | |
]], | |
{stroke: 'blue', fill: 'blue', 'fill-opacity': 0.3, 'stroke-width': 2} | |
); | |
const turfPoint = turf.point([49.26776779628557, 33.19880034774542], | |
{'marker-symbol': 'rocket', 'marker-color': '#F00', 'marker-size': 'large'}) //this is "A" marker coordinates | |
let nearestPoint = null | |
let minDistanceKm = null | |
const getClosestPointToPolygon = (arrayOfPoly) => { | |
arrayOfPoly.map(turfPolygon => { | |
const distanceKm = turf.pointToLineDistance(turfPoint, turf.polygonToLine(turfPolygon)) | |
if (!!!minDistanceKm || minDistanceKm > distanceKm) { | |
minDistanceKm = distanceKm | |
nearestPoint = turf.nearestPointOnLine(turf.polygonToLine(turfPolygon), turfPoint) | |
} | |
}) | |
return nearestPoint | |
} | |
const closestPoint = getClosestPointToPolygon([poly1,poly2,poly3]) | |
console.log(minDistanceKm) | |
return turf.featureCollection([poly1, poly2, poly3, turfPoint, closestPoint]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment