Help performing these steps >Sample >Display XY Data >Export Attribute Table

3409
14
Jump to solution
12-15-2015 02:46 PM
DannyLackey
New Contributor II

I have a manual process I'm trying to automate.  My question is mostly related to step 2, but I welcome any suggestion for any of the process I've described that may help me achieve my goal.  Thanks in advance!

The first part is dumping raster data:  Spatial Analyst Tools > Extraction > Sample > 

(Step 1) I've been able to do this piece with the following code:

import arcpy
from arcpy import env
from arcpy.sa import *
# Set environment settings
env.workspace = r"c:\InputRasters"
# Set local variables
inRasters = ["b_inst"]
locations = r"c:\Grids\50ft_Grid.shp"
outTable = r"c:\TestTable"
sampMethod = "NEAREST"
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute Sample
Sample(inRasters, locations, outTable, sampMethod)
Environments

(Step 2) Now I need to Display the XY data and export attribute table to either a dbf or Excel spreadsheet.

This piece of code seems to be what I need, but it seems to require a .dbf file.  The script from the previous step didn't output a .dbf, it output a folder called "info" containing the following files: arc.dir (1 kb), arc0000.dat (7,022 kb), arc0000.nit (1 kb), and arc0000.xml (1 kb).

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006z000000 

(Step 3) Finally, I'm thinking I can use this technique to dump the resulting dbf to .xls

arcgis desktop - Saving *.dbf as *.xls using Python? - Geographic Information Systems Stack Exchange

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

You need to specify the extension to the out_table parameter when calling the sample tool to create a dbf output. After that you'd need to call the following tool:

Make XY Event Layer

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/make-xy-event-layer.htm

Table to Excel

http://desktop.arcgis.com/en/desktop/latest/tools/conversion-toolbox/table-to-excel.htm

My only question is why are you needing to create the xy data if you're going to export it to a table, which is essentially going to remove the geometry. Could you not just call the Table to Excel tool directly with the output table of the Sample tool?

View solution in original post

14 Replies
FreddieGibson
Occasional Contributor III

You need to specify the extension to the out_table parameter when calling the sample tool to create a dbf output. After that you'd need to call the following tool:

Make XY Event Layer

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/make-xy-event-layer.htm

Table to Excel

http://desktop.arcgis.com/en/desktop/latest/tools/conversion-toolbox/table-to-excel.htm

My only question is why are you needing to create the xy data if you're going to export it to a table, which is essentially going to remove the geometry. Could you not just call the Table to Excel tool directly with the output table of the Sample tool?

DannyLackey
New Contributor II

You were totally right on the first step.  Adding the .dbf extension made all of the difference.  If I'm understanding your question correctly, my response is that these steps are what's done manually, so I am trying to duplicate the process.  I believe the XY data is actually needed and step 2 is necessary to get it, isn't it?  Unfortunately, this isn't my process and I know almost nothing about ArcGIS, but I do know that the final Excel spreadsheet consists of an X column, a Y column, and a "Value" column, which I believe represents the points from the 50 ft grid.  I'm guessing I do not need to preserve the geometry.  However, I'm fine attempting to bypass step 2 and try your suggestion - too much data is better than not enough.  So, my question now is, the Table to Excel script seems to require a gdb, but it sounds like you are suggesting I can go directly from my .dbf to an .xls file.  I tried this, but it fails.  Do you see where I'm going wrong?:

import arcpy
# Set environment settings
arcpy.env.workspace = r"C:\"
# Set local variables
in_table = "TestTable.dbf\TestTable"
out_xls = "test.xls"
# Execute TableToExcel
arcpy.TableToExcel_conversion(in_table, out_xls)

Error:

Traceback (most recent call last):

  File "c:\Table2XLS.py", line 8, in <module>

    arcpy.TableToExcel_conversion(in_table, out_xls)

AttributeError: 'module' object has no attribute 'TableToExcel_conversion'

0 Kudos
FreddieGibson
Occasional Contributor III

the path to your input table is invalid. A dbf table will not have a sub-table. TestTable.dbf should be the name of your table and not TestTable.dbf\TestTable. Also, what version of the software are you using. I don't believe that the TableToExcel tool is available until 10.2.X or higher.

0 Kudos
DannyLackey
New Contributor II

I originally tried just "TestTable.dbf" which resulted in the same error.  I am on 10.1.  Sounds like that is the problem.  Is there an alternate method?

0 Kudos
FreddieGibson
Occasional Contributor III

This tool would not be available at 10.1. Your options would be to either install the needed python libraries on your 10.1 machine and use them in conjunction with cursors to build the excel table or you could export the table to a csv file and load it into excel from there.

xlwt

https://pypi.python.org/pypi/xlwt

Otherwise you'd need to upgrade to gain access to the TableToExcel tool.

0 Kudos
DannyLackey
New Contributor II

I checked the link you provided.  I'm not familiar with virtualenv.  Not clear how I access this in order to input the command "pip install xlwt". 

0 Kudos
DannyLackey
New Contributor II

Reading about virtualenv: Virtual Environments — The Hitchhiker's Guide to Python .  It says to install virtualenv, I use this command: $ pip install virtualenv,  but I have no clue where to run this - windows command prompt?  powershell?  python command window?

0 Kudos
FreddieGibson
Occasional Contributor III

You'll need to install pip against your 2.6 install to execute the command. You should be able to install Pip with the instructions on the following page.

https://pip.pypa.io/en/stable/installing/#install-pip

Once you get pip installed you'd be able to use it to install xlwt.

0 Kudos
DannyLackey
New Contributor II

Forgive me Freddie - revealing my inexperience -  I tried opening a python command window and typed: python get-pip.py.  I'm SURE this is wrong...mostly because of the syntax errors.   haha   See post edit above.  Not sure where to enter this.

0 Kudos