Giter Site home page Giter Site logo

mapbox-gl-draw-union-mode's Introduction

mapbox-gl-draw-union-mode

Simple custom mode for mapbox-gl-draw that combines selected Polygons into a single Polygon.

Installation

Browser

Include the script in your HTML file

<script src="https://unpkg.com/[email protected]/dist/index.js"></script>

A UnionMode global variable will be available for you to use. See Usage for more details.

npm

npm install mapbox-gl-draw-union-mode

Usage

  1. Add the mode to your mapbox-gl-draw instance:
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import UnionMode from 'mapbox-gl-draw-union-mode';

const draw = new MapboxDraw({
    modes: Object.assign(MapboxDraw.modes, {
        union: UnionMode
    })
});
  1. Use the mode in your mapbox-gl-draw instance:
draw.changeMode('union');

After selecting multiple Polygons, simply change the mode to union, and the selected Polygons will be combined into a single Polygon!

Options

You can pass a function to handle how you want the properties of the combined Polygon to be merged. By default, it uses Object.assign to combine the properties of the selected Polygons.

It should be a function that takes two properties and returns a single property, for example:

/**
 * Merge two properties
 * @param {Object} prop1
 * @param {Object} prop2
 * @returns {Object} The merged properties
 */
function (prop1, prop1) { ... }

and then use it like this:

draw.changeMode('union', { propertyUnionFn: yourMergeFunction });

Disclaimer

If you ended up taking the union of non-intersecting Polygons, the resulting Polygon will be a MultiPolygon with the original Polygons as its members.

This mode only handles Polygon types, so you won't be able to combine the resulting MultiPolygon with other Polygons.

To fix, You can convert them back into individual Polygons using the "Uncombine Features" mode in mapbox-gl-draw and then combine them again after moving them to intersect.

mapbox-gl-draw-union-mode's People

Contributors

candh avatar

Watchers

 avatar

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.