0

I have made shapefiles from CSV by using the packages raster and rgdal, but I'd like to clip these shapefiles to a certain area, which I can do.
However, when I write try to write.csv it does not work.

Also, I lose X and Y after writeOGR.

datansd = read.csv("..csv", header=T,sep=",")
### tell R what the coordinates are and define the coordinate system ###
coordinates(datansd) = c("X","Y")
proj4string(datansd) = CRS("+init=epsg:32614")
### write the shapefile, dsn is the target folder, layer is the name of the shapefile ###
writeOGR(datansd, dsn = "Shapes", layer = "July2013UD5", driver = "ESRI     Shapefile")
sdata <- readOGR(dsn="C:.", layer="July2013UD5")
# READ POINT SHAPEFILE TRAINING DATA
> head(sdata)    
  name       Value Value2      Value3 sampleID 
  1 29435 98.94331        1 1.056688    w764 
  2 29436 98.87749        1 1.122505    w764 
  3 29437 98.83189        1 1.168107    w764 
  4 29438 98.78847        1 1.211534    w764 
  5 29439 98.77276        1 1.227239    w764 
  6 29440 98.78549        1 1.214514    w764
> enclosure <- readOGR(dsn="C:.", layer="polygon")
  OGR data source with driver: ESRI Shapefile 
  Source: "C:..", layer: "polygon"
  with 12 features and 13 fields
  Feature type: wkbPolygon with 2 dimensions
> plot(enclosure)
> points(sdata)
> data <- spTransform(sdata, CRS(proj4string(enclosure))) # transform  CRS
> points(data)
> head(data)   
  name       Value Value2      Value3 sampleID 
  1 29435 98.94331        1 1.056688    w764 
  2 29436 98.87749        1 1.122505    w764 
  3 29437 98.83189        1 1.168107    w764 
  4 29438 98.78847        1 1.211534    w764 
  5 29439 98.77276        1 1.227239    w764 
  6 29440 98.78549        1 1.214514    w764
> data_subset <- data[enclosure, ]
> plot(enclosure)
> points(data_subset)
> head(data_subset)
    name     Value Value2   Value3 sampleID 
    378 31632 96.04780        1 3.952204    w764 
    379 31633 95.86989        1 4.130108    w764 
    380 31634 95.69978        1 4.300223    w764 
    381 31635 95.41422        1 4.585780    w764 
    382 31636 95.09640        1 4.903604    w764 
    383 31637 94.67770        1 5.322302    w764
> write.csv(data_subset,file ="C:...csv", row.names = FALSE)
> data_subset = read.csv("Shapes/July2013UD5clip.csv",  header=T,sep=",")
> coordinates(datansd) = c("X","Y")
    Error in `coordinates<-`(`*tmp*`, value = c("X", "Y")) : 
    setting coordinates cannot be done on Spatial objects, where they have already   been set
> writeOGR(data_subset, dsn = "Shapes", layer = "July2013UD5clip", driver =           "ESRI Shapefile")
    Error: inherits(obj, "Spatial") is not TRUE
> sdata <- readOGR(dsn="C:../Shapes", layer="July2013UD5clip")
    Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv        =      use_iconv,  : 
    Cannot open layer

When I get the X, Y back into the shapefile attribute table, I then want to extract the raster values to the points and then write.csv of the points with their coordinates and new attributes.

How do I get the X, Y in the final product?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
EJrandom
  • 29
  • 6

1 Answers1

0

After this line

data_subset <- data[enclosure, ]

You have a (subsetted) SpatialPointsDataFrame. You an write that to disk as a 'shapefile'. If you want to write it as a table (csv), with the coordinates, you should do

write.csv(data.frame(data_subset), 'file.csv')

To extract values from a raster and then write to a csv file, you could do

e <- extract(raster, data_subset)
d <- data.frame(data_subset, e) 
write.csv(d, 'file.csv')
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63