R sf:使用ggplot2输出对象创建GeoPDF
我尝试使用在R sf:使用ggplot2输出对象创建GeoPDF,r,ggplot2,geospatial,sf,R,Ggplot2,Geospatial,Sf,我尝试使用在ggplot中创建的地图另存为*pdf,以便使用函数sf::st_write()创建地理参考pdf,但没有成功。在我的例子中: #Packages library(ggplot2) library(ggspatial) library(sf) # Get data set - x any are the points all.stands.predict<-read.csv("https://raw.githubusercontent.com/Leprechault
ggplot
中创建的地图另存为*pdf
,以便使用函数sf::st_write()
创建地理参考pdf,但没有成功。在我的例子中:
#Packages
library(ggplot2)
library(ggspatial)
library(sf)
# Get data set - x any are the points
all.stands.predict<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/prediction__bug_2021-03-18.csv")
all.stands.predict<-all.stands.predict[all.stands.predict[,3]=="VILA PALMA",] # Area selection
#Create a map
(sites <- st_as_sf(all.stands.predict, coords = c("x", "y"),
crs = 4326, agr = "constant"))
gg <- ggplot() +
geom_sf(data=sites, color="red") +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(0.3, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) + #Add a north arrow
annotation_scale(location = "bl", width_hint = 0.55) + #Add a scale bar
xlab("Longitude") + ylab("Latitude") +
theme_bw()
# I inspected the map created
plot(gg)
客观上,输出为:
Error in UseMethod("st_as_sf") :
method not applicable for 'st_as_sf' applied to a class object "c('gg', 'ggplot')"
任何将ggplot
对象转换为sf
对象的方法都不起作用。我还没有尝试使用像RQGIS
或RSAGA
这样的软件包
请注意,有一些方法可以使用
sf
?轻松创建GeoPDF地图解决方案中有两个步骤。首先,在光栅中转换ggplot2
对象,然后使用gdalutifs
包中的gdal\u translate
将创建的光栅(mygeotifgg.tif
)转换为geoPDF(mygeotifgg.pdf
)
# Save the plot
ggsave(plot=gg, "gg.tiff", device = "tiff", dpi = 600)
# Create a StackedRaster object from the saved plot
stackedRaster <- stack("gg.tiff")
# Get the GeoSpatial Components
lat_long <- ggplot_build(gg)$layout$panel_params[[1]][c("x_range","y_range")]
# Supply GeoSpatial data to the StackedRaster
extent(stackedRaster) <- c(lat_long$x_range,lat_long$y_range)
projection(stackedRaster) <- CRS("+proj=longlat +datum=WGS84")
# Create the GeoTiff
writeRaster(stackedRaster, "myGeoTiffgg.tif", options="PHOTOMETRIC=RGB", datatype="INT1U",overwrite=TRUE)
#Save raster as GeoPDF
gdalUtils::gdal_translate("myGeoTiffgg.tif","myGeoTiffgg.pdf",of="PDF", ot="Byte",
co="TILED=YES",verbose=TRUE, overwrite=T, a_srs="+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
}
#
#保存绘图
ggsave(plot=gg,“gg.tiff”,device=“tiff”,dpi=600)
#从保存的打印创建StackedMaster对象
堆垛机
# Save the plot
ggsave(plot=gg, "gg.tiff", device = "tiff", dpi = 600)
# Create a StackedRaster object from the saved plot
stackedRaster <- stack("gg.tiff")
# Get the GeoSpatial Components
lat_long <- ggplot_build(gg)$layout$panel_params[[1]][c("x_range","y_range")]
# Supply GeoSpatial data to the StackedRaster
extent(stackedRaster) <- c(lat_long$x_range,lat_long$y_range)
projection(stackedRaster) <- CRS("+proj=longlat +datum=WGS84")
# Create the GeoTiff
writeRaster(stackedRaster, "myGeoTiffgg.tif", options="PHOTOMETRIC=RGB", datatype="INT1U",overwrite=TRUE)
#Save raster as GeoPDF
gdalUtils::gdal_translate("myGeoTiffgg.tif","myGeoTiffgg.pdf",of="PDF", ot="Byte",
co="TILED=YES",verbose=TRUE, overwrite=T, a_srs="+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
}
#