Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

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://www.filefactory.com/file/1odb3hx60gwf/n/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)
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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