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

mapbox-gl-snap

v1.1.7

Published

A utility to snap points to vertices, edges, or midpoints on a Mapbox GL JS.

Downloads

89

Readme

MapboxSnap

Demo Page

MapboxSnap is a utility that works with Mapbox GL JS to enable snapping to specific geometric points, edges, or midpoints when drawing on the map. This library is designed to enhance accuracy during drawing by allowing users to snap to nearby features easily.

mapbox-gl-snap

Installation

npm install mapbox-gl @mapbox/mapbox-gl-draw @turf/turf mapbox-gl-snap

Importing the Library

Depending on whether you are using, Browser(purejs) ES Modules (import) or CommonJS (require), you should import the MapboxSnap library as follows:

  • For Browser (purejs):

    <script src="https://cdn.jsdelivr.net/gh/gislayer/mapbox-gl-snap/dist/purejs/mapbox-gl-snap.js"></script>
  • For ES Modules (import):

    import MapboxSnap from 'mapbox-gl-snap/dist/cjs/MapboxSnap';
  • For CommonJS (require):

    const MapboxSnap = require('mapbox-gl-snap/dist/esm/MapboxSnap');

Features

  • Layer-Based Snapping: Operates on specific layers, allowing you to snap only to the geometries within these layers.
  • Rule-Based Snapping: Allows snapping based on rules such as vertex, edge, or line, polygon edge midpoint. The snapping process respects these rules in order.
  • Dynamic Snap Radius: The snap radius is configurable, allowing control over how close the points need to be for snapping to occur.

TypeScript Declaration

If you're using TypeScript, you might need to manually add the following declaration for the mapbox-gl-snap module:

declare module 'mapbox-gl-snap' {
  import {
    Map
  } from 'mapbox-gl';
  import MapboxDraw from '@mapbox/mapbox-gl-draw';
  import {
    FeatureCollection,
    Geometry
  } from 'geojson';

  export interface MapboxSnapOptions {
    layers: string[];
    rules: ('vertex' | 'edge' | 'midpoint')[];
    radius: number;
  }

  export interface MapboxSnapProps {
    map: Map;
    drawing: MapboxDraw;
    options: MapboxSnapOptions;
    status ? : boolean;
    onSnapped ? : Function;
  }

  class MapboxSnap {
    constructor(options: MapboxSnapProps);
    setStatus(status: boolean): void;
    getMe(): MapboxSnap;
  }

  export default MapboxSnap;
}

Usage

Below is an example of how to use the MapboxSnap library.

1. Creating the Map and Drawing Objects

First, you need to create a Mapbox GL JS map and add the MapboxDraw control for drawing.

import mapboxgl from 'mapbox-gl';
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import MapboxSnap from 'mapbox-gl-snap';

const map = new mapboxgl.Map({
  container: 'map',
  style: `mapbox://styles/mapbox/dark-v11`,
  center: [0, 0],
  zoom: 16,
  pitch: 30,
  bearing: 45,
});

map.on('style.load', () => {
  const drawing = new MapboxDraw({
    displayControlsDefault: false,
    controls: {
      polygon: true,
      line_string: true,
      point: true,
      trash: true,
    },
  });

  map.addControl(drawing);
  
  // Initialize MapboxSnap Library
  const mapboxSnap = new MapboxSnap({
    map: map,
    drawing: drawing,
    options: {
      layers: ['layer1', 'layer2'],  // Array of layer IDs to snap to
      radius: 15,  // Snap radius in pixels
      rules: ['vertex', 'midpoint', 'edge']  // Snap rules
    },
    onSnapped:(fc:FeatureCollection)=>{
      // use on here
    }
  });
});

2. Understanding Snap Rules

  • layers: An array of layer IDs where the snapping will occur. The snapping is limited to geometries within these layers.

  • rules: An array defining the snap rules, which are checked in order:

    • vertex: Snaps to the vertex points of the geometry.
    • midpoint: Snaps to the midpoint of lines or polygon edges.
    • edge: Snaps to the edges of lines.

    The rules are applied sequentially. For instance, with rules: ['vertex', 'midpoint', 'edge'], the snapping will first attempt to snap to a vertex. If a vertex is found, it will snap to that and stop. If not, it will proceed to check the middle and then the edge.

3. Customizing the Snap Process

When creating the MapboxSnap instance, you can customize the snap radius (radius) and rules (rules). The radius specifies how close a point must be to snap to it, measured in pixels.

const mapboxSnap = new MapboxSnap({
  map: map,
  drawing: drawing,
  options: {
    layers: ['layer1', 'layer2'],  // Layers to snap to
    radius: 20,  // Snap radius in pixels
    rules: ['vertex', 'edge'],  // Only snap to vertices and edges
  },
  onSnapped:(fc:FeatureCollection)=>{
      // you can get snapped geometries
  }
});

License

This project is licensed under the MIT License.

Author: Ali Kilic - [email protected]

Author Web Site : Ali Kilic Blog Page

Project GitHub : mapbox-gl-snap

Blog Page : Read My Blog