Skip to content

Instantly share code, notes, and snippets.

View mhweber's full-sized avatar

Marc Weber mhweber

View GitHub Profile
@mhweber
mhweber / LineLengthsPolys.R
Last active August 29, 2015 14:07
This piece gets some summary statistics on spatial line data frame lengths within spatial polygon data frames in R
library(rgeos);library( rgdal)
polys <- readOGR('.','polys')
lines <- readOGR('.','lines')
if (proj4string(polys) == proj4string(lines){
plot(polys); plot(lines, add=T, color='Blue')
lines$LENGTHKM <- SpatialLinesLengths(lines)*0.001 # assumes lines are in projection using meters
line.sum <- over(polys, lines[6], fn=sum)
line.mean <- over(polys, lines[6], fn=mean)
line.min <- over(polys, lines[6], fn=min)
line.max <- over(polys, lines[6], fn=max)
@mhweber
mhweber / Lookup.R
Last active October 1, 2015 19:10
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
@mhweber
mhweber / Recode_PatMatch.R
Created September 16, 2015 15:10
A reminder snippet on how to recode a data frame variable based on pattern matching another variable in the same data frame
x = c("AA","BB","A:B", "CC","C:A")
df = data.frame("ID" = x)
df
df$colons[grep(":", df$ID)] ='yes'
df$colons[is.na(df$colons)] ='no'
df
@mhweber
mhweber / dbf2DF.py
Last active October 15, 2019 21:20 — forked from ryan-hill/dbf2DF.py
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
@mhweber
mhweber / SpatialLinesEndPoints.R
Created February 1, 2016 21:23
Function to build a spatial points data frame of end nodes using a spatial lines data frame - function is a modification of SpatialLinesMidPoints in the R maptools package.
SpatialLinesEndPoints = function(sldf){
Lns <- slot(sldf, "lines")
hash_lns <- sapply(Lns, function(x) length(slot(x, "Lines")))
N <- sum(hash_lns)
endpoints <- matrix(NA, ncol = 2, nrow = N)
Ind <- integer(length = N)
ii <- 1
for (i in 1:length(Lns)) {
Lnsi <- slot(Lns[[i]], "Lines")
for (j in 1:hash_lns[i]) {
@mhweber
mhweber / RasterizeShapefile.R
Created February 5, 2016 16:06
Simple method to rasterize a shapefile using the raster package in R
library(raster)
## grab US counties shapefile in Oregon
shp <- shapefile("C:/Users/mweber/Temp/OR_counties.shp")
## no dropping of projection metadata
r <- raster(shp,nrows=100,ncols=150)
r <- rasterize(shp,r,fun="first")
plot(shp,axes=TRUE,border="grey")
@mhweber
mhweber / NotebookExample
Last active March 31, 2016 20:51
Jupyter notebook for GIS Workgroup
This file has been truncated, but you can view the full file.
{
"cells": [
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
@mhweber
mhweber / Check Polygon Intersections
Created May 4, 2016 22:56
A function using shapely geometries to check for any intersecting polygons
import geopandas as gpd
from itertools import combinations
Feat1 = gpd.GeoDataFrame.from_file('Feat1.shp')
Feat2 = gpd.GeoDataFrame.from_file('Feat2.shp')
Feat3= gpd.GeoDataFrame.from_file('Feat3.shp')
# gather geometries in iterable a list
shapes = [Feat1, Feat2, Feat3]
# or to show catching overlap try
@mhweber
mhweber / TestGeorasters
Created May 6, 2016 21:06
Testing out the georasters package
import georasters as gr
import matplotlib.pyplot as plt
r1 = gr.from_file('wshed1')
NDV, xsize, ysize, GeoT, Projection, DataType = gr.get_geo_info('wshed1')
# change the data type from uint8 to uint32
r1.datatype=4
r1 = r1*2546909
r2 = gr.from_file('wshed2')
@mhweber
mhweber / geopandas_clip.py
Created June 17, 2016 17:16
A quick and dirty poor man's clip using geopandas. First does a union between a plot shapefile and an overlapping features shapefilie, then subsets where attributes of the features shapefile are not null to get the features clipped to boundaries of plots.
Plots = gpd.GeoDataFrame.from_file('Plots.shp')
Plots.plot()
Features = gpd.GeoDataFrame.from_file('Features.shp')
Features.plot()
# union features
Both = overlay(Plots, Features, how="union")
Both.head()
# 'clip' by getting rid of union features where attributes for plot are null in the union
Clips = Both[pd.notnull(Both['ORIG_FID'])]
Clips.plot()