haotu : an open lab notebook

2017/05/22

R intersect polygons and points

Filed under: Uncategorized — S @ 19:42

R intersect polygons and points

library(sf)
library(tidyverse)

# example data from raster package
soil <- st_read(system.file("external/lux.shp", package="raster")) %>% 
  # add in some fake soil type data
  mutate(soil = LETTERS[c(1:6,1:6)]) %>% 
  select(soil)

# field polygons
field <- c("POLYGON((6 49.75,6 50,6.4 50,6.4 49.75,6 49.75))",
        "POLYGON((5.8 49.5,5.8 49.7,6.2 49.7,6.2 49.5,5.8 49.5))") %>% 
  st_as_sfc(crs = st_crs(soil)) %>% 
  st_sf(field = c('x','y'), geoms = ., stringsAsFactors = FALSE)

# intersect - note that sf is intelligent with attribute data!
pi <- st_intersection(soil, field)
plot(soil$geometry, axes = TRUE)
plot(field$geoms, add = TRUE)
plot(pi$geoms, add = TRUE, col='red')

# add in areas in m2
attArea <- pi %>% 
  mutate(area = st_area(.) %>% as.numeric())

# for each field, get area per soil type
attArea %>% 
  as_tibble() %>% 
  group_by(field, soil) %>% 
  summarize(area = sum(area))

 

 

here

2016/07/14

aggregate by removing NA

Filed under: Manipulate Data in R, R, R Stats, Uncategorized — Tags: — S @ 08:43
na.collapse<-function(x)
{
 x.<-unique(x[!is.na(x)])
   if(length(x.)==0)
 {
   return(NA)
   } else {
     if(length(x.)==1){
     return(x.)
   } else {
     return(paste(x.,collapse="|"))
   }
  }
}
na.collapse(x)

2016/07/11

replace empty cells in google sheets

Filed under: Google, Sheets, Uncategorized — S @ 10:35
=if(isblank(A1),"myreplace",A1)

2016/06/24

find non-ascii in R

Filed under: errors in R, R, R, R Stats, Uncategorized — Tags: , , , , — S @ 06:34
tools::showNonASCII(readLines("myfiles.R"))

2016/04/25

assign nearest point to a polygon

Filed under: R, R spatial, R Stats, Uncategorized — S @ 11:31

 

wer<-SpatialPointsDataFrame(data.frame(x.subset[,"longitude"],x.subset[,"latitude"]),data=data.frame(x.subset))
n <- length(wer)
nearest <- character(n)
for (i in seq_along(nearest)) {
 nearest[i] <- names(polys)[which.min(gDistance(wer[i,], polys, byid=TRUE))]
}

This may give you warnings if your code is not projected into a planar coordinate system

http://stackoverflow.com/questions/26308426/how-do-i-find-the-polygon-nearest-to-a-point-in-r

2016/04/22

add unique id to duplicated grouped rows

Filed under: Manipulate Data in R, R, R, Uncategorized — S @ 12:30
library(plyr)
id(x)

2016/04/19

get latitude longitude raster cells

Filed under: R, R graphics, R spatial, Uncategorized — S @ 07:46
rasterToPoints(stacked) #this also gives the cell values of the raster layer or layers

http://gis.stackexchange.com/questions/142156/r-how-to-get-latitudes-and-longitudes-from-a-rasterlayer

 

 

number of layers in raster stack

Filed under: R, R graphics, R spatial, R Stats, Uncategorized — S @ 07:08
dimension(stacked)[3]

2016/04/18

stack rasters with different extents etc..

Filed under: arcmap, R, R, R spatial, R Stats, Uncategorized — S @ 13:42
two<-resample(two,one)
stack(one,two)

raster cell area size 1 degree cell size km2

Filed under: R, R spatial, R Stats, Uncategorized — Tags: , — S @ 08:14

For a 1×1 degree cell size raster

r <- raster(ncol=360, nrow=180) #or just the default r<-raster()
area(r)
area(r)$values

 

http://gis.stackexchange.com/questions/177622/r-calculate-raster-cell-size-in-map-units

 

http://gis.stackexchange.com/questions/29734/how-to-calculate-area-of-1-x-1-degree-cells-in-a-raster

Older Posts »

Blog at WordPress.com.