1

I am trying to identify the set of polygons (census tracts) that intersect another polygon (a police district). When I use the st_intersects function, I get all the polygons I expect but also a bunch of polygons on the outside the border. (In the example below, the green polygons are tracts, the red outline is the police district and the polygon labeled A is an example of a tract that st_intersect is including but I would like to exclude).

enter image description here

Similarly, if I use st_overlaps I get tracts that are both on the border outside (A) and inside (B) the district.

enter image description here

Finally, if I use st_contains, I don't get any tracts that border the district boundary, but this excludes tracts (like B above) that I would like to include.

enter image description here

Is there a way to select just those tracts that are contained within the police district but exclude those outside the border? Could I, for example, shrink the police district somehow so that the outside tracts don't touch the police district boundary at all? Or is there better way to achieve what I'm trying to do?

  • Is the northern edge of B hidden under the red border? Or does the red border cut B into two large pieces? Does the red border always follow tract borders? If it does, it might help to redo the plots with a wide line for the tracts and overlay a thin red line for the police district. – Spacedman Jan 17 at 8:33
  • Upon looking closer based on Spacedman's comment, it seems like the borders actually don't line up exactly. I'm going to experiment with scaling the district and see if sf_intersect then works. – Martin Jan 17 at 14:21
  • What about st_within? – GISKid Jan 18 at 15:09
  • @GISKid for some reason, st_within returns no tracts as within my district shape. – Martin Jan 21 at 21:28
1

In the end, it appears that the police boundaries don't line up precisely with the tracts, so that's why intersect was returning undesired tracts. I was able to get very close to my desired effect by scaling the district by 95%. Following the example in the SF vignette for affine transformations, I scaled by extracting the geometry, multiplying by my scale factor, and centering on the original centroid.

geom <- st_geometry(district_shape)
geom = (geom - st_centroid(geom)) * .95 + st_centroid(geom) 

Given the irregular shape of some of the districts, this doesn't always precisely what I'm looking for. But it gets me very close. (Below pink is scaled district, red is original district)original and scaled district

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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