Skip to content

Commit

Permalink
Introduced IsInArea
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRoth committed May 23, 2017
1 parent fd39e47 commit 82e8a40
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Package: knmiR
Type: Package
Title: Import KNMI Data
Version: 0.1.5.6
Version: 0.1.5.7
Authors@R: person("Martin", "Roth", email = "[email protected]",
role = c("aut", "cre"))
Date: 2017-03-11
Date: 2017-05-23
Description: Import KNMI data into R.
License: MIT + file LICENSE
LazyData: TRUE
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(EOBS)
export(EOBSLocal)
export(Earthquakes)
export(HomogenPrecip)
export(IsInArea)
export(KIS)
export(KnmiData)
export(License)
Expand Down
30 changes: 25 additions & 5 deletions R/ImportData.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,41 @@ EarthquakesDownload <- function(type, area, period, call) {
KnmiData(tmp, call, "Earthquakes")
}

#' Select earthquake sover area
#' Select earthquakes over area
#'
#' @param quakes data.table with earthquakes
#' @param area SpatialPolygons
#' @param area SpatialPolygons or SpatialPolygonsDataFrame
#' @export
ClipQuakes <- function(quakes, area) {
lat <- lon <- NULL
points <- as.data.frame(quakes[, list(lon, lat)])
points <- sp::SpatialPoints(points, CRS("+proj=longlat +datum=WGS84"))
points <- sp::spTransform(points, area@proj4string)
index <- which(!is.na(sp::over(points, area)))
index <- IsInArea(points, area)
return(quakes[index, ])
}


#' Is point in area?
#'
#' @param points SpatialPoints
#' @param area SpatialPolygons of SpatialPolygonsDataFrame
#'
#' @return logical indicating whether point is in area
#' @export
IsInArea <- function(points, area) {
points <- sp::spTransform(points, area@proj4string)
if (class(area) == "SpatialPolygons") {
index <- !is.na(sp::over(points, area))
} else if (class(area) == "SpatialPolygonsDataFrame") {
tmp <- sp::over(points, area)
index <- as.vector(!is.na(tmp[, 1, drop = FALSE]))
} else {
stop("Area should be of class `SpatialPolygons' or `SpatialPolygonsDataFrame'")
}
if (class(index) != "logical" & length(points) != length(index)) {
stop("index should be a logical of the same length as points")
}
index
}



6 changes: 3 additions & 3 deletions man/ClipQuakes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/IsInArea.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 82e8a40

Please sign in to comment.