Giter Site home page Giter Site logo

piklowry / geo2zip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blakek/geo2zip

0.0 1.0 0.0 1.35 MB

๐ŸŒŽ๐Ÿ” Quickly find the the nearest U.S. ZIP codes to a latitude/longitude geolocation using k-nearest neighbors

License: MIT License

JavaScript 8.02% TypeScript 91.98%

geo2zip's Introduction

geo2zip

๐ŸŒŽ๐Ÿ” Quickly find the the nearest U.S. ZIP codes to a latitude/longitude geolocation using k-nearest neighbors

Travis

Takes a geolocation and returns the nearest US ZIP codes.

โš ๏ธ NOTE: The top result may not return the actual ZIP code containing the location. This library returns the ZIP code where the approximate center of the ZIP is nearest the given location. Larger ZIP code areas may show lower in the results when searching nearby points because the center can be so far away. If you're relying on something as inaccurate as ZIP codes, the top result is likely close enough.

Also, ZIP codes change. They change in shape and location. New ones are created and old ones are removed. They're even recycled. If you know of a reliable, more up-to-date list of ZIP codes, please let us know by opening an issue.

Can I use this in the browser? Yes but please don't. This is made to run on a Node.js server. This depends on a roughly 2 MB list of US ZIP codes and sorts those in-memory. Please don't do that to my phone. If you're experimenting or know what you're doing, use this wherever and however it works for you.

Isn't sorting and filtering on a 2MB file slow in JavaScript? No. A lookup is created when the main file is imported/required. After the lookup is created, you should be able to run the examples in this README millions of times per second; showing output would be the limit. The tradeoff is your server may take a 1-2 seconds longer to start up initially (e.g. from npm run start).

Install

With Yarn or npm installed, run:

yarn add geo2zip

# ...or, if using `npm`
npm install geo2zip

Usage

Get the nearest ZIP to a location:

import geo2zip from 'geo2zip';

const location = {
  latitude: 34.659698,
  longitude: -88.242903
};

const closestZip = await geo2zip(location);

console.log(closestZip); // ['38873']

Get the closest 5 ZIPs to a location:

import geo2zip from 'geo2zip';

const location = {
  latitude: 34.659698,
  longitude: -88.242903
};

await geo2zip(location, { limit: 5 });
// [ '38873', '38838', '38827', '38859', '38852' ]

API

geo2zip(location, options)

location

Type: Object

A geolocation with valid latitude and longitude properties.

options

Type: Object

Properties:

  • limit - the number of results to return
    • Type: Number
    • Default: 1

See Also

  • standardize-geolocation - takes geolocations of different formats and outputs a standardized version
  • sphere-knn - find the k nearest neighbors for points on a sphere
  • us-zips - a list of US ZIP codes and their geolocations

License

MIT

geo2zip's People

Contributors

blakek avatar greenkeeper[bot] avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.