[PYTHON] Add fields to features with ArcPy

Python code

AddField.py


import arcpy

#Check input parameters
arcpy.AddMessage("GetArgumentCount() = {0}".format(arcpy.GetArgumentCount()))
for i in range(arcpy.GetArgumentCount()):  
    arcpy.AddMessage("type(GetParameter({0})) = {1}".format(i, type(arcpy.GetParameter(i))))
    arcpy.AddMessage("GetParameterAsText({0}) = {1}".format(i, arcpy.GetParameterAsText(i)))

inFeatureLayer = arcpy.GetParameter(0)
outFeatureClass = arcpy.GetParameter(1)

#Copy feature layer
arcpy.CopyFeatures_management(inFeatureLayer, outFeatureClass)

#Add field
arcpy.AddField_management(outFeatureClass, "field1", "DOUBLE", field_alias = "Field 1")
arcpy.AddField_management(outFeatureClass, "field2", "DOUBLE", field_alias = "Field 2")

fields = ["OID@", "SHAPE@X", "SHAPE@Y", "field1", "field2"]

#Enter a value in the field
with arcpy.da.UpdateCursor(outFeatureClass, fields) as cursor:
    arcpy.AddMessage("Field before input")
    for row in cursor:
        arcpy.AddMessage(row)
        row[3] = row[1]
        row[4] = row[2]
        cursor.updateRow(row)
del cursor

#Check the status after input
with arcpy.da.SearchCursor(outFeatureClass, fields) as cursor:
    arcpy.AddMessage("Field after input")
    for row in cursor:
        arcpy.AddMessage(row)
del cursor

Execution result

When executed as input with a feature layer containing two polygons.

2020-01-12-20-40-12.png

GetArgumentCount() = 2
type(GetParameter(0)) = <class 'arcpy._mp.Layer'>
GetParameterAsText(0) =Test polygon
type(GetParameter(1)) = <class 'geoprocessing value object'>
GetParameterAsText(1) = D:\GIS\ArcGIS_Project\GeometryTest\GeometryTest.gdb\Test polygon_AddField
Field before input
[1, 134.32332180386206, 30.338120870687362, None, None]
[2, 144.42135929631993, 39.08930527833865, None, None]
Field after input
[1, 134.32332180386206, 30.338120870687362, 134.32332180386206, 30.338120870687362]
[2, 144.42135929631993, 39.08930527833865, 144.42135929631993, 39.08930527833865]

2020-01-12-20-40-50.png

reference

--Copy Features (https://pro.arcgis.com/en/pro-app/tool-reference/data-management/copy-features.htm) --Add Field (https://pro.arcgis.com/en/pro-app/tool-reference/data-management/add-field.htm)

Recommended Posts

Add fields to features with ArcPy
How to add a package with PyCharm
Convert character strings to features with RoBERTa
Add images to iOS photos with Pythonista
Add Gaussian noise to images with python2.7
Add rows to an empty array with numpy
How to add arbitrary headers to response with FastAPI
Add / remove kernel to use jupyter with venv
Add convolution to MNIST
Add CSV export function to management screen with django-import-export
How to add help to HDA (with Python script bonus)
Convert 202003 to 2020-03 with pandas
Add information to the bottom of the figure with Matplotlib
Just add the driver to the shape key with blender
Access to dictionary fields
Add fake tilt shift effect to video with OpenCV + Python
[pyqtgraph] Add region to the graph and link it with the graph region
The usual way to add a Kernel with Jupyter Notebook
[Memo to add] Page to see when developing with GAE / P
Try to extract the features of the sensor data with CNN
Add / remove kernel to JupyterLab
Add a dictionary to MeCab
Connect to Wikipedia with Python
Post to slack with Python 3
Connect to Postgresql with GO
Output to syslog with Loguru
Add page number to PDF
Introduction to RDB with sqlalchemy Ⅰ
Easy to make with syntax
How to update with SQLAlchemy?
To run gym_torcs with ubutnu16
How to cast with Theano
How to Alter with SQLAlchemy?
Switch python to 2.7 with alternatives
Write to csv with Python
How to separate strings with','
Get image features with OpenCV
How to RDP with Fedora31
2 ways to deal with SessionNotCreatedException
Add System to pyenv-win versions
How to Delete with SQLAlchemy?
Add user dictionary to MeCab
[Hugo] Summary of how to add pages to sites built with Learn
Add users with SSH access to your Amazon EC2 Linux instance