Dr. Cho’s Website
Course Materials

# Manipulating raster data using ArcPy

Institute for Environmental and Spatial Analysis...University of North Georgia

## 1   NumPy

http://www.numpy.org

“The fundamental package for scientific computing with Python”

It comes with the default ArcGIS Pro installation.

``import numpy as np``

## 2   Making NoData cells

``````ras = arcpy.Raster('YOUR_RASTER_LAYER_NAME')

# convert raster to numpy array (numpy.ndarray class) since arcpy.Raster doesn't expose cell values directly
ras_a = arcpy.RasterToNumPyArray(ras)

ras2_a = ras_a.copy()
ras2_a[100:200,100:200] = 999
ras2 = arcpy.NumPyArrayToRaster(ras2_a, ras.extent.lowerLeft, ras.meanCellWidth, ras.meanCellHeight, 999)
arcpy.CopyRaster_management(ras2, 'P:\YOUR_PATH\ras2.tif')``````

## 3   Array coordinates ↔ Map coordinates

We need the `Extent`, `meanCellWidth`, and `meanCellHeight` attributes to map between `ndarray` and map coordinates.

``````# dimension of ras_a
ras_a.shape
# number of rows
(ras.extent.YMax-ras.extent.YMin)/ras.meanCellHeight
# number of columns
(ras.extent.XMax-ras.extent.XMin)/ras.meanCellWidth``````

## 4   get_array_indices

``````def get_array_indices(xy, ext, res):
'''
Get array indices using x, y, extent, and resolution
xy:  (x, y) indices
ext: raster extent
res: (width, height) raster resolution
'''
x = xy[0]
y = xy[1]
w = res[0]
h = res[1]
row = int((ext.YMax-y-h/2)/h)
col = int((x-w/2-ext.XMin)/w)
return row, col``````

## 5   Homework: Coordinate conversion

Complete this function.

``````def get_raster_coordinates(rc, ext, res):
'''
Get raster coordinates using row, column, extent, and resolution
rc:  (row, column) indices
ext: raster extent
res: (width, height) raster resolution
'''
row = rc[0]
col = rc[1]
w = res[0]
h = res[1]
return x, y``````