Skip to content

Commit

Permalink
add python code to convert geojson to json
Browse files Browse the repository at this point in the history
  • Loading branch information
Minerallo committed Jan 23, 2025
1 parent 001baa8 commit bb1c0e2
Show file tree
Hide file tree
Showing 4 changed files with 478 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ Testing/
doc/doxygen/
*.xcf
tests/unit_tests/approval_tests/received/*
contrib/gplate/data
contrib/gplate/plate-model-repo
contrib/pmm.log
108 changes: 108 additions & 0 deletions contrib/gplate/geojson2json.ipynb
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
}
366 changes: 366 additions & 0 deletions contrib/gplate/gplately2gwb.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/gwb-grid/continental_plate_composition_random.wb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
}
]
}

0 comments on commit bb1c0e2

Please sign in to comment.