Accessing raster data using ArcPy

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

1   ArcPy modules

  • arcpy.mp: Mapping module
  • arcpy.sa: Spatial analyst module
  • arcpy.da: Data access module
  • arcpy.na: Network analyst module
  • arcpy.sharing: Sharing module
  • arcpy.wmx: Workflow manager module

2   ArcGIS Pro Python window interface

python-window-arcpy-list.png

(P)Packages
(O)Objects
<M>Methods
<F>Functions
<T>Tools
( )Constants
(...)Groups

3   Help

help(arcpy)
help(arcpy.mp)
help(arcpy.mp.ArcGISProject)

4   Project

In most cases, we start from an ArcGISProject object (a project).

# specific project
prj = arcpy.mp.ArcGISProject(r'P:\YOUR_PATH\Project.aprx')

# current project
prj = arcpy.mp.ArcGISProject('CURRENT')

# help on an object?
help(prj)

5   Maps

An ArcGISProject may contain multiple Maps.

# all maps
for m in prj.listMaps():
    print(m.name)

# active map
m = prj.activeMap
print(m.name)

6   Layers

Usually, there are multiple Layers in a Map.

# all layers in the active map
for lyr in m.listLayers():
    print((lyr.name, lyr.dataSource, lyr.isRasterLayer, lyr.isFeatureLayer, lyr.isWebLayer))

7   Raster data

7.1   Add raster layers

Add your own raster layers or create a new one.

# let's export the current map as a color TIFF
filename = r'P:\YOUR_PATH\map.tif'
width = height = 1024
m.defaultView.exportToTIFF(filename, width, height, geoTIFF_tags=True)

# add the TIFF to the map
m.addDataFromPath(filename)

7.2   Raster

This is how you find a raster Layer and get its Raster object.

# find the first raster layer
for lyr in m.listLayers():
    if lyr.isRasterLayer:
        break

# get the raster object
ras = arcpy.Raster(lyr.name)
print(ras.name, ras.extent)

7.3   Raster attributes

A Raster object has many useful attributes.

# all attributes not starting with _
for i in dir(ras):
    if not i.startswith('_'):
        # ras.attribute_name is equivalent to getattr(ras, attribute_name)
        # why use this syntax? when attribute_name is string
        # e.g., ras.extent == getattr(ras, 'extent')
        print((i, getattr(ras, i)))

7.4   Raster to array

The Raster object does not expose its cell values as an attribute.

Then, how can you access/manipulate the cell values?

Convert the Raster object to a NumPy array. NumPy arrays are a GIS-agnostic data type. We can use NumPy functions to modify raster cells.

arr = arcpy.RasterToNumPyArray(ras)
print(arr)

7.5   Raster bands

Some raster Layers have four bands including red, green, blue, and alpha.

print(arr.shape)

7.6   Modifying cell values

Just use the NumPy array to change cell values.

# clone arr
arr2 = arr.copy()

# set the entire red band to 0
arr2[0] = 0

# set the green band at cell (50, 60) to 255
arr2[1][50][60] = 255

7.7   Save raster

Save the NumPy array as a raster file.

filename2 = r'P:\YOUR_PATH\map_wo_red.tif'
ras2 = arcpy.NumPyArrayToRaster(arr2, ras.extent.lowerLeft, ras.meanCellWidth, ras.meanCellHeight)
arcpy.CopyRaster_management(ras2, filename2)
m.addDataFromPath(filename2)