Skip to content

Instantly share code, notes, and snippets.

@cfriedline
Last active August 16, 2016 17:19
Show Gist options
  • Save cfriedline/f4d0ca9b410e0338fd927801c20eb324 to your computer and use it in GitHub Desktop.
Save cfriedline/f4d0ca9b410e0338fd927801c20eb324 to your computer and use it in GitHub Desktop.
csv_to_shp.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-08-16T13:10:44.473509",
"end_time": "2016-08-16T13:10:44.477722"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "import gpxpy.gpx\nimport os\nimport pandas as pd\nimport shapefile\nimport urllib",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-08-16T13:10:45.233536",
"end_time": "2016-08-16T13:10:45.437060"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "def getPRJwkt(epsg):\n \"\"\"\n Grab an WKT version of an EPSG code\n usage getPRJwkt(4326)\n\n This makes use of links like http://spatialreference.org/ref/epsg/4326/prettywkt/\n \"\"\"\n \n import urllib\n f=urllib.request.urlopen(\"http://spatialreference.org/ref/epsg/{0}/prettywkt/\".format(epsg))\n return (f.read())\nprint(getPRJwkt(4326))",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "b'GEOGCS[\"WGS 84\",\\n DATUM[\"WGS_1984\",\\n SPHEROID[\"WGS 84\",6378137,298.257223563,\\n AUTHORITY[\"EPSG\",\"7030\"]],\\n AUTHORITY[\"EPSG\",\"6326\"]],\\n PRIMEM[\"Greenwich\",0,\\n AUTHORITY[\"EPSG\",\"8901\"]],\\n UNIT[\"degree\",0.01745329251994328,\\n AUTHORITY[\"EPSG\",\"9122\"]],\\n AUTHORITY[\"EPSG\",\"4326\"]]'\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-08-16T13:10:46.048926",
"end_time": "2016-08-16T13:10:46.054202"
},
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "def get_spp_color(spp):\n if spp == 'T':\n return \"blue\"\n if spp == \"P\":\n return \"yellow\"\n if spp == \"E\":\n return \"magenta\"\n if spp == \"G\":\n return \"orange\"\n return \"white\"",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-08-16T13:10:47.745059",
"end_time": "2016-08-16T13:10:47.750526"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "coords = pd.read_csv(\"coords.txt\", sep=\"\\t\")",
"execution_count": 11,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-08-16T13:10:50.544924",
"end_time": "2016-08-16T13:10:50.563723"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "coords.head()",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": "<div>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>name</th>\n <th>lat</th>\n <th>lon</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>P-AL-5-13</td>\n <td>33.17237</td>\n <td>-86.17225</td>\n </tr>\n <tr>\n <th>1</th>\n <td>T-AR-2-7</td>\n <td>34.63728</td>\n <td>-93.15251</td>\n </tr>\n <tr>\n <th>2</th>\n <td>P-GA-4-8</td>\n <td>31.14648</td>\n <td>-82.21552</td>\n </tr>\n <tr>\n <th>3</th>\n <td>P-GA-2-12</td>\n <td>31.17607</td>\n <td>-83.53644</td>\n </tr>\n <tr>\n <th>4</th>\n <td>E-GA-1-4</td>\n <td>31.13760</td>\n <td>-82.21436</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " name lat lon\n0 P-AL-5-13 33.17237 -86.17225\n1 T-AR-2-7 34.63728 -93.15251\n2 P-GA-4-8 31.14648 -82.21552\n3 P-GA-2-12 31.17607 -83.53644\n4 E-GA-1-4 31.13760 -82.21436"
},
"metadata": {},
"execution_count": 12
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-08-16T13:11:00.128888",
"end_time": "2016-08-16T13:11:00.466218"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "w = shapefile.Writer(shapefile.POINT)\nw.field(\"name\")\nw.field(\"species\")\nw.field(\"color\")\nrecords = []\nshpfile = \"sample_shapefile\"\n\ndef record(row):\n w.point(row['lon'], row['lat'])\n spp = row['name'][0]\n w.record(row['name'], spp, get_spp_color(spp))\n \ncoords.apply(record, axis=1)\nw.save(shpfile)\n\nprj = open(\"%s.prj\" % shpfile, \"w\")\nepsg = getPRJwkt(4326)\nprj.write(epsg.decode(\"utf-8\"))\nprj.close()",
"execution_count": 13,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"pygments_lexer": "ipython3",
"file_extension": ".py",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"nbconvert_exporter": "python",
"version": "3.5.2",
"mimetype": "text/x-python",
"name": "python"
},
"gist": {
"id": "f4d0ca9b410e0338fd927801c20eb324",
"data": {
"description": "csv_to_shp.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/f4d0ca9b410e0338fd927801c20eb324"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment