Skip to content

Instantly share code, notes, and snippets.

@arbakker
Last active September 28, 2023 13:07
Show Gist options
  • Save arbakker/dd339a92f83a68c78136573d7ae08147 to your computer and use it in GitHub Desktop.
Save arbakker/dd339a92f83a68c78136573d7ae08147 to your computer and use it in GitHub Desktop.
Manipulate GeoJSON/JSON with jq

Manipulate (Geo)JSON with jq

Remove attributes with jq from a GeoJSON file:

cat points.geojson | jq ".features[].properties |= del(.c) | .features[].properties |= del(.d)"

Add property with default value:

cat points.geojson | jq ".features[].properties.z = true"

Add nested property with default values:

cat points.geojson | jq '.features[].properties.nested2 = {"q": "1","r": "2"}'

Unstringify stringified JSON:

cat points.geojson | jq '.features[].properties.nestedstring |= fromjson' 

Stringify property:

cat points.geojson | jq '.features[].properties.nested |= tostring' 

Rename property:

cat points.geojson | jq '.features[].properties |= with_entries(if .key == "d" then .key = "e" else . end)'

Add property with random value (calling different program):

#!/usr/bin/env bash
set -euo pipefail

function propValFunction(){
    shuf -n1  /usr/share/dict/dutch | xargs -0 echo -n
}

function addPropNameGeoJSON(){
    geojson_file="$1"
    prop_name="$2"
    prop_function="$3"
    count=0
    jq -c '.features[]' < "$geojson_file" |
    while IFS=$"\n" read -r c; do
        prop_val=$($prop_function) 
        jq  ".features[$count].properties.$prop_name = \"${prop_val}\"" < "$geojson_file" | sponge "$geojson_file"
        count=$(bc<<<"$count+1")
    done
}

GEOJSON_FILE="points.geojson"
PROP_NAME="label"

addPropNameGeoJSON $GEOJSON_FILE $PROP_NAME propValFunction

Convert JSON to GeoJSON:

JSON_STRING='{"locations": [{"lon": 151.079209240775, "lat": -33.8510778634911, "time": 1535260212.13903}, {"lon": 151.075161398092, "lat": -33.8468726325816, "time": 1535261260.22272}, {"lon": 151.074015443979, "lat": -33.8385713830357, "time": 1535261877.66436}]}'
JQ_QUERY='{"type": "FeatureCollection","name": "points","features":[.locations[] | {"type":"Feature","properties":{"time":.time},"geometry":{"type":"Point","coordinates":[.lon,.lat]}}]}'
echo $JSON_STRING  | jq  $JQ_QUERY

Query GeoJSON

Get first x features:

jq '.features = .features[:10]' < $GEOJSON_FILE

Get features with property url that satisfy regex:

jq '.features = [.features[] | select(.properties.url | test("M_01CZ2.tif|M_01HZ1.tif|M_01CZ1.tif|M_01DZ2.tif|M_01GN1.tif|M_01GZ2.tif|M_01GZ1.tif|M_01DZ1.tif|M_01GN2.tif|M_01HN1.tif"))]' < $GEOJSON_FILE
Display the source blob
Display the rendered blob
Raw
{
"type": "FeatureCollection",
"name": "points",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"a": "a",
"b": "b",
"c": "c",
"d": "d",
"nested": {
"q": "1",
"r": "2"
},
"nestedstring": "{\"q\":\"1\",\"r\":\"2\"}"
},
"geometry": {
"type": "Point",
"coordinates": [
6.65493,
28.690141
]
}
},
{
"type": "Feature",
"properties": {
"a": "e",
"b": "f",
"c": "g",
"d": "h",
"nested": {
"q": "1",
"r": "2"
},
"nestedstring": "{\"q\":\"1\",\"r\":\"2\"}"
},
"geometry": {
"type": "Point",
"coordinates": [
3.105634,
14.640845
]
}
},
{
"type": "Feature",
"properties": {
"a": "1",
"b": "2",
"c": "3",
"d": "4",
"nested": {
"q": "1",
"r": "2"
},
"nestedstring": "{\"q\":\"1\",\"r\":\"2\"}"
},
"geometry": {
"type": "Point",
"coordinates": [
25.584507,
27.47007
]
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment