Last active
May 15, 2019 04:59
-
-
Save jeroendereu/9b54e7d079f0ed7f4c7e91546384b62d to your computer and use it in GitHub Desktop.
Conduct a simple split by attributes on a shapefile. Similar to tools in QGIS and ArcGIS, but much faster (especially when working with large files)! A new shapefile will be created for every (group of) unique attribute(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false, | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import tkinter.filedialog as tkfd\n", | |
"import tkinter.simpledialog as tksd\n", | |
"import fiona" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false, | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# locate your input shapefile\n", | |
"infile = tkfd.askopenfilename(title = \"Select your shapefile\", \n", | |
" initialdir = \"C:/\", \n", | |
" filetypes = ((\"shapefile\", \"*.shp\"),(\"all files\", \"*.*\")))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# define destination -folder- where output -shapefiles- will be written\n", | |
"dest = tkfd.askdirectory(title = \"Select your output folder\", \n", | |
" initialdir = \"C:/\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# define field to split by attributes\n", | |
"field = tksd.askstring(\"Field\", \"Enter the name of the field to conduct the split by attributes on\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Works fine, for both unique and non-unique attribute values!\n", | |
"# records with the same attributes are grouped in seperate, new shapefiles...\n", | |
"# PROPER split by attributes for shapefiles! Hell Yeh!!\n", | |
"\n", | |
"with fiona.open(infile) as source:\n", | |
" meta = source.meta\n", | |
" for f in source:\n", | |
" outfile = os.path.join(dest, \"%s.shp\" % f['properties'][field])\n", | |
" try:\n", | |
" with fiona.open(outfile, 'a', **meta) as sink:\n", | |
" sink.write(f)\n", | |
" except:\n", | |
" with fiona.open(outfile, 'w', **meta) as sink:\n", | |
" sink.write(f)" | |
] | |
} | |
], | |
"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.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment