# Find all census tracts N miles from a lat, lon point

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".

Ideas?

• Does "covered by a disc" mean *wholly contained within the disc" or "intersecting the disc"? – whuber Aug 22 '13 at 12:11
• 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

## 1 Answer

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

Roughly:

``````    library(rgeos)

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

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