How to convert sample data from package R "weave" to shape file

I wrote a kernel density estimate in Java that takes input in the form of ESRI shapefiles and displays a GeoTIFF image of the surface being evaluated. To test this module, I need an example shapefile, and for some reason I was asked to extract it from the sample data included in R. The problem is that none of the sample data is a shapefile ...

So, I'm trying to use the shapefiles funciton convert.to.shapefile(4) package to convert the bei dataset included in the spartstat package to R into a shapefile. Unfortunately, this turned out to be more complicated than I thought. Does anyone have any experience with this? If you were so kind as to lend me a hand, I would really appreciate it.

Thanks Ryan

References: spatstat , shapefiles

+6
source share
2 answers

There are converter functions for Spatial objects in the spatstat and maptools packages that can be used for this. A shape file consists of at least points (or lines or polygons) and attributes for each object.

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

Binding bei to a Spatial object is simply indicated here without attributes, since there are no "labels" on the ppp object.

 spPoints <- as(bei, "SpatialPoints") 

A form file requires at least one attribute data column, so create a dummy file.

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

Using a SpatialPoints object and dummy data, generate a SpatialPointsDataFrame .

 spDF <- SpatialPointsDataFrame(spPoints, dummyData) 

At this point, you should definitely consider what the coordinate system used by bei , and whether you can imagine it using WKT CRS (the well-known coordinate system for text coordinates). You can assign this to a Spatial object as another SpatialPointsDataFrame argument SpatialPointsDataFrame or after creating with proj4string(spDF) <- CRS("+proj=etc...") (but this is a whole problem in itself so that we can write pages).

Download the rgdal package (this is the most common option, as it supports many formats and uses the GDAL library, but may not be available due to system dependencies.

 library(rgdal) 

(Use writePolyShape in the maptools package if rgdal not available).

The syntax is an object, then โ€œdata source nameโ€ (here is the current directory, this can be the full path to a .shp or folder), then a layer (for shape files, the file name without extension), and then the name of the output driver.

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

Note that recording will fail if "bei.shp" already exists and therefore unlink("bei.shp") must first be deleted.

List all the files starting with "bei":

 list.files(pattern = "^bei") [1] "bei.dbf" "bei.shp" "bei.shx" 

Please note that for ppp objects there is no common "as.Spatial" converter, as decisions must be made as to whether this is a dot with labels, etc. - it may be interesting to try to write one that reports on whether dummy data is needed, etc.

For more information and details on the differences between these data views, see the following vignettes:

Library (SP); vignette ("IP") library (spatstat); vignette ("spatstat")

+6
source

Common decision:

  • convert objects of class "ppp" or "owin" to the corresponding class objects from the package sp
  • use the writeOGR() function from the rgdal package to write a Shapefile

For example, if we look at the hamster dataset from spatstat :

 require(spatstat) require(maptools) require(sp) require(rgdal) data(hamster) 

first convert this object to a SpatialPointsDataFrame object:

 ham.sp <- as.SpatialPointsDataFrame.ppp(hamster) 

This gives us an sp object to work from:

 > str(ham.sp, max = 2) Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 303 obs. of 1 variable: ..@ coords.nrs : num(0) ..@ coords : num [1:303, 1:2] 6 10.8 25.8 26.8 32.5 ... .. ..- attr(*, "dimnames")=List of 2 ..@ bbox : num [1:2, 1:2] 0 0 250 250 .. ..- attr(*, "dimnames")=List of 2 ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots 

This object has a single variable in the @data slot:

 > head( ham.sp@data ) marks 1 dividing 2 dividing 3 dividing 4 dividing 5 dividing 6 dividing 

So, let's say now we want to write this variable as an ESRI Shapefile, we use writeOGR()

 writeOGR(ham.sp, "hamster", "marks", driver = "ESRI Shapefile") 

This will create several marks.xxx files in the hamster directory created in the current working directory. This set of files is a ShapeFile.

One of the reasons why I did not do the bei dataset above is because it does not contain any data, and therefore we cannot force it to be a SpatialPointsDataFrame object. There is data that we could use in bei.extra (loaded at the same time as bei ), but this additional data or in a regular grid. Therefore we need

  • convert bei.extra to a SpatialGridDataFrame object (say bei.spg )
  • convert bei to SpatialPoints object (say bei.sp )
  • overlay() bei.sp points to the bei.spg grid, getting values โ€‹โ€‹from the grid for each of the points in bei
  • which should give us a SpatialPointsDataFrame , which can be written using writeOGR() as above

As you can see, this is a bit more to give you a Shapefile. Will the hamster data example i show enough? If not, I can look out for Bivand et al. Tomorrow and follow the steps for bei .

+3
source

Source: https://habr.com/ru/post/886732/


All Articles