Skip to content

Commit 9658e82

Browse files
committed
Readd geojson2ndgeojson
1 parent 316c9eb commit 9658e82

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# geojson2ndgeojson
2+
3+
_GeoJSON to Newline Delimited GeoJSON_
4+
5+
## Purpose
6+
7+
In the case of needing to upload large geojson file, 500mb+, it is easier for stream uploading services to chunk such file where each feature is delimited by a new line.
8+
9+
### Turns:
10+
11+
_test.geojson_
12+
13+
```json
14+
{
15+
"type": "FeatureCollection",
16+
"features": [
17+
{
18+
"type": "Feature",
19+
"properties": { "name": "A" },
20+
"geometry": {
21+
"type": "Point",
22+
"coordinates": [[137, -4]]
23+
}
24+
},
25+
{
26+
"type": "Feature",
27+
"properties": { "name": "B" },
28+
"geometry": {
29+
"type": "Point",
30+
"coordinates": [[136, -4]]
31+
}
32+
}
33+
]
34+
}
35+
```
36+
37+
### Into:
38+
39+
_test.ndgeojson_
40+
41+
```json
42+
{"type":"Feature","properties":{"name":"A"},"geometry":{"type":"Point","coordinates":[[137,-4]]}}
43+
{"type":"Feature","properties":{"name":"B"},"geometry":{"type":"Point","coordinates":[[136,-4]]}}
44+
```
45+
46+
## Running
47+
48+
Install Dependencies:
49+
50+
```
51+
npm install JSONStream
52+
```
53+
54+
Run:
55+
56+
```
57+
node geojson2ndgeojson.js test.geojson
58+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// GeoJSON to Newline Delimited GeoJSON
2+
// npm install JSONStream
3+
// node geojson2ndgeojson.js test.geojson
4+
5+
const fs = require("fs");
6+
const readline = require("readline");
7+
const JSONStream = require("JSONStream");
8+
9+
const inputFile = process.argv[2];
10+
if (inputFile == null) {
11+
console.error("Please provide and input GeoJSON file path.");
12+
process.exit(1);
13+
}
14+
15+
const outputFile = inputFile
16+
.replace(".json", ".ndgeojson")
17+
.replace(".geojson", ".ndgeojson");
18+
19+
const readStream = fs.createReadStream(inputFile, { encoding: "utf8" });
20+
const writeStream = fs.createWriteStream(outputFile, { encoding: "utf8" });
21+
22+
const parser = JSONStream.parse("features.*");
23+
24+
console.log("Processing...");
25+
26+
readStream.pipe(parser);
27+
28+
parser.on("data", (feature) => {
29+
writeStream.write(JSON.stringify(feature) + "\n");
30+
});
31+
32+
parser.on("end", () => {
33+
console.log("Processing complete!");
34+
writeStream.end();
35+
});
36+
37+
parser.on("error", (err) => {
38+
console.error("Error parsing JSON:", err);
39+
});
40+
41+
writeStream.on("error", (err) => {
42+
console.error("Error writing to file:", err);
43+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": { "name": "A" },
7+
"geometry": {
8+
"type": "Point",
9+
"coordinates": [[137, -4]]
10+
}
11+
},
12+
{
13+
"type": "Feature",
14+
"properties": { "name": "B" },
15+
"geometry": {
16+
"type": "Point",
17+
"coordinates": [[136, -4]]
18+
}
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)