-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojson and polyline intersection.html
65 lines (53 loc) · 2.12 KB
/
geojson and polyline intersection.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
<head>
<title>Polyline Passes Through GeoJSON Check</title>
</head>
<body>
<h2>Polyline Passes Through GeoJSON Check</h2>
<div>
<label for="geojson">Enter GeoJSON:</label><br>
<textarea id="geojson" rows="5" cols="50"></textarea>
</div>
<div>
<label for="polyline">Enter Polyline (Comma-separated lat,lng pairs):</label><br>
<input type="text" id="polyline" style="width: 100%;">
</div>
<button onclick="checkPolyline()">Check</button>
<p id="result"></p>
<script>
function checkPolyline() {
var geojsonInput = document.getElementById("geojson").value;
var polylineInput = document.getElementById("polyline").value;
var geojson = JSON.parse(geojsonInput);
var polylinePoints = polylineInput.split(',').map(function (pair) {
var latLng = pair.split(',');
return [parseFloat(latLng[0]), parseFloat(latLng[1])];
});
var pointInsideGeoJSON = false;
if (geojson.type === 'Feature' && geojson.geometry.type === 'Polygon') {
var polygonCoordinates = geojson.geometry.coordinates[0];
pointInsideGeoJSON = isPointInsidePolygon(polylinePoints, polygonCoordinates);
}
var resultElement = document.getElementById("result");
if (pointInsideGeoJSON) {
resultElement.innerHTML = "The polyline passes through the given GeoJSON location.";
} else {
resultElement.innerHTML = "The polyline does not pass through the given GeoJSON location.";
}
}
function isPointInsidePolygon(point, polygon) {
var x = point[0], y = point[1];
var isInside = false;
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
var xi = polygon[i][0], yi = polygon[i][1];
var xj = polygon[j][0], yj = polygon[j][1];
var intersect = ((yi > y) !== (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) isInside = !isInside;
}
return isInside;
}
</script>
</body>
</html>