Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ideditor/location-conflation

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ideditor/location-conflation

Define complex geographic regions by including and excluding country codes and geojson shapes

  • 0.7.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

build npm version

location-conflation

🧩 Define complex geographic regions by including and excluding country codes and GeoJSON shapes.

What is it?

Location-conflation is a tool for generating GeoJSON features by including and excluding other locations and shapes.

⚡️ Try it now!: https://ideditor.github.io/location-conflation/

You can define a locationSet as an Object with include and exclude properties:

let locationSet = {
  include: [ Array of locations ],
  exclude: [ Array of locations ]
};

The "locations" can be any of the following:

  • Strings recognized by the country-coder library. These should be ISO 3166-1 2 or 3 letter country codes or UN M.49 numeric codes.
    Example: "de"
  • Filenames for .geojson features. If you want to use your own features, pass them to the LocationConflation constructor in a FeatureCollection - each Feature must have an id that ends in .geojson.
    Example: "de-hamburg.geojson"
  • Points as [longitude, latitude] coordinate pairs. A 25km radius circle will be computed around the point.
    Example: [8.67039, 49.41882]

Usage

To install location-conflation as a dependency in your project:

$  npm install --save @ideditor/location-conflation

location-conflation is distributed in both UMD and ES6 module formats for maxmimum compatibility. (Read more about Javascript module formats)

  • index.mjs - ES6 module
  • dist/index.js - UMD module, ES6 syntax
  • dist/index.es5.js - UMD module, ES5 syntax

Whether you require or import it, it should just work.

const LocationConflation = require('@ideditor/location-conflation');    // UMD import all
// or
import * as LocationConflation from '@ideditor/location-conflation';    // ES6 import all

You can also use location-conflation directly in a web browser. A good way to do this is to fetch the file from the jsDelivr CDN, which can even deliver minified versions.

The latest versions of many web browsers now support ES6 modules in script tags like this:

<script type="module" src="https://cdn.jsdelivr.net/npm/@ideditor/location-conflation@0.5/index.min.mjs"></script>

Older versions of modern ES6-capable browsers can still load the UMD build:

<script src="https://cdn.jsdelivr.net/npm/@ideditor/location-conflation@0.5/dist/index.min.js"></script>

Or if you need to support even older browsers like Internet Explorer, fetch the ES5 version:

<script src="https://cdn.jsdelivr.net/npm/@ideditor/location-conflation@0.5/dist/index.es5.min.js"></script>

 

Examples

const LocationConflation = require('@ideditor/location-conflation');
const myFeatures = require('./path/to/FeatureCollection.json');   // optional
const loco = new LocationConflation(myFeatures);
Southern Europe:
let locationSet = { include: ['039'] };    // 039 = Southern Europe
let result = loco.resolveLocationSet(locationSet);
Southern Europe
Southern Europe and Northern Africa:
let locationSet = { include: ['039','015'] };   // 015 = Northern Africa
let result = loco.resolveLocationSet(locationSet);
Southern Europe and Northern Africa
Southern Europe and Northern Africa, excluding Egypt and Sudan:
let locationSet = { include: ['039','015'], exclude: ['eg','sd'] };
let result = loco.resolveLocationSet(locationSet);
Southern Europe and Northern Africa, excluding Egypt and Sudan
The Alps, excluding Liechtenstein and regions around Bern and Zürich
let result = loco.resolveLocationSet({ include: ['alps.geojson'], exclude: ['li', [8.55,47.36], [7.45,46.95]] });
The Alps, excluding Liechtenstein and regions around Bern and Zürich

 

API Reference

 

# const loco = new LocationConflation(featureCollection)

Constructs a new LocationConflation instance.

Optionally pass a GeoJSON FeatureCollection of known features which can be used later as locations.

Each feature must have a filename-like id, for example: example.geojson

{
  "type": "FeatureCollection"
  "features": [
    {
      "type": "Feature",
      "id": "example.geojson",
      "properties": { … },
      "geometry": { … }
    }
  ]
}

 

