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

spatialmerge

v1.0.1

Published

Merging geospatial data in JS

Downloads

5

Readme

spatialmerge

A (relatively) fast library for spatial joining and merging data in JavaScript.

spatialmerge

The library is largely influenced by the geopandas merge and sjoin methods. Just as geopandas, spatialmerge utilizes a static spatial index provided by Flatbush (R-tree) to narrow down the number of features to test for intersection.

import { merge, sjoin } from 'spatialmerge'
// join the properties of a spatial dataset (GeoJSON FeatureCollection) with a
// non-spatial dataset (e.g. contents of a CSV file parsed using d3-dsv) 
// based on a common variable
const resultsMerge = merge(countryShapes, countryNames, { on: 'iso_a3' })

// join the properties of two spatial datasets (GeoJSON FeatureCollections)
const mergeSjoin = sjoin(citiesPoints, countryShapes)

Working examples, and an in-depth explanation of the two functions can be found in the user guide notebook.

Installing

npm install spatialmerge
# or
yarn add spatialmerge

For vanilla HTML in modern browsers, import spatialmerge from Skypack:

<script type="module">

import { merge, sjoin } from 'https://cdn.skypack.dev/spatialmerge'
// ...

</script>

For legacy environments, you can load spatialmerge’s UMD bundle from an npm-based CDN such as jsDelivr; a spatialmerge global is exported:

<script src="https://cdn.jsdelivr.net/npm/spatialmerge"></script>

User Guide

The User Guide is hosted as an interactive ObservableHQ notebook
https://observablehq.com/@chrispahm/hello-spatialmerge

API reference

spatialmerge.merge(leftFC, rightFC_or_arrayOfObjects, options = { on: <property>, mutate: false }])

Join the attributes of a GeoJSON FeatureCollection with an array of objects or another GeoJSON FeatureCollection based on a common variable (object key).

Parameters:

spatialmerge.sjoin(leftFC, rightFC[, options = { how: 'inner', op: 'intersects', matches: 'all', lsuffix: 'left', rsuffix: 'right' }])

Spatial join of two GeoJSON FeatureCollections.
See the User Guide for details.

Parameters:

  • leftFC, rightFC: <GeoJSON FeatureCollection>, required
  • options: <object>, optional
    • how: <string>, default: 'inner'
      The type of join:
      • ‘left’: use keys from left_df; retain only left_df geometry column
      • ‘right’: use keys from right_df; retain only right_df geometry column
      • ‘inner’: use intersection of keys from both dfs; retain only left_df geometry column
    • op: <string>, default: 'intersects'
      Binary predicate. Internally uses the corresponding turf.js modules.
      • 'intersects'
      • 'contains'
      • 'within'
      • 'crosses'
      • 'overlaps'
    • matches: <string>, default: 'all' Whether to output all results of the join operation, or only the first.
      • 'all'
      • 'first'
    • lsuffix: <string>, default: 'left'
      Suffix to apply to overlapping column names (left GeoJSON).
    • rsuffix: <string>, default: 'right'
      Suffix to apply to overlapping column names (right GeoJSON).
    • inclLeftIndex: <boolean>, default: false
      Whether to include the left index as a property value in the resulting GeoJSON FeatureCollection.

Performance

Spatially merging datasets is (almost) always a computationally intensive task. If you plan to use spatialmerge on the client side or in a Node.js / Deno server, be sure to wrap it in a WebWorker or worker_thread to avoid blocking the Rendering / Event Loop.

Resources:

  • https://www.smashingmagazine.com/2021/06/web-workers-2021/
  • https://github.com/GoogleChromeLabs/comlink
  • https://github.com/josdejong/workerpool

TODOs

  • Fix tests for sjoin including large files, potentially using git-lfs
  • Example using WebWorker
  • Performance comparison with @turf/tag and geopandas

Contribution

Contribution is highly appreciated 👍
Please open an issue in case of questions / bug reports or a pull request if you implemented a new feature / bug fix. In the latter case, please make sure to run npm test (and adapt test/test.js to your changes) and / or update the README 🙂

License

MIT @Christoph Pahmeyer