Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Last active July 9, 2022 14:24
Show Gist options
  • Save AlexArcPy/bf35477d89a6789f62de1d6466bd423d to your computer and use it in GitHub Desktop.
Save AlexArcPy/bf35477d89a6789f62de1d6466bd423d to your computer and use it in GitHub Desktop.
Make pandas data frame from feature class with shape
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def make_data_frame(in_table,use_geometry='xy'):\n",
" \"\"\"convert a file gdb feature class into a pandas DataFrame\n",
" Inputs:\n",
" use_geometry:\n",
" full - uses SHAPE@ with the whole arcpy.Geometry(), very expensive;\n",
" xy - gets XY values in the Shape field; makes sense only for point layers;\n",
" None - doesn't take any geometry properties.\n",
" \"\"\"\n",
" import arcpy\n",
" import pandas as pd\n",
" \n",
" fields = []\n",
" if use_geometry == 'xy':\n",
" fields_to_delete = map(str.upper,['Objectid','Shape_area','Shape_length'])\n",
" if use_geometry == 'full':\n",
" fields.append('SHAPE@')\n",
" fields_to_delete = map(str.upper,['Objectid','Shape','Shape_area','Shape_length'])\n",
" if not use_geometry:\n",
" fields_to_delete = map(str.upper,['Objectid','Shape','Shape_area','Shape_length'])\n",
"\n",
" fields.extend(map(str.upper,[str(f.name) for f in arcpy.ListFields(in_table)]))\n",
" fields_to_get = [field for field in fields if field not in fields_to_delete]\n",
"\n",
" feats = [f for f in arcpy.da.SearchCursor(in_table,fields_to_get)]\n",
" attribute_table = [dict(zip(fields_to_get,f)) for f in feats]\n",
" \n",
" df = pd.DataFrame(attribute_table)\n",
" df.columns = map(str.capitalize, [str(c) for c in df.columns]) \n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Area</th>\n",
" <th>Cnty_fips</th>\n",
" <th>Fips</th>\n",
" <th>Name</th>\n",
" <th>Pop1990</th>\n",
" <th>Pop2000</th>\n",
" <th>Pop90_sqmi</th>\n",
" <th>Shape@</th>\n",
" <th>State_fips</th>\n",
" <th>State_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2389</th>\n",
" <td>4115.7485</td>\n",
" <td>037</td>\n",
" <td>06037</td>\n",
" <td>Los Angeles</td>\n",
" <td>8863164.0</td>\n",
" <td>9424833.0</td>\n",
" <td>2153</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>06</td>\n",
" <td>California</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3085</th>\n",
" <td>964.9506</td>\n",
" <td>031</td>\n",
" <td>17031</td>\n",
" <td>Cook</td>\n",
" <td>5105067.0</td>\n",
" <td>5192969.0</td>\n",
" <td>5290</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>17</td>\n",
" <td>Illinois</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2822</th>\n",
" <td>1741.4574</td>\n",
" <td>201</td>\n",
" <td>48201</td>\n",
" <td>Harris</td>\n",
" <td>2818199.0</td>\n",
" <td>3302269.0</td>\n",
" <td>1618</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>48</td>\n",
" <td>Texas</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2299</th>\n",
" <td>9193.7166</td>\n",
" <td>013</td>\n",
" <td>04013</td>\n",
" <td>Maricopa</td>\n",
" <td>2122101.0</td>\n",
" <td>2942946.0</td>\n",
" <td>231</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>04</td>\n",
" <td>Arizona</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2381</th>\n",
" <td>4268.0716</td>\n",
" <td>073</td>\n",
" <td>06073</td>\n",
" <td>San Diego</td>\n",
" <td>2498016.0</td>\n",
" <td>2872950.0</td>\n",
" <td>585</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>06</td>\n",
" <td>California</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Area Cnty_fips Fips Name Pop1990 Pop2000 \\\n",
"2389 4115.7485 037 06037 Los Angeles 8863164.0 9424833.0 \n",
"3085 964.9506 031 17031 Cook 5105067.0 5192969.0 \n",
"2822 1741.4574 201 48201 Harris 2818199.0 3302269.0 \n",
"2299 9193.7166 013 04013 Maricopa 2122101.0 2942946.0 \n",
"2381 4268.0716 073 06073 San Diego 2498016.0 2872950.0 \n",
"\n",
" Pop90_sqmi Shape@ \\\n",
"2389 2153 <geoprocessing describe geometry object object... \n",
"3085 5290 <geoprocessing describe geometry object object... \n",
"2822 1618 <geoprocessing describe geometry object object... \n",
"2299 231 <geoprocessing describe geometry object object... \n",
"2381 585 <geoprocessing describe geometry object object... \n",
"\n",
" State_fips State_name \n",
"2389 06 California \n",
"3085 17 Illinois \n",
"2822 48 Texas \n",
"2299 04 Arizona \n",
"2381 06 California "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = make_data_frame(in_table=r'C:\\Program Files (x86)\\ArcGIS\\Desktop10.4\\TemplateData\\TemplateData.gdb\\USA\\counties',\n",
" use_geometry='full')\n",
"df.sort_values('Pop2000',ascending=False,inplace=True)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Area</th>\n",
" <th>Cnty_fips</th>\n",
" <th>Fips</th>\n",
" <th>Name</th>\n",
" <th>Pop1990</th>\n",
" <th>Pop2000</th>\n",
" <th>Pop90_sqmi</th>\n",
" <th>Shape@</th>\n",
" <th>State_fips</th>\n",
" <th>State_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1051</th>\n",
" <td>454.9630</td>\n",
" <td>003</td>\n",
" <td>10003</td>\n",
" <td>New Castle</td>\n",
" <td>441946.0</td>\n",
" <td>491415.0</td>\n",
" <td>971</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>10</td>\n",
" <td>Delaware</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1266</th>\n",
" <td>991.7808</td>\n",
" <td>005</td>\n",
" <td>10005</td>\n",
" <td>Sussex</td>\n",
" <td>113229.0</td>\n",
" <td>143294.0</td>\n",
" <td>114</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>10</td>\n",
" <td>Delaware</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1165</th>\n",
" <td>607.8421</td>\n",
" <td>001</td>\n",
" <td>10001</td>\n",
" <td>Kent</td>\n",
" <td>110993.0</td>\n",
" <td>127518.0</td>\n",
" <td>183</td>\n",
" <td>&lt;geoprocessing describe geometry object object...</td>\n",
" <td>10</td>\n",
" <td>Delaware</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Area Cnty_fips Fips Name Pop1990 Pop2000 Pop90_sqmi \\\n",
"1051 454.9630 003 10003 New Castle 441946.0 491415.0 971 \n",
"1266 991.7808 005 10005 Sussex 113229.0 143294.0 114 \n",
"1165 607.8421 001 10001 Kent 110993.0 127518.0 183 \n",
"\n",
" Shape@ State_fips State_name \n",
"1051 <geoprocessing describe geometry object object... 10 Delaware \n",
"1266 <geoprocessing describe geometry object object... 10 Delaware \n",
"1165 <geoprocessing describe geometry object object... 10 Delaware "
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#found counties spatially in Delaware\n",
"states_fc = r'C:\\Program Files (x86)\\ArcGIS\\Desktop10.4\\TemplateData\\TemplateData.gdb\\USA\\states'\n",
"delaware_state = [row[0] for row in arcpy.da.SearchCursor(states_fc,\"SHAPE@\",\n",
" \"STATE_NAME = 'Delaware'\")][0]\n",
"\n",
"def is_within_state(county_geom):\n",
" return county_geom.within(delaware_state)\n",
" \n",
"criterion = df['Shape@'].map(is_within_state)\n",
"df[criterion]"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment