-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started fxn to visualize wkt area, not sure if best fit yet #34
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#' Visualize well-known text area's on a map. | ||
#' | ||
#' This can be helpful in visualizing the area in which you are searching for | ||
#' occurrences with the \code{occ} function. | ||
#' | ||
#' @import ggmap ggplot2 assertthat rgeos | ||
#' @param x Input well-known text area (character) | ||
#' @param zoom Zoom level, defaults to 6 (numeric) | ||
#' @param maptype Map type, default is terrain (character) | ||
#' @export | ||
#' @examples \dontrun{ | ||
#' poly <- 'POLYGON((-111.06 38.84, -110.80 39.37, -110.20 39.17, -110.20 38.90, -110.63 38.67, -111.06 38.84))' | ||
#' wkt_vis(poly) | ||
#' | ||
#' poly2 <- 'POLYGON((-125 38.4,-125 40.9,-121.8 40.9,-121.8 38.4,-125 38.4))' | ||
#' wkt_vis(poly2) | ||
#' } | ||
|
||
wkt_vis <- function(x, zoom = 6, maptype = "terrain") | ||
{ | ||
assert_that(!is.null(x)) | ||
assert_that(is.character(x)) | ||
|
||
poly_wkt <- readWKT(x) | ||
df <- fortify(poly_wkt) | ||
center_lat <- min(df$lat) + (max(df$lat) - min(df$lat))/2 | ||
center_long <- min(df$long) + (max(df$long) - min(df$long))/2 | ||
map_center <- c(lon = center_long, lat = center_lat) | ||
species_map <- get_map(location = map_center, zoom = zoom, maptype = maptype) | ||
ggmap(species_map) + | ||
geom_path(data = df, aes(x = long, y = lat, group=group, size=2)) + | ||
theme(legend.position="") + | ||
xlab("Longitude") + | ||
ylab("Latitude") | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
\name{wkt_vis} | ||
\alias{wkt_vis} | ||
\title{Visualize well-known text area's on a map.} | ||
\usage{ | ||
wkt_vis(x, zoom = 6, maptype = "terrain") | ||
} | ||
\arguments{ | ||
\item{x}{Input well-known text area (character)} | ||
\item{zoom}{Zoom level, defaults to 6 (numeric)} | ||
\item{maptype}{Map type, default is terrain (character)} | ||
} | ||
\description{ | ||
This can be helpful in visualizing the area in which you | ||
are searching for occurrences with the \code{occ} function. | ||
} | ||
\examples{ | ||
\dontrun{ | ||
poly <- 'POLYGON((-111.06 38.84, -110.80 39.37, -110.20 39.17, -110.20 38.90, -110.63 38.67, -111.06 38.84))' | ||
wkt_vis(poly) | ||
poly2 <- 'POLYGON((-125 38.4,-125 40.9,-121.8 40.9,-121.8 38.4,-125 38.4))' | ||
wkt_vis(poly2) | ||
} | ||
} | ||
450bc16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this function. We could easily do this for a bounding box too. I've had to do something similar for both AntWeb and ecoengine in the past few days just to make sure that it works correctly. So set bbox, then generate a simple query, visualize, make sure all points are ~ where they should be.
Some thoughts: That quick leafletJS map seems significantly faster than a
ggmap
.Re: where this would fit, I'd say in that
spocc_toolkit.R
where we have some general functions, like where that lat/long check might go.450bc16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, it would be better to have an interactive map than a static one. Will try to do this with leaflet instead. We already have functions to go from bounding box to wkt and vice versa, so that's good
450bc16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Joe Cheng told me that we can go the other way with Shiny. Like just draw a box on a map, then return that bbox (or wkt) back to R. Haven't had time to explore that further.
450bc16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I think it would be great to have that. He has interactivity like that in his shiny app here http://glimmer.rstudio.com/jcheng/leaflet-demo/ Putting a marker on a click event, not drawing a polygon, but probably similar bits of code needed
450bc16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, we discussed this after his superzip presentation (based on the leaflet demo). It could be really useful not just for maps but across the board.