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.

9
  • 2
    Possible duplicate of Join spatial point data to polygons in R – Jeffrey Evans Apr 12 '18 at 17:14
  • 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
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.

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.