Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Create Table from Selection

$
0
0

This is a continuation of a problem that was solved earlier this week. The code below performs a select by location and was originally intended to export all feature classes from the SDE database to shapefiles. However, since the SDE uses coded calues, there is an output issue when using CopyFeatures_management.

The work around that was proposed was to create export to csv/xls, but the TableToExcel function only explicitly accepts tables in arcpy. Someone recommended using arcpy.Describe but how could I take what it reads and convert it to a table? Or is there another solution to this?

import arcpy
from arcpy import env

env.workspace = r'C:ArcScratchHM_Production.sde'

stateFeature = r'C:ArcScratchStateParishes_LDOTD_2007.shp'
parish = "Lafayette"
nameField = "PARISH"



arcpy.MakeFeatureLayer_management(stateFeature, 'SelectionStateLayer', '"' +     
str(nameField) + '" = ' + "'" + str(parish) + "'")

for fc in arcpy.ListFeatureClasses():

    if arcpy.Exists('lyr'): #cleanup check
        arcpy.Delete_management('lyr')

    arcpy.MakeFeatureLayer_management(fc, 'lyr') #converts feature class to lyr
    arcpy.SelectLayerByLocation_management('lyr', "WITHIN", 'SelectionStateLayer') #selection using parish layer

    outname = fc.replace(".","_") #replaces all . with _
    outpath = r"C:output{0}".format(outname)

    if arcpy.Exists(outpath): #cleanup check
        arcpy.Delete_management(output)

    arcpy.TableToExcel_conversion('lyr', outpath) #output selected features

arcpy.Delete_management('lyr')

Viewing all articles
Browse latest Browse all 767

Latest Images

Trending Articles



Latest Images