Skip to content

Instantly share code, notes, and snippets.

@mattjbayly
Created January 30, 2018 07:28
Show Gist options
  • Save mattjbayly/81ee234f7a88e180d8d3b6b450f8f009 to your computer and use it in GitHub Desktop.
Save mattjbayly/81ee234f7a88e180d8d3b6b450f8f009 to your computer and use it in GitHub Desktop.
R scale bar for sf plots
ScaleBarR <- function(
start_x=NA,
start_y=NA,
dist=NA,
source_prj=NA,
scale_prj=32610
){
dd <- data.frame(x=start_x, y=start_y, id=1)
coordinates(dd) <- ~x+y
dd <- st_as_sf(dd)
st_crs(dd) <- source_prj
dd <- st_transform(dd, scale_prj)
coords <- st_coordinates(dd)
dd2 <- data.frame(x=start_x+dist, y=start_y, id=1)
coordinates(dd2) <- ~x+y
dd2 <- st_as_sf(dd2)
st_crs(dd2) <- source_prj
dd2.s <- st_transform(dd2, scale_prj)
coords2 <- st_coordinates(dd2.s)
real_dist <- st_distance(dd2.s, dd)
print("Real Distance (m):")
print(real_dist[1])
val <- as.numeric(real_dist[1])
return(val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment