0

I am an ArcGIS user trying to get to grips with R and the way it handles spatial data.

I've got two spatial data objects:

class       : SpatialPolygonsDataFrame 
features    : 1 
extent      : 0.1353, 0.1376, 51.5668, 51.5678  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 2
names       : X_leaflet_id, feature_type 
value       :           81,      polygon 

and

class       : SpatialPolygonsDataFrame 
features    : 1 
extent      : 0.1386686, 0.1392818, 51.56686, 51.56768  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 6
names       :            NAME,   CODE, Shape_Length,  Shape_Area,            Attrib1, Attrib2 
value       : Example polygon, CodeP1,  0.002631075, 3.91693e-07, My first attribute,     999 

Note that the attributes of these objects are different. In my final analysis I will add attributes to the first object to align with the second.

How can I append or bind the first polygon as a record to the second, as an individual row? Note I'm not trying to do anything clever here, like a spatial analysis such as point in polygon. I just want to append the first polygon to the dataset that the second is held in, and I can't find out how.

Hester Lyons
  • 111
  • 1
  • 5
  • 2
  • Please search the site before posing a question. This issue has been addressed several times on this site and can be found by searching for: "[R] point in polygon" – Jeffrey Evans Apr 12 '18 at 17:15
  • @JeffreyEvans I don't think this is the same issue. I think the OP is asking for a bindind operation (similar to merge in Arcgis) – dof1985 Apr 12 '18 at 17:20
  • @Hester if you can add attributes to the data.frame of both spatial objects, in sense that they have exactly the same tabular structure, you can use rbind which has a method for the sp class. You can read the help page of the function by running ?rbind in an r console – dof1985 Apr 12 '18 at 17:22
  • @dof1985 That is just one example, and if you look at results is exactly the same issue, and there are many more post that use functions like over() to just return intersecting attributes rather than feature classes. – Jeffrey Evans Apr 12 '18 at 18:16
  • @JeffreyEvans, I don't think that was the OP's intention, but maybe it should be clarified in the question. – dof1985 Apr 12 '18 at 19:26
  • @dof1985 thank you, that was the kind of thing I was looking for. Point in polygon was not! Hence my comment that I wasn't trying to do a spatial analysis - that seems fairly straightforward in R. Thank you all for your comments, I will add an answer in a bit. – Hester Lyons Apr 13 '18 at 07:34
  • @Hester glad it helped, maybe you can edit your question and answer and clarify your question, since it was not clear for all. Maybe using the right terminology, e.g. binding, merging, would be beneficial. This will make your thread helpful for other users as well. – dof1985 Apr 14 '18 at 16:36
  • I've updated the question a little in the hope it's clearer. Part of the problem is the differences in terminology between ArcGIS and R, which also makes searching for documentation harder. in Arc or anything SQL related I'd be looking for 'append'. In R, I'm looking for bind, which is a term I hadn't heard until a couple of months ago. Add that to the ability to keep data in lists, vectors, matrices, data frames and tables, all of which do different things (rather than just a table or a shape file, which is where I come from), and the potential for confusion is considerable. Thank you. – Hester Lyons Apr 16 '18 at 07:43

2 Answers2

1

So the answer to this was straightforward as pointed out above by @dof1985. Here is a worked example - note the first polygon is a user polygon obtained by the mapedit editMod in Shiny, the second is from a dummy dataset. For the code below I have removed Shape_Length and Shape_Area from the dummy dataset so it is slightly different from the data quoted above.

    newData <- result()$finished ## This is how it comes in from 
                                 ## Shiny Leaflet module editMod
    ## Coerce into spatial data frame as it is currently a SF
    newPoly <- as(newData, "Spatial")
    ## Add required columns and give them a value (this will come in from a pop-up menu)
    newPoly$NAME <- "NewName"
    newPoly$CODE <- "CodeNEW"
    newPoly$Attrib1 <- "New Attrib"
    newPoly$Attrib2 <- 8888

    ## Drop the attributes we don't want
    newPoly$X_leaflet_id <- NULL
    newPoly$feature_type <- NULL

    ## Now merge with the existing data
    myPolys <- rbind(myPolys, newPoly)

Thank you all for your comments, I knew this was a fairly trivial question but when faced with the combined new concepts of R, R Spatial, Shiny, Leaflet and mapedit being unable to find a simple way to append a polygon to an existing dataset made me decide to look for help. And believe me, I did spend considerable time looking for an answer.

Hester Lyons
  • 111
  • 1
  • 5
0

When using rbind(), just make sure that both the arguments you use are SpatialDataFrames. You can check this using class(sf). If it is not a dataframe, then use st_as_sf() to convert them to a SpatialDataFrame before you rbind them.

Note : You can also use this to append to NULLs, especially when you are using a result from a loop and you want to cumulate the results.

Jerin Mathew
  • 101
  • 1