0

我已经使用 readOGR(来自包'rgdal')导入了一个 shapefile,并获得了一个 SpatialPolygonsDataFrame。当我使用'rasterize'功能(包'raster')我得到这个

http://img15.hostingpics.net/pics/427269plot.png

但我只想光栅化边缘,所以我可以获得一个看起来像这样的 GeoTiff

http://img15.hostingpics.net/pics/270288Rplot.png

4

2 回答 2

3

您可以使用rastersp,使用SpatialLines对象类型来做到这一点。试试这个例子并spdf用您导入的 shapefile 名称替换:

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 )

在此处输入图像描述 在此处输入图像描述

于 2013-03-07T12:26:17.640 回答
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 文件中得到一个多边形图,多边形内没有任何颜色。

于 2013-02-26T21:04:22.617 回答