I have a water shapefile from this website: https://geodata.lib.utexas.edu/catalog/stanford-ws441bn8045

water <- st_read("data/bayarea_allwater.shp")

A part of row 216 is far away from the mainland. Is there a way to remove that?

enter image description here

I want to remove this drop which is far away from mainland California.

I tried this but it is not what I want:

water<- water[-216,]

Because it also removes part near the mainland.

  • 1
    Your original text was unclear. I've revised it to state what I think you meant, but if it's all of feature 216 that you want removed, please Edit the Question to state that instead.
    – Vince
    Commented Dec 11, 2022 at 16:59
  • the problem is when I remove 216, it also remove some part near the mainland, which should be remain
    – user215748
    Commented Dec 11, 2022 at 17:01
  • 1
    Then my edit captured what you intended. Please include this outcome in the Question body.
    – Vince
    Commented Dec 11, 2022 at 17:05

2 Answers 2


First of all, the link you provided offers you to download a shapefile named bayarea_majorwater.shp. I can't see any shapefile named bayarea_allwater.shp, which you seem to be using, but let's just assume you renamed the file manually.

Second, there are only 56 rows in the dataset. Where does row no. 216 come from?

#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

x <- read_sf("bayarea_majorwater.shp")

# 54 objects (= rows) with 6 attributes (= columns)
#> [1] 54  6

Also, neither of the attributes (id, landpoly, ...) offers you a value of 216. So, either I'm working with a subset of the actual dataset only, or there is some major confusion in the question and also in the accepted answer.

But let's assume by "drop" you mean the buffer around the couple of small islands western of San Francisco (id == 8). Removing this e.g. via dplyr::filter(x, id != 8) also removes a part near the coast, so I assume I'm not completely wrong here. This is expected behaviour so far because of MULTIPOLYGON geometry type, which you want to cast to POLYGON only before subsetting:

x_poly <- st_cast(x, "POLYGON")

# grab random coordinate from buffer manually for topological check 
p <- c(-123.06061, 37.74031) |> 
  st_point() |> 
  st_sfc(crs = "epsg: 4326") |> 
  st_transform("epsg: 26910")

# get index of polygon intersecting with p
ind <- st_intersects(x_poly, p, sparse = FALSE)

# return only polygons which do not intersect with p
result <- x_poly[!ind, ]

# inspect
st_geometry(result) |> plot()

By the way, this question is tagged r, so pointing to ArcGIS or QGIS, because it's "much easier" (this really is a matter of taste...) is not really an option.

  • many thanks, it was very helpful
    – user215748
    Commented Dec 11, 2022 at 20:50
  • 2
    Alternatively, instead of using the st_intersects() function, you could use the st_difference() function to compute the difference between the original x_poly geometry and the p point. This would return the polygons that do not intersect with p, and they would automatically be valid since they are obtained by subtracting a valid point from a valid polygon.
    – Viv
    Commented Dec 11, 2022 at 22:24

The below should help:

water <- subset(water, water$ID != 216)

For selecting a part of the shapefile using QGIS use the below steps:

  1. Open QGIS and load the shapefile.
  2. Select the polygon feature you want to edit. You can do this by clicking on the feature in the map canvas or by using the Select Features tool in the toolbar.
  3. Once the feature is selected, click on the Edit tool in the toolbar.
  4. Use the Node Tool to select the vertices of the part of the polygon you want to delete. You can do this by clicking on each vertex or by using the Select Features tool to select a group of vertices.
  5. Once the vertices are selected, press the Delete key on your keyboard to delete them. This will remove the selected part of the polygon.
  6. Click the Save Edits button in the toolbar to save your changes.

Note: These steps assume that you are using QGIS 3.x. The steps may be slightly different in earlier versions of QGIS.

  • but then also the part near the mainland remove too
    – user215748
    Commented Dec 11, 2022 at 17:56
  • The part near mainland has the same ID of 216?
    – Viv
    Commented Dec 11, 2022 at 18:13
  • Yes, that is what my problem is
    – user215748
    Commented Dec 11, 2022 at 18:14
  • In that case using select tool in any GIS software will be much easier. If you have ArcGIS licence, try that else QGIS to select the part you don't want and delete it from the shapefile.
    – Viv
    Commented Dec 11, 2022 at 18:16
  • Ah okay, many thanks
    – user215748
    Commented Dec 11, 2022 at 18:23

Your Answer

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