Created
October 13, 2023 11:10
-
-
Save mikkohei13/bf6febd2d15880b8002383c627a98347 to your computer and use it in GitHub Desktop.
Convert FinBIF named places file (line transects) into geojson
This file contains hidden or 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
import json | |
def process_geojson_with_properties(input_filename, output_filename): | |
# Load the JSON file | |
with open(input_filename, "r") as file: | |
data = json.load(file) | |
# Extract all items under "results" | |
results = data.get("results", []) | |
features = [] | |
# Loop through each item in results | |
for result in results: | |
# Extract the desired path | |
gatherings = result.get("acceptedDocument", {}).get("gatherings", []) | |
result_id = result.get("id", "") | |
result_name = result.get("name", "") | |
master_linestring = [] | |
# Loop through each item in the gatherings list | |
for gathering in gatherings: | |
geometry = gathering.get("geometry", {}) | |
if geometry.get("type") == "LineString": | |
master_linestring.extend(geometry.get("coordinates", [])) | |
# Create a GeoJSON structure for the master linestring | |
feature = { | |
"type": "Feature", | |
"geometry": { | |
"type": "LineString", | |
"coordinates": master_linestring | |
}, | |
"properties": { | |
"id": result_id, | |
"name": result_name | |
} | |
} | |
features.append(feature) | |
geojson_data = { | |
"type": "FeatureCollection", | |
"features": features | |
} | |
# Save the GeoJSON data to a file | |
with open(output_filename, "w") as file: | |
json.dump(geojson_data, file) | |
# Re-process the named-places-multiple.json file with the modified function | |
process_geojson_with_properties("named-places.json", "routes.geojson") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment