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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
import crypto from "crypto";
import fs from "fs";
import path from "path";
const couchUsername = process.env.COUCHDB_USERNAME;
const couchPassword = process.env.COUCHDB_PASSWORD;
function getAuthHeader() {
if (!couchUsername || !couchPassword) {
throw new Error("CouchDB credentials not set in environment variables");
}
const auth = Buffer.from(`${couchUsername}:${couchPassword}`).toString("base64");
return `Basic ${auth}`;
}
// === CONFIG ===
const baseUrl = "https://kartta.hel.fi/ws/geoserver/avoindata/wfs";
const layers = [
"Aluesarjat_avainluvut_2024",
"Piirijako_pienalue",
"Seutukartta_liikenne_paatiet",
"Seutukartta_liikenne_metroasemat",
"Seutukartta_liikenne_metro_rata",
"Seutukartta_liikenne_juna_rata",
"Seutukartta_liikenne_juna_asema",
"Seutukartta_aluejako_pienalue",
"Seutukartta_aluejako_kuntarajat",
"Seutukartta_maankaytto_jarvet",
"Seutukartta_maankaytto_joet",
"Seutukartta_meren_rantaviiva",
"Toimipisterekisteri_palvelut",
];
const outputDir = path.join(process.cwd(), "app", "data");
const couchUrl = "https://couch.tammi.cc";
const dbName = "helsinki_wfs";
// Ensure output dir
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
function getHeaders() {
return new Headers({
Authorization: getAuthHeader(),
"Content-Type": "application/json",
});
}
// === COUCHDB HELPERS ===
async function createDatabase() {
const url = `${couchUrl}/${dbName}`;
try {
const res = await fetch(url, {
headers: getHeaders(),
method: "PUT",
});
if (res.ok || res.status === 412) {
console.log(`Database ${dbName} ready.`);
} else {
throw new Error(await res.text());
}
} catch (e) {
console.error("DB create error:", e.message);
}
}
async function ensureDesignDocs() {
const designDoc = {
_id: "_design/layers",
views: {
by_layer: {
map: `function(doc) {
if (doc.type === 'feature' && doc.layer) {
emit(doc.layer, null);
}
}`,
},
},
};
const url = `${couchUrl}/${dbName}/_design/layers`;
try {
const res = await fetch(url, { headers: getHeaders() });
if (res.status === 404) {
await fetch(url, {
body: JSON.stringify(designDoc),
headers: getHeaders(),
method: "PUT",
});
console.log("Created design document: layers/by_layer");
} else if (res.ok) {
const existing = await res.json();
designDoc._rev = existing._rev;
await fetch(url, {
body: JSON.stringify(designDoc),
headers: getHeaders(),
method: "PUT",
});
console.log("Updated design document");
}
} catch (e) {
console.error("Design doc error:", e.message);
process.exit(1);
}
}
// === DOWNLOAD ===
async function downloadLayer(layer) {
const url = `${baseUrl}?service=WFS&version=2.0.0&request=GetFeature&typeName=avoindata:${layer}&outputFormat=json&srsname=EPSG:4326`;
try {
const res = await fetch(url);
if (!res.ok) throw new Error(res.statusText);
const response = await res.json();
return response;
} catch (e) {
console.error(`Download: \n${url}\nfailed [${layer}] ${e.toString()}`);
return null;
}
}
function saveToFile(layer, data) {
const filePath = path.join(outputDir, `${layer}.geojson`);
fs.writeFileSync(filePath, JSON.stringify(data, null, "\t"));
console.log(`Saved: ${layer}.geojson`);
}
// === UPLOAD METADATA ===
async function uploadLayerMetadata(layer, featureCount) {
const docId = `layer_metadata:${layer}`;
const doc = {
_id: docId,
feature_count: featureCount,
last_updated: new Date().toISOString(),
name: layer,
projection: "EPSG:4326",
type: "layer_metadata",
};
const url = `${couchUrl}/${dbName}/${docId}`;
try {
const getRes = await fetch(url, { headers: getHeaders() });
if (getRes.ok) {
const existing = await getRes.json();
doc._rev = existing._rev;
}
const putRes = await fetch(url, {
body: JSON.stringify(doc),
headers: getHeaders(),
method: "PUT",
});
if (!putRes.ok) throw new Error(await putRes.text());
console.log(`Metadata updated: ${layer} (${featureCount} features)`);
} catch (e) {
console.error(`Metadata error [${layer}]:`, e.message);
}
}
// === UPLOAD SINGLE FEATURE (with deduplication) ===
async function uploadFeature(doc) {
const url = `${couchUrl}/${dbName}/${doc._id}`;
try {
const getRes = await fetch(url, { headers: getHeaders() });
if (getRes.ok) {
const existing = await getRes.json();
doc._rev = existing._rev;
const geomEqual = JSON.stringify(doc.geometry) === JSON.stringify(existing.geometry);
const propEqual = JSON.stringify(doc.properties) === JSON.stringify(existing.properties);
if (geomEqual && propEqual) {
return false; // skipped
}
}
const putRes = await fetch(url, {
body: JSON.stringify(doc),
headers: getHeaders(),
method: "PUT",
});
return putRes.ok;
} catch (e) {
console.warn(`Upload failed [${doc._id}]:`, e.message);
return false;
}
}
// === PROCESS LAYER ===
async function processLayer(layer) {
const geojson = await downloadLayer(layer);
if (!geojson || !geojson.features) {
console.warn(`No features in ${layer} ${geojson}`);
process.exit(1);
}
let uploaded = 0;
let skipped = 0;
for (const feature of geojson.features) {
// Stable ID: use feature.id, or property, or UUID
const propId =
feature.id ||
feature.properties?.id ||
feature.properties?.tunnus ||
feature.properties?.objectid ||
crypto.randomUUID();
const doc = {
_id: `feature:${layer}:${propId}`,
downloaded_at: new Date().toISOString(),
geometry: feature.geometry,
layer: layer,
properties: feature.properties || {},
type: "feature",
};
const success = await uploadFeature(doc);
success ? uploaded++ : skipped++;
}
await uploadLayerMetadata(layer, geojson.features.length);
console.log(`Done: ${layer} | Uploaded: ${uploaded} | Skipped: ${skipped}`);
}
// === MAIN ===
async function main() {
await createDatabase();
await ensureDesignDocs();
for (const layer of layers) {
await processLayer(layer);
// Optional: rate limiting
await new Promise((r) => setTimeout(r, 500));
}
console.log("All layers processed.");
}
if (process.argv[1] === new URL(import.meta.url).pathname) {
main().catch(console.error);
}
|