Created
April 9, 2018 23:05
Star
You must be signed in to star a gist
KML polygons to Shapefiles
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": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Populating the interactive namespace from numpy and matplotlib\n" | |
] | |
} | |
], | |
"source": [ | |
"%pylab inline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import zipfile\n", | |
"zip_ref = zipfile.ZipFile('all_refuge_kml_files.zip', 'r')\n", | |
"zip_ref.extractall('kml')\n", | |
"zip_ref.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"kml_files = [os.path.join(root, name)\n", | |
" for root, dirs, files in os.walk('kml')\n", | |
" for name in files\n", | |
" if name.endswith((\"kml\"))]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import fiona\n", | |
"import geopandas as gpd\n", | |
"fiona.drvsupport.supported_drivers['kml'] = 'rw'\n", | |
"fiona.drvsupport.supported_drivers['KML'] = 'rw'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"AnchorageCoastalWR GC p GC p \n", | |
"AnchorRiverFritzCreekCHA \n", | |
"CapeNewenhamSGR p \n", | |
"ChilkatRiverCHA GC p \n", | |
"CinderRiverCHA GC p \n", | |
"ClamGulchCHA p \n", | |
"CopperRiverCHA GC p \n", | |
"CreamersFieldMWR GC p \n", | |
"DeltaJunctionBisonRange p \n", | |
"DudeCreekCHA GC p \n", | |
"EgegikCHA GC p GC p \n", | |
"FoxRiverFlatsCHA GC p \n", | |
"GooseBaySGR GC p \n", | |
"HomerCHA GC p \n", | |
"HomerCHASP4 p \n", | |
"IzembekCHA p p p p p p p p p p p p p p p p p p p p p p p \n", | |
"KachemakBayCHA p p \n", | |
"KalginCHA GC p \n", | |
"MatanuskaValleyMooseRange p \n", | |
"McNeilSGR GC p \n", | |
"McNeilSGS GC p \n", | |
"MendenhallSGR GC p \n", | |
"MintoFlatsSGR GC p p GC p \n", | |
"OtherLDA GC p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p GC p GC p GC p GC p p GC p GC p GC p GC p p p p p p p p p p p p p GC p p p p GC p GC p GC p \n", | |
"PalmerFlatsSGR GC p GC p p GC p GC p \n", | |
"PilotPointCHA GC p GC p GC p \n", | |
"PortHeidenCHA GC p GC p \n", | |
"PortMollerCHA GC p p p GC p GC p GC p GC p GC p GC p GC p \n", | |
"RedoubtBayCHA GC p \n", | |
"StanPriceSWS GC p \n", | |
"SusitnaSGR GC p \n", | |
"TradingBaySGR GC p \n", | |
"TugidakIslandCHA GC p \n", | |
"WalrusIslandSGS p p p \n", | |
"WillowMountainCHA GC p \n", | |
"YakatageSGR GC p\n" | |
] | |
} | |
], | |
"source": [ | |
"plist = []\n", | |
"nlist = []\n", | |
"dlist = []\n", | |
"for fn in kml_files:\n", | |
" kml = gpd.read_file(fn)\n", | |
" bn = os.path.basename(fn).split('.')[0]\n", | |
" print (\"\\n\" + bn + \" \"),\n", | |
" for ind, row in kml.iterrows():\n", | |
" g = row['geometry']\n", | |
" gt = g.geom_type\n", | |
" if gt == 'Polygon':\n", | |
" print 'p',\n", | |
" plist.append(g)\n", | |
" nlist.append(bn)\n", | |
" dlist.append(row['Description'])\n", | |
" elif gt == 'GeometryCollection':\n", | |
" print 'GC',\n", | |
" for g in row['geometry']:\n", | |
" if g.geom_type == 'Polygon':\n", | |
" print 'p',\n", | |
" plist.append(g)\n", | |
" nlist.append(bn)\n", | |
" dlist.append(row['Description'])\n", | |
"# else:\n", | |
"# print ' Porkchop'\n", | |
"\n", | |
"gdf = gpd.GeoDataFrame({'Name': nlist, 'Description': dlist, 'geometry': plist})\n", | |
"gdf.crs = {'init' :'epsg:4326'}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Save Polygons\n", | |
"\n", | |
"Except for OtherLDA. I'll save those next." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"gdf.query(\"Name != 'OtherLDA'\").to_file('polygons.shp')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Save OtherLDA" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# other_fn = \"kml/KML/OtherLDA.kml\"\n", | |
"# other = gpd.read_file(other_fn)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"other = gdf.query(\"Name == 'OtherLDA'\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"tbl = other.Description.iloc[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table border=0 cellpadding=0 cellspacing=0 width=250 style=\"FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;\"><tr><td bgcolor=\"#E3E1CA\" align=\"right\"><font COLOR=\"#000000\"><b>LONGNAME</b></font></td><td bgcolor=\"#E4E6CA\"> <font COLOR=\"#008000\">Aleutian Islands Refuge</font></td></tr><tr><td bgcolor=\"#E3E1CA\" align=\"right\"><font COLOR=\"#000000\"><b>DSCRPTND</b></font></td><td bgcolor=\"#E4E6CA\"> <font COLOR=\"#008000\">AS 16.20.030A1</font></td></tr><tr><td bgcolor=\"#E3E1CA\" align=\"right\"><font COLOR=\"#000000\"><b>SHAPE_AREA</b></font></td><td bgcolor=\"#E4E6CA\"> <font COLOR=\"#008000\">15370.9634027</font></td></tr></table>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from IPython.display import HTML\n", | |
"HTML(tbl)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"u'<table border=0 cellpadding=0 cellspacing=0 width=250 style=\"FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;\"><tr><td bgcolor=\"#E3E1CA\" align=\"right\"><font COLOR=\"#000000\"><b>LONGNAME</b></font></td><td bgcolor=\"#E4E6CA\"> <font COLOR=\"#008000\">Aleutian Islands Refuge</font></td></tr><tr><td bgcolor=\"#E3E1CA\" align=\"right\"><font COLOR=\"#000000\"><b>DSCRPTND</b></font></td><td bgcolor=\"#E4E6CA\"> <font COLOR=\"#008000\">AS 16.20.030A1</font></td></tr><tr><td bgcolor=\"#E3E1CA\" align=\"right\"><font COLOR=\"#000000\"><b>SHAPE_AREA</b></font></td><td bgcolor=\"#E4E6CA\"> <font COLOR=\"#008000\">15370.9634027</font></td></tr></table>'" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"tbl" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import re" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_LONGNAME(tbl_html):\n", | |
" pat = re.compile(r\" <font COLOR=\\\"#008000\\\">([\\w. ]+)</font></td></tr><tr><td bgcolor=\\\"#E3E1CA\\\"\")\n", | |
" mtch = re.search(pat, tbl_html)\n", | |
" if mtch!=None:\n", | |
" out = mtch.group(1)\n", | |
" else:\n", | |
" out = \"\"\n", | |
" return out" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"u'Aleutian Islands Refuge'" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"get_LONGNAME(tbl)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/usr/local/lib/python2.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n", | |
"A value is trying to be set on a copy of a slice from a DataFrame.\n", | |
"Try using .loc[row_indexer,col_indexer] = value instead\n", | |
"\n", | |
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", | |
" \"\"\"Entry point for launching an IPython kernel.\n" | |
] | |
} | |
], | |
"source": [ | |
"other['longname'] = other.Description.apply(get_LONGNAME)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"other.to_file(\"OtherLDA.shp\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.14" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment