Project

General

Profile

« Previous | Next » 

Revision f0275b0b

Added by Benoit Parmentier over 8 years ago

adding clipping functions for polygons and points in function script product assessment part1

View differences:

climate/research/oregon/interpolation/global_product_assessment_part1_functions.R
4 4
#Combining tables and figures for individual runs for years and tiles.
5 5
#AUTHOR: Benoit Parmentier 
6 6
#CREATED ON: 05/24/2016  
7
#MODIFIED ON: 05/24/2016            
7
#MODIFIED ON: 08/31/2016            
8 8
#Version: 1
9 9
#PROJECT: Environmental Layers project     
10 10
#COMMENTS: Initial commit, script based on part NASA biodiversity conference 
......
50 50
library(zoo)
51 51
library(xts)
52 52
library(lubridate)
53
library(mosaic)
53 54

  
54 55
###### Function used in the script #######
55 56
  
......
207 208
  return(date_str)
208 209
}
209 210

  
211
gClip <- function(shp, bb, keep.attribs=TRUE,outDir=NULL,outSuffix=NULL){
212
  #Purpose: clipping SpatialPolygonsDataFrame using another SpatialPolygonsDataFrame 
213
  #shp: input shapefile that we would like to clip
214
  #bb: input shapefile used for clipping, can be and extent raster object, matrix of coordinates 
215
  #keep.attribs: join attributes to spatial feature
216
  #outDir: output directory
217
  #outSuffix: output suffix attached to the name of new file
218
  
219
  #Authors: Benoit Parmentier, Modified code originating at: 
220
  #http://robinlovelace.net/r/2014/07/29/clipping-with-r.html
221
  
222
  #Comments: 
223
  #- Note that some attribute should be updated: e.g. areas, length etc. of original polygons
224
  #- Add bbox option for spdf
225
  #- Send error if different carthographic projection used
226
  
227
  ### BEGIN ####
228
  
229
  #First check inputs used from clipping, output dir and suffix
230
  
231
  if(class(bb) == "matrix"){
232
    b_poly <- as(extent(as.vector(t(bb))), "SpatialPolygons") #if matrix of coordinates
233
  }
234
  if(class(bb)=="SpatialPolygonsDataFrame"){
235
    b_poly <- bb #if polygon object, keep the same
236
  } 
237
  
238
  if(class(bb)=="SpatialPointsDataFrame"){
239
    b_poly <- as(extent(bb), "SpatialPolygons") #make a Spatial Polygon from raster extent
240
  }
241
  
242
  if(class(bb)=="exent"){
243
    b_poly <- as(extent(bb), "SpatialPolygons") #make a Spatial Polygon from raster extent
244
  }
245
  rm(bb) #remove from memory in case the polygon file is a giant dataset
246
  
247
  #If no output dir present, use the current dir
248
  if(is.null(outDir)){
249
    outDir=getwd()
250
  }
251
  #if no output suffix provided, use empty string for now
252
  if(is.null(outSuffix)){
253
    outSuffix=""
254
  }
255
  
256
  #Second, clip using rgeos library
257
  new.shape <- gIntersection(shp, b_poly, byid = T)
258
  
259
  #Third, join the atrribute back to the newly created object
260
  if(keep.attribs){
261
    #create a data.frame based on the spatial polygon object
262
    new.attribs <- data.frame(do.call(rbind,strsplit(row.names(new.shape)," ")),stringsAsFactors = FALSE)
263
    #test <-over(shp,bb)
264
    
265
    #new.attrib.data <- shp[new.attribs$X1,]@data #original data slot? #not a good way to extract...
266
    new.attrib.data <- as.data.frame(shp[new.attribs$X1,])
267
    row.names(new.shape) <- row.names(new.attrib.data)
268
    new.shape <-SpatialPolygonsDataFrame(new.shape, new.attrib.data) #Associate Polygons and attributes
269

  
270
  }
271
  
272
  #Writeout shapefile (default format for now)
273
  infile_new.shape <- paste("clipped_spdf",outSuffix,".shp",sep="")
274
  writeOGR(new.shape,dsn= outDir,layer= sub(".shp","",infile_new.shape), 
275
           driver="ESRI Shapefile",overwrite_layer="TRUE")
276
  
277
  return(new.shape)
278
}
279

  
210 280
############################ END OF SCRIPT ##################################

Also available in: Unified diff