-
Notifications
You must be signed in to change notification settings - Fork 2
/
Clip
35 lines (26 loc) · 1.24 KB
/
Clip
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
33
34
35
#This script clips a large point shapefile (both within the US and in the Atlantic Ocean)
#of Hurricane points to states polygon.
#Import rgdal
library(rgdal)
#Import point Hurricane data
Hurricane <- readOGR(dsn = "data/Hurricane.shp", layer = "Hurricane")
#Import states polygon
states <- readOGR(dsn = "data\\UnitedStates.shp", layer = "UnitedStates")
#View the extents for both layers
bbox(Hurricane)
bbox(states)
#Create a new Hurricane layer and project the new layer to the polygon layer
HurricanePrj <- spTransform(Hurricane, CRSobj = CRS(proj4string(states)))
#View the new layers extent
bbox(HurricanePrj)
#Plot both points and polygons to ensure the the CRS transformation worked.
plot(states)
points(HurricanePrj)
#Create new object and set the newly projected layer to the polygon
HurricaneCL <- HurricanePrj[states, ]
#View the new layer to verify the clip worked.
plot(HurricaneCL)
#Write newly created clipped hurricane points to a shapefile. Set the object to the name of
#file in this script, set the dsn to the folder you want to put the new shapefile, name the new
#shapefile using the layer object, and set the driver to "ESRI Shapefile."
writeOGR(obj = HurricaneCL, dsn = "tempdir", layer = "HurricaneCL", driver = "ESRI Shapefile")