Created
November 8, 2017 16:01
-
-
Save jlisic/f4b96019a887c9f28aac9ad85dd80219 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# fun with sp objects | |
library(sp) | |
set.seed(100) | |
n <- 8 | |
m <- rpois(lambda=2,n)+1 | |
x_range <- c(0,100) | |
y_range <- c(30,90) | |
x <- runif(n, min=x_range[1], max=x_range[2] ) | |
y <- runif(n, min=y_range[1], max=x_range[2] ) | |
core_points <- cbind(x,y,1:n) | |
colnames(core_points) <- c('x','y','label') | |
core_points_new <- c() | |
for( i in 1:n) { | |
core_point <- matrix(rep(core_points[i,,drop=FALSE],each=m[i]),nrow=m[i]) | |
core_point <- cbind( core_point, | |
core_point[,1] + runif(m[i]), | |
core_point[,2] + runif(m[i]) | |
) | |
core_points_new <- rbind(core_points_new, core_point) | |
} | |
colnames(core_points_new) <- c('x','y','label','x_local','y_local') | |
# create spatial lines | |
matrix_to_spatial_lines <- function( x, from, to, label ) { | |
a <- list() | |
if( missing(label) ) { | |
for( i in 1:NROW(x) ) { | |
a[[i]] <- Lines( list(Line( rbind( x[i,from], x[i,to] ) )), | |
ID=as.character(i) ) | |
} | |
} else { | |
for( i in 1:NROW(x) ) { | |
a[[i]] <- Lines( list(Line( cbind( x[i,from], x[i,to] ) )), | |
ID=as.character(x[i,label]) ) | |
} | |
} | |
return( SpatialLines( a ) ) | |
} | |
b <- matrix_to_spatial_lines(core_points_new, c('x_local','y_local'), c('x','y')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment