Skip to content

Instantly share code, notes, and snippets.

View tonjadwyer's full-sized avatar

Tonja Dwyer tonjadwyer

View GitHub Profile
@tonjadwyer
tonjadwyer / compare_fields_with_sets.py
Created February 7, 2019 19:55
ArcGIS-Python WIndow-Compare feature class fields using sets
l1_name = []
with arcpy.da.SearchCursor(r"layer1", ("OID@", "Name")) as cursor:
for row in cursor:
l1_name.append(row[1])
l2_name = []
with arcpy.da.SearchCursor(r"layer2", ("OID@", "Name")) as cursor2:
for row in cursor2:
l2_name.append(row[1])
@tonjadwyer
tonjadwyer / walk directory.py
Last active February 7, 2019 19:43
python walk directory to list (thispointer.com)
# puts all file paths in filelist
# puts all folders in dirlist
filelist = []
dirlist = []
for (dirpath,dirnames,filenames) in os.walk('my\path\to\walk'):
filelist += [os.path.join(dirpath,file) for file in filenames]
dirlist += dirnames
@tonjadwyer
tonjadwyer / build_field_map
Created January 6, 2018 04:50
Build an ArcPy FieldMappings object from source and target feature classes using field map tuples.
#
# fm_input is a field mapping tuple
# fm_input = (("target1","source1"),("target2","source2"),("target3","source3"))
def build_field_map(target, source, fm_input):
field_mapps = arcpy.FieldMappings()
field_mapps.addTable(target)
@tonjadwyer
tonjadwyer / expression_from_cal
Last active January 6, 2018 04:42
Pull a field calculator .cal file into a string
def expression_from_cal(infile):
with open(infile, "rb") as data:
text = data.read().decode('utf-16-le')
print_msg(text)
return text.strip()
@tonjadwyer
tonjadwyer / cal_to_expression_codeblock
Created September 28, 2017 03:52
Get expression and codeblock from .cal file as strings.
def expression_code_block_from_cal(infile):
with open(infile, "rb") as data:
text = data.read().decode('utf-16-le')
lines = text.splitlines()
# trim the trailing spaces
@tonjadwyer
tonjadwyer / arcpy-table-to-dict.py
Created March 2, 2017 18:52
Read an ArcGIS table into a dictionary.
#Source: http://gis.stackexchange.com/questions/54804/fastest-methods-for-modifying-attribute-tables-with-python
# fc - is the full path name to table or feature class
def make_attribute_dict(fc, key_field, attr_list=['*']):
attdict = {}
fc_field_objects = arcpy.ListFields(fc)
fc_fields = [field.name for field in fc_field_objects if field.type != 'Geometry']
if attr_list == ['*']:
valid_fields = fc_fields
else:
@tonjadwyer
tonjadwyer / gist:a85b46b630cecbaa40119876752c4d6d
Created December 13, 2016 22:22
Excel Formula Find Right
=IF(ISERROR(FIND("\",A2)),A2, RIGHT(A2,LEN(A2) - FIND("|",SUBSTITUTE(A2,"\","|",LEN(A2)-LEN(SUBSTITUTE(A2,"\",""))))))