• Stars
    star
    541
  • Rank 82,114 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

polygon-clipping

Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.

CI codecov License: MIT npm

Quickstart

const polygonClipping = require('polygon-clipping')

const poly1 = [[[0,0],[2,0],[0,2],[0,0]]]
const poly2 = [[[-1,0],[1,0],[0,1],[-1,0]]]

polygonClipping.union       (poly1, poly2 /* , poly3, ... */)
polygonClipping.intersection(poly1, poly2 /* , poly3, ... */)
polygonClipping.xor         (poly1, poly2 /* , poly3, ... */)
polygonClipping.difference  (poly1, poly2 /* , poly3, ... */)

API

/* All functions take one or more [multi]polygon(s) as input */

polygonClipping.union       (<geom>, ...<geoms>)
polygonClipping.intersection(<geom>, ...<geoms>)
polygonClipping.xor         (<geom>, ...<geoms>)

/* The clipGeoms will be subtracted from the subjectGeom */
polygonClipping.difference(<subjectGeom>, ...<clipGeoms>)

Input

Each positional argument (<geom>) may be either a Polygon or a MultiPolygon. The GeoJSON spec is followed, with the following notes/modifications:

  • MultiPolygons may contain touching or overlapping Polygons.
  • rings are not required to be self-closing.
  • rings may contain repeated points, which are ignored.
  • rings may be self-touching and/or self-crossing. Self-crossing rings will be interpreted using the non-zero rule.
  • winding order of rings does not matter.
  • inner rings may extend outside their outer ring. The portion of inner rings outside their outer ring is dropped.
  • inner rings may touch or overlap each other.

Output

For non-empty results, output will always be a MultiPolygon containing one or more non-overlapping, non-edge-sharing Polygons. The GeoJSON spec is followed, with the following notes/modifications:

  • outer rings will be wound counter-clockwise, and inner rings clockwise.
  • inner rings will not extend outside their outer ring.
  • rings will not overlap, nor share an edge with each other.
  • rings will be self-closing.
  • rings will not contain repeated points.
  • rings will not contain superfluous points (intermediate points along a straight line).
  • rings will not be self-touching nor self-crossing.
  • rings may touch each other, but may not cross each other.

In the event that the result of the operation is the empty set, output will be a MultiPolygon with no Polygons: [].

Correctness

Run: npm test

The tests are broken up into unit tests and end-to-end tests. The end-to-end tests are organized as GeoJSON files, to make them easy to visualize thanks to GitHub's helpful rendering of GeoJSON files. Browse those tests here.

Performance

The Martinez-Rueda-Feito polygon clipping algorithm is used to compute the result in O((n+k)*log(n)) time, where n is the total number of edges in all polygons involved and k is the number of intersections between edges.

Settings

Global settings are set via environment variables.

  • POLYGON_CLIPPING_MAX_QUEUE_SIZE and POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS: Aims to prevent infinite loops - usually caused by floating-point math round-off errors. Defaults are 1,000,000.

Changelog

This project adheres to Semantic Versioning.

The full changelog is available at CHANGELOG.md.

Authors

Sponsors

Please contact Mike Fogel if you or your company is interested in sponsoring work on specific bug fixes or feature requests.

Based on

More Repositories

1

django-timezone-field

A Django app providing DB, form, and REST framework fields for zoneinfo and pytz timezone objects.
Python
390
star
2

django-simple-email-confirmation

Simple email confirmation for django.
Python
170
star
3

django-settings-context-processor

Makes specified django settings visible in template rendering context.
Python
19
star
4

geojson-clipping

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

django-matrix-field

A Django app providing database store for matrices.
Python
13
star
6

django-signup-login

Django unified signup and login views.
Python
4
star
7

Skeletor

Fusion 360 extension to 'skeletorize' your models
Python
4
star
8

django-npo-google-checkout

Implements google checkout's non-profit checkout and notificaiton API's.
Python
4
star
9

get-overpass

Get OpenStreetMap objects via the Overpass API as GeoJSON
JavaScript
3
star
10

assorted-unix-utils

An assortment of unix scripts/helpers I use.
Shell
3
star
11

geojson-cli-difference

Subtract Polygons & MultiPolygons in your GeoJSON files from each other.
JavaScript
2
star
12

auto-trac-on-dreamhost

Automates install of Trac on a Dreamhost hosting account.
Shell
2
star
13

linode-dns-manager-sorter

Intuitive sorting of the linode DNS manager tables.
JavaScript
2
star
14

geojson-cli-union

Union/dissolve Polygons & MultiPolygons in your GeoJSON files into each other.
JavaScript
1
star
15

mfogel.github.io

mike.fogel.ca homepage
HTML
1
star
16

django-html-image

Django image models and helpers for common use cases.
Python
1
star
17

natural-earth-admin-mvt

Natural Earth Admin features in Mapbox Vector Tile format
Makefile
1
star
18

btdt

Been There, Done That
JavaScript
1
star
19

dotfiles

Dotfiles I use
Vim Script
1
star
20

django-zipcode-zone-mapper

Django manage.py commands to generate kml files with colored zones derrived from Tiger zipcode/zcta data.
Python
1
star