Please review any and all PUBLIC repositories, groups and associate files. These allow anyone on the Internet to access without authentication. Repository and group owners are responsible for their content and permission settings. Go to your project(s), click on Settings > General and expand the "Visibility, project features, permissions" to change this setting.

Commit 5a5931ef authored by Wigal, Jacob (CIV)'s avatar Wigal, Jacob (CIV)

Delete geojson_conversion.ipynb

parent 7b57463b
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### About\n",
"\n",
"This notebook is for:\n",
"\n",
"Converting geospatial data files such as Shapefiles, Geopackages, and KMLs to GeoJSON files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GeoJSON Conversion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For Shapefiles, Geopackages, KML, and others:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Running the next cell will prompt you to enter the filepath of the folder containing all the files you wish to convert to GeoJSON. Then, it will attempt to convert any geospatial data file within the folder ((e.g. if you have three shapefiles in the directory, there will be three converted geojson files). It will place the converted files in a new folder within your directory called \"geojson\". This works for all layers that geopandas can read, which is essentially all normal vector layer types GeoJSON for every shape file. Note: Running this cell will also reproject the data to CRS WGS84 (EPSG 4326) for all files with a different CRS."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import geopandas as gpd\n",
"import re\n",
"\n",
"def convert2geojson():\n",
" \n",
" # Ask user for filepath of folder containing files to convert\n",
" def enterfilepath():\n",
" while True:\n",
" filepath=(input(\"Enter filepath of the folder containing your files: \"))\n",
" if os.path.exists(filepath):\n",
" break\n",
" print(\"Not a valid filepath.\")\n",
" os.chdir(filepath)\n",
" print(\"Converting all files in\" , os.getcwd(), 'to geojson.')\n",
" enterfilepath()\n",
" \n",
" # Make a geojson folder if it doesn't exist\n",
" current_directory = os.getcwd()\n",
" geojson_directory = current_directory + '/geojson'\n",
"\n",
" if not os.path.exists(geojson_directory):\n",
" os.mkdir('geojson')\n",
" print('Will place all converted files in new \"geojson\" folder.' + '\\n')\n",
" else:\n",
" print('Will place all converted files in existing \"geojson\" folder.' + '\\n')\n",
" \n",
" # Iterate over files and attempt to convert all to geojson\n",
" for file in os.listdir(current_directory):\n",
" \n",
" # Skip folders and listed filetypes\n",
" if os.path.isdir(file):\n",
" continue\n",
" if not file.lower().endswith(('txt','jpg','jpeg','geojson','ds_store','pdf',\\\n",
" 'xlsx','doc','docx','git','qpj','prj','dbf','shx','cpg')):\n",
" print('Converting ' + file + '...')\n",
" \n",
" try: \n",
" # Load data\n",
" file = str(current_directory) + '/' + str(file)\n",
" data = gpd.read_file(file)\n",
" \n",
" # Alert user if CRS is changing with conversion\n",
" if data.crs['init'] != 'epsg:4326':\n",
" match = re.search('epsg:.[^\\']*', str(data.crs)) #use regex to split each dms string into integer components to input in the dms_to_decimal function\n",
" crs = str(match.group())\n",
" print('Original CRS was ' + crs + '...will be converted to epsg:4326')\n",
" \n",
" # Create new filepath with geojson folder and extension\n",
" head, tail = os.path.split(file)\n",
" filename = head + '/geojson/'+ tail\n",
" filename_ext = os.path.splitext(filename)[0] + '.geojson'\n",
" data.to_file(filename_ext, driver = 'GeoJSON')\n",
" #Print success message\n",
" print('Success: ' + file + ' was converted to GeoJSON' + '\\n')\n",
" \n",
" except:\n",
" # Alert user if file conversion failed\n",
" print('EXCEPTION: '+ file + ' is not a geopandas readable filetype') \n",
" print(file + ' could not be converted.' + '\\n')\n",
"\n",
" print('Done.')\n",
" \n",
"convert2geojson()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment