Week 5 Arcpy
Week 5: Arcpy
What is ArcPy¶
- What is arcpy
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
Key features of arcpy¶
- Geoprocessing Tools: Provides access to hundreds of tools, such as buffers, spatial joins, and overlay operations.
- Automation: Automates repetitive GIS tasks, like updating feature classes or running models, which improves workflow efficiency.
- Spatial Analysis: Supports complex spatial operations like proximity analysis, surface analysis, and spatial statistics.
- Data Management: Handles the creation, manipulation, and conversion of spatial data formats (e.g., shapefiles, geodatabases).
- Map Production: Helps generate and modify maps by automating tasks such as adding layers, symbols, and exporting maps.
Importing arcpy¶
- The ArcGIS Pro Conda environment is called arcgispro-py3 and is in the following folder
C:\Program Files\ArcGIS\Pro\bin\Python\envs
- Select the Arcpy conda environment
- Using
import arcpy
import arcpy to your python environment
Modules in arcpy¶
- The majority of
modules
represent an ArcGIS Pro toolbox that include variety of ArcGIS Pro tools.
- For example, access
Simplity 3D Line tool
using arcpy
In [1]:
Copied!
import arcpy.ddd
arcpy.ddd.Simplify3DLine()
import arcpy.ddd
arcpy.ddd.Simplify3DLine()
Classes in arcpy¶
- A
class
in Python is a blueprint forcreating objects (instances)
. - Classes define objects with properties and methods that allow for more complex data structures or functions, such as layers, spatial references, or cursor objects for working with data.
For example, create Point object using arcpy.Point
In [ ]:
Copied!
pts = arcpy.Point(-71.8023, 42.2626)
pts = arcpy.Point(-71.8023, 42.2626)
Functions in arcpy¶
Definition: A function is a reusable piece of code that performs a specific task. Functions take inputs (arguments), perform operations, and return outputs.
Purpose: Functions in arcpy perform specific geoprocessing tasks or operations on GIS data.
Examples in arcpy:
arcpy.Buffer_analysis()
: Creates a buffer around input features.
Working with Layers¶
Determing the layer type and property¶
In [48]:
Copied!
import arcpy
import pandas as pd
import arcpy
import pandas as pd
After importing ArcPy
, start with setting a workspace to retrieve and store files.
In [29]:
Copied!
path_work = r'D:\OneDrive - Clark University\Clark_Python_Data\Week05_arcpy\zip_MA'
path_work = 'D:/OneDrive - Clark University/Clark_Python_Data\Week05_arcpy/zip_MA'
path_work = 'D:\\OneDrive - Clark University\\Clark_Python_Data\\Week05_arcpy\\zip_MA\\ZIP_ma.aprx'
path_work = r'D:\OneDrive - Clark University\Clark_Python_Data\Week05_arcpy\zip_MA'
path_work = 'D:/OneDrive - Clark University/Clark_Python_Data\Week05_arcpy/zip_MA'
path_work = 'D:\\OneDrive - Clark University\\Clark_Python_Data\\Week05_arcpy\\zip_MA\\ZIP_ma.aprx'
arcpy.mp.ArcGISProject("path_work")
references the ArcGIS Pro project.
In [30]:
Copied!
aprx = arcpy.mp.ArcGISProject(path_work)
aprx = arcpy.mp.ArcGISProject(path_work)
aprx.listMaps("Map")
iterates through maps from the project
maps.listLayers()
retrieves a list of layers within a specific map.
arcpy.Describe()
provides information about the layer object.
In [31]:
Copied!
for maps in aprx.listMaps("Map"):
for lyr in maps.listLayers():
desc = arcpy.Describe(lyr)
print('{} is a {}'.format(desc.name, desc.dataType))
for maps in aprx.listMaps("Map"):
for lyr in maps.listLayers():
desc = arcpy.Describe(lyr)
print('{} is a {}'.format(desc.name, desc.dataType))
2024_Vehicle_Level_Crash_Details.shp is a FeatureLayer zip_Worcester.shp is a FeatureLayer
In [35]:
Copied!
map = aprx.listMaps("Map")[0]
layer = map.listLayers()[0]
print(layer.name)
map = aprx.listMaps("Map")[0]
layer = map.listLayers()[0]
print(layer.name)
2024_Vehicle_Level_Crash_Details
Properties in layers¶
In [45]:
Copied!
layer.isBasemapLayer
layer.isBasemapLayer
Out[45]:
False
In [40]:
Copied!
###fina a way to plot layers
###fina a way to plot layers
Symbology in Layer¶
- Symbology in GIS is the visual representation of spatial data, which helps in understanding and interpreting the information on maps.
- Symbology involves the styles and colors used to depict different features in a layer.
- Adjusting symbology can enhance map readability and convey specific patterns or trends.
In [46]:
Copied!
layer.symbology
layer.symbology
Out[46]:
<arcpy._symbology.Symbology at 0x2565ec07a90>
In [ ]:
Copied!
pd.DataFrame.spatial
pd.DataFrame.spatial