How to convert a sample dataset from R package “spatstat” to ShapeFile

I write a kernel density estimator in Java, which inputs and outputs the GeoTIFF image of the estimated surface in the form of ESRI ShapeFile To test this module, I need a sample ShapeFile. For whatever reason, I was told to retrieve one from the sample data contained in R The problem is that no sample data is ShapeFile

So I tried to use funciton convert. In the shapefiles package to. ShapeFile (4) converts the Bei dataset contained in the spatstat package in R to ShapeFile Unfortunately, it turned out to be more difficult than I thought Does anyone have this experience? I would appreciate it if you could help me very kindly

Thank you, Ryan

References: spatstat, shapefiles

Solution

There are converter functions for spatial objects in the spatstat and maptools packages for this purpose Shapefiles consist of at least points (or lines or polygons) and attributes of each object

library(spatstat)
library(sp)
library(maptools)
data(bei)

To cast Bei into a spatial object, this is just a point without attributes, because there is no "tag" on the PPP object

spPoints <- as(bei,"SpatialPoints")

ShapeFile needs at least one column of attribute data, so create a virtual data

dummyData <- data.frame(dummy = rep(0,npoints(bei)))

Generate spatialpointsdataframe using spatialpoints object and virtual data

spDF <- SpatialPointsDataFrame(spPoints,dummyData)

At this point, you must consider what coordinate system Bei uses and whether you can use wkt CRS (well-known text coordinate reference system) You can assign it to the spatial object as another parameter of the spatialpointsdataframe, or use proj4string (SPDF) < - CRS ("proj = etc....) After creation (but this is a complete problem, we can write the page ourselves) Load the rgdal package (this is the most commonly used option because it supports many formats and uses the GDAL library, but may not be available due to system dependencies

library(rgdal)

(if rgdal is not available, use writepolyshape in maptools package)

The syntax is the object, then the "data source name" (here is the current directory, which can be the full path of. SHP or folder), then the layer (for ShapeFile file name without extension), and then the name of the output driver

writeOGR(obj = spDF,dsn = ".",layer = "bei",driver = "ESRI Shapefile")

Note that if "Bei. SHP" already exists, the write will fail, so you must first delete unlink ("Bei. SHP")

List all files starting with "Bei":

list.files(pattern = "^bei")

[1] "bei.dbf" "bei.shp" "bei.shx"

Note that there is no universal "as. Spatial" converter for PPP objects, because you have to decide whether this is a marked point pattern, etc. - it may be interesting to try to write one, it is necessary to report whether virtual data is useful, etc

For more information and details on the differences between these data representations, see the following illustrations:

Library (SP); Sketch ("SP") library (spatstat); Halo ("spatstat")

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>