Skip to content

Commit

Permalink
Use sax to process OSM data in a streaming manner
Browse files Browse the repository at this point in the history
  • Loading branch information
rudokemper committed May 8, 2024
1 parent 5c7d94c commit 6fabfbd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
12 changes: 2 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"path": "^0.12.7",
"pg": "^8.11.5",
"pmtiles": "3.0.3",
"sharp": "^0.33.2",
"xmldom": "^0.6.0"
"sax": "^1.3.0",
"sharp": "^0.33.2"
},
"devDependencies": {
"@babel/core": "^7.23.9",
Expand Down
49 changes: 28 additions & 21 deletions src/download_resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs";
import path from "path";
import axios from "axios";
import pLimit from "p-limit";
import sax from "sax";
import osmtogeojson from "osmtogeojson";
import xmldom from "xmldom";

Expand Down Expand Up @@ -334,28 +335,34 @@ export const requestOpenStreetMapData = async (bounds, tempDir) => {
console.log(`OpenStreetMap data downloaded!`);

// Convert OSM XML data to OSM JSON using xmldom
// In the future, we might want to use a more robust OSM parser like
// https://github.com/tyrasd/osmtogeojson/blob/gh-pages/parse_osmxml.js
const parser = new xmldom.DOMParser();
const osmData = parser.parseFromString(
fs.readFileSync(`${outputDir}/data.osm`, "utf-8"),
);
console.log(`Converting OpenStreetMap data to GeoJSON...`);

// Convert OSM JSON to GeoJSON
const geojson = osmtogeojson(osmData);
const parser = sax.createStream(true);
let osmData = "";

// Filter out lines and points only
geojson.features = geojson.features.filter(
(feature) =>
feature.geometry.type === "LineString" ||
feature.geometry.type === "Point",
);
parser.on("text", (text) => {
osmData += text;
});

fs.writeFileSync(
`${outputDir}/openstreetmap.geojson`,
JSON.stringify(geojson, null, 4),
);
console.log(
`\x1b[32mOpenStreetMap data successfully downloaded and converted to GeoJSON!\x1b[0m`,
);
parser.on("end", () => {
// Convert OSM JSON to GeoJSON
const geojson = osmtogeojson(osmData);

// Filter out lines and points only
geojson.features = geojson.features.filter(
(feature) =>
feature.geometry.type === "LineString" ||
feature.geometry.type === "Point",
);

fs.writeFileSync(
`${outputDir}/openstreetmap.geojson`,
JSON.stringify(geojson, null, 4),
);
console.log(
`\x1b[32mOpenStreetMap data successfully downloaded and converted to GeoJSON!\x1b[0m`,
);
});

fs.createReadStream(`${outputDir}/data.osm`).pipe(parser);
};

0 comments on commit 6fabfbd

Please sign in to comment.