0

I wish to combine multiple shapefiles for different regions and assign them names in R/sf. I'm not sure if my procedure is orthodox. That may also be the reason for the incorrect boundary box which results.

The raw data is a set of three shapefiles downloaded from this site (in Korean). I attach the files, as they are not accessible outside Korea. The files are for the Seoul, Incheon and Gyeonggi-do regions. I want to combine them into the Seoul Capital Area. At the same time, I wish to name them in Roman script. Here's the code:

library(sf)

#Read, combine and name shapefiles
sca_nsdi <- rbind(data.frame(st_read("LARD_ADM_SECT_SGG_11.shp"), Name="Seoul", stringsAsFactors=F), 
                  data.frame(st_read("LARD_ADM_SECT_SGG_28.shp"), Name="Incheon", stringsAsFactors=F), 
                  data.frame(st_read("LARD_ADM_SECT_SGG_41.shp"), Name="Gyeonggi-do", stringsAsFactors=F))

#Convert back to sf dataframe (according to https://github.com/r-spatial/sf/issues/193#issuecomment-276354417)
st_geometry(sca_nsdi) <- sca_nsdi$geometry

I'm not sure if this the right way to go. The boundary box of the merged set is the same as the boundary box of Seoul, which is smaller than surrounding Gyeonggi-do. I don't know how to correct that, but I'm also wondering if my procedure produces other undesirable side-effects which I haven't detected yet.

st_bbox(sca_nsdi)==st_bbox(sca_nsdi[sca_nsdi$Name=="Seoul",]) #returns all TRUE
plot(sca_nsdi$geometry) #returns truncated map
plot(sca_nsdi$geometry[sca_nsdi$Name=="Seoul",], add=T, lwd=2) #superimprints Seoul in bolder lines

enter image description here

2

I think you are doing a lot of unnecessary conversion! I'd do something like this.

First read the data into separate objects. Then I can experiment:

s11 = st_read("LARD_ADM_SECT_SGG_11.shp")
s28 = st_read("LARD_ADM_SECT_SGG_28.shp")
s41 = st_read("LARD_ADM_SECT_SGG_41.shp")

For example I can simply rbind the spatial objects and it seems to work as expected:

sall = rbind(s11, s41, s28)

It has all the original columns and the expected number of rows. But you want to add the different Name to each element. Well you can cbind a new column:

snamed = cbind(s28,Name="Incheon")

for example, and that works as expected - you get a spatial object with a new column filled with that value, and it keeps the correct bounds, rows, columns, CRS etc. So [cracks knuckles] combine those and...

sall = rbind(
  cbind(s11,Name="Seoul"),
  cbind(s28,Name="Incheon"),
  cbind(s41,Name="Gyeonggi-do"))

and then:

plot(sall[,"Name"])

enter image description here

I'm not sure where your code breaks down, but it must be losing the "spatial-special" nature at some point (possibly the data frame conversions). I'm not sure its worth tracking down but it might be educational. I think my code is clearer and simpler in any case.

2
  • Thanks @Spacedman! When I plot, the legend is placed on the right vertically (and clipped). How come yours is placed below horizontally? – syre May 13 '20 at 3:20
  • 1
    That depends on the height-to-width ratio of your graphic window - R will fit the legend in the best place depending on where it can find the space. Resize your window and re-do the plot and you can change it. There may be an option to force it. If you want to know more and can't find it in the help, try asking a new question. – Spacedman May 13 '20 at 7:07

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.