Git Product home page Git Product logo

find-store's Introduction

Find the nearest store

This is a simple example API to find the nearest store given an address or a zip code. The problem can be broken down in two parts, geocoding and spatial index.

Geocoding

The first step to solve the problem is to use a geocoding service to get the coordinates (latitude and longitude) of a given address or zip code.

There are multiple geocoding services available, probably the most famous is the Google Geocoding API, but for this project, I'm using the HERE Geocoder API.

The service will take the address or zip code as input and will return, among other things, the coordinates that we need for the next step.

Spatial index

The second step is to query a predefined dataset to find the nearest store to the coordinates obtained from the previous step. To do that efficiently, we need to build a spatial index, otherwise, we would have to scan to whole dataset every time.

As this is just a simple test, and we don't have a big dataset (less than 2000 stores), I'm loading the CSV file and building that index on memory, using the kdbush libray which creates the index based on a flat KD-tree.

To find the nearest store, I'm using the geokdbush library, a kdbush extension which provides a method to return the closest points from a given location, based on a kdbush index.

In a production environment with a large dataset, we would need a database to persist the stores. In this case, the idea is still the same, creating a spatial index, but that should be supported by the database. If using Postgres, for example, we could use PostGIS which is spatial database extender for PostgreSQL.

Live Demo

A live demo could be found here:

https://findstore-demo.herokuapp.com/closest

Parameters

Parameter Notes
address Free-form text containing address elements.
zip Numeric zip code, e.g. 12345678. Dashes will be automatically stripped.
units Whether to display units in miles ("mi") or kilometers ("km"). Default: "mi".

* address and zip cannot be used simultaneously, but one of them is required.

Running the project locally

After cloning the project, first install the dependencies:

npm install

To run the project in your local machine, you're going to need a HERE account. Create an .env file inside of the src/config directory, following the structure of the .env.example file, using the APP ID and APP CODE that you can find on your HERE dashboard.

Then, simply start the project:

npm start

Running the tests

To run the tests, HERE credentials are not required. Just run:

npm test

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.