-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
174 lines (162 loc) · 5.87 KB
/
map.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const startLocation = L.latLng(29.701939, -95.273283)
var arrCSV;
var isUploaded = false;
var map = L.map('map').setView([29.701939, -95.273283], 5);
L.tileLayer('https://tile.mgoconnect.org/osm/{z}/{x}/{y}.png', { maxZoom: 19 }).addTo(map);
L.Control.geocoder().addTo(map);
// Set up start location mark
L.marker([29.701939, -95.273283]).addTo(map).bindPopup("Start Location")
// Create Excel
var wb = XLSX.utils.book_new();
wb.Props = {
Title: "OSRM",
Subject: "OSRM",
Author: "OSRM",
CreatedDate: new Date()
};
async function codeAddress(address) {
let arr = [];
for (let i = 0; i < address.length; i++) {
arr.push(new Promise((resolve, reject) => {
let encode = encodeURI(removeZipCode(address[i][0]))
fetch(`https://geo.mygovernmentonline.org/search.php?q=${encode}&format=json&limit=1`)
.then(response => response.text())
.then(result => {
let temp = JSON.parse(result)
if(temp.length === 0){
resolve()
} else {
arrCSV[i].push(temp[0].lat);
arrCSV[i].push(temp[0].lon);
resolve(L.latLng(temp[0].lat, temp[0].lon));
}
})
.catch(error => console.log('error', error));
}));
}
await Promise.all(arr).then(values => {
console.log("updated arr object: ", arrCSV);
let result = values.filter(function (row) {
return row !== undefined
})
for (let i = 0; i < Math.ceil(result.length / 50); i++) {
let arr = result.slice(i * 50, i * 50 + 50)
setTimeout(() => {
console.log(i);
L.Routing.control({
waypoints: [startLocation, ...arr],
show: false,
router: L.Routing.osrmv1({
serviceUrl: 'https://osrm.mgoconnect.org/route/v1'
})
}).on('routeselected', function (e) {
let route = e.route;
wb.SheetNames.push(`OSRM${i}`)
console.log("Route Result sub: ", route)
const ws_data = [
[
"Type",
"Distance",
"Time",
"Road",
"Direction",
"Index",
"Mode",
"Modifier",
"Text"
],
...route.instructions.map(item => [
item.type,
item.distance,
item.time,
item.road,
item.direction,
item.index,
item.mode,
item.modifier,
item.text
])
]
let ws = XLSX.utils.aoa_to_sheet(ws_data);
wb.Sheets[`OSRM${i}`] = ws;
}).addTo(map);
}, i*1000)
}
})
}
function removeZipCode(address) {
let temp = address.slice(0, address.indexOf("-"));
return temp.replace(/[^a-zA-Z0-9 ]/g, "")
}
function s2ab(s) {
let buf = new ArrayBuffer(s.length);
let view = new Uint8Array(buf);
for (let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
function uploadCSV() {
if ($("#fileToUpload").get(0).files.length === 0) {
alert("Please upload the file first.");
return;
}
let fileUpload = $("#fileToUpload").get(0);
let files = fileUpload.files;
if (files[0].name.toLowerCase().lastIndexOf(".csv") === -1) {
alert("Please upload only CSV files");
return;
}
let reader = new FileReader();
let bytes = 500000;
reader.onloadend = async function (evt) {
let lines = evt.target.result;
if (lines && lines.length > 0) {
let line_array = CSVToArray(lines);
console.log("CSV array: ", line_array);
arrCSV = line_array
if (lines.length === bytes) {
line_array = line_array.splice(0, line_array.length - 1);
}
await codeAddress(line_array)
document.querySelector('#btnExportRouteResult').disabled = false;
document.querySelector('#btnExportCoordinates').disabled = false;
}
}
let blob = files[0].slice(0, bytes);
reader.readAsBinaryString(blob);
}
function exportRoute() {
let wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });
saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), 'OSRM.xlsx');
}
function exportCoordinate() {
let csvContent = arrCSV.map(e => e.join(",")).join("\r\n");
let blob = new Blob([csvContent], {type: 'text/csv;charset=utf-8;'})
saveAs(blob, 'coordinates.csv')
}
function CSVToArray(strData, strDelimiter) {
strDelimiter = (strDelimiter || ",");
let objPattern = new RegExp(
(
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
let arrData = [[]];
let arrMatches = null;
while (arrMatches = objPattern.exec(strData)) {
let strMatchedDelimiter = arrMatches[1];
let strMatchedValue = [];
if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) {
arrData.push([]);
}
if (arrMatches[2]) {
strMatchedValue = arrMatches[2].replace(new RegExp("\"\"", "g"), "\"");
} else {
strMatchedValue = arrMatches[3];
}
arrData[arrData.length - 1].push(strMatchedValue);
}
return (arrData);
}