[R-sig-Geo] Convert SpatialPointsDataFrame to SpatialPolygons or SpatialPolygonsDataFrame

Michael Sumner mdsumner at gmail.com
Mon Dec 26 13:12:15 CET 2016


On Mon, 26 Dec 2016 at 22:39 Miluji Sb <milujisb at gmail.com> wrote:

> Dear Michael,
>
> Merry Christmas and seasons's greetings! Thank you for your reply, I am
> providing more information and my attempts to convert
> to SpatialPolygonsDataFrame below.
>
> The original SpatialPointsDataFrame has the following attributes:
>
> class       : SpatialPointsDataFrame
> features    : 21441
> extent      : -179.5, 179.5, -89.5, 83.5  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +ellps=WGS84
> variables   : 3
> names       : ID_cell,    lon,   lat
> min values  :    2304, -179.5, -89.5
> max values  :   64800,  179.5,  83.5
>
> The data looks like this:
>
> dput(head(shp at data,20))
> structure(list(ID_cell = c(2304L, 2305L, 2306L, 2307L, 2308L,
> 2309L, 2310L, 2311L, 2618L, 2619L, 2620L, 2621L, 2622L, 2623L,
> 2624L, 2625L, 2626L, 2627L, 2628L, 2629L), lon = c(-36.5, -35.5,
> -34.5, -33.5, -32.5, -31.5, -30.5, -29.5, -82.5, -81.5, -80.5,
> -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5
> ), lat = c(83.5, 83.5, 83.5, 83.5, 83.5, 83.5, 83.5, 83.5, 82.5,
> 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5
> )), .Names = c("ID_cell", "lon", "lat"), data_types = c("N",
> "F", "F", "C", "C", "C", "C"), row.names = c("0", "1", "2", "3",
> "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
> "16", "17", "18", "19"), class = "data.frame")
>
> I tried the following:
>
> x = as(SpatialPixelsDataFrame(shp, shp at data, tolerance=.00086),
> "SpatialPolygonsDataFrame"), it seemed to have worked but got the following
> warning:
>
> Warning message:
> In points2grid(points, tolerance, round) :
>   grid has empty column/rows in dimension 2
>
> Now the converted SpatialPolygonsDataFrame has the following attributes:
>
> class       : SpatialPolygonsDataFrame
> features    : 21441
> extent      : -180, 180, -90, 84  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +ellps=WGS84
> variables   : 3
> names       : ID_cell,    lon,   lat
> min values  :    2304, -179.5, -89.5
> max values  :   64800,  179.5,  83.5
>
> structure(list(ID_cell = c(2304L, 2305L, 2306L, 2307L, 2308L,
> 2309L, 2310L, 2311L, 2618L, 2619L, 2620L, 2621L, 2622L, 2623L,
> 2624L, 2625L, 2626L, 2627L, 2628L, 2629L), lon = c(-36.5, -35.5,
> -34.5, -33.5, -32.5, -31.5, -30.5, -29.5, -82.5, -81.5, -80.5,
> -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5
> ), lat = c(83.5, 83.5, 83.5, 83.5, 83.5, 83.5, 83.5, 83.5, 82.5,
> 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5
> )), .Names = c("ID_cell", "lon", "lat"), data_types = c("N",
> "F", "F", "C", "C", "C", "C"), row.names = c("g144", "g145",
> "g146", "g147", "g148", "g149", "g150", "g151", "g458", "g459",
> "g460", "g461", "g462", "g463", "g464", "g465", "g466", "g467",
> "g468", "g469"), class = "data.frame")
>
> Is this correct or even on the right track? I hope I have provided enough
> information. Thank you so much.
>
>
That is certainly one correct conversion of a set of points, interpreted as
a regular grid of cells about the points-as-centres, then converted to
polygons (quite different from the wrong guess I made at first). It's
extremely inefficient because of all the redundant coordinates stored for
every polygon, but that may not be a concern.

Without knowing your requirements it's not possible to know if this is a
useful way to go. You can avoid the redundancy by keeping the thing as a
grid, and I would explore the raster package for an alternative to the
SpatialPixelsDataFrame used here, it doesn't allow mixed columns of data
types on a data frame but does have a lot of high-level tools for regular,
numeric grids.

We still don't know why you wanted polygons, or whether the tolerance of
"regular" used will affect the results you are after but other than that
and the inefficiencies inherent in the storage I don't see any real problem
with what you're doing.

If you can detail why you want to do this, and what comes next, there's
probably better advice to be given.

Cheers, Mike.

SIncerely,
>
> Milu
>
> On Mon, Dec 26, 2016 at 7:24 AM, Michael Sumner <mdsumner at gmail.com>
> wrote:
>
> It is but you will need at least a grouping ID for each polygon ring
> within each object, and an order within each ring. Is ID_cell the object ID?
>
> If there is only a single ring polygon within each object and the order is
> native it's simplest, but the constructors require very particular
> arrangements of your data, there is no abstractions for this. See
> raster::spPolygons and spbabel::sp for alternative constructors, but other
> wise see Polygon, Polygons, SpatialPolygons and SpatialPolygonsDataFrame in
> sp.
>
> There's not enough information in your post to be sure of what you have,
> but I'm happy to help if you provide more detail about how your data is
> organized. You could post sample data, or describe them in more detail. Do
> you have actual  polygons here or are you attempting to estimate them from
> point data?
>
> To add more alternatives the new sf package simplifies the constructors a
> lot, for a new family of classes, but still you need to nest matrices of
> coordinates manually to use them rather than specify their organization
> abstractly.  I'm working on tools to do that, but there is only spbabel so
> far (it's really just a two-level grouping of coordinates for spatial
> objects and their parts, but various legacies make it ever more
> complicated).
>
> Cheers, Mike
>
> On Sun, Dec 25, 2016, 02:28 Miluji Sb <milujisb at gmail.com> wrote:
>
> I have a SpatialPointsDataFrame with the following attributes. Is it
> possible to convert this to a SpatialPolygons or SpatialPolygonsDataFrame?
> Thank you!
>
> Sincerely,
>
> Milu
>
> class       : SpatialPointsDataFrame
> features    : 21441
> extent      : -179.5, 179.5, -89.5, 83.5  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +a=6367470 +b=6367470 +no_defs
> variables   : 7
> names       : ID_cell,    lon,   lat, Field4, Field5, Field6, Field7
> min values  :    2304,    0.5,   0.5,     NA,     NA,     NA,     NA
> max values  :   64800, -179.5, -89.5,     NA,     NA,     NA,     NA
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> --
> Dr. Michael Sumner
> Software and Database Engineer
> Australian Antarctic Division
> 203 Channel Highway
> Kingston Tasmania 7050 Australia
>
>
> --
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list