forked from glsf91/nlalert
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNlAlertGeo.js
More file actions
146 lines (125 loc) · 5.32 KB
/
Copy pathNlAlertGeo.js
File metadata and controls
146 lines (125 loc) · 5.32 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
.pragma library
function clampRange(value) {
var parsed = parseInt(value)
if (isNaN(parsed)) return 25
return Math.max(1, Math.min(250, parsed))
}
function validCoordinates(latitude, longitude) {
return !isNaN(latitude) && !isNaN(longitude)
&& latitude >= -90 && latitude <= 90
&& longitude >= -180 && longitude <= 180
}
function normalizePostcode(value) {
return String(value || "").toUpperCase().replace(/[^0-9A-Z]/g, "")
}
function validPostcode(value) {
return /^[1-9][0-9]{3}[A-Z]{2}$/.test(normalizePostcode(value))
}
function formatPostcode(value) {
var normalized = normalizePostcode(value)
if (normalized.length !== 6) return normalized
return normalized.substring(0, 4) + " " + normalized.substring(4)
}
function parseWgs84Point(value) {
var match = /^POINT\s*\(\s*(-?[0-9]+(?:\.[0-9]+)?)\s+(-?[0-9]+(?:\.[0-9]+)?)\s*\)$/i.exec(String(value || ""))
if (!match) return null
var longitude = parseFloat(match[1])
var latitude = parseFloat(match[2])
if (!validCoordinates(latitude, longitude)) return null
return {"latitude": latitude, "longitude": longitude}
}
function parsePostcodeResponse(body, requestedPostcode) {
var requested = normalizePostcode(requestedPostcode)
if (!validPostcode(requested) || !body || !body.response || !(body.response.docs instanceof Array)) return null
for (var i = 0; i < body.response.docs.length; i++) {
var document = body.response.docs[i]
if (!document || normalizePostcode(document.postcode) !== requested) continue
var point = parseWgs84Point(document.centroide_ll)
if (!point) continue
var place = document.woonplaatsnaam ? String(document.woonplaatsnaam) : ""
return {
"postcode": requested,
"formattedPostcode": formatPostcode(requested),
"latitude": point.latitude,
"longitude": point.longitude,
"label": place ? formatPostcode(requested) + " " + place : formatPostcode(requested)
}
}
return null
}
function parsePolygon(polygonText) {
var points = []
if (!polygonText) return points
var pairs = String(polygonText).replace(/^\s+|\s+$/g, "").split(/\s+/)
for (var i = 0; i < pairs.length; i++) {
var parts = pairs[i].split(",")
if (parts.length !== 2) continue
var latitude = parseFloat(parts[0])
var longitude = parseFloat(parts[1])
if (validCoordinates(latitude, longitude)) points.push({"lat": latitude, "lon": longitude})
}
return points
}
function pointInPolygon(latitude, longitude, polygonText) {
var points = parsePolygon(polygonText)
if (points.length < 3) return false
var inside = false
for (var i = 0, j = points.length - 1; i < points.length; j = i++) {
var yi = points[i].lat
var xi = points[i].lon
var yj = points[j].lat
var xj = points[j].lon
var crosses = ((yi > latitude) !== (yj > latitude))
&& (longitude < (xj - xi) * (latitude - yi) / (yj - yi) + xi)
if (crosses) inside = !inside
}
return inside
}
function pointToSegmentDistanceKm(latitude, longitude, start, end) {
var kmPerLatitude = 111.32
var kmPerLongitude = 111.32 * Math.max(0.2, Math.cos(latitude * Math.PI / 180))
var startX = (start.lon - longitude) * kmPerLongitude
var startY = (start.lat - latitude) * kmPerLatitude
var endX = (end.lon - longitude) * kmPerLongitude
var endY = (end.lat - latitude) * kmPerLatitude
var deltaX = endX - startX
var deltaY = endY - startY
var lengthSquared = deltaX * deltaX + deltaY * deltaY
var projection = lengthSquared > 0
? -(startX * deltaX + startY * deltaY) / lengthSquared
: 0
projection = Math.max(0, Math.min(1, projection))
var closestX = startX + projection * deltaX
var closestY = startY + projection * deltaY
return Math.sqrt(closestX * closestX + closestY * closestY)
}
function distanceToPolygonKm(latitude, longitude, polygonText) {
var points = parsePolygon(polygonText)
if (points.length < 3) return -1
if (pointInPolygon(latitude, longitude, polygonText)) return 0
var minimum = 1000000
for (var i = 0; i < points.length; i++) {
var next = points[(i + 1) % points.length]
minimum = Math.min(minimum, pointToSegmentDistanceKm(latitude, longitude, points[i], next))
}
return minimum === 1000000 ? -1 : minimum
}
function distanceToAreasKm(latitude, longitude, areas) {
if (!validCoordinates(latitude, longitude) || !(areas instanceof Array)) return -1
var minimum = 1000000
for (var i = 0; i < areas.length; i++) {
var distance = distanceToPolygonKm(latitude, longitude, areas[i])
if (distance >= 0) minimum = Math.min(minimum, distance)
}
return minimum === 1000000 ? -1 : minimum
}
function matchesRange(latitude, longitude, areas, rangeKm) {
var distance = distanceToAreasKm(latitude, longitude, areas)
return distance >= 0 && distance <= clampRange(rangeKm)
}
function formatDistance(distanceKm) {
if (distanceKm < 0) return "Onbekende afstand"
if (distanceKm < 0.05) return "Ingestelde locatie ligt in het waarschuwingsgebied"
if (distanceKm < 10) return distanceKm.toFixed(1).replace(".", ",") + " km tot het waarschuwingsgebied"
return Math.round(distanceKm) + " km tot het waarschuwingsgebied"
}