Last active
May 6, 2018 18:02
-
-
Save ryan-hill/db60260c510aa720bf1bfa13423f71d7 to your computer and use it in GitHub Desktop.
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/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
gages <- spTransform(gages, proj4string(pts2)) | |
portland <- pts2[pts2$cities == 'Portland', ] | |
m <- gDistance(portland, gages, byid=T) | |
sum(m < 50000) | |
#Bonus - How to get the station names within 50km | |
gages$STATION_NM[which(m < 50000)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment