-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountiesFromGPX.html
97 lines (83 loc) · 2.99 KB
/
CountiesFromGPX.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
<html><head>
<title>Determine counties from GPX</title>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
var output = [];
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
var gcCodes = [];
var coords = [];
var next = 0;
var outarea = document.getElementById('list');
outarea.value = "DETERMINING COUNTIES...";
// files is a FileList of File objects.
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
reader.onload = function(evt) {
var re2 = /<wpt lat=\"([0-9\.]+)\"\slon=\"(-[0-9\.]+)\"/g;
var re = /<name>(GC[A-Z0-9]*)<\/name>/g;
var lines = this.result.split('\n');
var line = '';
for (var i = 0; i < lines.length; i++) {
line = lines[i];
var m1 = re.exec(line);
var m2 = re2.exec(line);
if (m1 !== null) {
if (coords[next] !== null) {
coords[next] = coords[next] + "," + m1[1];
next++;
}
}
if (m2 !== null) {
var lat = m2[1];
var lon = m2[2];
coords[next] = lat + "," + lon;
gcCodes[next] = null;
}
}
// Async gather county data
const getPlaces = async () => {
for (var item of coords) {
let parts = item.split(",");
const lineOut = await getPlace(item);
if (parts[2] !== undefined) {
output.push(parts[2] + " " + lineOut.County.name + "," + lineOut.State.code + "\n");
}
}
updateView(output);
};
getPlaces();
};
reader.readAsText(f);
}
}
function updateView(output) {
var outarea = document.getElementById('list');
outarea.value = output.join(' '),
outarea.setSelectionRange(0, 99999);
}
async function getPlace(item) {
let parts = item.split(",");
var url = "https://geo.fcc.gov/api/census/block/find?latitude=" + parts[0] + "&longitude=" + parts[1] + "&censusYear=2020&format=json";
let res = await fetch(url);
return await res.json();
}
$(document).ready(function() {
document.getElementById('files').addEventListener('change', handleFileSelect, false);
});
</script>
</head>
<body>
<h3>Show Counties page from GPX</h3>
GPX: <input type="file" id="files" name="files[]"> <br>
<textarea id="list" rows="25" cols="80"></textarea>
<br><b>Reload page to run again</b>
<div id="error"></div>
</body></html>