-
Notifications
You must be signed in to change notification settings - Fork 2
/
TheKarte-geocoder-OSM-nominatim.html
executable file
·113 lines (91 loc) · 3.76 KB
/
TheKarte-geocoder-OSM-nominatim.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
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
<!DOCTYPE html>
<!--
Geocodes addresses to geographical location (latitude, longitude).
Uses open OpenstreetMap with Nominatim.
Coordinates are exported in WGS84.
-->
<html>
<head>
<meta charset="UTF-8">
<title>TheKarte-geocoder</title>
<link rel="icon" type="image/svg+xml" href="img/TheKarte-logo.svg" />
<link rel="icon" type="image/png" href="img/TheKarte-logo.png" sizes="96x96">
<style>
.dropArea {
height: 30em;
width: 30em;
margin-top: 1em;
margin-left: auto;
margin-right: auto;
border-style: dotted;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
<script>
/**
@param {element} inputArea An input area containing one address per line.
*/
function geocode(inputArea) {
const addresses = inputArea.value;
const addressList = addresses.split('\n');
const addressListTrimmed = addressList.map(a => a.trim());
console.log(addressListTrimmed);
coordinateList = new Array();
//Requests address synchronuous.
for (let i = 0; i < addressListTrimmed.length; i++) {
if (addressList[i].length == 0) {
console.warn('Line ' + i + ' is empty or contains whitespaces only.');
coordinateList.push([addressList[i]]);
}
let url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + encodeURI(addressList[i]);
console.log('Sending: ' + addressList[i]);
console.log(' Request-URL: ' + url);
let request = new XMLHttpRequest();
request.open("GET", url, false);
request.send(null);
if (request.status == 200) {
let json_response = JSON.parse(request.responseText);
if (json_response.length == 0) {
coordinateList.push([addressList[i]]);
} else {
coordinateList.push([addressList[i], json_response[0].lon, json_response[0].lat]);
continue;
}
}
}
inputArea.value = coordinateList.map(a => a.join(';')).join('\n');
}
/**
Localization: parses stringified number.
Accounts for european decimal separator before parsing.
@param {string} str One stringified number.
*/
function string2number(str) {
if (typeof str === 'string' && str.includes(',')) {
return str.replace(',', '.');
}
return str;
}
</script>
</head>
<body style="height:100%; margin: 0;">
<div style="position: fixed; height: 100%; width: 100%; text-align: center; vertical-align: center">
<h1>TheKarte: Geocoder (via OpenstreetMap Nominatim)</h1>
<p>
How to use:
<ol>
<li>Paste addresses into the text area below (one address per line).</li>
<li>To start geocoding, press the start-button.</li>
<li>Each address is processed individually and the latitude as well as longitude added in the text area.</li>
</ol>
<b>NOTE:</b> If nominatim could not determine an address, then nothing is shown.
</p>
<input type="button" value="Start" onclick="geocode(document.getElementById('inputArea'))"></input>
<div class="dropArea">
<textarea id="inputArea" class="dropArea"></textarea>
</div>
</div>
</body>
</html>