Skip to content

Instantly share code, notes, and snippets.

View M-Bryant's full-sized avatar

Mark Bryant M-Bryant

  • AECOM
  • Auckland, New Zealand
  • 22:35 (UTC +13:00)
View GitHub Profile

Commit Message Structure

<type>[optional scope]: <description>

Quick examples

  • feat: new feature
  • fix(scope): bug in scope
  • feat!: breaking change / feat(scope)!: rework API
  • chore(deps): update dependencies

Commit types

@M-Bryant
M-Bryant / list_join_tables.py
Last active February 17, 2017 04:58
arcpy: list joined tables
import arcpy
def list_join_tables(in_table):
"""Returns a list of tables currently joined to a table or feature class.
List includes the in_table
Returns empty list if no joins
"""
fields = [f.name for f in arcpy.Describe(in_table).fields]
return = list(set([f.split(".")[0] for f in fields if "." in f]))
@M-Bryant
M-Bryant / monotonic.py
Created December 14, 2016 00:53
Set of python functions for checking list of number increasing
def strictly_increasing(L):
"""Returns TRUE if the values in the list are strictly increasing
Always increasing; never remaining constant or decreasing or null.
"""
return all(x<y for x, y in zip(L, L[1:]))
def strictly_decreasing(L):
"""Returns TRUE if the values in the list are strictly decreasing
Always decreasing; never remaining constant or increasing or null.
@M-Bryant
M-Bryant / fieldExists.py
Last active April 4, 2022 07:48
arcpy: Check if field exists
import arcpy
def fieldExists(dataset: str, field_name: str) -> bool:
"""Return boolean indicating if field exists in the specified dataset."""
return field_name in [field.name for field in arcpy.ListFields(dataset)]
@M-Bryant
M-Bryant / sortValues.py
Last active August 1, 2016 07:20
arcpy: Sort a separated value into an order
def sortValues(the_value, delimiter=','):
"""Sort a separated value into an order
Useful to run on ArcGIS processes where there is no control on returned values.
For example SpatialJoin, JOIN_ONE_TO_ONE, with FieldMapping Merge Rule = Join
"""
if the_value == None:
return the_value
# Create a Python list by splitting the string on the delimeter
the_list = the_value.split(delimiter)
@M-Bryant
M-Bryant / query_domain.py
Created April 19, 2016 01:58
arcpy: Query domains
domains = arcpy.da.ListDomains(r'C:\temp\test.gdb')
for domain in domains:
print(u'\nDomain name: {0}'.format(domain.name))
if domain.domainType == 'CodedValue':
coded_values = domain.codedValues
for val, desc in coded_values.iteritems():
print(u'\t{0} : {1}'.format(val, desc))
elif domain.domainType == 'Range':
print(u'\tMin: {0}'.format(domain.range[0]))
@M-Bryant
M-Bryant / create_output_gdb.py
Created November 26, 2015 23:33
arcpy: create_output_gdb
import os
import arcpy
def create_output_gdb(parent_folder, gdb_name):
"""Creates a new gdb in the folder
"""
arcpy.AddMessage("Checking output workspace...")
gdb_path = os.path.join(parent_folder, gdb_name + ".gdb")
# Generate a unique name if the gdb already exists
if arcpy.Exists(gdb_path):
@M-Bryant
M-Bryant / get_workspace.py
Created August 19, 2015 01:09
arcpy: get workspace from feature class, without arpy
import os
def get_workspace(in_feature_class):
''' Get the workspace for a feature class'''
workspace = os.path.dirname(in_feature_class)
if [any(ext) for ext in ('.sde', '.gdb', '.mdb') if ext in os.path.splitext(workspace)]:
return workspace
else:
return os.path.dirname(workspace)
@M-Bryant
M-Bryant / arcpy: getUniqueFieldValuesFromTable.py
Last active August 29, 2015 14:17
Use SearchCursor to return a list of unique values in the specified field
def getUniqueFieldValuesFromTable(in_table, in_field_name):
'''
Use SearchCursor to return a list of unique values in the specified field
in_table: The feature class, layer, table, or table view <string>
in_field_name: Single field name <string>
returns: pythonList
'''
pySet = set()
with arcpy.da.SearchCursor(in_table, in_field_name) as cursor:
@M-Bryant
M-Bryant / Arcpy single quote input string
Created November 25, 2014 04:36
the current best practice for single quoting python strings
Expression = "'{0}'".format(arcpy.GetParameterAsText(3)) # single quoted string literal