Created
September 28, 2023 13:08
-
-
Save 2803media/ffe5e825883d2b6f568a62de22dcebf5 to your computer and use it in GitHub Desktop.
jq geojson
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
cat /home/lecadastre/public_html/update/new_communes/sources/communes-5m.json | jq ".features[].properties" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment