Dr. Cho’s Website
Course Materials

Manipulating point features using ArcPy

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

1   Cursors

Cursors allow you to traverse over features. There are three types of cursors:

  • InsertCursor: When you need to create new features
  • UpdateCursor: When you need to update all/specific existing features
  • SearchCursor: When you need to search for all/specific features

2   Create random points

2.1   Create an empty feature class

Since you cannot save an empty FeatureSet (remember the invalid JSON structure?), we have to use management tools to create an empty feature class.

# create a new shapefile
# create a spatial reference for Raleigh, NC
spatRef = arcpy.SpatialReference(3358)

# arcpy.CreateFeatureclass_management returns a Result object; take the first item, which is the full path
fc = arcpy.CreateFeatureclass_management(r'P:\tmp', 'random_points.shp', 'POINT', None, None, None, spatRef)[0]

# add some fields
arcpy.AddField_management(fc, 'x', 'DOUBLE')
arcpy.AddField_management(fc, 'y', 'DOUBLE')

2.2   Get some information

What do you need to create random points in the map?

Map Extent!

ext = arcpy.mp.ArcGISProject('CURRENT').activeMap.defaultCamera.getExtent()

2.3   Random coordinates

We need to use a pseudo-random number generator.

import random

random.random() returns a random number in $[0, 1)$ (i.e., $0\leq x<1$).

Equation for random coordinates?

\begin{align*} x& = x_\text{min} + r_x\times(x_\text{max}-x_\text{min})\\ y& = y_\text{min} + r_y\times(y_\text{max}-y_\text{min}) \end{align*}

2.4   Add new points

Use the InsertCursor and Point to add new points.

# here we'll insert geometry, x, and y
with arcpy.da.InsertCursor(fc, ['SHAPE@', 'x', 'y']) as cur:
  # 100 random points
  for i in range(100):
    x = ext.lowerLeft.X + random.random() * ext.width
    y = ext.lowerLeft.Y + random.random() * ext.height
    p = arcpy.Point(x, y)
    cur.insertRow([p, x, y])

2.5   Complete code

import random

# create a new shapefile
# create a spatial reference for Raleigh, NC
spatRef = arcpy.SpatialReference(3358)

# arcpy.CreateFeatureclass_management returns a Result object; take the first item, which is the full path
fc = arcpy.CreateFeatureclass_management(r'P:\tmp', 'random_points.shp', 'POINT', None, None, None, spatRef)[0]

# add some fields
arcpy.AddField_management(fc, 'x', 'DOUBLE')
arcpy.AddField_management(fc, 'y', 'DOUBLE')

# get the extent of the active map
ext = arcpy.mp.ArcGISProject('CURRENT').activeMap.defaultView.camera.getExtent()

# here we'll insert geometry, x, and y
with arcpy.da.InsertCursor(fc, ['SHAPE@', 'x', 'y']) as cur:
  # 100 random points
  for i in range(100):
    x = ext.lowerLeft.X + random.random() * ext.width
    y = ext.lowerLeft.Y + random.random() * ext.height
    p = arcpy.Point(x, y)
    cur.insertRow([p, x, y])

3   Update points

Use the UpdateCursor to retrieve certain features.

x_center = (ext.lowerLeft.X + ext.upperRight.X) / 2
x_delta = ext.width * 0.5
with arcpy.da.UpdateCursor(fc, ['SHAPE@', 'x', 'y'], f'x > {x_center}') as cur:
  for row in cur:
    x = row[1] + x_delta
    y = row[2]
    p = arcpy.Point(x, y)
    cur.updateRow([p, x, y])

4   Exercise: Draw a circle using points