Skip to content
Snippets Groups Projects
Commit 5a5931ef authored by Wigal, Jacob (CIV)'s avatar Wigal, Jacob (CIV) :sparkles:
Browse files

Delete geojson_conversion.ipynb

parent 7b57463b
No related branches found
No related tags found
No related merge requests found
%% 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
import os
import geopandas as gpd
import re
def convert2geojson():
# Ask user for filepath of folder containing files to convert
def enterfilepath():
while True:
filepath=(input("Enter filepath of the folder containing your files: "))
if os.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'
if not os.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
for file in os.listdir(current_directory):
# Skip folders and listed filetypes
if os.path.isdir(file):
continue
if not file.lower().endswith(('txt','jpg','jpeg','geojson','ds_store','pdf',\
'xlsx','doc','docx','git','qpj','prj','dbf','shx','cpg')):
print('Converting ' + file + '...')
try:
# Load data
file = str(current_directory) + '/' + str(file)
data = gpd.read_file(file)
# Alert user if CRS is changing with conversion
if data.crs['init'] != 'epsg:4326':
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
crs = str(match.group())
print('Original CRS was ' + crs + '...will be converted to epsg:4326')
# Create new filepath with geojson folder and extension
head, tail = os.path.split(file)
filename = head + '/geojson/'+ tail
filename_ext = os.path.splitext(filename)[0] + '.geojson'
data.to_file(filename_ext, driver = 'GeoJSON')
#Print success message
print('Success: ' + file + ' was converted to GeoJSON' + '\n')
except:
# Alert user if file conversion failed
print('EXCEPTION: '+ file + ' is not a geopandas readable filetype')
print(file + ' could not be converted.' + '\n')
print('Done.')
convert2geojson()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment