Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@rapideditor/location-conflation

rapideditor2.2kISC1.5.0

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

geofence, location, locationset, include, exclude, geojson

readme

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:

  • Strings recognized by the country-coder library.
    These include ISO 3166-1 2 or 3 letter country codes, UN M.49 numeric codes, and supported Wikidata QIDs.
    Examples: "de", "001", "conus", "gb-sct", "Q620634"
    👉 A current list of supported codes can be found at https://ideditor.codes

  • Filenames for custom .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.
    Examples: "de-hamburg.geojson", `"newjersey.geojson"`_

  • Circular areas defined as [longitude, latitude, radius?] Array.
    Radius is specified in kilometers and is optional. If not specified, it will default to a 25km radius.
    Examples: [8.67039, 49.41882], [-88.3726, 39.4818, 32]

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.5/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' with {type: 'json'};  // optional
const loco = new LocationConflation(myFeatures);

Southern Europe:

const locationSet = { include: ['039'] };    // 039 = Southern Europe
const result = loco.resolveLocationSet(locationSet);
Southern Europe

Southern Europe and Northern Africa:

const locationSet = { include: ['039','015'] };   // 015 = Northern Africa
const result = loco.resolveLocationSet(locationSet);
Southern Europe and Northern Africa

Southern Europe and Northern Africa, excluding Egypt and Sudan:

const locationSet = { include: ['039','015'], exclude: ['eg','sd'] };
const result = loco.resolveLocationSet(locationSet);
Southern Europe and Northern Africa, excluding Egypt and Sudan

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]] });
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 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": { … }
    }
  ]
}

 

# loco.validateLocation(location)

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

  • Strings recognized by the country-coder library.
    👉 A current list of supported codes can be found at https://ideditor.codes
    Examples: "de", "001", "conus", "gb-sct", "Q620634"

  • Filename-like identifiers of custom .geojson features.
    Examples: "de-hamburg.geojson", `"newjersey.geojson"`_

  • Circular areas defined as [longitude, latitude, radius?] Array.
    Radius is specified in kilometers and is optional. If not specified, it will default to a 25km radius.
    Examples: [8.67039, 49.41882], [-88.3726, 39.4818, 32]

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)

Getter/setter for "strict mode". Current versions 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;            // setter: pass a true/false value to set the strict mode
const isStrict = loco.strict;   // getter: 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!

 

Contributing

Prerequisites

  • Node.js version 18 or newer
  • git for your platform

Installing

  • Clone this project, for example: git clone git@github.com:rapideditor/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.

changelog

What's New

location-conflation is an open source project. You can submit bug reports, help out, or learn more by visiting our project page on GitHub: :octocat: https://github.com/rapideditor/location-conflation

Please star our project on GitHub to show your support! ⭐️

Breaking changes, which may affect downstream projects, are marked with a ⚠️

1.5.0

2025-Apr-28
  • Drop support for Node 18
  • Update country-coder to v5.4.0

1.4.1

2024-Dec-13
  • Update country-coder to v5.3.1

1.4.0

2024-Jul-17
  • Update country-coder to v5.3.0

1.3.0

2023-Sep-18
  • ⚠️ Use a named export for ESM, 'default' export for CJS/IIFE (#68)
  • ⚠️ Change .strict() to a normal class property. Also remove .cache() accessor
  • Replace mfogel/polygon-clipping with luizbarboza/polyclip-ts (#67)
  • Fix map on https://location-conflation.com (#66)
  • Bump dependency versions
  • Supported engines now "node": ">=18"

1.2.1

2023-Jul-12
  • Update country-coder to v5.2.1

1.2.0

2023-Mar-12
  • Bump dependency versions
  • Switch location-conflation to a scoped package under the rapideditor org: @rapideditor/location-conflation
    • ⚠️ Note: projects that depend on location-conflation may need to update their code

1.1.0

2022-Dec-09
  • Update country-coder to v5.1

1.0.2

2021-Jun-24
  • Remove "browser" from the export map

1.0.1

2021-Jun-17
  • Add an export map to package.json, fix file extensions again

1.0.0

2021-Jun-17
  • ⚠️ Initial stable release
    • Note: Built files will no longer be checked into GitHub
  • ⚠️ Replace microbundle with esbuild for super fast build speed. Package outputs are now:
    • "module": "./index.mjs" - ESM, modern JavaScript, works with import
    • "main": "./dist/location-conflation.cjs" - CJS bundle, modern JavaScript, works with require()
    • "browser": "./dist/location-conflation.iife.js" - IIFE bundle, modern JavaScript, works in browser <script> tag
    • No longer distributing ES5 builds
  • ⚠️ location-conflation is marked as "type": "module" now
  • ⚠️ Dropped support for old browsers like Internet Explorer on https://ideditor.codes
  • Update to country-coder v5.0

0.9.0

2021-Jun-05
  • Update to country-coder v4.1

0.8.0

2021-Mar-27
  • Add support for optional radius value for point locations.
    • Radius is specified in kilometers and is optional. If not specified, it will default to a 25km radius.

0.7.0

2020-Dec-30
  • Perf improvement: Don't union features iteratively with locationReducer (#26)

0.6.0

2020-Oct-26
  • Update to country-coder v4

0.5.0

2020-Aug-25
  • ⚠️ Refactor - API now has:
    • validateLocation / validateLocationSet - fast, return stable ids
    • resolveLocation / resolveLocationSet - slower, resolve GeoJSON features
    • All functions now return similar result objects
  • :warning: Introduce strict / non-strict modes. Location-conflation defaults to "strict" mode now.
    • 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.
    • Can change modes by calling .strict(val), for example:
      const loco = new LocationConflation(features).strict(false); // not strict
  • Add tests and document everything

0.4.0

2020-Aug-20
  • Replace turf with mfogel/polygon-clipping (#1, #2, #20)
  • Include stringify convenience method
  • Update country-coder and other dependencies

0.3.0

2020-Feb-13
  • Use uppercase Wikidata identifiers, lowercase geojson filenames
  • Handle cases like UK which have both members and geometry (#4)
  • :warning: Breaking changes: several function renames and changes to internals
    • locationToFeature() -> resolveLoation()
    • isValidLocation() -> validateLocation()
    • Copy country-coder features rather than modifying inplace (less hacky)
    • Use wikidata identifiers as the country coder feature ids and cache keys now
  • Update country-coder dependency

0.2.0

2020-Jan-15
  • Ensure that feature results always have both id and properties.id

0.1.0

2020-Jan-13
  • Initial release