Select to view content in your preferred language

Select by location to Excel file

613
1
12-07-2013 05:19 AM
kevinmahony
New Contributor
I have been using ArcGIS for a few years now but have only started using Python scripting for ArcGIS for a month or so. I am trying to create a script and i am having a hard time trying to get it to work (Error after Error).
In ArcGIS i have used SELECT BY LOCATION and exported the selected records as a dBASE table into my file. I have been able to create the Python script to help me do this and also to convert it into an Excel file.

import arcpy

fc = 'Z:\\GIS TEST\\Codes\\Codes\\K.Dun\\Export_Output_2.dbf'
CSVFile = 'Z:\\Excel.csv'

fields = [f.name for f in arcpy.ListFields(fc)]

for i,f in enumerate(fields):
    if f == 'Shape' or f == 'Shape_Length' or f == 'OBJECTID':
        del fields

with open(CSVFile, 'w') as f:
    f.write(','.join(fields)+'\n') #csv headers
    with arcpy.da.SearchCursor(fc, fields) as cursor:
        for row in cursor:
            f.write(','.join([str(r) for r in row])+'\n')

This is fine but it requires me to manually use SELECT BY LOCATION and export the dBASE (.dbf) file before i run the script. Is there any script i can write that will allow to SEARCH BY LOCATION then export the selected records into an Excel file all in one go.

I created a SEARCH BY LOCATION script but don't know how i would join them.

import arcpy
arcpy.env.workspace = "Z:\GIS TEST\Select_by_Location"

arcpy.MakeFeatureLayer_management('Bld_Locations.shp', 'Bld_Locations_lyr')
arcpy.SelectLayerByLocation_management('Bld_Locations_lyr', "WITHIN_A_DISTANCE", 'Breakout_Location.shp', "2000 Meters", "NEW_SELECTION")

Anyone have any ideas?

Please remember my Python knowledge is very limited.
Tags (2)
0 Kudos
1 Reply
RichardFairhurst
MVP Alum
I have been using ArcGIS for a few years now but have only started using Python scripting for ArcGIS for a month or so. I am trying to create a script and i am having a hard time trying to get it to work (Error after Error).
In ArcGIS i have used SELECT BY LOCATION and exported the selected records as a dBASE table into my file. I have been able to create the Python script to help me do this and also to convert it into an Excel file.

import arcpy

fc = 'Z:\\GIS TEST\\Codes\\Codes\\K.Dun\\Export_Output_2.dbf'
CSVFile = 'Z:\\Excel.csv'

fields = [f.name for f in arcpy.ListFields(fc)]

for i,f in enumerate(fields):
    if f == 'Shape' or f == 'Shape_Length' or f == 'OBJECTID':
        del fields

with open(CSVFile, 'w') as f:
    f.write(','.join(fields)+'\n') #csv headers
    with arcpy.da.SearchCursor(fc, fields) as cursor:
        for row in cursor:
            f.write(','.join([str(r) for r in row])+'\n')

This is fine but it requires me to manually use SELECT BY LOCATION and export the dBASE (.dbf) file before i run the script. Is there any script i can write that will allow to SEARCH BY LOCATION then export the selected records into an Excel file all in one go.

I created a SEARCH BY LOCATION script but don't know how i would join them.

import arcpy
arcpy.env.workspace = "Z:\GIS TEST\Select_by_Location"

arcpy.MakeFeatureLayer_management('Bld_Locations.shp', 'Bld_Locations_lyr')
arcpy.SelectLayerByLocation_management('Bld_Locations_lyr', "WITHIN_A_DISTANCE", 'Breakout_Location.shp', "2000 Meters", "NEW_SELECTION")

Anyone have any ideas?

Please remember my Python knowledge is very limited.


Since the layer is a shapefile in your second script it is by default also a .dbf file (which is the database underlying a shapefile).  I believe you just need to make the layer selection of the second script the fc input of the first script.  So I think the script simply needs to be:

import arcpy

arcpy.env.workspace = "Z:\GIS TEST\Select_by_Location"

arcpy.MakeFeatureLayer_management('Bld_Locations.shp', 'Bld_Locations_lyr') 
arcpy.SelectLayerByLocation_management('Bld_Locations_lyr', "WITHIN_A_DISTANCE", 'Breakout_Location.shp', "2000 Meters", "NEW_SELECTION")

fc = 'Bld_Locations_lyr'
CSVFile = 'Z:\\Excel.csv'

fields = [f.name for f in arcpy.ListFields(fc)]

for i,f in enumerate(fields):
    if f == 'Shape' or f == 'Shape_Length' or f == 'OBJECTID':
        del fields

with open(CSVFile, 'w') as f:
    f.write(','.join(fields)+'\n') #csv headers
    with arcpy.da.SearchCursor(fc, fields) as cursor:
        for row in cursor:
            f.write(','.join([str(r) for r in row])+'\n')
0 Kudos