aboutsummaryrefslogtreecommitdiffstats
path: root/download.js
blob: 173af626777002a7619d86010cdad36e8ee79662 (plain) (blame)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";

const couchUsername = process.env.COUCHDB_USERNAME;
const couchPassword = process.env.COUCHDB_PASSWORD;

/**
 * Generates the Basic Auth header for CouchDB using environment variables.
 * @returns {string} The Basic Auth header string.
 * @throws {Error} If CouchDB credentials are not set.
 */
function getAuthHeader() {
	if (!couchUsername || !couchPassword) {
		throw new Error(
			"CouchDB credentials COUCHDB_USERNAME COUCHDB_PASSWORD not set in environment variables",
		);
	}
	const auth = Buffer.from(`${couchUsername}:${couchPassword}`).toString("base64");
	return `Basic ${auth}`;
}

const baseUrl = "https://kartta.hel.fi/ws/geoserver/avoindata/wfs";
const layers = [
	"Aluesarjat_avainluvut_2024",
	"Piirijako_peruspiiri",
	"Piirijako_pienalue",
	"RaideJokeri_pysakit",
	"RaideJokeri_ratalinja",
	"Seutukartta_aluejako_kuntarajat",
	"Seutukartta_aluejako_pienalue",
	"Seutukartta_liikenne_juna_asema",
	"Seutukartta_liikenne_juna_rata",
	"Seutukartta_liikenne_metro_rata",
	"Seutukartta_liikenne_metroasemat",
	"Seutukartta_liikenne_paatiet",
	"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 });
}

/**
 * Creates headers for CouchDB requests, including authorization.
 * @returns {Headers} The Headers object for fetch requests.
 */
function getHeaders() {
	return new Headers({
		// biome-ignore lint/style/useNamingConvention: database
		Authorization: getAuthHeader(),
		"Content-Type": "application/json",
	});
}

/**
 * Creates the CouchDB database if it doesn't exist.
 * @returns {Promise<void>}
 * @throws {Error} If database creation fails (other than already exists).
 */
async function createDatabase() {
	const url = `${couchUrl}/${dbName}`;
	const res = await fetch(url, {
		headers: getHeaders(),
		method: "PUT",
	});
	if (res.ok || res.status === 412) {
		console.log(`Database ${dbName} ready.`);
		return;
	} else {
		throw new Error(await res.text());
	}
}

/**
 * Ensures the design documents (views) exist in the database, creating or updating as needed.
 * @returns {Promise<void>}
 */
async function ensureDesignDocs() {
	const designDoc = {
		_id: "_design/layers",
		views: {
			// biome-ignore lint/style/useNamingConvention: database
			by_layer: {
				map: `function(doc) {
          if (doc.type === 'feature' && doc.layer) {
            emit(doc.layer, null);
          }
        }`,
			},
		},
	};

	const url = `${couchUrl}/${dbName}/_design/layers`;
	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");
		return;
	} 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");
		return;
	}
	// If neither, implicitly return void, but log unexpected status
	console.warn(`Unexpected status when ensuring design docs: ${res.status}`);
}

/**
 * Downloads a GeoJSON layer from the WFS service.
 * @param {string} layer - The name of the layer to download.
 * @returns {Promise<object>} The parsed GeoJSON object.
 * @throws {Error} If the fetch fails.
 */
async function downloadLayer(layer) {
	const url = `${baseUrl}?service=WFS&version=2.0.0&request=GetFeature&typeName=avoindata:${layer}&outputFormat=json&srsname=EPSG:4326`;
	const res = await fetch(url);
	if (!res.ok) throw new Error(res.statusText);
	const response = await res.json();
	return response;
}

/**
 * Saves GeoJSON data to a local file.
 * Note: This function is defined but not currently used in the script. It could be called in processLayer if local saving is desired.
 * @param {string} layer - The layer name for the file.
 * @param {object} data - The GeoJSON data to save.
 * @returns {void}
 */
function saveToFile(layer, data) {
	const filePath = path.join(outputDir, `${layer}.geojson`);
	fs.writeFileSync(filePath, JSON.stringify(data, null, "\t"));
	console.log(`Saved: ${layer}.geojson`);
}

/**
 * Uploads or updates metadata for a layer in CouchDB.
 * @param {string} layer - The layer name.
 * @param {number} featureCount - The number of features in the layer.
 * @returns {Promise<void>}
 * @throws {Error} If the upload fails.
 */
async function uploadLayerMetadata(layer, featureCount) {
	const docId = `layer_metadata:${layer}`;

	const doc = {
		_id: docId,
		// biome-ignore lint/style/useNamingConvention: database
		feature_count: featureCount,
		// biome-ignore lint/style/useNamingConvention: database
		last_updated: new Date().toISOString(),
		name: layer,
		projection: "EPSG:4326",
		type: "layer_metadata",
	};

	const url = `${couchUrl}/${dbName}/${docId}`;
	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)`);
	return;
}

/**
 * Uploads a single feature document to CouchDB, with deduplication check.
 * @param {object} doc - The feature document to upload.
 * @returns {Promise<boolean>} True if uploaded/updated, false if skipped (no changes).
 * @throws {Error} If the upload fails.
 */
async function uploadFeature(doc) {
	const url = `${couchUrl}/${dbName}/${doc._id}`;
	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",
	});

	if (!putRes.ok) throw new Error(await putRes.text());
	return true; // uploaded or updated
}

/**
 * Processes a single layer: downloads GeoJSON, uploads features with dedup, and updates metadata.
 * @param {string} layer - The layer to process.
 * @returns {Promise<{uploaded: number, skipped: number}>} Counts of uploaded and skipped features.
 * @throws {Error} If download or uploads fail.
 */
async function processLayer(layer) {
	const geojson = await downloadLayer(layer);
	if (!geojson || !geojson.features) {
		throw new Error(`No features in ${layer}: ${JSON.stringify(geojson)}`);
	}

	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}`,
			// biome-ignore lint/style/useNamingConvention: database
			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}`);
	return { skipped, uploaded };
}

/**
 * Main entry point: sets up database, processes all layers.
 * @returns {Promise<void>}
 */
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.");
	return;
}

if (process.argv[1] === new URL(import.meta.url).pathname) {
	main().catch(console.error);
}