arcpy

Resources

Connect
with
ArcGIS Pro

Import arcpy


import arcpy

# optional - to give some output on the terminal
print(":::")

Set workspace


arcpy.env.workspace = "c:/github/arcpy/arcpy_intro/Default.gdb"

What
data do
we have?

List feature classes


feature_classes = arcpy.ListFeatureClasses()

Print feature classes


for fc in feature_classes:
    print(fc)

"Finished" message


print("...")

listfcs.py


import arcpy

print(":::")

arcpy.env.workspace = "c:/github/arcpy/arcpy_intro/Default.gdb"

feature_classes = arcpy.ListFeatureClasses()

for fc in feature_classes:
    print(fc)

print("...")

in
more
detail

fields.py


import arcpy

print(":::")

arcpy.env.workspace = "c:/github/arcpy/arcpy_intro/Default.gdb"

fields = arcpy.ListFields("bygning")

for field in fields:
    print("{0} is a type of {1} with a length of {2}"
          .format(field.name, field.type, field.length))

print("...")

Get
some

data

searchcursor.py

import arcpy

print(":::")

arcpy.env.workspace = "c:/github/arcpy/arcpy_intro/Default.gdb"

cursor = arcpy.SearchCursor(
    "c:/github/arcpy/arcpy_intro/Default.gdb/bygning",
    fields="bygningsnummer",
    where_clause="bygningsnummer = 20239077")

row = cursor.next()

while row:
    print(row.getValue("bygningsnummer"))
    row = cursor.next()

print("...")

Add
some
data

addfield.py

import arcpy

print(":::")

arcpy.env.workspace = "c:/github/arcpy/arcpy_intro/Default.gdb"

inFeatures = "bygning"
fieldName = "RES_capacity"
fieldPrecision = 9
fieldAlias = "capacity"

arcpy.AddField_management(inFeatures,
                          fieldName,
                          "LONG",
                          fieldPrecision,
                          field_alias=fieldAlias,
                          field_is_nullable="NULLABLE")

print("...")

updatecursor.py

import arcpy

print(":::")

arcpy.env.workspace = "c:/github/arcpy/arcpy_intro/Default.gdb"

cursor = arcpy.UpdateCursor(
    "c:/github/arcpy/arcpy_intro/Default.gdb/bygning",
    where_clause="bygningsnummer = 20239077")

row = cursor.next()

while row:
    row.setValue("RES_capacity", 1111)
    cursor.updateRow(row)
    print(row.getValue("bygningsnummer"), end = " ")
    print(row.getValue("RES_capacity"))
    row = cursor.next()

print("...")

arcpy

By sverres

arcpy

  • 524