Skip to content

Instantly share code, notes, and snippets.

@giswqs
Created September 10, 2020 01:58
Show Gist options
  • Save giswqs/265433e8d2dff292aa7997ade5c77901 to your computer and use it in GitHub Desktop.
Save giswqs/265433e8d2dff292aa7997ade5c77901 to your computer and use it in GitHub Desktop.
merge geojsons
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import ee\nimport geemap",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Map = geemap.Map()\nMap",
"execution_count": 2,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "82e0772a1d1e48f6a455a4de866b5890"
}
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# poly1 = geemap.geojson_to_ee('poly1.geojson')",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Map.addLayer(poly1, {}, \"Poly1\")",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Map.centerObject(poly1, 16)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import glob",
"execution_count": 6,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Search all geojson files under the working directory"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "files = glob.glob('*.geojson')\nprint(files)",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "['poly2.geojson', 'poly1.geojson', 'poly3.geojson']\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ee_list = []",
"execution_count": 8,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Convert geojson files to FeatureCollection"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for file in files:\n ee_list.append(geemap.geojson_to_ee(file))",
"execution_count": 9,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Merge all feature collections as one single FeatureCollection"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "fc = ee.FeatureCollection(ee_list).flatten()",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "fc.getInfo()",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "{'type': 'FeatureCollection',\n 'columns': {'clmt_rg': 'String',\n 'country': 'String',\n 'county': 'String',\n 'dscrptn': 'String',\n 'dt_cllc': 'String',\n 'dt_frmt': 'String',\n 'elevatn': 'Float',\n 'email': 'String',\n 'entity': 'String',\n 'exprmnt': 'String',\n 'fild_cd': 'String',\n 'fild_nm': 'String',\n 'fl_frmt': 'String',\n 'max_lat': 'Float',\n 'max_lng': 'Float',\n 'min_lat': 'Float',\n 'min_lng': 'Float',\n 'sbmttd_': 'String',\n 'season': 'String',\n 'species': 'String',\n 'state': 'String',\n 'system:index': 'String',\n 'zip_cod': 'String'},\n 'features': [{'type': 'Feature',\n 'geometry': {'type': 'Polygon',\n 'coordinates': [[[-97.05166597033683, 41.974425278649925],\n [-97.05334939216945, 41.97445319563305],\n [-97.0534431660458, 41.97314640174551],\n [-97.05175467769998, 41.9730905666214],\n [-97.05166597033683, 41.974425278649925]]]},\n 'id': '0_0',\n 'properties': {'clmt_rg': 'Continental, no dry season, hot summer',\n 'country': 'United States of America',\n 'county': 'Stanton',\n 'dscrptn': 'Corteva Exchange',\n 'dt_cllc': '2020-08-21',\n 'dt_frmt': 'vector',\n 'elevatn': 444.9,\n 'email': '[email protected]',\n 'entity': 'Benson Hill',\n 'exprmnt': 'Field Trials',\n 'fild_cd': 'NENF',\n 'fild_nm': 'Norfolk, NE - NENF - 23-26',\n 'fl_frmt': 'geoJson',\n 'max_lat': 41.97445319563305,\n 'max_lng': -97.05166597033683,\n 'min_lat': 41.9730905666214,\n 'min_lng': -97.0534431660458,\n 'sbmttd_': 'Pedro Gonzalez',\n 'season': '2020S',\n 'species': 'Glycine max',\n 'state': 'Nebraska',\n 'zip_cod': '68701'}},\n {'type': 'Feature',\n 'geometry': {'type': 'Polygon',\n 'coordinates': [[[-97.05166597033683, 41.974425278649925],\n [-97.05334939216945, 41.97445319563305],\n [-97.0534431660458, 41.97314640174551],\n [-97.05175467769998, 41.9730905666214],\n [-97.05166597033683, 41.974425278649925]]]},\n 'id': '1_0',\n 'properties': {'clmt_rg': 'Continental, no dry season, hot summer',\n 'country': 'United States of America',\n 'county': 'Stanton',\n 'dscrptn': 'Corteva Exchange',\n 'dt_cllc': '2020-08-21',\n 'dt_frmt': 'vector',\n 'elevatn': 444.9,\n 'email': '[email protected]',\n 'entity': 'Benson Hill',\n 'exprmnt': 'Field Trials',\n 'fild_cd': 'NENF',\n 'fild_nm': 'Norfolk, NE - NENF - 23-26',\n 'fl_frmt': 'geoJson',\n 'max_lat': 41.97445319563305,\n 'max_lng': -97.05166597033683,\n 'min_lat': 41.9730905666214,\n 'min_lng': -97.0534431660458,\n 'sbmttd_': 'Pedro Gonzalez',\n 'season': '2020S',\n 'species': 'Glycine max',\n 'state': 'Nebraska',\n 'zip_cod': '68701'}},\n {'type': 'Feature',\n 'geometry': {'type': 'Polygon',\n 'coordinates': [[[-97.05166597033683, 41.974425278649925],\n [-97.05334939216945, 41.97445319563305],\n [-97.0534431660458, 41.97314640174551],\n [-97.05175467769998, 41.9730905666214],\n [-97.05166597033683, 41.974425278649925]]]},\n 'id': '2_0',\n 'properties': {'clmt_rg': 'Continental, no dry season, hot summer',\n 'country': 'United States of America',\n 'county': 'Stanton',\n 'dscrptn': 'Corteva Exchange',\n 'dt_cllc': '2020-08-21',\n 'dt_frmt': 'vector',\n 'elevatn': 444.9,\n 'email': '[email protected]',\n 'entity': 'Benson Hill',\n 'exprmnt': 'Field Trials',\n 'fild_cd': 'NENF',\n 'fild_nm': 'Norfolk, NE - NENF - 23-26',\n 'fl_frmt': 'geoJson',\n 'max_lat': 41.97445319563305,\n 'max_lng': -97.05166597033683,\n 'min_lat': 41.9730905666214,\n 'min_lng': -97.0534431660458,\n 'sbmttd_': 'Pedro Gonzalez',\n 'season': '2020S',\n 'species': 'Glycine max',\n 'state': 'Nebraska',\n 'zip_cod': '68701'}}]}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Map.addLayer(fc, {}, \"ROIs\")",
"execution_count": 12,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"hide_input": false,
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Table of Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"language_info": {
"name": "python",
"version": "3.8.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "merge geojsons",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment