I have used geosphere::areaPolygon()
with success many times but now I have encountered an issue.
I have a polygon sp2 containing another polygon sp1. So sp2 should have larger area than sp1. When I calculate each area with areaPolygon()
, I get the opposite result.
areasp1 = 10133977
areasp2 = 9858811
I used gSymdifference to find the symmetrical different polygon sp3, which has
areasp3 = 275165.4
sp1 : red dashed line
sp2 : black line
sp3 : green dotted line
Code I used:
library(sp)
library(geosphere)
library(rgeos)
library(raster)
s1 <- data.frame(lon= c(130.4327, 129.85127, 121.00775, 84.9176, 60.40123,
58.97929, 58.55841, 94.95237),lat= c(30.25074, 29.83075, 23.7992, 28.25964,
36.89905, 37.72305, 52.58793, 43.47459))
s2 <- data.frame(lon= c(130.4327, 129.85127, 121.00775, 54.35377, 58.55841,
94.95237),lat= c(30.25074, 29.83075, 23.7992, 31.61798, 52.58793, 43.47459))
sp1 <- SpatialPolygons(list(Polygons(list(Polygon(s1)),1)))
crs(sp1) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
sp2 <- SpatialPolygons(list(Polygons(list(Polygon(s2)),1)))
crs(sp2) <-CRS ("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
areasp1 <- areaPolygon(sp1)/10^6 # to get area in square km
areasp2 <- areaPolygon(sp2)/10^6
sp3 <- gSymdifference(sp1,sp2,checkvalidity = TRUE)
areasp3 <- areaPolygon(sp3)/10^6
All polygons were tested for self-intersections or other issues with gIsValid()
, so it has nothing to do with the problem mentioned here.
Any idea on why sp1 has larger area than sp2?
Note: If you sum areasp2
+ areasp3
, it almost equals areasp1
. I don't know if this is by chance.