I am trying to get the size of a polygon from OSM, using osmar to download the data. However, sanity check tells me the are is not right.
Below is an example of what I mean.
(1) Geographical area around Hyde Park in London. Extracting all ways and relations tagged as 'park'.
devtools::install_github('osmdatar/osmdata')
library(osmdata)
library(osmar)
library(sp)
library(sf)
library(rgeos)
osmO <- get_osm(center_bbox(-0.167919, 51.5072682, 2000, 2000))
ids_relations <- osmO$relations$tags[osmO$relations$tags$v=="park","id"]
ids_ways <- osmO$ways$tags[osmO$ways$tags$v=="park","id"]
ids_sub <- find_down(osmO, way(c(ids_relations, ids_ways)))
sp_sub_park <- as_sp(subset(osmO, ids = ids_sub), "polygons")
Now, I want to know the area of each of these 'parks' [Fig1] (the big one in the middle being Hyde Park).
spplot(sp_sub_park, c("version"), colorkey = FALSE, col.regions=c('green'))
There are two ways:
1) Use the slot 'area' in the polygon itself.
a1 <- sapply(sp_sub_park@polygons, function(x) x@area)
2) Calculate the area with the specified projection.
bg_poly_t <- spTransform(sp_sub_park, CRS("+proj=longlat +datum=WGS84"))
a2 <- rgeos::gArea(bg_poly_t, byid=TRUE)
These two give me the same result [Fig2] (note the two biggest areas are Hyde Park, split in two by a road)
plot(a1*1000000, a2*1000000)
However, the size is not what I would expect. The area is returned in square-km (plotted square-meters). According to that the two parts of Hyde Park add up to about 300 square-meters, the size of a big flat but not a park (Hyde Park ~1.420.000 square-meters).
Any ideas?
rgeos::gArea
gave you a warning, which you could have used as a clue. – Three