Have you encountered data structured like the list of my favorite beaches below? Maybe you have a text file or spreadsheet that contains location and attribute information, and you want to display it on a map in ArcGIS Pro.
My favorite beaches
Avila, -120.7313, 35.17797
Coronado, -117.1944, 32.6864
Jalama, -120.50, 34.51
Newport, -117.9198, 33.6053
Santa Cruz, -122.01853, 36.963414
Esri’s Python site package (called arcpy) can help automate a process to read in location information, such as geographic coordinates, and turn each location into a feature in a geodatabase feature class table. The arcpy InsertCursor object adds new records into an attribute table. This post will guide you through the steps to do this.
If you are new to Python programming, before reading through this post, it may be helpful to watch the live training seminar, Python 101 for ArcGIS, to enhance your understanding of the fundamentals.
You can work your way through this post simply by reading or you can be hands-on by following the guided steps and start up ArcGIS Pro to try out and experiment with the code yourself. To get started, all you need is ArcGIS Pro.
Get Started
To set up for this process you open up ArcGIS Pro, create a map, and change its basemap.
Start ArcGIS Pro without a template.
From the Insert tab, Project group, insert a new map and change its name to California Beaches.
From the Map tab, Layer group, change the basemap to Oceans and zoom in to California.
Create a Geodatabase
Notebooks are used to create, document, save, share, and test run Python code.
In ArcGIS Pro, from the Insert tab, Project group, insert a new notebook.
In the Catalog pane, change the notebook name to CreateBeaches.ipynb.
The new Notebook has a blue outlined cell. Python code is written and organized into cells.
Add the following code into the cell. If you copy and paste, use CTRL+V to paste the code into the cell.
arcpy.management.CreateFolder("C:/", "temp")
arcpy.management.CreateMobileGDB("C:/temp", "Coastal")
Left of the cell you will see empty square brackets [ ], when you run the code you will see a star inside [*]. After the code successfully runs you will see a number inside [1].
The code above uses arcpy to run the CreateFolder and the CreateMobileGDB tools from the Management toolbox. The syntax arcpy.toolboxname.toolname can be used to run any of the over 1,600 tools in ArcGIS Pro.
When your notebook matches the code in the one above, click the Run button to create the Coastal mobile geodatabase in your c:\temp folder.
After a code cell runs it turns gray. Messages report run time or errors.
Under the messages a new empty cell is ready for more code. Cells contain manageable blocks of Python code that you can write and run independently (or all together). When a cell has the blue outline, it is the active cell and its code will run if you click the Run button.
Create a Feature Class
Feature classes are geodatabase tables composed of records and fields. A feature class is different than a standalone table because it has a special and different field called Shape. It is where point, line, or polygon geometry is stored.
Add the following code into the second cell.
sr = arcpy.SpatialReference(4326)
arcpy.management.CreateFeatureclass("C:/temp/Coastal.geodatabase",
"Beaches", "POINT", spatial_reference = sr)
The first line uses WKID 4326 to set up a spatial reference to the WGS84 geographic coordinate system. The beach Longitude and Latitude coordinates are WGS84. The second line creates the Beaches point feature class using the CreateFeatureclass tool.
When your cell matches this example, click the Run button to create the Beaches feature class.
When the cell finishes running, in the Catalog pane, navigate to the C:\Temp folder to confirm the geodatabase and feature class are both there.
Coastal.geodatabase is stored as a single file. Mobile geodatabases use the open-source database format called SQLite. The main.Beaches feature class uses the SQLite naming convention.
Look in the Contents pane to see the Beaches layer on the map.
Add a Field to the Feature Class Table
A table is made up of fields, sometimes called columns, attributes, headings, and even Attribute Fields. They each have a data type like number, text string, date, blob, ID, and geometry.
Back in the notebook add the following code into the third cell.
arcpy.env.workspace = "C:/temp/Coastal.geodatabase"
arcpy.management.AddField("Beaches", "BeachName", "TEXT", field_length = 10)
The first line sets the environment workspace to the Coastal mobile geodatabase. The next line runs the AddField tool to add a text field called BeachName to the Beaches feature class. The field is 10 characters wide.
Click the Run button to add the BeachName field to the Beaches feature class.
Add a Record to the Table
In a table the rows are called records; in a feature class the rows are called features. The InsertCursor adds rows to a table.
In the Notebook’s fourth cell add the following code.
cursor = arcpy.da.InsertCursor("Beaches", ["BeachName", "SHAPE@XY"])
row = ["Jalama", (-120.50, 34.51)]
cursor.insertRow(row)
The first line of code creates an InsertCursor that will be used to insert a new beach. The row variable contains a list with Jalama beach and a tuple of its coordinate pair.
Click the Run button to add Jalama Beach to the Beaches layer.
Add Many Records to the Table
The InsertCursor can add one record at a time as shown above, however when structured inside a looping routine many records can be added in one shot!
In the Notebook’s fifth cell add the following code. Take notice of the for loop and how it has two indented lines. If you copy and paste that indent might be lost. Before you test run the cell ensure that your indenting looks that that shown below.
BeachList = [
["Coronado", (-117.1944, 32.6864)],
["Avila", (-120.7313, 35.17797)],
["Santa Cruz", (-122.01853,36.963414)],
["Newport", (-117.9198, 33.6053)]
]
for Beach in BeachList:
row = Beach
cursor.insertRow(row)
del cursor
The first lines (1 to 6) represent a list of beaches where each beach has its own unique list containing a name and a tuple for the pair of the WGS84 coordinates. The for loop (lines 7 to 9) goes to each beach in the list and adds it into the Beaches feature class. When the cursor is done doing its job the best practice is to remove it from memory using the Python del statement.
Click the Run button to add four more beaches.
Bring the map to the front to see the beaches! If the beaches don’t appear zoom in and out on the map to refresh. The point symbols are small and hard to see. Size and color change is next.
Back in the Notebook, in the next new cell, add the following code.
aprx = arcpy.mp.ArcGISProject("current")
m = aprx.listMaps("California Beaches")[0]
lyr = m.listLayers("main.Beaches")[0]
lblClass = lyr.listLabelClasses()[0]
lblClass.expression = "$feature.BeachName"
lyr.showLabels = True
This code navigates to the current project, gets the map, gets the beaches layer, and changes label expression to the BeachName field, and finally turns on the layer's labels.
Run the cell.
Bring the California Beaches Map to the foreground to see labels on all five beaches!
Back in the Notebook, in the next new cell, add the following code.
sym = lyr.symbology
sym.renderer.symbol.color = {'RGB' : [255, 0, 0, 60]}
sym.renderer.symbol.size = 12
lyr.symbology = sym
This code gets and sets the layer’s symbol renderer to change color to red, 60% transparent, and increase the point symbol size to 12.
Run the cell.
Bring the California Beaches Map to the foreground to see the color and size changes.
Congratulations! You just added a feature to a feature class using an arcpy cursor.
ArcGIS Pro cursors give you record-by-record access to your geospatial data. As you become more comfortable with Python, you will discover even more innovative ways to apply it.
Cursors represent a powerful leap forward in how you customize your ArcGIS Pro experience. Whether you need to fine-tune existing data, generate entirely new features, or uncover insights through targeted analytics, cursors provide the precision and flexibility that standard tools sometimes lack. Embracing cursors unlocks a new level of problem-solving within your GIS workflows. Think of them as your key to turning good data into actionable intelligence.
___________________
Learn More
For more information on cursors in ArcGIS Pro, check out these resources:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.