Created
May 29, 2023 18:04
-
-
Save akhenakh/180a9830dd4105c3640c5a97db5e21e2 to your computer and use it in GitHub Desktop.
Convert big GeoJSON FeatureCollections into a line by line features without loading the full file into memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"io" | |
"os" | |
"github.com/peterstace/simplefeatures/geom" | |
) | |
func main() { | |
r := bufio.NewReader(os.Stdin) | |
decoder := json.NewDecoder(r) | |
var f geom.GeoJSONFeature | |
for { | |
token, err := decoder.Token() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
panic(err) | |
} | |
if token == "features" { | |
_, err = decoder.Token() | |
if err != nil { | |
panic(err) | |
} | |
break | |
} | |
} | |
enc := json.NewEncoder(os.Stdout) | |
for decoder.More() { | |
if err := decoder.Decode(&f); err != nil { | |
panic(err) | |
} | |
if err := enc.Encode(f); err != nil { | |
panic(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jq is great but the command
cat big.geoson | jq -c '.features[]' > big.geojsonseq
is using a lot of memory.Use this snippet like this.
cat big.geoson |./jsonparser > big.geojsonseq