HowTo:  Extract values from a field and write them to a text file using Python at ArcGIS 10.x

相关信息
Article ID: 41442
Software:
ArcGIS - ArcEditor 10
ArcGIS - ArcInfo 10
ArcGIS - ArcView 10
ArcGIS for Desktop Advanced 10.1, 10.2
ArcGIS for Desktop Standard 10.1, 10.2
ArcGIS for Desktop Basic 10.1, 10.2
Platforms:
Windows Vista, Server 2008, Windows 7, Windows 8, Server 2012

问题描述
Instructions provided describe how to select a feature by an attribute, select all the features that share a boundary with it, and then export the values of all of the features to a text file. This article is specific to using the ArcPy module installed with ArcGIS 10.x.

To complete this procedure using Python requires the use of the Search Cursor method to iterate through the values of the field.
已邀请:

易智瑞技术支持

赞同来自:

解决方案
Below are the general steps for the procedure followed by code examples using a zip code polygon shapefile.


1.Set up the environment and define needed variables:

# Import modules
import arcpy, os, sys, string

# Create envrionmental variables
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"D:\Data" # This is the folder that contains the data

# Set variables
input = "testdata.shp" # The zip code shapefile
lyr = "test_layer" # The name of the Make Feature Layer output


2.Convert the input shapefile into a Feature Layer, so it can be used in the Select Layer by Attributes and Location tools.

# Processes to make feature layer...
arcpy.MakeFeatureLayer_management(input, lyr)


3.Create the text file to which the output is to be written. If it does not already exist, Python will create it in this step.
 # Create the text file to write the zip values to...
outFile = open(r"D:\Data\zipcodes.text", "w")

4. Build the first Search Cursor to iterate through the polygon shapefile that contains the values in the field. There is a second kind of cursor that can be used at version 10.1 and later; skip to the final step for that sample.
   # Build search cursor for all 10.x...

fcSearch = arcpy.SearchCursor(lyr, "", "", "ZIP")


5.Construct a loop that runs all the needed processes on each value from the field, in this case, Select Layer by Attribute and Select Layer by Location. The Select by Attribute needs a specific value from the field for each iteration, so an expression variable (exp) is created for this.
for fcRow in fcSearch:
# Create expression based on the current row's zip code value
exp = '"ZIP" = ' + "'" + str(fcRow.getValue("ZIP")) + "'"
# Process: Select Layer by Attribute...
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", exp)
# Process: Select Layer by Location...
arcpy.SelectLayerByLocation_management(lyr, "BOUNDARY_TOUCHES", lyr, "", "NEW_SELECTION")


6.Build another search cursor within this loop to iterate through all the values associated with the rows found in the Select by Location process and write those values in the text file. This step is completely contained within the loop.
 # Build another search cursor to grab the zip values out of lyr...
txtSearch = arcpy.SearchCursor(lyr, "", "", "ZIP")
outFile.write("Zip Code: " + str(fcRow.getValue("ZIP")) + "\n")
for txtRow in txtSearch:
zval = str(txtRow.getValue("ZIP"))
outFile.write(zval + "\n")

7.Outside of the loop, close the text file and release all the variables from memory.

outFile.close() # This closes the text file
del input, lyr, fcRow, txtRow, txtSearch, fcSearch, outFile, zva

8.If using the Data Access Search Cursor in this process, the final part of the code is similar to this:

# Build data access search cursor for 10.1+
fcSearch = arcpy.da.SearchCursor(lyr, ["ZIP"])
for fcRow in fcSearch:
exp = '"ZIP" = ' + "'" + str(fcRow[0]) + "'"
# Process: Select Layer by Attribute...
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", exp)
# Process: Select Layer by Location...
arcpy.SelectLayerByLocation_management(lyr, "BOUNDARY_TOUCHES", lyr, "", "NEW_SELECTION")

# Build another search cursor to grab the zip values out of lyr...
txtSearch = arcpy.da.SearchCursor(lyr, ["ZIP"])
outFile.write("Zip Code: " + str(fcRow[0]) + "\n")
for txtRow in txtSearch:
zval = str(txtRow[0])
outFile.write(zval + "\n")

outFile.close() # This closes the text file
del input, lyr, fcRow, txtRow, txtSearch, fcSearch, outFile, zval



其它相关参考
  1. Extract values from a field and write them to a text file using Python in ArcGIS 9.xInstructions provided describe how to select a feature by attribute, select all the features that share a boundary with it, and then export the values of all of the features to a text file. To complete this procedure using Python requires the ...
  2. Help 10.2 - Select Layer By Attribute
  3. Help 10.2 - Select Layer By Location
  4. Help 10.2 - Data Access SearchCursor
  5. Help 10.2 - SearchCursor
  6. Python Documentation - Input and Outputs - Reading and Writing Files


创建及修改时间
Created: 7/25/2013

Last Modified: 8/8/2013
原文链接
http://support.esri.com/en/kno ... 41442

要回复问题请先登录注册