Home

Awesome

build npm version

location-conflation

🧩 Define complex geographic regions (geofences) 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.

It is useful for generating geofences in a declarative way, so your application doesn't need to bundle or fetch so much geodata.

⚡️ Try it now!: https://location-conflation.com/

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

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

The "locations" can be any of the following:

Installing

Use in Node

npm install @rapideditor/location-conflation

location-conflation is distributed in CJS and ESM module formats for maxmimum compatibility. (Read more about Javascript module formats)

const LocationConflation = require('@rapideditor/location-conflation').default;  // require CJS
// or
import { LocationConflation } from '@rapideditor/location-conflation';   // import ESM

Use in Browsers

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

When you load this file in a <script> tag, you'll get a LocationConflation global to use elsewhere in your scripts:

<head>
<script src="https://cdn.jsdelivr.net/npm/@rapideditor/location-conflation@1.4/dist/location-conflation.iife.min.js"></script>
</head>
…
<script>
  var loco = new LocationConflation.default();
</script>

👉 This project uses modern JavaScript syntax for use in supported node versions and modern browsers. If you need support for legacy environments like ES5 or Internet Explorer, you'll need to build your own bundle with something like Babel.

 

Examples

import { LocationConflation } from '@rapideditor/location-conflation';
import myFeatures from './fixtures/features.json' assert {type: 'json'};  // optional
const loco = new LocationConflation(myFeatures);

Southern Europe:

const locationSet = { include: ['039'] };    // 039 = Southern Europe
const result = loco.resolveLocationSet(locationSet);
<img width="800px" alt="Southern Europe" src="https://raw.githubusercontent.com/rapideditor/location-conflation/main/docs/images/example1.png"/>

Southern Europe and Northern Africa:

const locationSet = { include: ['039','015'] };   // 015 = Northern Africa
const result = loco.resolveLocationSet(locationSet);
<img width="800px" alt="Southern Europe and Northern Africa" src="https://raw.githubusercontent.com/rapideditor/location-conflation/main/docs/images/example2.png"/>

Southern Europe and Northern Africa, excluding Egypt and Sudan:

const locationSet = { include: ['039','015'], exclude: ['eg','sd'] };
const result = loco.resolveLocationSet(locationSet);
<img width="800px" alt="Southern Europe and Northern Africa, excluding Egypt and Sudan" src="https://raw.githubusercontent.com/rapideditor/location-conflation/main/docs/images/example3.png"/>

The Alps, excluding Liechtenstein and regions around Bern and Zürich

const result = loco.resolveLocationSet({ include: ['alps.geojson'], exclude: ['li', [8.55,47.36], [7.45,46.95]] });
<img width="800px" alt="The Alps, excluding Liechtenstein and regions around Bern and Zürich" src="https://raw.githubusercontent.com/rapideditor/location-conflation/main/docs/images/example4.png"/>

 

API Reference

 

<a name="constructor" href="#constructor">#</a> const <i>loco</i> = new <b>LocationConflation</b>(<i>featureCollection</i>)

Constructs a new LocationConflation instance.

Optionally pass a GeoJSON FeatureCollection of custom features which can be referred to later as locations.

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

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

 

<a name="validateLocation" href="#validateLocation">#</a> <i>loco</i>.<b>validateLocation</b>(<i>location</i>)

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

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,

 

<a name="validateLocationSet" href="#validateLocationSet">#</a> <i>loco</i>.<b>validateLocationSet</b>(<i>locationSet</i>)

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,

 

<a name="resolveLocation" href="#resolveLocation">#</a> <i>loco</i>.<b>resolveLocation</b>(<i>location</i>)

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,

 

<a name="resolveLocationSet" href="#resolveLocationSet">#</a> <i>loco</i>.<b>resolveLocationSet</b>(<i>locationSet</i>)

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,

 

<a name="strict" href="#strict">#</a> <i>loco</i>.<b>strict</b>(<i>val</i>)

Getter/setter for "strict mode". Current versions of LocationConflation start out in strict mode by default.

loco.strict = false;            // setter: pass a true/false value to set the strict mode
const isStrict = loco.strict;   // getter: return the current value

 

<a name="stringify" href="#stringify">#</a> <i>loco</i>.<b>stringify</b>(<i>object</i>, <i>options</i>)

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!

 

Contributing

Prerequisites

Installing

Building

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.