"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",
" # 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",
" 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
}
%% Cell type:markdown id: tags:
### About
This notebook is for:
Converting geospatial data files such as Shapefiles, Geopackages, and KMLs to GeoJSON files
%% Cell type:markdown id: tags:
# GeoJSON Conversion
%% Cell type:markdown id: tags:
### For Shapefiles, Geopackages, KML, and others:
%% Cell type:markdown id: tags:
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 id: tags:
``` python
importos
importgeopandasasgpd
importre
defconvert2geojson():
# Ask user for filepath of folder containing files to convert
defenterfilepath():
whileTrue:
filepath=(input("Enter filepath of the folder containing your files: "))
ifos.path.exists(filepath):
break
print("Not a valid filepath.")
os.chdir(filepath)
print("Converting all files in",os.getcwd(),'to geojson.')
enterfilepath()
# Make a geojson folder if it doesn't exist
current_directory=os.getcwd()
geojson_directory=current_directory+'/geojson'
ifnotos.path.exists(geojson_directory):
os.mkdir('geojson')
print('Will place all converted files in new "geojson" folder.'+'\n')
else:
print('Will place all converted files in existing "geojson" folder.'+'\n')
# Iterate over files and attempt to convert all to geojson