npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

us-census-geocoder

v1.1.0

Published

Handles bulk geocoding for United States addresses with the U.S. Census Bureau's free batch geocoding API.

Downloads

303

Readme

US Census Geocoder

This TypeScript module provides a Geocoder class that geocodes United States addresses with the free U.S. Census Geocoder. It uses the batch API to process up to 10,000 addresses at a time, and provides convenient methods for large-scale batch processing of address records.

Usage

  1. Create a Geocoder instance. The most straightforward example is shown below, which queries the most recent census database.
const geocoder = new Geocoder()

The first parameter selects the census database to use (either current, 2021, or 2020). For example, new Geocoder('2020') queries address locations in the 2020 database.

You can also get the FIPS codes for the state, district, census block, and census tract, a second parameter selects the geography year to use (possible values are current, 2010, 2017, 2018, 2019, 2020, and 2021).

For example, this geocoder searches using the most recent location database, and returns FIPS codes for the state/district/census tract/census block for the year 2018.

const geocoder = new Geocoder('current', '2018')

To retrieve the most recent results for geocoding requests, use current for both parameters.

new Geocoder('current', 'current')

For most use cases, basic geocoding is all that is required for retrieving latitude/longitude - it will be a bit faster to only retrieve FIPS codes if you really need them.

  1. Add addresses to geocode with the .add function. You can add any number of addresses to the queue, and the geocoder will automatically batch them into appropriately sized requests to the U.S. Census API.

First argument is a unique ID for the address, i.e. it should be unique from all other IDs passed to .add.

const geocoder = new Geocoder()

// Add a fully-specified address
geocoder.add('some-id', {
	address: '123 Drury Lane',
	city: 'Gumdrop Forest',
	state: 'CA',
	zip: '12345'
})

// Not all address parts need to be specified
geocoder.add('another-id', {
	address: '123 Drury Lane',
	city: 'Gumdrop Forest',
	state: 'CA'
})

// Many addresses can still be geocoded with only an address and city
geocoder.add('something-else', {
	address: '123 Drury Lane',
	city: 'Gumdrop Forest'
})
  1. Call the async function geocode to submit a batch of up to 10,000 addresses.
await geocoder.geocode()

If you are geocoding more than 10,000 addresses, you should either call geocode repeatedly until .hasGeocodeBatch() returns false, or use the stream pattern shown in the basic usage pattern below.

Stream pattern

The simplest usage pattern is shown below.

import { Geocoder, Address } from 'us-census-geocoder'

async function runGeocoding() {
	const geocoder = new Geocoder()

	// Get a list of addresses for geocoding
	const addresses: Array<Address> = [ /* {}, {}, ... */ ]
	const counter = 0

	// Give each address a unique sequential ID
	for (const address in addresses) {
		geocoder.add('' + counter, address)
		counter++
	}

	// Submit a single batch of geocoding requests if less than 10,000 addresses
	const results = await geocoder.geocode()
	console.log(results[0])
	/**
		{
			"id": "unique-id",
			"lat": "32.49839",
			"lon": "-129.29839",
			"query": "123 Drury Lane, Gumdrop Forest, CA, 12345",
			"address": "123 DRURY LN, GUMDROP FOREST, CA, 12345-9876",
			"roadway": "4242424242",
			"side": "L",
			"exact": true
		}
	*/

	// You can also retrieve request responses by ID
	const specificResult = geocoder.get('15')

	// If geocoding more than 10,000 requests, use a loop to sequentially process batches of requests
	while (geocoder.hasGeocodeBatch()) {
		const batchResult = await geocoder.geocode()
		// ... process batch of results, or retrieve from cache after finishing geocoding
	}
}

Stream processing

When processing large numbers of records, the geocoding cache can grow unnecessarily large. The best pattern for processing large number of records is to call useCache(false) when initializing the geocoder, and processing each individual batch of results as they come in.

import { Geocoder } from 'us-census-geocoder'
import { v4 as uuid } from 'uuid'

const geocoder = new Geocoder()
geocoder.useCache(false)

const addressSource = [ /* ... */ ]

// Keep looping until break is called
while(true) {
	const sourceComplete = false

	// Get a full batch of geocoding requests
	while (geocoder.currentBatchSize() < geocoder.maxBatchSize()) {
		// Add addresses to the geocoder somehow (array stub shown)
		if (addressSource.length > 0) {
			geocoder.add(uuid(), { /* ... */ })
		}
		// If there are no more source addresses, set a flag to true
		else {
			sourceComplete = true
		}
	}

	const results = await geocoder.geocode()
	// ... process results

	// Break the loop after processing the final batch of results
	if (sourceComplete)
		break
}

Links