0% found this document useful (0 votes)
74 views1 page

ArcPyCheatsheet 3.3

hhhhhh

Uploaded by

hi157hi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views1 page

ArcPyCheatsheet 3.3

hhhhhh

Uploaded by

hi157hi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Esri ArcPy Cheat Sheet

®
TM

What Is ArcPy? Installing ArcPy The ArcPy package is part of the default Python environment arcgispro-py3 that
is distributed with ArcGIS Pro and ArcGIS Server.
ArcPy (often referred to as the ArcPy site package) provides Python access for all geoprocessing tools, including extensions, as
TM

To get started with a script:


well as a wide variety of useful functions and classes for working with and interrogating GIS data. Using Python and ArcPy, you can
develop an infinite number of useful programs that operate on geographic data. import arcpy

Listing Data ArcPy Environments Cursors ArcPy Modules


ArcPy list functions are used for listing ArcGIS® data. Most of these functions Environment settings are accessed from arcpy.env.EnvManager can be Cursors are used to access and manipulate records in a table or feature A module defines a collection of
list data from the current ArcPy environment workspace. See the ArcPy used in a with statement to temporarily set an environmental variable for class. Cursors place a lock on the data being accessed. After using a functions, classes, operators, and
Environments section for setting your workspace. a section of the script. Environment settings set in a script will go out of cursor, delete the cursor object. This can be accomplished with the cursor’s other constructs.
scope (reset) once the script terminates. context manager. Once the context manager code block is exited, the
Returns a list of datasets arcpy.ListDatasets() cursor object is automatically deleted. ArcPy modules:
Get environment:
Charts
Returns a list of feature classes arcpy.ListFeatureClasses() print(arcpy.env.workspace) with arcpy.da.SearchCursor(...) as cursor: arcpy.charts
Set the workspace environment: for row in cursor:
Returns a list of files arcpy.ListFiles() Data Access
arcpy.env.workspace = “C:/<path>“ ... arcpy.da
Returns a list of the rasters arcpy.ListRasters() Temporarily set workspace environment: This avoids explicitly calling del <cursor>. Mapping
with arcpy.EnvManager(workspace=“C:/<path>“): arcpy.mp
Returns a list of tables arcpy.ListTables() Returns rows of attribute values
... Metadata
arcpy.da.SearchCursor( arcpy.metadata
Returns a list of workspaces arcpy.ListWorkspaces() in_table, field_names, ...
Network Analyst
A few ArcPy list functions require passing in the workspace/dataset to search. Describing data ) arcpy.nax and arcpy.na
Query the properties of an object such as data type, fields, and indexes. Updates or deletes rows of attribute values Sharing
Returns a list of the indexes in a specified dataset arcpy.sharing
Returned values can be used to conditionally operate on the data. There arcpy.da.UpdateCursor(
arcpy.ListIndexes(dataset)
are two varieties of Describe shown below; the former is slower but great in_table, field_names, ... ArcPy modules representing
Returns a list of fields in a specified dataset for exploring properties, while the latter is faster and therefore useful in ) an ArcGIS Pro toolbox:
arcpy.ListFields(dataset) production code.
Inserts rows of attribute values 3D Analyst
Lists the versions the connected user has permission to use Describes a data element and returns a dictionary arcpy.da.InsertCursor( arcpy.ddd
arcpy.ListVersions(sde_workspace) (slower, great for exploration) in_table, field_names, ... AllSource
arcpy.da.Describe(value) ) arcpy.intelligence
Alternatively, the data access module (arcpy.da) helps you find and access Analysis
files using Walk, which is based on the Python built-in os.walk. Unlike in the Describes a data element and returns an object arcpy.analysis
list functions above, the top directory is passed as an argument. (faster, great for production code) Aviation
arcpy.Describe(value) arcpy.aviation
Traverses the directory tree and returns the directory path,
Business Analyst
directory names, and file names arcpy.ba
arcpy.da.Walk(top, ...) Running Geoprocessing Tools with ArcPy Select Cartography
arcpy.cartography
Getting Code Snippets from Tools Parameters Environments
Cursors Field Tokens Accessing tool help
Conversion
arcpy.conversion
Use cursor field tokens to access special fields or data of a dataset. Input Features
Here are some useful tokens: Crime Analysis and Safety
NationalParks arcpy.ca
Run
OID@ Object ID Output Feature Class Data Interoperability
arcpy.di
GLOBALID@ Global UID Schedule JoshuaTree_NationalPark
Data Management
Reset Parameters Expression arcpy.management
SHAPE@X X-coordinate
Data Reviewer
Copy Python Command Load Save Remove
SHAPE@Y Y-coordinate arcpy.reviewer
SQL
Defense
SHAPE@Z Z-coordinate Run arcpy.defense
Where NAME is equal to JOSHUA TREE
Editing
SHAPE@M M-coordinate
Catalog Geoprocessing Add Clause arcpy.edit
SHAPE@XY Centroid x,y Coordinates GeoAI
arcpy.geoai
SHAPE@XYZ Centroid x,y,z coordinates arcpy.analysis.Select(“NationalParks“, “JoshuaTree_NationalPark“, “Name = ‘JOSHUA TREE‘“) GeoAnalytics Desktop
arcpy.geoanalytics
SHAPE@AREA Area
Constructing Geometries Geometry Properties and Methods GeoAnalytics Server
SHAPE@LENGTH Length Geometry objects define a spatial location and an associated geometric Geometry objects have properties, methods, and operators that can be used arcpy.gapro
shape. The primary geometry objects are PointGeometry, Multipoint, to query and manipulate the geometry. Availability varies by geometry type. Geocoding
SHAPE@ Geometry object Polyline, and Polygon. The basic building blocks of geometry objects are arcpy.geocoding
Generally useful properties:
Point objects. Geostatistical Analyst
SHAPE@WKT Well-known text representation
type Type of geometry arcpy.ga
P1 = arcpy.Point(-22.00, 64.00)
SHAPE@WKB Well-known byte representation Image Analyst
Complex geometries are defined by an Array of Point objects. spatialReference Spatial reference object arcpy.ia
SHAPE@JSON Esri JSON representation A1 = arcpy.Array( Indoor Positioning
pointCount Number of points in geometry
[ arcpy.indoorpositioning
arcpy.Point(-22.00, 64.00), For example: Indoors
Data Conversion arcpy.Point(-22.50, 53.85) if G1.type == “point“: arcpy.indoors
Besides providing access to records and attributes of datasets with Cursors Linear Referencing
] ...
and Describe, the data access module (arcpy.da) also helps convert to arcpy.lr
)
other popular Python data formats such as the NumPy Structured Array Generally useful methods: Location Referencing
and the Arrow Table. Note: Do not confuse Point and Array objects with the primary geometry arcpy.locref
objects. The latter are constructed from the former. buffer(distance) Buffer the geometry
Converts a feature class to NumPy structured array Maritime
The primary geometry objects can be constructed from Point and Array distanceTo(other) Distance to another geometry arcpy.maritime
arcpy.da.FeatureClassToNumPyArray(
objects as follows: Multidimension
in_table, field_names, ...
within(geometry) Is geometry within another arcpy.md
) Create a PointGeometry object—defined by a single point object
Network Analyst
arcpy.PointGeometry(P1) For example: arcpy.nax, arcpy.na
Converts a NumPy structured array to a point feature class
distance = G1.distanceTo(G2)
arcpy.da.NumPyArrayToFeatureClass( Create a Multipoint object—an ordered collection of points Network Diagram
in_array,out_table,shape_fields, ... defined by an array of point objects arcpy.nd
) arcpy.Multipoint( Geometry Operators Oriented Imagery
arcpy.Array([P1, P2, ...] The primary geometry objects also support the following operators to arcpy.oi
Converts a NumPy structured array to a table
) perform relational operations: Parcels
arcpy.da.NumPyArrayToTable( arcpy.parcels
in_array, out_table Create a Polyline—a path defined by an array of point objects + Intersect G3 = G1 + G2 Public Transit
) arcpy.Polyline( arcpy.transit
arcpy.Array([P1, P2, ...] | Union G3 = G1 | G2
Converts a table to a NumPy structured array Raster Analysis
) G3 = G1 - G2 arcpy.ra
arcpy.da.TableToNumPyArray( - Difference
in_table, field_names, ... Ready To Use
Create a Polygon object—a closed shape defined by an array of
^ Symmetric Diff. G3 = G1 ^ G2 arcpy.agolservices
) points (To close the shape, the last point in the array must equal
Reality Mapping
the first.) == Equals G1 == G2
Converts a table or feature class to an Apache Arrow table arcpy.rm
arcpy.Polygon(
arcpy.da.TableToArrowTable(in_table) Server
arcpy.Array( != Not Equals G1 != G2
arcpy.server
[P1, P2, P3, ..., P1]
Space Time Pattern Mining
Extracting Geometries )
Geoprocessing Tool Results arcpy.stpm
You can also extract geometries from existing features in two ways: )
Most geoprocessing tools return a Result object. The Result object Spatial Analyst
1) Setting the output parameter of a geoprocessing tool to an empty maintains information about a tool operation after it has completed. arcpy.sa
Geometry object will output a list of Geometry objects. Spatial Statistics
Memory Workspace You can use the Result object directly as input to another tool, or extract
arcpy.stats
geoms = arcpy.management.CopyFeatures( Significantly speed up processing times with the memory workspace. the outputs, messages, and parameters.
“C:/<path>“, arcpy.Geometry() Standard Feature Analysis
The memory workspace is a memory-based workspace that supports R1 = arcpy.analysis.Buffer(infc, outfc, 100) arcpy.sfa
) output feature classes, tables, and raster datasets. To write to the memory
Territory Design
2) Read geometries with arcpy.da cursors using the SHAPE@ field token. workspace, specify an output dataset path beginning with memory and Get the number of outputs
arcpy.td
with no file extension. The memory workspace is used for intermediate R1.outputCount
# Store geometries in a Python list Topographic Production
output, but final output should be persisted to disk. arcpy.topographic
buffered_points = [ Get a given output
row[0].buffer(100) outfc = r“memory\tempOutput“ R1.getOutput(index) Trace Network
for row arcpy.tn
in arcpy.da.SearchCursor(fc, “SHAPE@“) Get a given input Utility Network
] R1.getInput(index) arcpy.un

Get the geoprocessing tool messages Workflow Manager


arcpy.wmx
R1.getMessages()
Note: Whenever the characters <> encase a word, it marks a
placeholder for an object or value to be replaced by the user. Note: Geoprocessing tools in the Image Analyst (arcpy.ia) and Spatial
Analyst (arcpy.sa) modules return a Raster object.
Copyright © 2024 Esri. All rights reserved. Esri, the Esri globe logo, The Science of Where, ArcGIS, ArcPy, and other Esri marks referenced in this work are trademarks, service marks, or registered marks
of Esri in the United States, the European Community, or certain other jurisdictions. To learn more about Esri marks go to: https://www.esri.com/content/dam/esrisites/en-us/media/legal/copyrights-and-
trademarks/esri-product-naming-guide.pdf.
Other companies and products or services mentioned herein may be trademarks, service marks, or registered marks of their respective mark owners.

G4856739

You might also like