Spatial Data in R: Domains of a Multiclass SVM Parcel

I have columns data.frame

with columns x

and and y

and a column class

that gives the classification of each point within the existing multi-level SVM model. Here's some sample code:

library(rgdal)
library(rgeos)
library(e1071)  # for svm()
library(sp)
library(raster)
library(maptools)
library(plyr)

## Create a mask of the data region, as a data frame of x/y points.
covering <- function(data, xlen=150, ylen=150) {
    # Convex hulls of each class data points:
    polys <- dlply(data, .(class), function(x) Polygon(x[chull(x[-3]), -3]))
    # Union of the hulls:
    bbs <- unionSpatialPolygons(SpatialPolygons(list(Polygons(polys, 1))), 1)

    # Pixels that are inside the union polygon:
    grid <- expand.grid(x=seq(min(data$x), max(data$x), length.out=xlen),
                        y=seq(min(data$y), max(data$y), length.out=ylen))
    grid[!is.na(over(SpatialPoints(grid), bbs)), ]
}

set.seed(123)
data <- rbind(data.frame(x=rnorm(1000, 5*runif(1)), y=rnorm(1000, 5*runif(1)), class='A'),
              data.frame(x=rnorm(1000, 5*runif(1)), y=rnorm(1000, 5*runif(1)), class='B'),
              data.frame(x=rnorm(1000, 5*runif(1)), y=rnorm(1000, 5*runif(1)), class='C'))

m <- svm(class ~ x+y, data)
grid <- covering(data)

par(mfrow=c(1,2))
plot(y ~ x, data, col=data$class)
plot(y ~ x, grid, col=predict(m, grid), pch=20)

      

plot results

What I want to do next is convert this result to an object of SpatialPolygons

some type with one Polygons

object per factor level, for export to GeoJSON so that it can be used in a mapping application. What's a good way to do this? Do I have to write procedures myself to track the image (as a matrix) and find the boundaries between regions?

I looked through the docs for rasterToPolygons()

, but I couldn't figure out how to apply it to my situation, so I would welcome some help.

Eventually my data will be geospatial with real latitude / longitude information, but I wanted to try this simpler case first.

+3


source to share


1 answer


You need to convert your second image to bitmap (see here ).

sp.grid <- cbind(grid, col = predict(m, grid))
coordinates(sp.grid) <- ~ x + y
gridded(sp.grid) <- TRUE
sp.grid <- raster(sp.grid)

      



Then your attempt works.

grid.pols <- rasterToPolygons(sp.grid, n = 16, dissolve = TRUE)
plot(grid.pols)

class       : SpatialPolygonsDataFrame 
features    : 3 
extent      : -1.842044, 7.259169, -2.298892, 5.512507  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
variables   : 1
names       : col 
min values  :   1 
max values  :   3

      

+1


source







All Articles