Skip to content

Commit

Permalink
started fxn to visualize wkt area, not sure if best fit yet #34
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Mar 10, 2014
1 parent ac97ec3 commit 450bc16
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(spocc_rcharts_togeojson)
export(spocc_stylegeojson)
export(spocc_togeojson)
export(wkt2bbox)
export(wkt_vis)
import(AntWeb)
import(XML)
import(assertthat)
Expand Down
35 changes: 35 additions & 0 deletions R/wkt_vis.r
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")
}
27 changes: 27 additions & 0 deletions man/wkt_vis.Rd
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)
}
}

5 comments on commit 450bc16

@karthik
Copy link
Member

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.

@sckott
Copy link
Contributor Author

@sckott sckott commented on 450bc16 Mar 10, 2014

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

@karthik
Copy link
Member

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.

@sckott
Copy link
Contributor Author

@sckott sckott commented on 450bc16 Mar 10, 2014

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

@karthik
Copy link
Member

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.

Please # to comment.