Skip to content

Instantly share code, notes, and snippets.

View hwidlund's full-sized avatar

Heather Widlund hwidlund

View GitHub Profile
@hwidlund
hwidlund / data-int-config.py
Last active September 25, 2016 22:23
Data integration config
from os.path import dirname, join, realpath
import arcpy
import ConfigParser ## note that in Python 3.x, name will change to configparser
#-------------------------------------------------------------------------------
# Function to write each section with its keys & values
#-------------------------------------------------------------------------------
def write_config(keys, values, config, section):
config.add_section(section)
@hwidlund
hwidlund / snippet_checksnaps1.py
Last active September 17, 2016 19:59
Code for script tool to check if lines snap to points
import arcpy
arcpy.env.overwriteOutput = True
snapPoints = arcpy.GetParameterAsText(0)
roadLines = arcpy.GetParameterAsText(1)
outFc = arcpy.GetParameterAsText(2)
searchDistance = arcpy.GetParameterAsText(3)
try:
@hwidlund
hwidlund / snippet-concat.py
Created June 22, 2016 23:13
Snippet for concatenating parsed fields without extra spaces
# concatenate parsed address fields without extra spaces
import arcpy
featureClass = "c:\\myfakepath\\data.gdb\\fc"
# create an expression to find records where the STREETNO field is not blank
# this indicates an address to be concatenated
expression = "STREETNO IS NOT NULL"
# create a list of the fields to be concatenated
# (the last field in the list is the field to be concatenated in to)
import arcpy
# input feature class and field name to check for duplicates
inputFC = r"c:\mygeodatabase.gdb\myfeatureclass"
myField = "myField"
# create new, empty lists for duplicate values and unique values
dupeList = []
uniqueList = []
@hwidlund
hwidlund / snippet-fieldtruncate.py
Last active May 7, 2016 20:02
Snippet to truncate feature class field to new length
import arcpy
# get parameters
fc = "c:/geodatabase.gdb/featureclass" ## feature class
field = "field" ## name of field to truncate
newLength = 100 ## new length: whatever is needed
# create new name, length, expression to truncate existing data
newField = field + "tmp"
newLengthSlice = newLength - 1
@hwidlund
hwidlund / snippet-get-fields-restrict.py
Last active May 4, 2016 23:50
Snippets for returning subsets of feature class fields
fc = "c:/path/geodatabase.gdb/featureclass"
# restrict returned fields by wildcard
fieldList = arcpy.ListFields(fc,"OWN*")
# restrict returned fields by field type
fieldList = arcpy.ListFields(fc,"","String")
# exclude required fields such as Shape and OID
# (https://blogs.esri.com/esri/arcgis/2012/08/30/field-mapping-and-python-scripting)
@hwidlund
hwidlund / snippet-fieldlen-compare.py
Last active May 4, 2016 22:26
Code snippet for comparing string field lengths using dictionaries
import arcpy
srcFC = "c:/path/Geodatabase.gdb/srcFc"
destFC = "c:/path/Geodatabase.gdb/destFc"
# get field lists from each feature class, restricting to "String" feature types
srcFields = arcpy.ListFields(srcFC,"","String")
destFields = arcpy.ListFields(destFC,"","String")
# create empty dictionaries
@hwidlund
hwidlund / snippet-fc-fields.py
Last active May 4, 2016 21:28
Snippets to work with feature class fields in Python and arcpy
import arcpy
featureClass = "c:/path/Geodatabase.gdb/fc"
# Get a list of field objects from a feature class
fcFields = arcpy.ListFields(featureClass)
# Print the field names, types and lengths using string formatting
for field in fcFields:
print("{0}: type = {1} length = {2}".format(field.name, field.type, field.length))
@hwidlund
hwidlund / checkfields.py
Created May 1, 2016 22:01
Snippet to use string format to examine ArcGIS feature class fields
import arcpy
source ="path/to/featureclass"
srcFlds = arcpy.ListFields(source)
for field in srcFlds:
print("{0} type = {1} length = {2}".format(field.name, field.type, field.length))