Code Monkey home page Code Monkey logo

geojson-clipping's Introduction

geojson-clipping

Apply boolean polygon clipping operations (union, intersection, difference, xor) to Polygons & MultiPolygons in your GeoJSON files.

npm version build status test coverage

Quickstart

$ npm install -g geojson-clipping
$ cat poly1.geojson poly2.geojson | geojson-clipping union > union.geojson

Usage

$ geojson-clipping union        [-o <path>] [<path> ... ]
$ geojson-clipping intersection [-o <path>] [<path> ... ]
$ geojson-clipping xor          [-o <path>] [<path> ... ]
$ geojson-clipping difference   [-o <path>] [<path> ... ] [ -s <path> ]

Input/output streams:

  • stdin: Input GeoJSON objects, whitespace separated, may be piped in via stdin.
  • stdout Output GeoJSON is by default written to stdout.
  • stderr: Any warnings generated are by default written to stderr.

Input

Input GeoJSON objects may be provided via:

  • the positional <path> arguments, each of which may point to any of:
    • a GeoJSON file
    • a file with multiple GeoJSON objects in it, whitespace separated (ex: ndjson)
    • a directory, in which case any files with a .geojson extension found immediately within it will be used as input
  • stdin (objects may optionally be separated by whitespace)
  • the -s / --subject <path> option, for the difference() operation only

The following GeoJSON object types are acceptable input:

  • Polygon
  • MultiPolygon
  • Feature containing Polygon or MultiPolygon
  • FeatureCollection containing an array of acceptable Features

If a GeoJSON object with a different type is encountered (ex: Point) the offending object will be dropped and a warning will be printed to stderr.

Output

The computed GeoJSON is by default written to stdout, but may be redirected using the -o / --output <path> option. The output GeoJSON will have the following properties:

  • it will be a single GeoJSON Feature object with a geometry of type MultiPolygon.
  • the Feature's properties attribute will be set to null.
  • if the geometry of resulting from the operation is the empty set (ex: intersection of non-overlapping polygons) then the coordinates of the MultiPolygon will be [].
  • it will have the qualities guaranteed by polygon-clipping.

Options

-s / --subject <path>

Valid only for difference() operation. Invalid for other operations.

Use the file identified by <path> as input GeoJSON, and consider it to be the subject in the difference() operation. That is to say, all other GeoJSON objects will be subtracted from this GeoJSON object.

If this option is not supplied, the subject will by default be the first GeoJSON object read in from stdin.

-b / --bboxes

Valid only for difference() operation. Invalid for other operations.

Scan input GeoJSON filenames for pre-computed stringified bounding boxes. If no bounding box is found in the filename, then compute a bounding box from the GeoJSON coordinates. Examples of filenames containing bounding boxes:

  • [-10,-10,10,10].json
  • 424242.[-58.5314588,-34.705637,-58.3351249,-34.5265535].geojson

If a the bounding box of a given GeoJSON object does not overlap the bounding box of the subject, that GeoJSON object is dropped from the calculation as it cannot contribute to the end result, resulting in a performance boost. In the case that the bounding box was extracted from the filename, the non-contributing GeoJSON object is dropped without reading the file in from disk.

-o / --output <path>

Write the output GeoJSON object out to a newly-created file located at <path>.

If this option is not supplied, the ouput GeoJSON will be written to stdout.

-i / --id <id>

Add to the output GeoJSON object the Feature id <id>. If <id> can be parsed as a number it will be written into the output GeoJSON as a number, else it will be written out as a string.

If this option is not supplied, the output GeoJSON will not have a Feature id.

-q / --quiet

Suppress any warnings generated.

If this option is not supplied, any warnings generated will be written to stderr.

-h / --help

Display help message and exit.

-v / --version

Display version string and exit.

Examples

Equivalent ways to take the union of three GeoJSON objects:

$ cat poly1.geojson poly2.geojson poly3.geojson | geojson-clipping union > union.geojson
$ geojson-clipping union poly1.geojson poly2.geojson poly3.geojson > union.geojson
$ geojson-clipping union -o union.geojson poly1.geojson poly2.geojson poly3.geojson

Equivalent ways to take the difference (aka subtract) a directory full of GeoJSON objects from another GeoJSON object:

$ cat subject.geojson | geojson-clipping difference ./directory-with-geojson-files > difference.geojson
$ geojson-clipping difference -s subject.geojson -o difference.geojson ./directory-with-geojson-files

Changelog

0.3 (2018-04-01)

  • Performance improvements
  • enhance -b / --bboxes to better support bounding boxes that cross the antimeridian #4

0.2 (2018-03-09)

  • add -i / --id <id> option #3
  • add -b / --bboxes option for difference() operation #2
  • create output directories as needed #1

0.1 (2018-03-07)

  • Initial release

geojson-clipping's People

Contributors

mfogel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mccahan bt-maps

geojson-clipping's Issues

Writting output to file doesn't create directories if needed

If the --output flag is used and specifies a file within a directory that doesn't exist, the command fails with an error.

Better behavior would be to create any needed directories that don't exist. So this should work, assuming permissions allow for the creation of the needed dir.

geojson-clipping union file1.geojson file2.geojson -o does-not-exist-yet/output.geojson

Add support for using bounding boxes in filenames to short-circuit computation

New feature:

For the difference operation, add an additional optional command line flag -b / --bboxes. If the --bboxes flag is present, filenames of GeoJSON files should be scanned for stringified bounding boxes. If found, that bounding box should be compared with the subject's bounding box. If there is no overlap, then there is no need to read that file in off disk.

The format of the bounding box in the filename would be the same as is produced by geojson-cli-explode

Limit memory usage

When processing many inputs that don't all fit in node's default heap size, rather than just crash with an out-of-memory error it would be better to read in a subset of the inputs, process those, then read in another set of inputs, process those, etc.

-b / --bboxes doesn't handle crossing the antimeridian well

When the -b / --bboxes option is used, the bounding box calculated for the subject doesn't handle shapes that cross the antimeridian well. Instead of forming a small bounding box that crosses the antimeridian, it forms one that wraps around the other side of the globe in one big band.

This geojson spec describes the preferred way to handle this situation. The bounding box should not be considered to be [minX, minY, maxX, maxY], but rather [W, S, E, N] limits of the shape.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.