Using R, I'd like to find all census tracts within a given distance of a point (lon,lat).

i.e. All census tracts within 20 miles of 34.0522, 118.2428

I have used the sp library and over command to locate the tract of a given point, but would like to use it to find all the tracts covered by a "disc".


  • Does "covered by a disc" mean *wholly contained within the disc" or "intersecting the disc"? – whuber Aug 22 '13 at 12:11
  • 1
    The end goal is all census tracts within N miles of a point. So, I assume that every tract touched by any part of the dist is included – user21067 Aug 22 '13 at 17:15

I would suggest to use the rgeos library. You could use the gIntersects function as well as gBuffer to create your "disc".



    con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/CHE_adm2.RData")

    pt1 = readWKT("MULTIPOINT(8.5 47)") # some location
    buf = gBuffer(pt1, width=0.2)  # the "disc" - width is in projection units
    proj4string(buf) = proj4string(gadm)

    intersects = gIntersects (buf, gadm, byid=TRUE) 
    selection = subset(gadm, as.vector(intersects))

    plot (gadm)
    plot (selection, add=T, col="red") 
    plot (buf, add=T, lwd = 3)
  • Nice suggestion. I have the shape files from the us census and use the "over" command in R to find the specific tract for a given point. How would I integrate that shape file into your proposed solution? Thanks! – user21067 Aug 22 '13 at 17:16
  • I edited my code above to correspond more closely to your question. Based on your answer to the comment above I have also used the gIntersects function here. You can use the byid=True option to loop through the polygons in your SpatialPolygons object (which I assume you generated from your original shape file when reading it into R). – cengel Aug 23 '13 at 11:09

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.