Skip to content

Instantly share code, notes, and snippets.

View ryan-hill's full-sized avatar

Ryan Hill ryan-hill

View GitHub Profile
@ryan-hill
ryan-hill / setRasterNAto0.py
Created September 11, 2015 19:02
Code snippet to convert NA values within conterminous USA (NLCD data as mask) to 0s
path = 'xyz/'
mask_grid = path + 'nlcd2006.tif'
arcpy.env.mask = mask_grid
PCH = path + 'PCH.tif'
PCH = Con(IsNull(PCH),0,PCH)
PCH.save(path + 'PCH2.tif')
#predData is dataframe of predictor variable where 1st column is a unique site ID
vect = names(predData)[2:length(predData)]
combos = cbind(combn(vect, 2)[1,], combn(vect, 2)[2,])
#Useful for comparing pairwise combo models
combos = data.frame(combos); combos$AUC <- 0.0
names(combos) <- c('Pred1','Pred2','AUC')
@ryan-hill
ryan-hill / dbf2DF.py
Last active July 3, 2023 16:09
Import DBF file to Pandas data frame in Python
import pysal as ps
import pandas as pd
'''
Arguments
---------
dbfile : DBF file - Input to be imported
upper : Condition - If true, make column heads upper case
'''
def dbf2DF(dbfile, upper=True): #Reads in DBF files and returns Pandas DF
db = ps.open(dbfile) #Pysal to open DBF
@ryan-hill
ryan-hill / Lookup.R
Created October 1, 2015 19:10 — forked from mhweber/Lookup.R
This code takes a lookup table and applies it to a data frame, updating only values for records that occur in the lookup table using indexing and match
# Create data frame 1
x = c("ID1","ID2","ID3","ID4","ID5")
y = c("C1","C2","C3","C4","C5")
d1 = data.frame("SiteID" = x, "Value" = y)
d1
# Create lookup table
x = c("ID2","ID5")
y = c("C5","C2")
lookup = data.frame("SiteID" = x, "Value" = y)
lookup
@ryan-hill
ryan-hill / float2int.py
Created October 14, 2015 00:35 — forked from bryanluman/float2int.py
Converting a large DEM with rasterio
import numpy as np
import rasterio
"""
2014-02-13
Bryan Luman
Use it however you like at your own risk
Problem:
I have a huge DEM converted from LiDAR LAS points. I'd like to make it slightly
import numpy as np
import pandas as pd
#Read in .csv file
nlcd = pd.read_csv('NLCD2011_FINAL.csv')
#Select desired columns - selects 1st column and then 23rd column to end
nlcd = nlcd.iloc[:, np.r_[:1, 23:len(nlcd.columns)]]
#Strip out 'Ws' string from column names that contain it
newnames = [w.replace('Ws', '') for w in nlcd.columns]
#rename columns
nlcd.columns = newnames
#Code loops through StreamCat files on drive and combines into long table
#Also removes ancillary columns
combine_streamcat = function(x, wd){
hydro.rgns <- c("01","02","03S","03N","03W","04","05","06","07","08","09","10L","10U","11","12","13","14","15","16","17","18")
for(i in 1:length(hydro.rgns)){
print(hydro.rgns[i])
if(i == 1){
outDF = read.csv(paste0(wd, x, '_Region', hydro.rgns[i], '.csv'))
}else{
tmpDF = read.csv(paste0(wd, x, '_Region', hydro.rgns[i], '.csv'))
@ryan-hill
ryan-hill / walk_up.py
Created October 19, 2017 16:50 — forked from debboutr/walk_up.py
recursive function to walk up flow table. NOTE: this will not handle braided flowlines!
from StreamCat_functions import dbf2DF
pre = 'D:/NHDPlusV21/NHDPlusGL/NHDPlus04'
fline = dbf2DF('D%s/NHDSnapshot/Hydrography/NHDFlowline.dbf' % pre)
flow = dbf2DF('%s/NHDPlusAttributes/PlusFlow.dbf' pre)[['TOCOMID','FROMCOMID']]
def recurs(val, ups):
print val
ups = ups + flow.ix[flow.TOCOMID == val].FROMCOMID.tolist()
if 0 in ups:
@ryan-hill
ryan-hill / gages-near-portland.R
Last active May 6, 2018 18:02
Gist used to create hidden answers for challenge questions in Intro to GIS in R course (https://ryan-hill.github.io/sfs-r-gis-2018/)
#Read in gages data and convert to spatial points data frame
#Give it the **pts** CRS and reproject to **pts2** CRS
#Select out Portland and use `gDistance` from `rgeos` package with portand as x and gages as y in the function.
#Sum across TRUE/FALSE values in query. R will count TRUE == 1 and FALSE == 0.
library(sp); library(rgeos)
gages <- read.csv('./data/StreamGages.csv')
gages <- SpatialPointsDataFrame(gages[c('LON_SITE','LAT_SITE')], gages)
gages@proj4string <- pts@proj4string
@ryan-hill
ryan-hill / gages-near-portland2.R
Last active May 6, 2018 18:03
Gist used to create hidden answers for challenge questions in Intro to GIS in R course (https://ryan-hill.github.io/sfs-r-gis-2018/)
#Read in gages data and convert to spatial points data frame
#Give it the **pts** CRS and reproject to **pts2** CRS
#Select out Portland and use `gBuffer` from `rgeos` package with width = 50,000 meters.
#Use `over` function from `sp` package to identify overlapping points with 50 km buffer.
library(sp); library(rgeos)
gages <- read.csv('./data/StreamGages.csv')
gages <- SpatialPointsDataFrame(gages[c('LON_SITE','LAT_SITE')], gages)
gages@proj4string <- pts@proj4string