R - 仅栅格化SpatialPolygonsDataFrame的边缘

时间:2013-02-26 15:02:50

标签: r shapefile edge rasterize

我使用readOGR(来自包'rgdal')导入了shapefile,并获得了SpatialPolygonsDataFrame。当我使用'rasterize'函数(包'栅格')时,我得到了这个 http://img15.hostingpics.net/pics/427269plot.png 但我只想光栅化边缘,所以我可以获得一个看起来像这样的GeoTiff http://img15.hostingpics.net/pics/270288Rplot.png 任何的想法? (我是R的新人!)提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以使用rastersp使用SpatialLines对象类型执行此操作。试试这个例子,用您导入的shapefile名称替换spdf

spdf <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1]) # Read in your datafile. You can use readOGR or readShapePoly, it doesn't really matter.
sldf <- as( spdf , "SpatialLinesDataFrame") # Create a lines object. This gives you the borders of the polygons
r <- raster( nrow = 180 , ncols = 360 , ext = extent(spdf) ) # Create a template raster file which will form the mask you will rasterzie to (so if you want a more precise 
r <- rasterize( sldf , r ) # Depending on the resolution of your target raster and the complexity of your shapefile this may take a few seconds or a few minutes to run

您可以根据需要保存光栅文件。

plot( spdf )
plot( r )

enter image description here enter image description here

答案 1 :(得分:0)

通常,要在ggplot2

中绘制此多边形数据
library(ggplot2)
# Convert the SpatialPolygons object to a data.frame, which ggplot2 needs
poly_data_frame = fortify(poly_spatialpolygon)
ggplot(poly_data_frame, aes(x = x, y = y)) + geom_polygon(fill = "transparent")
ggsave("poly_plot.png")

现在你最终得到了PNG文件中的多边形图,而多边形内没有任何颜色。