2

I am working with a shapefile in R that is made up of 48 polygons. It is a map of Sweden with a line plotted to denote a biological boundary between north and south. I want to figure out the latitude of the line (see plot) but I cannot figure out how to extract that information from the shapefile.

I am wondering how to go about this. Can I plot each polygon separately to see if one/multiple polygons make up this line? ggmap of shapefile showing line between N and S

1 Answer 1

1

With the sf package you can very easily iterate over the geometries:

gadm <- raster::getData("GADM", country="SWE", level=1)

library("sf")
sf_geom <- st_geometry(st_as_sf(gadm))
# use st_read instead of st_as_sf to read the shapefile

for(i in seq_along(sf_geom)) {
  plot(sf_geom[i], main=i, axes=TRUE)
  Sys.sleep(1)
}
2
  • OK thanks. I have managed to parse out each polygon's geometry using the st_geometry function. Now, I am interested in seeing whether a set of coordinates lies in one particular polygon. I was using the over() function before to check if SpatialPoints lie within a SpatialPolygon but that does not work anymore since st_geometry is now a geometry set. See code: LC2 <- SpatialPoints(LC[,1:2]) proj4string(LC2) <- proj4string(map_wgs84) map2 <- as(map_wgs84, "SpatialPolygons") inside <- !is.na(over(LC2, map2))
    – Hannah
    Feb 24, 2017 at 15:52
  • Nevermind! solved. Thanks for your help. LC2 <- SpatialPoints(LC[,1:2]) proj4string(LC2) <- proj4string(map_wgs84) map2 <- as(map_wgs84, "SpatialPolygons") inside <- !is.na(over(LC2, map2[46]))
    – Hannah
    Feb 24, 2017 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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