Skip to content

Instantly share code, notes, and snippets.

@pyRobShrk
pyRobShrk / CalcAreaLength.bas
Created May 4, 2018 15:54
This module adds text to a free-form Excel line or polygon, which shows the length or area of that drawing in inches
Sub CalcLength()
'Subroutine calculates the distance of straight line or "scribble" line
'It has not been tested in any other Office software, but it should work with minor modification
'By Rob Sherrick, 4/12/2018
Dim dpi As Integer
dpi = Application.InchesToPoints(1)
Length = 0
A = 1
On Error Resume Next
@pyRobShrk
pyRobShrk / compressSHP.py
Last active March 21, 2018 17:05
ArcMap script to copy a shapefile into a new smaller shapefile (in memory)
# Compress Shapefile by reducing Text Field lengths, dropping M and Z
# Unfortunately .management.AlterField only works for unpopulated text fields
# Process: Copy Table to NumPy array, Determine max length of text fields
# Create blank feature with previous as template, alter text field length
# Copy features to new feature class in memory
import arcpy.management as mg
import numpy as np
def compressSHP(inSHP, outSHP):
fields = [f.name for f in arcpy.ListFields(inSHP) if not f.name == 'Shape']
@pyRobShrk
pyRobShrk / Write Geojson To Feature Class.py
Created March 14, 2018 15:59 — forked from d-wasserman/Write Geojson To Feature Class.py
This gist is intended to share a sample worker function to write Geojson to an ArcGIS Feature Class using Arcpy.
def write_geojson_to_records(geojson):
"""Will write geojson to a list of dictionaries with geomtry keys holding coordinates and storing the properties
to dictionaries."""
gjson_data = json.loads(geojson, encoding='utf-8')
records = []
arc_print("Geojson being read by row...")
for feature in gjson_data["features"]:
try:
row = {}
row["geometry"] = feature["geometry"]
@pyRobShrk
pyRobShrk / googlePolyline.py
Last active February 28, 2018 00:42 — forked from signed0/gist:2031157
Google Polyline encoder & decoder
'''Provides utility functions for encoding and decoding linestrings using the
Google encoded polyline algorithm.
'''
def encode_coords(coords):
'''Encodes a polyline using Google's polyline algorithm
See http://code.google.com/apis/maps/documentation/polylinealgorithm.html
for more information.
@pyRobShrk
pyRobShrk / writeFieldAliases.py
Last active March 21, 2018 15:36
arcpy script to write field aliases based on an Excel lookup table using pandas
# start with an excel table with two columns
# first row should be headers "FIELD" and "ALIAS"
# copy both columns to clipboard including headers
# use copyFieldsToClipboard to get the list
# optionally add a column called "RENAME" and run renameFields
import pandas as pd
def copyFieldsToClipboard(lyr):
fieldList = [f.name for f in arcpy.ListFields(lyr)]
@pyRobShrk
pyRobShrk / mlpIDW.py
Last active May 2, 2018 23:20
multiple linearly projected inverse distance weighting
import numpy as np
import pandas as pd
#~~~~USER INPUT PARAMETERS~~~~
maxNearPoints = 20
anisotropyFactor = 15
searchTransv = 200 #meters
searchLongitud = searchTransv*anisotropyFactor #meters
gridSize = 3 #meters
invDistPower = 1
@pyRobShrk
pyRobShrk / invdisttree.py
Created February 10, 2018 18:31
Inverse Distance & K-D Tree Interpolation
""" invdisttree.py: inverse-distance-weighted interpolation using KDTree
fast, solid, local
https://stackoverflow.com/a/3119544
"""
from __future__ import division
import numpy as np
from scipy.spatial import cKDTree as KDTree
# http://docs.scipy.org/doc/scipy/reference/spatial.html
__date__ = "2010-11-09 Nov" # weights, doc
@pyRobShrk
pyRobShrk / multipleLinearAnalysis.bas
Created December 6, 2017 18:19
PRESS Statistic, Hat Matrix, and Leverage Points for Multiple Linear Regressions in Excel
' Multiple Linear Regressions in Excel
' This group of functions uses the Hat Matrix and
' Leverages to calculate the PRESS statistic
' A column of ones must precede your X columns (exR)
' https://en.wikipedia.org/wiki/PRESS_statistic
' https://en.wikipedia.org/wiki/Projection_matrix
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Hat(exR As Range) As Variant
'Returns a symmetrical array of dimensions n by n
@pyRobShrk
pyRobShrk / RivBathyInterp.py
Created December 4, 2017 21:00
Anisotropic river bathymetry interpolation
# This script interpolates river bathymetry in an anisotropic way.
# Following the stream centerline, the interpolation is weighted longitudinally.
# This is accomplished by projecting the sinuous river to a straight line (s, n) coordinates,
# where s is distance downstream and n is distance left/right perpindicular to centerline.
# In (s, n) coordinates, the river is then compressed longitudinally, and then interpolated normally.
# The compression gives interpolation weight to upstream/downstream points instead of going up the bank.
# For best results the centerline should be smoothed, otherwise the inside bend has overlapping points.
# Input Excel file has three sheets, with simple X, Y, Z columns. X, Y only for centerline.
import pandas as pd
@pyRobShrk
pyRobShrk / USGS10mElev.py
Last active July 1, 2019 21:27
Python function to query USGS 10 meter 3DEP elevation for a given point (lat, long)
from http import client
def USGS10mElev(lat,lon):
usgs = client.HTTPConnection('nationalmap.gov')
usgs.request('GET','/epqs/pqs.php?x=%.6f&y=%.6f&units=FEET&output=xml'% (lon, lat))
result = usgs.getresponse()
if result.status == 200:
xml = result.read()
return float(xml[xml.find(b'<Elevation>')+11:xml.find(b'</Elevation>')-1])
else: return request