Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support US Census Reverse Geocoding #172

Open
jessecambon opened this issue Aug 2, 2022 · 5 comments
Open

Support US Census Reverse Geocoding #172

jessecambon opened this issue Aug 2, 2022 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jessecambon
Copy link
Owner

The US Census service supports reverse geocoding through searchtype='coordinates'. See code example and data output format here.

We could potentially support this in tidygeocoder, but the output format is quite different from other geocoding services so it would require some data formatting to work properly within the package.

@jessecambon jessecambon added the enhancement New feature or request label Aug 2, 2022
@LizBlackman
Copy link

Hi Jesse,
Is this enhancement going to go live anytime soon?

@jessecambon
Copy link
Owner Author

jessecambon commented Jan 26, 2023

Hi @LizBlackman, unfortunately I don't think I'll have availability in the near future so probably not. However, you are welcome to install the branch I was working with and try it out:

devtools::install_github("jessecambon/tidygeocoder", ref = "census-reverse-geocoding")

You should then be able to use method="census" in reverse_geo() or reverse_geocode(). I was conflicted on how or if to implement this capability, because the Census reverse geocoder provides data in a very different format than the other supported services (multiple rows for each coordinate where each row is a different type of geography). You can see an example with a linked script here: #165 (comment)

Alternatively, you could use the script referred to in the link above to reverse geocode with the census service directly (ie. not using tidygeocoder). Hope that helps and feel free to give some feedback on output data format/etc.

@jessecambon jessecambon added the help wanted Extra attention is needed label Jan 26, 2023
@LizBlackman
Copy link

Hi @jessecambon!

Thank you for the quick response!

I worked on this for quite some time on Friday and went through stack overflow to find a way to clean up the output that I got back from the reversegeo() function. i needed to manually geocode a ~3200 lat/long combos. I did this thorugh a loop and got quite a few lists. From this large list i had to simplify that data to get teh CTs that i needed. While it is not clean, i got what i needed from the output. See my code below:
`GEOID1 <- apply(lat_long1, 1, function(row) reverse_geo(row['Latitude'], row['Longitude'], method="census", full_results=TRUE, custom_query = list(benchmark= "Public_AR_Census2020", vintage="Census2010_Census2020")))

alltypes<- unique(unlist(sapply(c(1:length(GEOID1)), function(x){
unlist(lapply(GEOID1[[x]]$geography, function(l) l[[1]]))
})))

all.values <- lapply(c(1: length(GEOID1)), function(x){
types <- unlist(lapply(GEOID1[[x]]$geography, function(l) l[[1]]))
matches <- match(alltypes, types)
values <- GEOID1[[x]]$geoid[matches]
})

all.values <- do.call("rbind", all.values)
all.values <- as.data.frame(all.values)
names(all.values) <- alltypes

lat_long1[, alltypes]<-NA
lat_long1[,alltypes]<- all.values
lat_long1 <-lat_long1 %>%
rename(GEOID='Census Tracts') %>%
select(TempID:census_block,
GEOID)`

Assistance came from this stack overflow entry

@jessecambon
Copy link
Owner Author

Awesome, glad you got it working. I'm assuming you may not be able to share these coordinates you are using, but could you possibly provide an end-to-end example that I could run with a few sample coordinates that shows the end data format? You could use something like reprex or whatever is easiest.

Also, just to confirm - you are using the census-reverse-geocoding branch version of tidygeocoder, correct?

@LizBlackman
Copy link

LizBlackman commented Jan 31, 2023

Hi Jesse,

I learned how to use reprex just for you. Let me know if what i shared works, it should...

test_reverse.csv

lat_long1<- read.csv("test_reverse.csv")

devtools::install_github("jessecambon/tidygeocoder", ref = "census-reverse-geocoding")
library(tidygeocoder)




GEOID1 <- apply(lat_long1, 1, function(row) reverse_geo(row['Latitude'], row['Longitude'],  method="census", full_results=TRUE, custom_query = list(benchmark= "Public_AR_Census2020", vintage="Census2010_Census2020")))

alltypes<- unique(unlist(sapply(c(1:length(GEOID1)), function(x){
  unlist(lapply(GEOID1[[x]]$geography, function(l) l[[1]]))
})))

all.values <- lapply(c(1: length(GEOID1)), function(x){
  types <- unlist(lapply(GEOID1[[x]]$geography, function(l) l[[1]]))
  matches <- match(alltypes, types)
  values <- GEOID1[[x]]$geoid[matches]
})

all.values <- do.call("rbind", all.values)
all.values <- as.data.frame(all.values)
names(all.values) <- alltypes

lat_long1[, alltypes]<-NA
lat_long1[,alltypes]<- all.values

view(lat_long1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants