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

leaflet-polyutils

v0.3.0

Published

Utilities for Leaflet Polylines

Downloads

6

Readme

Build Status

leaflet.polyutils

A set of utilities for Leaflet polylines. Each can be used independently.

L.PolyPrune.prune(latlngs, tolerance)

L.PolyPrune has a single static method to remove points from a polyline that do not deviate the line from more than a configurable number of meters.

Example:

L.PolyPrune.prune(polyline.getLatLngs(), 5);

L.PolyTrim

A utility class to delete heading or trailing points from a polyline.

PolyTrim Constants

  • L.PolyTrim.FROM_START: Direction constant.
  • L.PolyTrim.FROM_END: Direction constant.

PolyTrim Methods

  • L.polyTrim (polyline, direction): Creates a new L.PolyTrim instance.
  • trim(n): Trims the polyline, and returns the number of removed points (may be smaller than n is polyline size was smaller than n).
  • getDirection(): Returns the confirgured direction (L.PolyTrim.FROM_START or L.PolyTrim.FROM_END).
  • getPolySize(): Returns the polyline size before trimming.

Example:

var polytrim = L.polyTrim(polyline, L.PolyTrim.FROM_END);
var trimmed = polytrim.trim(10); // delete the 10 last points of the polyline
trimmed += polytrim.trim(5); // delete 5 more points
// trimmed is 15 if polyline had at least 15 points

L.PolyStats

A utility class to compute statistics for a polyline:

  • To each LatLng point of the polyline are added the following properties:
    • i: Index of this point in the polyline.
    • dist: Distance from start (in meters).
    • chrono: Estimated time to travel from start of polyline to this point, based on the configured speed profile.
    • chrono_rt: Estimated time to travel from start of polyline to end then back to this point, based on the configured speed profile.
  • To the polyline itself is added a stats object with following properties:
    • minalt: Smallest elevation value of the polyline points.
    • maxalt: Greatest elevation value of the polyline points.
    • climbing: Cumulated positive elevation difference between each consecutive polyline points.
    • descent: Cumulated negative elevation difference between each consecutive polyline points.

Speed profiles

A speed profile is a parametrized function:

speed = f(slope)

Slope between two points is computed by dividing their elevation difference by the distance separating them. Speed is in meters per second.

Several functions are supported, which use different types of parameters:

| Function method | Parameters | Description | | -------------------------- | -----------| ----------- | | L.PolyStats.REFSPEEDS | Array of [slope, speed] pairs, sorted by increasing slope| The speed is computed by taking the proportional speed between the 2 closest slopes | | L.PolyStats.LINEAR | Array of 2 numbers [a, b] | The speed is computed with formula: speed = a*slope + b | | L.PolyStats.POWER | Array of 2 numbers [a, b] | The speed is computed with formula: speed = a*slope^b | | L.PolyStats.POLYNOMIAL | Array of numbers [p0, p1, ..., pn] | The speed is computed with formula: speed = p0 + p1*slope^1 + ... + pn*slope^n|

Encoding:

A speed profile is encoded as a json value with two fields:

  • method is the function method, as listed above
  • parameters is the parameters value as defined above

Examples:

var sp1 = {
  method : L.PolyStats.REFSPEEDS,
  parameters : [ [-5, 1.2638], [3, 1.25], [2, 1.1111], [6, 0.9722] ]
};
var sp2 = {
  method : L.PolyStats.POLYNOMIAL,
  parameters : [ 1.1, -0.1, -0.001 ]
};

PolyStats Constructor Options

  • chrono: (default: true) If false then computation of chrono properties is skipped, gaining some computation time.
  • speedProfile: (default is { method: L.PolyStats.REFSPEEDS, parameters: [0, 1.25] }) The speed profile as defined above.
  • onUpdate: (default: undefined) If set, this method will be called (without any parameter) at the end of each statistics computation.
  • minspeed: (default: 0.05) Computed speeds below this value will be floored to this value

PolyStats Methods

  • L.polyStats(polyline, options): Creates a new L.PolyStats instance.
  • setSpeedProfile(speedprofile): Sets a new speed profile. This triggers an update of the statistics if options.chrono is true.
  • updateStatsFrom(i): Updates the statistics because point index i was changed.
  • computeSpeedProfileFromTrack(geojson, method, iterations, pruning, polydeg, threshold): Computes a speedprofile based on a recorded GPS trace. parameters are:
    • geojson:: mandatory trace in geojson format.
    • method: the speed profile function method to use.
    • iterations: 1 for no pruning (recommended), 2 or more to iterate and exclude trace points which are more than pruning% different from the computed value.
    • pruning: the percentage (number between 0 and 1) parameter for above described pruning
    • polydeg: the polynomial degree to use, if POLYNOMIAL method is used (recommended value is 2)
    • threshold: ignore speed from the GPS input traces which are below this value.
  • computeSpeedProfileFromSpeeds(refspeeds, method, iterations, pruning, polydeg, threshold): Same as above method, but taking as input an array of [slope,speed] pairs instead of a geojson trace.

Example:

var polystats = L.polyStats(polyline, {
        speedProfile: {
          method : L.PolyStats.REFSPEEDS,
          parameters : [ [-5, 1.2638], [3, 1.25], [2, 1.1111], [6, 0.9722] ]
        }
      })
polytrim.updateStatsFrom(0);
var pts = polyline.getLatLngs();
var lastpt = pts[pts.length - 1];
var txt = 'polyline length is ' + laspt.dist + 'm' +
          ', will take ' + lastpt.chrono + 's to travel';

Live demo

https://opoto.github.io/leaflet.polyutils/examples