Friday, 4 November 2016

How to tell if a GeoJSON path intersects with another feature in Leaflet?


I have an application where the user draws a path (a series of connected straight lines) and this path may not intersect any feature in a particular GeoJSON layer.


I need to check that no point along these lines intersect the GeoJSON layer, not just the endpoints.


How can I perform this check?



Answer



You can try the Turf library and a method like intersect: http://turfjs.org/docs/#intersect


Here's the code example from that library:


var poly1 = {
"type": "Feature",
"geometry": {

"type": "Polygon",
"coordinates": [[
[-122.801742, 45.48565],
[-122.801742, 45.60491],
[-122.584762, 45.60491],
[-122.584762, 45.48565],
[-122.801742, 45.48565]
]]
}
}

var poly2 = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[
[-122.520217, 45.535693],
[-122.64038, 45.553967],
[-122.720031, 45.526554],
[-122.669906, 45.507309],
[-122.723464, 45.446643],

[-122.532577, 45.408574],
[-122.487258, 45.477466],
[-122.520217, 45.535693]
]]
}
}

var intersection = turf.intersect(poly1, poly2);

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...