New: Introducing PHP and Composer Support.Read the Announcement
Socket
Book a DemoInstallSign in
Socket

@terrestris/geojson-walk

Package Overview
Dependencies
Maintainers
17
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@terrestris/geojson-walk

Validate and clean GeoJSON

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
5
-16.67%
Maintainers
17
Weekly downloads
 
Created
Source

geojson-walk

A tiny library to apply a function to a GeoJSON. In case of a FeatureCollection, the function will also be applied to the single Features.

This can be used to eg. gather statistics about the GeoJSON (spatial extent, attributes, …) or to change / clean it. Have a look at the examples below to get an idea of what is possible.

Installation

npm i @terrestris/geojson-walk

API

This module exposes one function walk:

walk(o, fn, doExpectChange) ⇒ object

Walk the GeoJSON and apply the provided function to it or its features (in case of a FeatureCollection). If you expect your function to change the passed GeoJSON Feature Collection, then pass the third parameter as true. In that case the return value of the function determines what ends up in the returned GeoJSON. If you return something truthy, the return value will be used, otherwise the Feature will not be in the returned collection.

Kind: global function Returns: object - The (possibly changed) GeoJSON.

ParamTypeDescription
oobjectThe GeoJSON we will walk. We do not check whether this is actually a fully valid GeoJSON.
fnfunctionThe function we will call. Will receive the GeoJSON or each feature of the collection.
doExpectChangebooleanWhether you expect your function might change the GeoJSON.

Example

Simple usage example:

import walk from '@terrestris/geojson-walk';

const filterForProp = (f) => {
  if (f.properties.humpty === 'dumpty') {
    return f;
  } else {
    return null;
  }
};

const featureCollection = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      properties: {
        humpty: 'dumpty'
      },
      geometry: {
        type: 'Point',
        coordinates: [
          0,
          1
        ]
      }
    },
    {
      type: 'Feature',
      properties: {
        humpty: 'trumpty'
      },
      geometry: {
        type: 'Point',
        coordinates: [
          2,
          3
        ]
      }
    }
  ]
};

const cleanCollection = walk(featureCollection, filterForProp, true);

contributions welcome

npm version GitHub

Build Status Coverage Status

dependencies Status devDependencies Status Known Vulnerabilities

FAQs

Package last updated on 02 May 2024

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