Arcpy get start¶
What is ArcPy¶
- What is arcpy
ArcPy
was introduced withArcGIS 10.0
ArcPy
is a collection of modules, classes and functions which give access to all the geoprocessing tools inArcGIS
from withinPython
- Most
geoprocessing
scripts will start with: import arcpy - Note:
ArcPy
replaces the olderarcgisscripting
module
try:
import arcpy
except ImportError:
print("import arcpy error")
Setting Current Workspace¶
After importing ArcPy
, most scripts start with setting a workspace to retrieve and store files
arcpy.env.workspace = r".\database"
arcpy.env.workspace = "./database"
arcpy.env.workspace = ".\\database"
In the code above env
is a class and workspace
is a property of this class
arcpy.< class>.< property>
arcpy.ListWorkspaces()
['.\\database\\results', '.\\database\\testdata.gdb']
arcpy.ListFeatureClasses()
['amtrak_stations.shp', 'cities.shp', 'counties.shp', 'new_mexico.shp', 'railroads.shp', 'resultbuffers_poly.shp', 'resultLittlePolys.shp', 'resultWatersheds_polygon.shp']
arcpy.ListRasters()
['resultElevationFloat..tif', 'resultElevationFloat.tif', 'resultsElevationFloat.tif']
Set overwriteOutput
property
arcpy.env.overwriteOutput = True
Get a complete list of properties
arcpy.env.cellSize = 30
arcpy.ListEnvironments()[:10]
['autoCommit', 'XYResolution', 'processingServerUser', 'gpuId', 'XYDomain', 'processingServerPassword', 'scratchWorkspace', 'recycleProcessingWorkers', 'cartographicPartitions', 'terrainMemoryUsage']
ArcPy Functions¶
- All
geoprocessing tools
areArcPy
functions
- Additional
ArcPy
functions:- listing data
- Retrieving and setting properties
- Many more…
- General syntax
arcpy.< functionname>(< arguments>)
- Documentation of ArcPy functions
arcpy.ListTools()[:10]
['ChangePointDetection_stpm', 'CreateSpaceTimeCube_stpm', 'CreateSpaceTimeCubeDefinedLocations_stpm', 'CreateSpaceTimeCubeMDRasterLayer_stpm', 'CurveFitForecast_stpm', 'DescribeSpaceTimeCube_stpm', 'EmergingHotSpotAnalysis_stpm', 'EvaluateForecastsByLocation_stpm', 'ExponentialSmoothingForecast_stpm', 'FillMissingValues_stpm']
ArcPy Classes¶
- Some
tool parameters
are complicated/detailed- e.g.
coordinate system
- e.g.
ArcPy
classes
are used to work with these parameters- Classes are used to create objects
- Classes have
properties
andmethods
- General syntax
arcpy.< classname>(< parameters>)
- Documentation for Arcpy classes
- Feature classes
- Raster
feature_class = "./database/cities.shp"
# Create a list of fields using the ListFields function
fields = arcpy.ListFields(feature_class)
# Print table header
print("{:<20} {:<20} {:<15} {:<12} {:<12} {:<10} {:<10}".format("Field", "Alias", "Type", "Is Editable", "Required", "Scale", "Precision"))
# Iterate through the list of fields
for field in fields:
# Print field properties
print("{:<20} {:<20} {:<15} {:<12} {:<12} {:<10} {:<10}".format(
field.name,
field.aliasName,
field.type,
field.editable,
field.required,
field.scale,
field.precision
))
Field Alias Type Is Editable Required Scale Precision FID FID OID 0 1 0 0 Shape Shape Geometry 1 1 0 0 CITIESX020 CITIESX020 Double 1 0 0 11 FEATURE FEATURE String 1 0 0 0 NAME NAME String 1 0 0 0 POP_RANGE POP_RANGE String 1 0 0 0 POP_2000 POP_2000 Integer 1 0 0 8 FIPS55 FIPS55 String 1 0 0 0 COUNTY COUNTY String 1 0 0 0 FIPS FIPS String 1 0 0 0 STATE STATE String 1 0 0 0 STATE_FIPS STATE_FIPS String 1 0 0 0 DISPLAY DISPLAY SmallInteger 1 0 0 1
The following example creates a spatial reference object
based on an existing .shp
file ‐ properties of this object can then be used
prjfile = "railroads.shp"
info = arcpy.Describe(prjfile)
print(info)
<geoprocessing describe data object object at 0x000001C2CBB5E670>
spatialref = info.spatialReference
myref = spatialref.name
print(myref)
GCS_North_American_1983
arcpy.env.overwriteOutput = True
arcpy.Clip_analysis("railroads.shp","new_mexico.shp","results/routes_clip.shp")
arcpy.analysis.Clip("railroads.shp","new_mexico.shp","results/routes_clip.shp") ## The same
Messages
Tool Parameters¶
- A good understanding of
tool parameters
is essential - Parameters have properties:
- Name
- Type (
feature class
,integer
, etc.) - Direction (
input
oroutput
) Required
oroptional
- Required tool parameters are listed first
- Optional tool parameters can be left out
But what if some need to be set? It can be accomplished in the following ways:
- By setting the optional parameters using an empty string (
""
) or the number sign ("#"
) - By specifying by name the parameter that needs to be set, bypassing all others
- Buffer (Analysis)
arcpy.Buffer_analysis("railroads.shp","results/railroads_1000ft_buffer.shp","1000 FEET","","","ALL","")
arcpy.Buffer_analysis("railroads.shp","results/railroads_1000ft_buffer.shp","1000 FEET", dissolve_option="ALL")
Messages
Variables Provided by users (in tool design)¶
import arcpy
infc = arcpy.GetParameterAsText(0)
outfc = arcpy.GetParameterAsText(1)
arcpy.Copy_management(infc, outfc)
Results Objects¶
ArcPy
returns the output of a tool as aResult object
- Documentation
mycount = arcpy.GetCount_management("railroads.shp")
print(mycount)
86
myresult = arcpy.Clip_analysis("railroads.shp","new_mexico.shp","results/routes_clip.shp")
print(myresult)
.\database\results\routes_clip.shp
Multiple Operations using Result Objects¶
Result objects
can be used as the input
into another function
temp_buffer = arcpy.Buffer_analysis("railroads.shp","results/railroads_1000ft_buffer.shp","1000 FEET", dissolve_option="ALL")
count = arcpy.GetCount_management(temp_buffer)
print(count)
1
Access Tool’s Syntax¶
arcpy.Usage()
can query the syntax of each tool
tools = arcpy.ListTools("*_analysis")
print(tools)
['ApportionPolygon_analysis', 'Buffer_analysis', 'Clip_analysis', 'CountOverlappingFeatures_analysis', 'CreateThiessenPolygons_analysis', 'Enrich_analysis', 'Erase_analysis', 'Frequency_analysis', 'GenerateNearTable_analysis', 'GenerateOriginDestinationLinks_analysis', 'GraphicBuffer_analysis', 'Identity_analysis', 'Intersect_analysis', 'MultipleRingBuffer_analysis', 'Near_analysis', 'PairwiseBuffer_analysis', 'PairwiseClip_analysis', 'PairwiseDissolve_analysis', 'PairwiseErase_analysis', 'PairwiseIntegrate_analysis', 'PairwiseIntersect_analysis', 'PolygonNeighbors_analysis', 'RemoveOverlapMultiple_analysis', 'Select_analysis', 'SpatialJoin_analysis', 'Split_analysis', 'SplitByAttributes_analysis', 'Statistics_analysis', 'SummarizeNearby_analysis', 'SummarizeWithin_analysis', 'SymDiff_analysis', 'TableSelect_analysis', 'TabulateIntersection_analysis', 'Union_analysis', 'Update_analysis']
arcpy.Usage("Union_analysis")
'Union_analysis(Features {Ranks};Features {Ranks}..., out_feature_class, {All attributes | All attributes except feature IDs | Only feature IDs}, {cluster_tolerance}, {GAPS | NO_GAPS})'
We can also use Python build-in help
function
print(help(arcpy.Union_analysis))
Help on function Union in module arcpy.analysis: Union(in_features=None, out_feature_class=None, join_attributes=None, cluster_tolerance=None, gaps=None) Union_analysis(in_features;in_features..., out_feature_class, {join_attributes}, {cluster_tolerance}, {gaps}) Computes a geometric union of the input features. All features and their attributes will be written to the output feature class. INPUTS: in_features (Value Table): The input feature classes or layers. When the distance between features is less than the cluster tolerance, the features with the lower rank will snap to the feature with the higher rank. The highest rank is one. All of the input features must be polygons. join_attributes {String}: Specifies which attributes from the input features will be transferred to the output feature class.ALL-All the attributes from the input features will be transferred to the output feature class. This is the default.NO_FID-All the attributes except the FID from the input features will be transferred to the output feature class.ONLY_FID-Only the FID field from the input features will be transferred to the output feature class. cluster_tolerance {Linear Unit}: The minimum distance separating all feature coordinates (nodes and vertices) as well as the distance a coordinate can move in x or y (or both).Changing this parameter's value may cause failure or unexpected results. It is recommended that you do not modify this parameter. It has been removed from view on the tool dialog box. By default, the input feature class's spatial reference x,y tolerance property is used. gaps {Boolean}: Specifies whether a feature will be created for areas in the output that are completely enclosed by polygons. Gaps are areas in the output feature class that are completely enclosed by other polygons (created from the intersection of features or from existing holes in the input polygons). These areas are not invalid, but you can identify them for analysis. To identify the gaps in the output, set this parameter to NO_GAPS, and a feature will be created in these areas. To select these features, query the output feature class based on all the input feature'svalues being equal to -1. FIDGAPS-A feature will not be created for an area in the output that is completely enclosed by polygons. This is the default. NO_GAPS-A feature will be created for an area in the output that is completely enclosed by polygons. This feature will have no attribute values and will have avalue of -1. FID OUTPUTS: out_feature_class (Feature Class): The feature class that will contain the results. None
Q1¶
Write a script that creates a File Geodatabase
in the Results
folder, and copies those Shapfiles
that are either Point
or Polygon
shapeType from the database
folder to the newly created File Geodatabase
.
Note that you cannot use hard-coded filename (e.g. cities.shp
) to specify which Shapefile
to copy.
## Create File Geodatabase
outDb = arcpy.CreateFileGDB_management('./database/results' ,'MyGDB.gdb')
print(outDb)
./database/results/MyGDB.gdb
# List all feature classes in the workspace
fc_list = arcpy.ListFeatureClasses()
print(fc_list)
# Loop through each feature class
for fc in fc_list:
# Get the feature type of the current feature class
desc = arcpy.Describe(fc)
if desc.shapeType in ["Point", "Polygon"]: # Check if the feature type is Point or Polygon
# Define the output feature class path
outFeatureClass = str(outDb) + '\\' + str(fc).replace('.shp', '')
# Copy the feature class
arcpy.CopyFeatures_management(fc, outFeatureClass)
# Print a success message
print('Successful Copy: ' + str(fc))
else:
# Print a message indicating that the feature class is skipped
print('Skipping ' + str(fc) + ' because it is not a Point or Polygon feature class')
['amtrak_stations.shp', 'cities.shp', 'counties.shp', 'new_mexico.shp', 'railroads.shp', 'resultbuffers_poly.shp', 'resultLittlePolys.shp', 'resultWatersheds_polygon.shp'] Successful Copy: amtrak_stations.shp Successful Copy: cities.shp Successful Copy: counties.shp Successful Copy: new_mexico.shp Skipping railroads.shp because it is not a Point or Polygon feature class Successful Copy: resultbuffers_poly.shp Successful Copy: resultLittlePolys.shp Successful Copy: resultWatersheds_polygon.shp
Q2¶
Write a script that copies polygon
-type feature classes
and float
-type raster dataset
s from testdata.gdb
to the Results
folder.
Feature classes
can be saved as Shapefile
format while raster
datasets can be saved as TIF
format.
Note that you cannot use hard-coded filename (e.g. buffers_poly
) to specify which file to copy.
import os
# Set the workspace to the current directory
arcpy.env.workspace = os.getcwd() + "./database/testdata.gdb"
# List all feature classes in the geodatabase
fc_list = arcpy.ListFeatureClasses()
# Iterate over the feature classes
for fc in fc_list:
desc = arcpy.Describe(fc)
if desc.shapeType == "Polygon":
# Define the output feature class path
outFeatureClass = os.path.abspath("./database/results/") + "/" + os.path.basename(fc) + ".shp"
# Copy the feature class
arcpy.CopyFeatures_management(fc, outFeatureClass)
# Print a success message
print('Successful Copy: ' + str(fc))
# List all raster classes in the geodatabase
rs_list = arcpy.ListRasters()
# Iterate over the feature classes
for rs in rs_list:
desc = arcpy.Describe(rs)
if desc.isInteger == False:
# Define the output feature class path
outRasterClass = os.path.abspath("./database/results/") + "/" + os.path.basename(rs) + ".tif"
# Copy the raster dataset
arcpy.CopyRaster_management(rs, outRasterClass)
# Print a success message
print('Successful Copy: ' + str(rs))
Successful Copy: buffers_poly Successful Copy: LittlePolys Successful Copy: Watersheds_polygon Successful Copy: ElevationFloat
Q3¶
After completing Task 2, you should have the float-type
raster
dataset(s) in the Results folder.
Write a script to print out the properties
for each float-type
raster dataset, including the spatial reference name
, cell size
, columns
, rows
, Min
, Max
, and Mean
values.
Note that you cannot use hard-coded filename to specify which raster dataset to use.
# Set the workspace to the 'results' folder
arcpy.env.workspace = os.getcwd() + "./database/results"
# List all rasters in the workspace
rc_lst = arcpy.ListRasters()
print("List of Rasters:")
print(rc_lst)
List of Rasters: ['ElevationFloat.tif']
# Iterate over each raster in the list
for raster in rc_lst:
# Get raster information
raster_info = arcpy.Describe(raster)
# Print raster information in a formatted way
print("\nRaster Name: {}".format(raster_info.name))
print("Spatial Reference Name: {}".format(raster_info.spatialReference.name))
print("Cell Size: {}".format(raster_info.meanCellWidth))
print("Columns: {}, Rows: {}".format(raster_info.width, raster_info.height))
# Get raster properties
min_value = arcpy.GetRasterProperties_management(raster, 'MINIMUM')
max_value = arcpy.GetRasterProperties_management(raster, 'MAXIMUM')
mean_value = arcpy.GetRasterProperties_management(raster, 'MEAN')
# Print raster properties
print("Min: {}, Max: {}, Mean: {}".format(min_value, max_value, mean_value))
Raster Name: ElevationFloat.tif Spatial Reference Name: USA_Contiguous_Albers_Equal_Area_Conic_USGS_version Cell Size: 30.0 Columns: 233, Rows: 207 Min: 4309, Max: 5832, Mean: 4907.5136753423
Q4¶
After completing Task 2, you should have the polygon
-type Shapefiles
in the Results
folder.
Write a script to print out the String
-type fields with length > 10
for each Shapefile
.
Note that you cannot use hard-coded filename to specify which Shapefile
to use
fc_list = arcpy.ListFeatureClasses()
fc_list
['buffers_poly.shp', 'LittlePolys.shp', 'railroads_1000ft_buffer.shp', 'routes_clip.shp', 'Watersheds_polygon.shp']
# Iterate over each feature class in the list
for feature_class in fc_list:
try:
# Get the list of fields for the current feature class
field_list = arcpy.ListFields(feature_class)
# Print the feature class name
print("\nFeature Name: {}".format(feature_class.replace('.shp', '')))
# Iterate over each field in the field list
for field in field_list:
# Check if the field is a string type and has a length greater than 10
if field.type == 'String' and field.length > 10:
# Print the field name
print("Field Name: {}".format(field.baseName))
except Exception as e:
# Print the exception message if an error occurs
print("Error occurred while processing feature class {}: {}".format(feature_class, str(e)))
continue
Feature Name: buffers_poly Field Name: SITE_NO Field Name: STATION_NM Field Name: LAND_NET_D Field Name: MAP_NM Field Name: DATA_TYPES Field Name: INSTRUMENT Feature Name: LittlePolys Field Name: STAID Feature Name: railroads_1000ft_buffer Feature Name: routes_clip Field Name: RROWNER1 Field Name: RROWNER2 Field Name: RROWNER3 Feature Name: Watersheds_polygon Field Name: STAID