I can produce Polygon layer from matrix in following way:

pols <- lapply(cdf, Polygon)
    pls <- lapply(seq_along(pols), function(i) Polygons(list(pols[[i]]), ID = i))
    sps <- SpatialPolygons(pls)
    sps_df <- SpatialPolygonsDataFrame(sps, data.frame(x = rep(NA, length(pls)), row.names = names(pls)))

while cdf is a matrix of points (x, y) for the polygon(s). My question is: how to do the same for points or even circles? I don't have a function to cast like pols <- lapply(cdf, Point) to produce SpatialPoints object from many points.


Read the help for SpatialPoints, specifically the arguments:


  coords: numeric matrix or data.frame with coordinates (each row is a
          point); in case of SpatialPointsDataFrame an object of class
          SpatialPoints-class is also allowed

So a simple two-column matrix (here with four rows) can be a set of four points:

> SpatialPoints(cbind(runif(4),runif(4)))
     coords.x1 coords.x2
[1,] 0.9010205 0.5651983
[2,] 0.4403097 0.3789879
[3,] 0.3639780 0.1379548
[4,] 0.1035837 0.9666854
Coordinate Reference System (CRS) arguments: NA 

Add coordinate system as needed via proj4string= as per the documentation.

Points are simpler than polygons (which can be made of multiple rings and islands and so on) so the constructor is simpler, and there's no Point class like there is Polygon for SpatialPolygons.

  • Yes, maybe too easy for a class handler. – Peter.k Jun 4 '19 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.