1
votes

My colleague has sent me several shapefiles for an analysis. There are supposed to be holes in these shapefiles. Here is an example of what one of the shapefiles looks like on their computer in ArcGIS: Map from colleague, a shapefile with many holes

But, on my computer, whether I use QGIS or R (I don't have ArcGIS on my machine), the file looks like this (zoomed in on the left side of the first shapefile image):

a shapefile with overlapping polygons instead of holes

I tried "Fix Geometries" in QGIS version 3.12, which didn't get me the whole way there--some holes were restored, other overlapping polygons remained:

a shapefile with some overlapping polygons and some holes

I tried fixing this in R (version 3.6.1, running on a Mac OS 10.15.4) with clgeo_Clean() from the cleanGeo package, but all that did was delete the overlapping polygons without turning them back into holes. I need the holes to be preserved, and I don't have access to ArcGIS from my home computer (thanks, covid-19). Is there any way to repair this file, or something my colleague can do differently when sending it to me? Here is a link to a sample file (the original, not one processed with "Fix Geometries"): link to sample problem shapefile

1

1 Answers

2
votes

I read the shapefile in using sf and cast to polygon to try and intersect the individual polygons that were overlayed on other polygons rather than differenced. Some of those polygons seemed to have invalid geometries so it was necessary to also use lwgeom::st_make_valid(). I was then able to successfully intersect the polygons and filter those polygons that overlap.

If you wish you can also st_union the result to get back to multipolygon rather than a collection of polygons.

library(lwgeom)
library(sf)
library(dplyr)
library(ggplot2)

shp <- read_sf('AAbe.shp')

shp2 <- shp %>%
  st_cast("POLYGON")  %>%
  st_make_valid() %>%
  st_intersection() %>%
  filter(n.overlaps < 2)

ggplot(shp2) +
  geom_sf(fill = 'yellow') +
  coord_sf(xlim = c(-98, -96), ylim = c(17, 20))

zoomed in image of holes in shapefile