How to CRUD in ArcGIS using GIS Data (Feature class or Shapefile)
It has been almost 5 years, I work with ArcGIS Desktop one of Esri Products. From editing to design data in enterprise geodatabase, and geoprocessing toolbox to model builder. Esri cares their customers by creating an end-end solution without have to worry of how the process. However there is a time we as GIS Engineer or Specialist have to go back to the fundamental of how to do a CRUD (Create, Read, Update, and Delete)
GIS cannot be separated with the data. I always told my clients, GIS Data is special. It is different than the other data you have seen before. The data has image (Geometry) and information (tabular). The geometry classified into three main classes: Point, Polyline or Line in short, and Polygon (Area). This geometry is populated in Shape’s attribute field of a shapefile.
If the data is a point shaped, it cannot be populated with polyline. This also applies to the other geometries as well. Point for point, Polyline for polyline, and Polygon for polygon, alongside with their information or attributes.
Fundamentals of GIS Data
The create a shape’s attribute field it has to do a fixated process. The simplest is point where the coordinates (x,y) value is converted to a point.class. However Polyline and Polygon has a different approach. The list of of coordinates in the polyline and polygon must be converted to a list of point.class. Next the point.class converts to an array. In the final move, the array converts to a geometry object either it is Polyline where the first coordinate not equal to last coordinate or Polygon where the first coordinate equal to last coordinate
In this article, it will show you how to do CRUD, create, read, update and delete the data in the table of feature class or shapefile
arcpy.da.InsertCursor (Create)
To create a data, first we must create a tuple. GIS data uses tuple-data sequence because shape’s attribute field is immutable. To insert a data, first we need a feature class or a shapefile. At the minimum, the attribute fields are OID and SHAPE. There are going to be shown two examples: point, and polyline.
First we need to set up the feature class or shapefile address path, and create a dummy list of tupple. Then we set up the connection to the geodatabase table using arcpy.da.InsertCursor. The next line is using for-iteration to insert the data per index of the list
For polyline the fundamental is same, but to create a polyline from a coordinate they need to be created as an array first with arcpy.Array. Then, the array is converted to a line with arcpy.Polyline
In case of polygon data is also similar to polyline. However there is a tight rule in polygon. Polygon first coordinate must close the last coordinate which means the first coordinate and last coordinate have to be exact 100%. The last one is after using arcpy.Array, they must be converted to polygon using arcpy.Polygon
arcpy.da.SearchCursor (Read)
This command is very powerful when it combines with itteration command i.e. for-. Note, since OID is not a ‘true’ index of the table, it need to define the very first index as variable equal as zero. Then, to open up the connection to the feature class or shapefile use arcpy.da.SearchCursor
When accesing with the iteration command, the row in the table is defined as geometry object. Thus, it has to be parsed by accessing the attribute field column index that has the same sequences as the list of fields
You may wonder why in the script do not use del cursor_source. It is because in the script there is with — as command. Outside the command, the connection will be deleted automatically
arcpy.da.UpdateCursor (Update)
Update the data is almost no different than read the data. The only different is there is a write activity in the select row. To update the data there must be a conditional statement (if-else). The if-else must accessing the field index which has similar sequence in the fields variable. After that, return the value by writing the data using .updateRow(data)
Delete
There is two way of delete the data. First we use truncate toolbox or arcpy.management.TruncateTable, to delete a whole tabular data and reset the OID index. Last, we use arpy.da.SearchCursor and delete the select row with arcpy.management.DeleteRows
That is all how to a CRUD. This article aims to simplify the explanation from Esri documentation. With the fundamentals of CRUD, it will bring you further to do an advance spatial analysis. You can also elaborate it with pandas dataframe. But be warned, the shape’s field (SHAPE@) cannot be inserted to pandas dataframe, you need Spatial Dataframe ArcGIS or Geopandas Dataframe