Wednesday, 5 December 2018

r - Converting SpatialPoints to Normal for exporting .csv


I have a rasterbrick and point shapefile. I have working code for extracting values of pixels from spatial points, but I need to export results to .csv file. I suppose that the key of this problem is to convert SpatialPointsDataFrame to normal one. But I do not know how to complete this task. Here is part of my code:


library(rgdal)
library(raster)
library(ggplot2)

# setting working directory
vstupni_adresar <- choose.dir(getwd(), "Zadej cestu k pracovnímu adresáři")
prac_adresar <- setwd(vstupni_adresar)


# choose a raster brick
r <- brick(choose.files(caption = "Načti data vstupující do klasifikace"))

# choose directory, where point shapefile is located
vyber_shp <- choose.dir(getwd(), "Vyber adresář, kde se nachází trénovací data")

# extract values from shapefile
sdata <- readOGR(dsn = vyber_shp, layer = "trenink")
sdata@data <- na.omit(data.frame(sdata@data, extract(r, sdata)))


# export to vsv
df <- fortify(sdata)
csv_zapis <- write.csv(df, file="Data_extrakce.csv")


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...