-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
223 lines (185 loc) · 5.57 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const fs = require("fs");
const fetch = require("node-fetch");
const reproject = require("reproject");
const DEBUG = false;
const EPSG = {
"EPSG:3301":
"+proj=lcc +lat_1=59.33333333333334 +lat_2=58 +lat_0=57.51755393055556 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs",
"EPSG:3857":
"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs",
};
const options = {
headers: {
accept: "application/json, text/plain, */*",
"accept-language": "en-GB,en;q=0.9",
"cache-control": "no-cache",
pragma: "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
},
referrer: "https://www.sadamaregister.ee/",
referrerPolicy: "strict-origin-when-cross-origin",
body: null,
method: "GET",
mode: "cors",
};
const formatNumbers = (val) => {
if (isNaN(val)) return "-";
return Number(val).toFixed(1);
};
const fetchPorts = async () => {
const url = "https://sadamaregister.ee/api/ports/public-active";
const geojson = {
type: "FeatureCollection",
features: [],
};
const response = await fetch(url, options);
const json = await response.json();
let ports = json.map(({ publicId, name }) => [
publicId,
name.replace(/\s+/g, " ").trim(),
]);
if (DEBUG) ports = ports.slice(0, 5);
console.log("Total ports:", ports.length);
ports.forEach((port, i) => {
setTimeout(async () => {
const feature = await fetchPort(port, options);
if (feature) geojson.features.push(feature);
if (i == ports.length - 1) {
geojson.features.sort(function (a, b) {
return (
parseFloat(a.properties.sadamaregister) -
parseFloat(b.properties.sadamaregister)
);
});
fs.writeFile(
"public/raw.json",
JSON.stringify(geojson, null, 2),
(err) => {
if (err) throw err;
},
);
fs.writeFile("public/data.json", JSON.stringify(geojson), (err) => {
if (err) throw err;
});
fs.writeFile(
"public/data.js",
`var sadamadgeoJson = ${JSON.stringify(geojson)};`,
(err) => {
if (err) throw err;
},
);
}
}, i * 200); // 0.2s delay for scraping
});
};
const fetchPort = async (port) => {
const [id, name] = port;
const url = `https://sadamaregister.ee/api/ports/${id}/public-details`;
const response = await fetch(url, options);
const json = await response.json();
const {
portManagerContacts,
portMainData,
portTechnicalData,
harbourMasterData,
} = json;
if (!portMainData) {
throw `Missing json ${id} ${name}`;
}
let { muutmineKp } = portMainData;
let {
sadamaPidajaEesnimi,
sadamaPidajaArinimiPerenimi,
sadamaPidajaTelefon,
sadamaPidajaEpost,
sadamaPidajaKoduleht,
} = portManagerContacts;
let { veesoidukiMaxPikkus, veesoidukiMaxLaius, veesoidukiMaxSyvis } =
portTechnicalData;
let {
sadamaKaptenEesnimi,
sadamaKaptenPerenimi,
sadamaKaptenTelefon,
sadamaKaptenEpost,
} = harbourMasterData;
let omanik = "-";
if (sadamaPidajaEesnimi && sadamaPidajaArinimiPerenimi) {
omanik = sadamaPidajaEesnimi + " " + sadamaPidajaArinimiPerenimi;
} else if (sadamaPidajaArinimiPerenimi) {
omanik = sadamaPidajaArinimiPerenimi;
}
let omanik_telefon = "-";
if (sadamaPidajaTelefon && sadamaPidajaTelefon.length !== 0) {
omanik_telefon = sadamaPidajaTelefon.join(" ").split(" +").join(";+");
}
let omanik_epost = "-";
if (sadamaPidajaEpost) {
omanik_epost = sadamaPidajaEpost;
}
let koduleht = "-";
if (sadamaPidajaKoduleht) {
koduleht = sadamaPidajaKoduleht;
}
let sadamakapteni_nimi = "-";
if (sadamaKaptenEesnimi && sadamaKaptenPerenimi) {
sadamakapteni_nimi = sadamaKaptenEesnimi + " " + sadamaKaptenPerenimi;
}
let sadamakapteni_telefon = "-";
if (sadamaKaptenTelefon && sadamaKaptenTelefon.length !== 0) {
sadamakapteni_telefon = sadamaKaptenTelefon
.join(" ")
.split(" +")
.join(";+");
}
let sadamakapteni_epost = "-";
if (sadamaKaptenEpost) {
sadamakapteni_epost = sadamaKaptenEpost;
}
let max_pikkus = "-";
if (veesoidukiMaxPikkus) {
max_pikkus = formatNumbers(veesoidukiMaxPikkus);
}
let max_laius = "-";
if (veesoidukiMaxLaius) {
max_laius = formatNumbers(veesoidukiMaxLaius);
}
let max_sygavus = "-";
if (veesoidukiMaxSyvis) {
max_sygavus = formatNumbers(veesoidukiMaxSyvis);
}
let coords = portTechnicalData.sadamaAsukoht;
const input = {
type: "Point",
coordinates: [coords.x, coords.y],
};
const output = reproject.reproject(input, "EPSG:3301", "EPSG:4326", EPSG);
const [lon, lat] = output.coordinates;
let feature = {
type: "Feature",
properties: {
sadamaregister: id,
sadama_nimi: name,
omanik: omanik,
omanik_telefon: omanik_telefon,
omanik_epost: omanik_epost,
koduleht: koduleht,
sadamakapteni_nimi: sadamakapteni_nimi,
sadamakapteni_telefon: sadamakapteni_telefon,
sadamakapteni_epost: sadamakapteni_epost,
max_pikkus: max_pikkus,
max_laius: max_laius,
max_sygavus: max_sygavus,
modified_date: muutmineKp,
},
geometry: {
type: "Point",
coordinates: [+lon.toFixed(5), +lat.toFixed(5)],
},
};
return feature;
};
fetchPorts();