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

tree-geocoder

v0.3.2

Published

A Node.js CLI and library for geocoding based on GeoNames data and the TREE prefix traversal approach

Downloads

6

Readme

tree-geocoder

npm

A command line tool and library for geocoding based on the TREE traversal approach over GeoNames and OSMNames data. The tool matches GeoNames and OSMNames entities using either prefix or suffix-based approaches. To foster interoperability, data entities are modeled using a JSON-LD seralization based on the GeoNames Ontology and the OpenStreetMap Vocabulary respectively.

Prefix-based geocoding means that, querying for gent will result in entities like Geonames's Gent, Gentinnes, Gentbrugge and OSM's Gent, Genté, Gentil etc. On the other hand, suffix-based geocoding for terdam will result in entities such as Rotterdam, Amsterdam, Achterdam and also fuzzy matched results like Overdam and Veerdam.

The tool assigns a score to every matched entity based on the Sørensen–Dice coefficient, which is calculated using the string-similarity library.

Results can be obtained as soon as possible in a streaming and unsorted way using the --streaming option (see example below) or can be awaited for a complete answer that will be sorted by score.

Furthermore, this tool supports filtering options based on the minimum accepted score (e.g., --minScore 0.7) and entity properties (e.g., only entities that represent administrative regions: --filter "geonames:featureClass geonames:A, osm:boundary osm:Administrative" ) .

Requirements

This tool requires Node.js v12.x or superior.

Geographic coverage

Up until now, we support the following countries to perform geocoding queries:

  • Prefix queries: Austria, Belgium, Denmark, France, Germany, Great Britain, Greece, Italy, Luxembourg, Spain, Switzerland, The Netherlands and USA.
  • Suffix queries: Belgium, France and The Netherlands.

Command line interface

Install it:

npm install -g tree-geocoder

Use the help option -h to see what are the possible uses:

tree-geocoder -h
Usage: tree-geocoder [options] <query>

