Created
August 20, 2023 22:52
-
-
Save louismeunier/4897b653f2c40411d25ab6082a75df35 to your computer and use it in GitHub Desktop.
find furthest point from wca competitions in continental us
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
| # mostly borrowed from https://stackoverflow.com/questions/38253715/r-find-point-farthest-from-set-of-points-on-rasterized-usa-map | |
| library("sf") | |
| library("rgdal") | |
| library("raster") | |
| library("RMySQL") | |
| # competition data from sql | |
| mysqlconnection = dbConnect(RMySQL::MySQL(), | |
| dbname='wca_dev', | |
| host='localhost', | |
| port=3306, | |
| user='root', | |
| password='password') | |
| # change query to use different date, etc | |
| all_comps = dbGetQuery(mysqlconnection, "select id, latitude/1000000 as lat, longitude/1000000 as lon from competitions where start_date < 20230819") | |
| # shp file | |
| us_shp <- read_sf("/users/louismeunier/downloads/cb_2018_us_state_20m/cb_2018_us_state_20m.shp") | |
| # only want cont. us | |
| continental <- us_shp[!us_shp$NAME %in% c("Alaska", "Hawaii", "Puerto Rico"), ] | |
| # rasterize | |
| r <- raster(ncol=100, nrow=100) | |
| extent(r) <- extent(continental) | |
| rr <- rasterize(continental, r) | |
| coords <- cbind(all_comps$lon, all_comps$lat) | |
| D <- distanceFromPoints(object=rr, xy=coords) | |
| # ignore points outside cont. us | |
| D[which(is.na(rr[]))] <- NA | |
| plot(D, xaxt='n', yaxt='n') | |
| points(coords, pch=19, cex=0.25) | |
| lines(continental) | |
| # get max point | |
| D.dataframe <- data.frame(rasterToPoints(D)) | |
| D.dataframe.sorted <- D.dataframe[order(D.dataframe$layer),] | |
| maxpoint <- D.dataframe[D.dataframe$layer ==max(D.dataframe$layer),] | |
| points(maxpoint$x, maxpoint$y, pch=19, cex=1, col='red') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment