TransWikia.com

API to reverse geocode latitude, longitude to census tract?

Geographic Information Systems Asked by Citizentools on April 27, 2021

I have a bunch of latitude,longitude coordinates (mostly city/town centroids) in the United States for which I’d like to find the containing census tract.

For instance,

input => nn.nnn, -nn.nnn 
output => 010300

Is there a free or low-cost API out there that can help? (Essentially Yahoo Placefinder, with the additional response of census tract.)

(I understand that many cities include multiple census tracts and I’d expect that some small towns contain only parts of one or more census tracts. I’m comfortable ignoring that issue for now.)

5 Answers

The FCC census block conversion API is exactly what you're looking for.

Correct answer by tmcw on April 27, 2021

You could do it locally instead of an online API. One solution with R: Census boundary data for the USA is available through TIGER at census.gov; if you're thinking about the US then you could download the US states and use a function to get the tracts. For example, using California and a random (or random-ish) point:

library(maps)
library(maptools)


tractLookup <- function(x, y, state) {
  pt <- SpatialPoints(data.frame(x = x, y = y))
  overlay.pt <- overlay(pt, state) # what index number does pt fall inside?
  return(census$TRACT[overlay.pt]) # give the Tract number from the census layer
}

Try it out:

california <- readShapePoly("~/Downloads/US_2000_Census_Tracts/tr06_d00_shp/tr06_d00.shp")
tractLookup(-123.123, 40.789, california)

Gives 0004, which is correct.

# Look at the map
plot(census)
map('state', c('California'), lwd = 2, col = 'green', add = F) # optional
points(-123.123, 40.789, col = 'red', lwd = 2)

This works, but takes 5 seconds on my Mac, which would probably be too much if you're going to do a lot of this; I suspect someone will weigh in shortly with a PostGIS solution that will be a million times quicker ...

Answered by Simbamangu on April 27, 2021

In PostGIS, if you have a table of points, and the census boundary information that @Sminbamangu refers to you could calculate this using the following approach:

SELECT 
       c.census_tract, 
       p.point_id
FROM
       census_boundary as c,
       table_of_points as p
WHERE
       ST_Contains(c.geom, p.geom);

You can see the description ST_Contains here. This would return a table of the points, and the census tract where they are located.

Answered by djq on April 27, 2021

As mentioned by @Simbamangu, this could easily done locally by just about any desktop GIS software on the market.

For free & open source, take a look at a "join attributes by location" in QGIS.

If you plan to need to do this repeatedly over time, you could consider putting the datasets into PostGIS and creating a spatial join (see this example in the online help). You can then visualize the results in QGIS.

Answered by RyanKDalton on April 27, 2021

You can use Geocodio to convert latitude/longitude coordinates to FIPS codes.

We don't have a reverse geocooding to FIPS example in our docs, but it works the same way as the forward endpoint. You can see an example here (note: this DEMO API key has a limit per day, so you may need to create a free API key to see this. The first 2,500 lookups per day are free, no credit card required.)

https://api.geocod.io/v1.6/reverse?q=38.9002898,-76.9990361&&fields=census&api_key=DEMO

You can also see all of the data returned here: https://www.geocod.io/docs/?python#census-block-tract-fips-codes-amp-msa-csa-codes

Disclosure: I am one of the founders of Geocodio.

Answered by Michele Hansen on April 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP