-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoSpatialPoints.R
32 lines (31 loc) · 1.26 KB
/
toSpatialPoints.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#'
#' @name toSpatialPoints
#' @title toSpatialPoints
#' @description Convert a data.frame with lonlat columns to a SpatialPoints object
#'
#' @param x an object of class "data.frame"
#' @param lonlat a vector of length 2 given the lon/lat column names e.g. c("Lon","Lat")
#' @param verbose TRUE (by default) to display logs
#' @return an object of class "SpatialPoints"
#'
#' @author Emmanuel Blondel \email{emmanuel.blondel1@@gmail.com}
#'
toSpatialPoints <- function(x, lonlat, verbose = TRUE){
if(missing(lonlat) | is.null(lonlat)) stop("Lon/Lat columns are missing")
if(length(lonlat) != 2) stop("lonlat should be a vector of length 2")
if(class(lonlat) != "character") stop("lonlat should be of class 'character'")
#geo-reference the data
if(verbose) message(paste0("Input data has ", nrow(x), " records"))
qFilter <- is.na(x[,lonlat[1]]) | x[,lonlat[1]]=="" | is.na(x[,lonlat[2]]) | x[,lonlat[2]]==""
toremove <- x[qFilter,]
if(nrow(toremove) > 0){
if(verbose) message(paste0("Removing ", nrow(toremove), " records with empty coordinates"))
x <- x[!qFilter,]
}
x[,lonlat[1]] <- as.numeric(x[,lonlat[1]])
x[,lonlat[2]] <- as.numeric(x[,lonlat[2]])
spdf <- x
coordinates(spdf) <- lonlat
proj4string(spdf) <- CRS("+init=epsg:4326")
return(spdf)
}