Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / geoid.py
Last active November 14, 2022 22:40
Python classes to calculate geoid height in meters NAVD 88 (Geoid 12b) and EGM 96
import numpy as np
from scipy.interpolate import RectBivariateSpline as Spline
import pygeodesy as geo
from pygeodesy.ellipsoidalVincenty import LatLon
class Geoid12B(): #NAD 83 Ellipsoid
# https://www.ngs.noaa.gov/GEOID/GEOID12B/GEOID12B_CONUS.shtml
# Download a Geoid Grid in little endian binary format ('g2012bu5.bin')
def __init__(self, leBinFile):
glamn, glomn, dla, dlo = np.fromfile(leBinFile,'<f8',4)
@pyRobShrk
pyRobShrk / GIStable2Clipboard.py
Created August 6, 2018 21:19
ArcPy function to copy the attribute table to clipboard for pasting into Excel
import pandas as pd
def GIStable2Clipboard(inTable, exportFields="all"):
desc = arcpy.Describe(inTable)
if exportFields =="all":
exportFields = [f.name for f in arcpy.ListFields(inTable) if not f.name == desc.shapeFieldName]
aliases = [f.aliasName for f in arcpy.ListFields(inTable) if not f.name == desc.shapeFieldName]
pd.DataFrame.from_records(arcpy.da.TableToNumPyArray(inTable,exportFields),
index=desc.OIDFieldName, columns=aliases).to_clipboard()
@pyRobShrk
pyRobShrk / encodeLongLatCoords.py
Last active January 5, 2022 17:57
Convert lat/long coordinates into an ascii string. Useful for database storage outside of geodata formats.
# Objective: A system capable of encoding geographic points, and polylines in ASCII characters
# Obviously Google and geohash have similar algorithms, but with limited precision
#
# A 24-bit integer can store binary integer values up to 16,777,215
# Using PEM format, 24 bits can be represented as 4 base-64 characters with the alphabet:
# ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
# A polyline can be represented by a string of characters,
# where each 8 characters is a longitude/latitude location
# For maximum flexibility, a polyline or point string is prefixed with three characters
# The first two characters specifies the origin - the lower left corner of a MGRS GZD