我已经使用包 raster 和 rgdal 从 CSV 制作了 shapefile,但我想将这些 shapefile 剪辑到某个区域,我可以这样做。
但是,当我尝试 write.csv 时它不起作用。
另外,在 writeOGR 之后我丢失了 X 和 Y。
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
当我将 X、Y 返回到 shapefile 属性表中时,我想将栅格值提取到点,然后将点的坐标和新属性写入 .csv。
如何在最终产品中获得 X、Y?