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

mongoose-quadtree

v0.1.0

Published

mongoose quadtree index

Downloads

4

Readme

Mongoose Quadtree

Build Status

Overview

Mongoose-quadtree adds quadtree abilities to a model that has an appropiate lat/lng coordinates field. Think of it as an index.

This plugin is designed to return spatial data in an organized and compressed manner. Leverging LZ-String, data can be compressed and queried and a very efficent manner.

The api returns all model data contained in the quadtree indexes leaf nodes that intersect with the specified lat/lng bounds. The data can be returned compressed (which is recommended), use LZString to decompress the data. The data is compressed in a format compatiable with localStorage (i.e packs data into uint16 bits)

All API methods return mongoose promises!

alt tag

How to use

Initilization

The first step is to include the plugin, below is an example setup.

var mongoose = require('mongoose');
var quadtree = require('../mongoose-quadtree');

var Schema = mongoose.Schema;
var ModelSchema = new Schema({
    name: {
        type: String,
        required: true
        },
     coordinates: {
         type: [Number],
        },
    }
);

ModelSchema.plugin(quadtree, {
    index: true, 
    threshold: 100,
    seperation: 0.05,
    //conn: 'mongodb://localhost/test', required if connection isn't explict
    collectionName: 'model_collection' 
});

var Model;

if (mongoose.models.Model) {
    Model = mongoose.model('Model');
} else {
    Model = mongoose.model('Model', ModelSchema, 'model_collection');
}

exports.Model = Model;

collectionName is a REQUIRED field.

The next step is to initilize the tree. Call this function somewhere after making a connection

var forceRebuild = true;
var promise = Model.initTree(forceRebuild);
promise.onResolve(function(err) {
    console.log("Tree built");
});

You have the option of always rebuilding the tree or not. I recommend keeping this active for now. Building takes a minute. You can wait on the build using the returned mongoose promise.

Querying

Model.findSubtree({'en': [7, 14], 'ws': [6, 12]})
    .onResolve(function(err, tree) {
        // Returns a quadtree with leaf nodes intersected by specified bounds
        console.log(tree);
    });

Model.findNodes({'en': [7, 14], 'ws': [6, 12]})
    .onResolve(function(err, nodes) {
        // Returns leaf nodes intersected by specified bounds
        console.log(nodes);
    });

Query the quadtree as shown above. These static methods return promises that will be fulfilled. Data in leaf nodes will be compressed using LZString.compressToUTF16.

Leaf node data is ALWAYS an array (one element array in compressed case)

Updating

Updates happen transparently on save and remove calls ;) Don't even worry about it.

Status

In development, it is safe to use now mostly, but I'd recommend rebuilding the index nightly.

TODO

Hook into mongo update calls, currently only save, remove are listened Benchmarks?