如何从 R 包“spatstat”转换样本数据集到形状文件中
我用 Java 编写了一个核密度估计器,它接受 ESRI shapefile 形式的输入并输出估计表面的 GeoTIFF 图像。为了测试这个模块,我需要一个示例 shapefile,并且出于某种原因,我被告知从 R 中包含的示例数据中检索一个。问题是示例数据都不是 shapefile...
所以我尝试使用shapefiles 包的函数 convert.to.shapefile(4)
将 R 中 spatstat 包中包含的 bei 数据集转换为 shapefile。不幸的是,事实证明这比我想象的要困难。有人有这方面的经验吗?如果您愿意帮我一把,我将不胜感激。
谢谢, 瑞安
I have written a kernel density estimator in Java that takes input in the form of ESRI shapefiles and outputs a GeoTIFF image of the estimated surface. To test this module I need an example shapefile, and for whatever reason I have been told to retrieve one from the sample data included in R. Problem is that none of the sample data is a shapefile...
So I'm trying to use the shapefiles package's funciton convert.to.shapefile(4)
to convert the bei dataset included in the spatstat package in R to a shapefile. Unfortunately this is proving to be harder than I thought. Does anyone have any experience in doing this? If you'd be so kind as to lend me a hand here I'd greatly appreciate it.
Thanks,
Ryan
References:
spatstat,
shapefiles
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
spatstat
和maptools
包中的Spatial
对象的转换器函数可用于此目的。 shapefile 至少由每个对象的点(或线或多边形)和属性组成。将
bei
强制转换为Spatial
对象,这里仅指向没有属性的点,因为ppp
对象上没有“标记”。shapefile 至少需要一列属性数据,因此创建一个虚拟数据。
使用
SpatialPoints
对象和虚拟数据生成SpatialPointsDataFrame
。此时,您绝对应该考虑
bei
使用的坐标系是什么,以及是否可以使用WKT CRS
(众所周知的文本坐标参考系统)来表示它。您可以将其分配给Spatial
对象作为SpatialPointsDataFrame
的另一个参数,或者在使用proj4string(spDF) <- CRS("+proj=etc. ..")
(但这本身就是一个完整的问题,我们可以在其上编写页面)。加载
rgdal
包(这是最通用的选项,因为它支持多种格式并使用GDAL库,但由于系统依赖关系可能不可用。(在中使用
writePolyShape
如果rgdal
不可用,则使用maptools
包。语法是对象,然后是“数据源名称”(这里是当前目录,这可以是完整路径) 。 .shp,或文件夹),然后是图层(对于 shapefile,是不带扩展名的文件名),然后是输出驱动程序的名称。
请注意,如果“bei.shp”已经存在,则写入将失败,因此必须先删除<。 code>unlink("bei.shp")。
列出以“bei”开头的所有文件:
请注意,
ppp
对象没有通用的“as.Spatial”转换器,因为决策必须确定这是否是一个要点带有标记的图案等等 - 尝试编写一个报告是否需要虚拟数据等可能会很有趣,请参阅以下小插图以获取有关这些数据表示之间的差异的更多信息和详细信息:
library(sp);小插图(“sp”)
库(spatstat);小插图(“spatstat”)
There are converter functions for
Spatial
objects in thespatstat
andmaptools
packages that can be used for this. A shapefile consists of at least points (or lines or polygons) and attributes for each object.Coerce
bei
to aSpatial
object, here just points without attributes since there are no "marks" on theppp
object.A shapefile requires at least one column of attribute data, so create a dummy.
Using the
SpatialPoints
object and the dummy data, generate aSpatialPointsDataFrame
.At this point you should definitely consider what the coordinate system used by
bei
is and whether you can represent that with aWKT CRS
(well-known text coordinate reference system). You can assign that to theSpatial
object as another argument toSpatialPointsDataFrame
, or after create withproj4string(spDF) <- CRS("+proj=etc...")
(but this is an entire problem all on its own that we could write pages on).Load the
rgdal
package (this is the most general option as it supports many formats and uses the GDAL library, but may not be available because of system dependencies.(Use
writePolyShape
in themaptools
package ifrgdal
is not available).The syntax is the object, then the "data source name" (here the current directory, this can be a full path to a .shp, or a folder), then the layer (for shapefiles the file name without the extension), and then the name of the output driver.
Note that the write would fail if the "bei.shp" already existed and so would have to be deleted first
unlink("bei.shp")
.List any files that start with "bei":
Note that there is no general "as.Spatial" converter for
ppp
objects, since decisions must be made as to whether this is a point patter with marks and so on - it might be interesting to try writing one, that reports on whether dummy data was required and so on.See the following vignettes for further information and details on the differences between these data representations:
library(sp); vignette("sp")
library(spatstat); vignette("spatstat")
一般的解决方案是:
"ppp"
或"owin"
类对象从sp
包转换为适当的类对象rgdal 包中的 writeOGR()
函数用于写出 Shapefile例如,如果我们考虑
spatstat
中的hamster
数据集:首先将此对象转换为
SpatialPointsDataFrame
对象:这为我们提供了一个
sp
对象:该对象在
@data
槽中有一个变量:所以说我们现在想要将此变量写为 ESRI Shapefile,我们使用 writeOGR()
这将在创建的目录
hamster
中创建几个marks.xxx
文件当前工作目录。这组文件就是 ShapeFile。我没有对
bei
数据集执行上述操作的原因之一是它不包含任何数据,因此我们无法将其强制转换为SpatialPointsDataFrame
对象。 有我们可以使用的数据,在bei.extra
中(与bei
同时加载),但是这些额外的数据还是在常规网格上。因此,我们必须SpatialGridDataFrame
对象(例如bei.spg
)到
SpatialPoints
对象(例如bei.sp
)overlay()
,bei.sp
指向 <代码>bei.spg grid,从网格中为bei
中的每个点生成值,writeOGR()
写出的SpatialPointsDataFrame
如上如您所见,只是为了给您一个 Shapefile,这需要更多的工作。我展示的
仓鼠
数据示例是否足够?如果没有,我可以找到我的 Bivand 等明天并执行bei
的步骤。A general solution is:
"ppp"
or"owin"
classed objects to appropriate classed objects from thesp
packagewriteOGR()
function from packagergdal
to write the Shapefile outFor example, if we consider the
hamster
data set fromspatstat
:first convert this object to a
SpatialPointsDataFrame
object:This gives us a
sp
object to work from:This object has a single variable in the
@data
slot:So say we now want to write out this variable as an ESRI Shapefile, we use
writeOGR()
This will create several
marks.xxx
files in directoryhamster
created in the current working directory. That set of files is the ShapeFile.One of the reasons why I didn't do the above with the
bei
data set is that it doesn't contain any data and thus we can't coerce it to aSpatialPointsDataFrame
object. There are data we could use, inbei.extra
(loaded at same time asbei
), but these extra data or on a regular grid. So we'd have tobei.extra
to aSpatialGridDataFrame
object (saybei.spg
)bei
to aSpatialPoints
object (saybei.sp
)overlay()
thebei.sp
points on to thebei.spg
grid, yielding values from the grid for each of the points inbei
SpatialPointsDataFrame
that can be written out usingwriteOGR()
as aboveAs you see, that is a bit more involved just to give you a Shapefile. Will the
hamster
data example I show suffice? If not, I can hunt out my Bivand et al tomorrow and run through the steps forbei
.