Skip to content

Instantly share code, notes, and snippets.

View M-Bryant's full-sized avatar

Mark Bryant M-Bryant

  • AECOM
  • Auckland, New Zealand
  • 00:03 (UTC +13:00)
View GitHub Profile
@M-Bryant
M-Bryant / Template
Last active May 17, 2019 15:25
arcpy: Sample Template
"""----------------------------------------------------------------------------
Name: $[ActiveDoc-Name]
Description:
Requirements: ArcGIS Desktop Standard (10.2)
Python Version: 2.7
Inputs:
Outputs:
@M-Bryant
M-Bryant / hasSelection
Created November 7, 2013 03:26
arcpy: Check whether specified layer has a selection
def hasSelection(lyr):
''' Check whether specified layer has a selection. '''
import arcpy
desc = arcpy.Describe(lyr)
if len(desc.FIDSet) == 0:
# Layer has no selection
return False
else:
# Layer has a selection
return True
@M-Bryant
M-Bryant / dummyNAN.py
Created November 24, 2013 20:47
arcpy: Set/Check Not a number (NAN)
def setNAN():
minx = float('nan')
def checkNAN():
import math
if math.isnan(minx):
return 1
@M-Bryant
M-Bryant / rotate point.py
Last active August 29, 2015 13:56
Rotate an xy cooordinate about a specified origin
def RotateXY(x,y,xc=0,yc=0,angle=0,units='DEGREES'):
"""Rotate an xy cooordinate about a specified origin
returning a new xy cooordinate
x,y xy coordinates
xc,yc center of rotation
angle angle
units "DEGREES" (default) or "RADIANS"
"""
import math
@M-Bryant
M-Bryant / ArcMap multiple hyperlink via script
Created August 22, 2014 06:14
Open multiple relative hyperlinks in ArcGIS for Desktop (python)
@M-Bryant
M-Bryant / isCADLayer.py
Last active August 29, 2015 14:08
arcpy: checks if ArcMap layer is a CAD layer
def isCADLayer(layer):
''' Returns True if a layer is a CAD layer.
'''
desc = arcpy.Describe(layer)
if hasattr(desc, "dataType") and (desc.dataType == "FeatureLayer"):
describeTable = arcpy.Describe(desc.catalogPath)
if describeTable.dataElementType == "DEFeatureClass":
describePath = arcpy.Describe(describeTable.path)
if hasattr(describePath, "dataType")
and (describePath.dataType == "CadDrawingDataset"):
@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
@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 / 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 / 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):