# loco.validateLocation(location)

Validates a given location. The "locations" can be:

  • Points as [longitude, latitude] coordinate pairs. Example: [8.67039, 49.41882]
  • Filenames for known .geojson features. Example: "de-hamburg.geojson"
  • Strings recognized by the country-coder library. Example: "de"

If the location is valid, returns a result Object like:

{
  type:     'point', 'geojson', or 'countrycoder'
  location:  the queried location
  id:        the stable identifier for the feature
}

If the location is invalid,

  • in strict mode, throws an error
  • in non-strict mode, returns null

 

# loco.validateLocationSet(locationSet)

Validates a given locationSet. Pass a locationSet Object like:

{
  include: [ Array of locations ],
  exclude: [ Array of locations ]
}

If the locationSet is valid, returns a result Object like:

{
  type:         'locationset'
  locationSet:  the queried locationSet
  id:           the stable identifier for the feature
}

If the locationSet is invalid or contains any invalid locations,

  • in strict mode, throws an error
  • in non-strict mode, invalid locations are quietly ignored. A locationSet with nothing included will be considered valid, and will validate as if it were a locationSet covering the entire world. { type: 'locationset', locationSet: ['Q2'], id: +[Q2] }

 

# loco.resolveLocation(location)

Resolves a given location into a GeoJSON feature. This is similar to validateLocation, but runs slower and includes the actual GeoJSON in the result. Results are cached, so if you ask for the same thing multiple times we don't repeat the expensive clipping operations.

The returned GeoJSON feature will also have an area property containing the approximate size of the feature in km². (This is helpful for sorting features)

If the location is valid, returns a result Object like:

{
  type:     'point', 'geojson', or 'countrycoder'
  location:  the queried location
  id:        the stable identifier for the feature
  feature:   the resolved GeoJSON feature
}

If the location is invalid,

  • in strict mode, throws an error
  • in non-strict mode, returns null

 

# loco.resolveLocationSet(locationSet)

Resolves a given locationSet into a GeoJSON feature. This is similar to validateLocationSet, but runs slower and includes the actual GeoJSON in the result. Results are cached, so if you ask for the same thing multiple times we don't repeat the expensive clipping operations.

The returned GeoJSON feature will also have an area property containing the approximate size of the feature in km². (This is helpful for sorting features)

If the locationSet is valid, returns a result Object like:

{
  type:         'locationset'
  locationSet:  the queried locationSet
  id:           the stable identifier for the feature
  feature:      the resolved GeoJSON feature
}

If the locationSet is invalid or contains any invalid locations,

  • in strict mode, throws an error
  • in non-strict mode, invalid locations are quietly ignored. A locationSet with nothing included will be considered valid, and will validate as if it were a locationSet covering the entire world. { type: 'locationset', locationSet: ['Q2'], id: +[Q2] }

 

# loco.strict(val)

Get/set "strict mode". New instances of LocationConflation start out in strict mode by default.

  • In strict mode, any invalid location or locationSet throws an error.
  • In non strict mode, invalid locations are ignored, and locationSets that include nothing are assumed to include the entire world.
loco.strict(false);                // pass a true/false value to set the strict mode
const isStrict = loco.strict();    // pass no value to return the current value

 

# loco.stringify(object, options)

Convenience method that wraps json-stringify-pretty-compact to stringify the given object. Optional options parameter gets passed through to json-stringify-pretty-compact.

loco.stringify(someGeoJson, { maxLength: 100 });    // Make it pretty!

# loco.cache()

Convenience method to access the internal feature _cache. You probably shouldn't use it except for debugging.

 

Contributing

Prerequisites

Installing

  • Clone this project, for example: git clone git@github.com:ideditor/location-conflation.git
  • cd into the project folder,
  • Run npm install to install libraries

Building

  • npm run build

Thanks!

location-conflation is really just a wrapper around these other great projects:

License

This project is available under the ISC License. See the LICENSE.md file for more details.

Keywords

FAQs

Package last updated on 30 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc