Skip to content

Instantly share code, notes, and snippets.

View danmaps's full-sized avatar
🏠
Working from home

Danny McVey danmaps

🏠
Working from home
  • SCE
  • Redlands, CA
View GitHub Profile
@danmaps
danmaps / lookup_floc_by_objectid.py
Created December 14, 2023 20:29
created as part of a proximity analysis to make use of the output of the "generate near table tool". only after this was complete did I realize that a vlookup would do this pretty easily... i think.
projected_fc = "joined"
new_field = "nearby_FLOC"
floc_id_field = "FLOC_ID"
# Add a new field
arcpy.management.AddField(projected_fc, new_field, "TEXT")
# Step 1: Create a dictionary to map OBJECTID to FLOC_ID
floc_id_dict = {}
with arcpy.da.SearchCursor(projected_fc, ["OBJECTID", floc_id_field]) as search_cursor:
@danmaps
danmaps / validate_and_calculate_field.py
Created August 21, 2023 20:01
Validate and calculate fields
def validate_and_calculate_field(layer, field, expression):
# Validate field exists
fields = [f.name for f in arcpy.ListFields(layer)]
if field not in fields:
raise ValueError(f"Field {field} does not exist in layer")
# Validate expression fields exist
def get_fields_from_expression(expression):
matches = re.findall(r"!\w+!", expression)
@danmaps
danmaps / string_calendar.py
Last active May 15, 2022 16:57
calendar as string like btwb
from calendar import monthrange, day_name
from datetime import datetime, date
def is_saturday(d):
return day_name[d.weekday()] == "Saturday"
def is_current_month(d):
return (d.year, d.month) == (datetime.now().year, datetime.now().month)
import arcpy
fc1 = 'buildings'
fc2 = 'buildings_1'
fields1 = ['osm_id', 'type']
fields2 = ['osm_id', 'test']
libraries = []
# loop through each row in fc1 and add the osm_id to the libraries list
with arcpy.da.SearchCursor(fc1, fields1) as search_cursor:
@danmaps
danmaps / move_tifs.py
Last active July 16, 2020 19:30
based on a discussion at the 2020 virtual UC
layer = 'grid' # if running this script outside of ArcGIS, this should be a full path to a feature class, not a layer name
fields = ['ID'] # this is field which matches the .tif filename
tif_folder = r'c:\my_tifs'
out_folder = r'c:\output'
with arcpy.da.SearchCursor(layer, fields) as cursor:
for r in cursor: # for each row in the layer:
print(r[0]) # print the ID
tif_file = os.path.join(tif_folder,r[0]+'.tif') # get the path to the matching .tif file (c:\my_tifs\01234.tif)
@danmaps
danmaps / one_second_circle.py
Created March 11, 2020 20:22
blink leds on circuit python playground
# blinky vs blinky_gap
# inspired by https://learn.adafruit.com/sipping-power-with-neopixels?view=all
import time
from adafruit_circuitplayground import cp
cp.pixels.brightness = .004
def blinky(color,t=1):
t = t/10
@danmaps
danmaps / cowzen.py
Created August 30, 2019 22:44 — forked from AgustinLado/cowzen.py
Get a random line from the Zen of Python without a print (preferably for a colorful cow to chant!)
# Get a random line from the Zen of Python without a print (preferably for a colorful cow to chant!)
# Replace stdout by an anonymous class that returns nothing on write()
import sys
stdout = sys.stdout
sys.stdout = type('BlackHole', (), {'write': (lambda self, string: '')})()
# This import's output has now been supressed
import this
@danmaps
danmaps / regex_sample.py
Created November 27, 2017 19:25
python regular expressions sample for replacing strings using wildcards
import re
strings = ['abc@1@xyz','abc@2@xyz','abc@789@xyz','abc@defghijklmno@xyz']
for text in strings:
regextest = re.sub("@.*@",str(strings.index(text)),text) # .* means zero or more of any character
print regextest
'''expected output
abc0xyz
abc1xyz
@danmaps
danmaps / remove-fedlands-from-mxd-batch.py
Last active October 31, 2017 20:52
Takes an input directory and walks through it, finding all MXDs and searching for fedlands layers
#-------------------------------------------------------------------------------
# Title: Remove all fedlands layers
# Purpose: Remove all fedlands from MXDs. This script takes an input
# directory and walks through it, finding all MXDs and searching
# for fedlands layers
#
# Author: Esri Support
# Based on: https://github.com/Esri/developer-support/tree/master/python/arcpy-python/remove-all-basemaps-batch
#
# Created: 10/31/2017
import glob, os
def batchfile(script_folder, batchfile_folder, pyexe):
os.chdir(script_folder)
for file in glob.glob("*.py"):
script = os.path.splitext(file)[0]
batch = batchfile_folder+script+".bat"
with open(batch, 'w+') as f:
print batch
f.write("echo executing with "+pyexe+"\n")