location-conflation

April 29, 2026 · View on GitHub

build npm version

location-conflation

đź§© Define complex geographic regions (geofences) by including and excluding country codes and GeoJSON shapes.

⚡️ Try it live at https://location-conflation.com/


What is it?

location-conflation generates GeoJSON features by combining other locations and shapes. It's a declarative way to describe geofences, so your application doesn't need to bundle or fetch large amounts of geodata.

A locationSet is an object with include and exclude arrays:

const locationSet = {
  include: [ /* locations */ ],
  exclude: [ /* locations */ ]
};

A location can be any of:

KindExample
A country-coder identifier (ISO codes, UN M.49 codes, Wikidata QIDs, etc.)"de", "001", "conus", "gb-sct", "Q620634"
A custom .geojson filename (add custom features via the constructor or addFeatures)"de-hamburg.geojson", "new_jersey.geojson"
A circular area [lon, lat, radius?] (radius in km, defaults to 25)[8.67039, 49.41882], [-88.3726, 39.4818, 32]

Warning

For numeric-looking country-coder identifiers (for example UN M.49 codes), pass strings like "001" and "039". Avoid using numeric literals in JavaScript, because leading-zero values may be treated like octal numbers and represent a different number than you expect.

Tip

The full list of recognized country-coder identifiers is browsable at https://ideditor.codes.


Installing

Node

npm install @rapideditor/location-conflation

The package is distributed as both CJS and ESM:

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

Browser

Fetch the IIFE bundle from the jsDelivr CDN. Loading it via <script> exposes a LocationConflation global:

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

Note

This project uses modern TypeScript syntax. If you need to support legacy environments like ES5 or Internet Explorer, build your own bundle with a tool 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 result = loco.resolveLocationSet({ include: ['039'] });   // 039 = Southern Europe
Southern Europe

Southern Europe and Northern Africa

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

Southern Europe and Northern Africa, excluding Egypt and Sudan

const result = loco.resolveLocationSet({ include: ['039', '015'], exclude: ['eg', 'sd'] });
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

Spatial index

If your application has many objects with locationSet values (e.g. presets, rules, rendering styles) and you need to test which of them apply at a given point, the spatial-index API makes that lookup fast.

Register your objects once:

const presets = [
  { id: 'amenity/cafe', locationSet: { include: ['de'] } },
  { id: 'amenity/atm',  locationSet: { include: ['001'] } },      // 001 = world
  { id: 'parking/bike', locationSet: { include: ['us-nj.geojson'] } },
];
loco.registerLocationSets(presets);
// Each preset now has a stable `locationSetID`, e.g. '+[Q183]'

Then query by point as often as you like:

const hits = loco.locationSetsAt([8.68, 49.41]);   // Heidelberg
// => Map { '+[Q183]' => 357386, '+[Q2]' => 511207893 }
if (hits.has('+[Q183]')) { /* Germany preset applies here */ }

Note

locationSetsAt does no polygon clipping — lookups are backed by country-coder's built-in spatial index for country-coder regions, and by which-polygon for custom .geojson and point-radius features.

Important

registerLocationSets is tolerant of bad input so a batch of thousands of objects won't be rejected over a single typo:

  • Objects with a missing, empty, or invalid locationSet fall back to world (+[Q2]).
  • Individual invalid include/exclude components are silently ignored.

The single-item validate* / resolve* methods, by contrast, always throw on invalid input.

See the API reference for full details on registerLocationSets, locationSetsAt, rebuildIndex, and getLocationSetArea.


API

Full reference: API.md

MethodPurpose
new LocationConflation(features?)Construct an instance, optionally seeded with custom .geojson features.
addFeatures / removeFeatures / clearFeaturesManage the custom feature cache.
validateLocation / validateLocationSetValidate without resolving geometry.
resolveLocation / resolveLocationSetValidate and resolve to GeoJSON (cached).
registerLocationSetsRegister locationSet-bearing objects for fast point-in-polygon lookups.
locationSetsAtQuery which indexed locationSets cover a point.
rebuildIndex / getLocationSetAreaSpatial-index maintenance / inspection.
LocationConflation.stringifyPretty-print GeoJSON with sensible defaults.

Contributing

See CONTRIBUTING.md.

Thanks!

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

License

Available under the ISC License. See LICENSE.md.