-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add python code to convert geojson to json
- Loading branch information
Showing
4 changed files
with
478 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Geojson2json.py is a simple script that directly converts GeoJSON files extracted using the GPlates GUI to the World Builder-compatible JSON format. Currently, it supports continental plate geometries. \n", | ||
"\n", | ||
"The script reads the GeoJSON file, processes polygon features, and assigns default or provided values for \n", | ||
"maximum depth and compositions. The output JSON file follows the World Builder structure, making it ready \n", | ||
"for use in geodynamic modeling applications.\n", | ||
"\n", | ||
"Future improvements may integrate this script with GPlate2GWB for a more streamlined workflow.\n", | ||
"\n", | ||
"Usage:\n", | ||
" python geojson2json.py\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"\n", | ||
"# Configuration defined by the user\n", | ||
"geojson_file = \"reconstructed_400.00Ma.geojson\" # Input GeoJSON file\n", | ||
"output_file = \"world_builder_model.json\" # Output JSON file\n", | ||
"default_max_depth = 350e3\n", | ||
"default_compositions = [0]\n", | ||
"\n", | ||
"try:\n", | ||
" # Load GeoJSON file\n", | ||
" with open(geojson_file, 'r') as f:\n", | ||
" geojson_data = json.load(f)\n", | ||
"\n", | ||
" features = []\n", | ||
"\n", | ||
" for feature in geojson_data.get('features', []):\n", | ||
" if feature.get('geometry', {}).get('type') == 'Polygon':\n", | ||
" name = feature.get('properties', {}).get('NAME', 'Unknown')\n", | ||
" coordinates = [list(coord) for coord in feature['geometry']['coordinates'][0]]\n", | ||
"\n", | ||
" # Use provided properties or fall back to defaults\n", | ||
" max_depth = feature.get('properties', {}).get('MAX_DEPTH', default_max_depth)\n", | ||
" compositions = feature.get('properties', {}).get('COMPOSITIONS', default_compositions)\n", | ||
"\n", | ||
" feature_data = {\n", | ||
" \"model\": \"continental plate\",\n", | ||
" \"name\": name,\n", | ||
" \"max depth\": max_depth,\n", | ||
" \"coordinates\": coordinates,\n", | ||
" \"composition models\": [\n", | ||
" {\n", | ||
" \"model\": \"uniform\",\n", | ||
" \"compositions\": compositions,\n", | ||
" \"max depth\": max_depth\n", | ||
" }\n", | ||
" ]\n", | ||
" }\n", | ||
" features.append(feature_data)\n", | ||
"\n", | ||
" output_data = {\n", | ||
" \"version\": \"1.1\",\n", | ||
" \"interpolation\": \"continuous monotone spline\",\n", | ||
" \"coordinate system\": {\n", | ||
" \"model\": \"spherical\",\n", | ||
" \"depth method\": \"starting point\"\n", | ||
" },\n", | ||
" \"features\": features\n", | ||
" }\n", | ||
"\n", | ||
" # Save output to JSON file\n", | ||
" with open(output_file, 'w') as out_f:\n", | ||
" json.dump(output_data, out_f, indent=4)\n", | ||
"\n", | ||
" print(f\"Conversion successful! Output saved to {output_file}\")\n", | ||
"\n", | ||
"except Exception as e:\n", | ||
" print(f\"Error processing file: {e}\")\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "extra_postprocess", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.15" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
} | ||
] | ||
} | ||
|