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 🙏

© 2025 – Pkg Stats / Ryan Hefner

polytoken

v2.0.0

Published

Efficient database indexing for geospatial queries

Downloads

12

Readme

polytoken

Polytoken is a utility that handles the hashing of one or more arbitrary data dimensions, for use in database indexing and queries.

Usage

const polytoken = require('polytoken');

let longLatDimension = new polytoken.coreDimensions.LongLatDimension({
	step: {
		type: 'exponential',
		base: 0.001,  // Degrees of latitude/longitude
		multipler: 10,
		stepNum: 5
	}
});

let timeDimension = new polytoken.coreDimensions.TimeDimension({
	step: {
		type: 'exponential',
		base: 1000,  // Seconds
		multipler: 5,
		stepNum: 4
	}
});

let polytype = new polytoken.Polytype([ longLatDimension, timeDimension ]);

// Range to tokenize
let polygon = {
	type: 'Polygon',
	coordinates: [ [ [ 1, 1 ], [1, 2 ], [2, 2], [2, 1], [1, 1] ] ]
};
let timeRange = [ '2015-01-01', '2015-02-28' ];
let rangeTokens = polytype.getRangeTokens([ polygon, timeRange ]);
// Save rangeTokens on database object

// Point to tokenize
let point = [ 1.5, 1.6 ];
let time = '2015-01-05T00:05:35Z';
let pointTokens = polytype.getTokensForPoint(point, time);
// Use pointTokens to query against rangeTokens for matching database entries

// Do this to filter out returned database entries
let pointTuple = [ /* Point from query */, /* Time from query */ ];
let rangeTuples = [
	[ /* Polygon from document 0 */, /* Time range from document 0 */],
	/* ... */
];
rangeTuples = rangeTuples.filter((rangeTuple) => polytype.checkRangeInclusion(rangeTuple, pointTuple);

Glossary

  • Dimension - A single 'type' of data. For example, the LongLat dimension handles GeoJSON data that represents data on a 2D geographic plane, and the Time dimension handles temporal data.
  • Point - A single discrete point on the dimension. This would be a longitute / latitude pair for LongLat, and a date for Time.
  • Range - A specification of a (usually infinite) set of points on a dimension. This is a Polygon or MultiPolygon object for LongLat, and a start and end time pairing for Time.
  • Polytype - A class that combines multiple dimensions together.
  • Token - A string that represents a specific range for a dimension.
  • Polytoken - A combination of tokens from multiple dimensions.

Principles

Polytoken is designed to be a geohashing library extensible into multiple and arbitrary dimensions. When only a single LongLat dimension is passed into the Polytype, it will act as a simple geohasher. If both a LongLat and Time dimension are passed, it can hash data that takes place over both an area and range of time (for example, a parade). The Dimension class can also be extended to create new dimensions for a specific purpose.

The options argument to each Dimension specifies how tokens will be generated. Usually, an exponentially increasing range of token sizes is desired, and the fields under step specify how this is generated. An example object:

{
	step: {
		type: 'exponential',
		base: 0.001,  // The smallest token size
		multipler: 10 // Each token size will be this many times larger than the previous one
		stepNum: 5 // The total number of token sizes
	}
}

These parameters should be chosen carefully. Smaller values for multiplier and larger values for stepNum will yield greater accuracy in token generation, at the expense of more tokens being generated by getTokensForPoint, which will linearly increase the execution times of queries.