Skip to content

Instantly share code, notes, and snippets.

Christopher Moravec morehavoc

View GitHub Profile
@morehavoc
morehavoc / field_copy.py
Created Dec 24, 2014
That Pesky CreateFeatureClass tool strikes again, this code will copy fields from one feature class to another so you don't need to use a template!
View field_copy.py
def _add_field(input_table, field):
import arcpy
if getattr(field, 'isNullable', True):
nullable = 'NULLABLE'
else:
nullable = 'NON_NULLABLE'
# there is a bug in 10.2.1 and lower (sum is 13) that prevents us from adding non_nullable fields, so we have
# to protect against that bug.
if nullable == 'NON_NULLABLE' and sum([int(i) for i in GetArcpyVersion().split('.') ]) <= 13:
@morehavoc
morehavoc / remove_fields.py
Created Dec 12, 2014
Remove those pesky Shape_Length__ and Shape_Area__fields
View remove_fields.py
print "Importing arcpy"
import arcpy
from os.path import join
workspace = r"path to .sde connection here"
fields_to_remove = ["Shape_STLength__", "Shape_STArea__"]
arcpy.env.workspace = workspace
print "Listing Feature Classes"
layers= arcpy.ListFeatureClasses()
for layer in layers:
print "Processing {}".format(layer)
@morehavoc
morehavoc / triplequotes.py
Last active Aug 29, 2015
Triple Quotes in Calculate Field Tool
View triplequotes.py
arcpy.CalculateField_management(TableName, NewColumn, """!OldColumn!.replace("A", "B")""", "PYTHON_9.3", "#")
@morehavoc
morehavoc / CollectInfoToCSV.pyt
Last active Aug 29, 2015
Collect datasets and information about them from a folder to a CSV!
View CollectInfoToCSV.pyt
import arcpy
from os.path import abspath, join, basename, dirname
import csv
class Toolbox(object):
def __init__(self):
self.label = "Collect Info to CSV Toolbox"
self.alias = "CollectInfoToCSV"
# List of tool classes associated with this toolbox
View gist:a8212a5ba4d9e12a86d3
import arcpy
from os.path import abspath, join, basename, dirname
import csv
class Toolbox(object):
def __init__(self):
self.label = "Collect Info to CSV Toolbox"
self.alias = "CollectInfoToCSV"
# List of tool classes associated with this toolbox
@morehavoc
morehavoc / walk_data.py
Created Jul 25, 2014
Quick Method to list all of the feature classes, even in folders, in any geodatabase and in feature datasets
View walk_data.py
import arcpy
from os.path import abspath, join
directory = r"C:\Users\cmoravec\Documents\ArcGIS"
def walk_directory(directory):
output_list = list()
for base_dir, child_dirs, filenames in arcpy.da.Walk(directory, datatype='FeatureClass'):
for filename in filenames:
data = join(abspath(base_dir), filename)
output_list.append(data)
@morehavoc
morehavoc / ExportMXD.pyt
Last active Aug 29, 2015
An extension of export_mxd.py that provides an ESRI toolbox that does the same thing as the export_mxd.py script.
View ExportMXD.pyt
import arcpy
import os.path
class Toolbox(object):
def __init__(self):
self.label = "Simple Export Toolbox"
self.alias = "ExportMXD"
# List of tool classes associated with this toolbox
self.tools = [ExportMXD]
@morehavoc
morehavoc / CalculateUniqueID.py
Created Jul 16, 2014
A quick script to create a unique ID based on the value of a different column. For example, the column value might have 3 rows with value A and 2 rows with B, the script would calc a new column that would make A_001, A_002, A_003, B_001, B_002
View CalculateUniqueID.py
global id_value_dict
id_value_dict = dict()
def calc_new_id(base_id_value):
global id_value_dict
if base_id_value in id_value_dict:
id_value_dict[base_id_value] += 1
else:
id_value_dict[base_id_value] = 1
return str(base_id_value) + "_" + str(id_value_dict[base_id_value]).zfill(3)
@morehavoc
morehavoc / export_mxd.py
Last active Aug 29, 2015
Quick script to convert all the MXD's in a folder into a particular export type, like a PDF, or JPEG. Automatically picks up all of the files within any folder below the folder specified.
View export_mxd.py
import arcpy
import os.path
base_directory = r"C:\temp"
export_type = "PDF"
## Ensure that the export_type exists
if not hasattr(arcpy.mapping, "ExportTo" + export_type.upper()):
print ("*** You must specify an export type of one of: ***")
print ("*** AI, BMP, EMF, EPS, GIF, JPEG, PDF, PNG, SVG, TIFF***")
You can’t perform that action at this time.