Skip to content

Instantly share code, notes, and snippets.

View morehavoc's full-sized avatar

Christopher Moravec morehavoc

View GitHub Profile
@morehavoc
morehavoc / export_mxd.py
Last active August 29, 2015 14:04
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.
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***")
@morehavoc
morehavoc / CalculateUniqueID.py
Created July 16, 2014 05:21
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
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 / ExportMXD.pyt
Last active August 29, 2015 14:04
An extension of export_mxd.py that provides an ESRI toolbox that does the same thing as the export_mxd.py script.
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 / walk_data.py
Created July 25, 2014 20:28
Quick Method to list all of the feature classes, even in folders, in any geodatabase and in feature datasets
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 / gist:a8212a5ba4d9e12a86d3
Created July 31, 2014 20:51
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
@morehavoc
morehavoc / CollectInfoToCSV.pyt
Last active August 29, 2015 14:04
Collect datasets and information about them from a folder to a CSV!
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 / triplequotes.py
Last active August 29, 2015 14:05
Triple Quotes in Calculate Field Tool
arcpy.CalculateField_management(TableName, NewColumn, """!OldColumn!.replace("A", "B")""", "PYTHON_9.3", "#")
@morehavoc
morehavoc / remove_fields.py
Created December 12, 2014 16:14
Remove those pesky Shape_Length__ and Shape_Area__fields
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 / field_copy.py
Created December 24, 2014 22:50
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!
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 / main.py
Last active February 10, 2023 04:24
Example class for running a stepper motor in micopython
import machine
import time
DELAY = 0.01
STEPS = 27
class Motor:
def __init__(self, CA1, CA2, CB1, CB2, halfSteps=False):
self._coils = (CA1, CA2, CB1, CB2)
self._pattern = 0