Options:
  --maxResults, <maxResults>  Maximum amount of desired results
  --minScore <minScore>       Minimum accepted score (based on Dice's coefficient) for matched results (value between 0 and 1)
  --mode, <mode>              Select "prefix" or "suffix" for specific matching mode. Suffix-based matching will be done by default
  --filter, <filter>          Enter "predicate object" pairs (within double quotes and separated by comma) that would be matched over found entities. E.g., "geonames:featureClass geonames:A, osm:boundary osm:Administrative, osm:hasTag 'addr:country=BE'"
  --streaming                 Get streaming results. Sorting by string similarity cannot be guaranteed with streaming results
  -h, --help                  display help for command

Use it for a prefix-based geocoding query:

tree-geocoder --mode prefix --maxResults 5 your_query

Use it for a suffix-based geocoding query:

tree-geocoder --maxResults 10 your_query

Get results in a streaming way and pipe them into other applications:

tree-geocoder --streaming your_query | other_awesome_application

Filter results based on entity properties and string similarity. In this example we filter to have only streets and roads (of all kinds) having a score of 0.6 or higher:

tree-geocoder --minScore 0.6 --filter "geonames:featureClass geonames:R, osm:highway '*'" your_query

A more specific filter for only GeoNames water streams and OSM residential roads in France:

tree-geocoder --filter "geonames:featureCode geonames:H.STM, geonames:countryCode geonames:FR, osm:highway osm:Residential, osm:hasTag 'addr:country=FR'" your_query

Refer to the documentation of Geonames classes and OSM tags for more information on what is possible in the filtering option.

As an example on how the results of a query look see the following query for entbru which will include the following:

tree-geocoder entbru
[
  {
      "@id": "http://www.openstreetmap.org/way/7523153",
      "https://w3id.org/openstreetmap/terms#name": "Meentbrug",
      "score": 0.7692307692307693,
      "https://w3id.org/openstreetmap/terms#highway": "https://w3id.org/openstreetmap/terms#Unclassified",
      "http://www.w3.org/2003/01/geo/wgs84_pos#lat": 5.19221665E1,
      "http://www.w3.org/2003/01/geo/wgs84_pos#long": 4.4825865E0,
      "http://www.opengis.net/ont/geosparql#asWKT": "POINT (4.4825865 51.9221665)",
      "https://w3id.org/openstreetmap/terms#hasTag": "addr:country=NL",
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": "https://w3id.org/openstreetmap/terms#Way"
  },
  {
      "@id": "https://sws.geonames.org/2797652",
      "http://www.geonames.org/ontology#countryCode": "http://www.geonames.org/ontology#BE",
      "score": 0.7142857142857143,
      "http://www.geonames.org/ontology#featureClass": "http://www.geonames.org/ontology#P",
      "http://www.geonames.org/ontology#featureCode": "http://www.geonames.org/ontology#P.PPL",
      "http://www.geonames.org/ontology#name": "Gentbrugge",
      "http://www.opengis.net/ont/geosparql#asWKT": "POINT (3.76509 51.03692)",
      "http://www.w3.org/2000/01/rdf-schema#isDefinedBy": "https://sws.geonames.org/2797652/about.rdf",
      "http://www.w3.org/2003/01/geo/wgs84_pos#lat": 5.103692E1,
      "http://www.w3.org/2003/01/geo/wgs84_pos#long": 3.76509E0,
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": "http://www.geonames.org/ontology#Feature"
  }
  ...
]

Library

This tool can be used both in the backend (Node.js) and in the browser by means of a tool such as Webpack.

Install it in your project npm install tree-geocoder.

The TreeGeocoder class exposes the AsyncGenerator function geocode(), which can be used as follows:

Use it synchronously and get sorted (by score) results:

const TreeGeocoder = require("tree-geocoder");

async function runQuery() {
    const tgc = new TreeGeocoder();
    const opts = {
        query: "your_query",
        minScore: 0.6 // Optional. Values between 0 and 1 (higher means more similar)
        maxResults: 10, // Optional
        mode: "prefix", // Or don't define to get suffix-based results
        filter: { // Optional. Define the predicate-object pairs that will be matched against each type of entity
            "https://w3id.org/openstreetmap/terms#": { // Rules for OSM entities
                "https://w3id.org/openstreetmap/terms#highway": "https://w3id.org/openstreetmap/terms#Motorway",
                "https://w3id.org/openstreetmap/terms#hasTag": "addr:country=BE"
            },
            "http://www.geonames.org/ontology#": { // Rules for GeoNames entities
                "http://www.geonames.org/ontology#featureClass": "http://www.geonames.org/ontology#P",
                "http://www.geonames.org/ontology#countryCode": "http://www.geonames.org/ontology#BE"
            }
        }
    };

    const results = (await tgc.geocode(opts).next()).value;
    console.log(results);
}

runQuery();

Or consume it as a stream and get (unsorted) results as soon as possible:

const TreeGeocoder = require("tree-geocoder");

async function runQuery() {
    const tgc = new TreeGeocoder();
    const opts = {
        query: "your_query",
        minScore: 0.6, // Optional. Values between 0 and 1 (higher means more similar)
        maxResults: 100, // Optional
        streaming: true,
        filter: { // Optional. Define the predicate-object pairs that will be matched against each type of entity
            "https://w3id.org/openstreetmap/terms#": { // Rules for OSM entities
                "https://w3id.org/openstreetmap/terms#highway": "https://w3id.org/openstreetmap/terms#Motorway",
                "https://w3id.org/openstreetmap/terms#hasTag": "addr:country=BE"
            },
            "http://www.geonames.org/ontology#": { // Rules for GeoNames entities
                "http://www.geonames.org/ontology#featureClass": "http://www.geonames.org/ontology#P",
                "http://www.geonames.org/ontology#countryCode": "http://www.geonames.org/ontology#BE"
            }
        }
    };

    for await (const result of tgc.geocode(opts)) {
        console.log(result);
    }
}

runQuery();