2.0.2 • Published 4 years ago

geo-points-and-paths v2.0.2

Weekly downloads
18
License
MIT
Repository
github
Last release
4 years ago

geoPointsAndPaths

Library for storing and manipulating coordinate data as Points and Paths, with common methods including path simplification, point to point distance etc. Any number of named parameters (such as 'elevation') can be associated with each point. Distance calculations between points are based on Haversine - there are more accurate, slower, methods available and any number of nom libraries to calculate them.

Features of Point class:

Features of Path class:

GeoFunctions:

Importing the classes and functions

Import the classes and functions

Point Class

You can instantiate a Point in two ways. First option is to provide a list of paramaters, which must include 'lat' and 'lng' or will throw an error:

The other option is to instantiate an empty instance and use the lng/lat setters:

To add additional parameters after instantiation (no matter which way instance was created) use addParams method. Note that if a parameter is supplied that already exists on the point, it is silently ignored and not overwritten:

To delete parameters from a point use the deleteParams method. Note that lng and lat cannot be deleted, attempt to do so will be silently ignored:

To read parameters from a point, use getters for lat/lng or getParams for other parameters:

Path Class

Define an array of Point instances along with some parameter arrays for future use:

Instantiate a Path - must be an array of points; point-like objects are not accepted because the Path lass needs access to the methods on the Point class to fulfil its functions

Parameters can now be added to the path via the addParamToPoints method. This takes an array, which must have the same lengths as the number of points in the path, and adds each point to a Point instance. In this way if points are added or deleted, the correct parameters are always on the correct points.

Parameters can be deleted in a similar manner

Values of parameters can be accessed as follows:

The following getters also exist on the class:

Access geoFunctions directly

All geoFunctions will take Points or Point-like objects of the form {"lat":xxx, "lng":xxx}:

Distance between two points:

Distance between a great circle line joining two points (defined by the first two points) and a third point:

Bearing between two points:

Bearing between two points and compass cardinal (input in RADIANS):

Convert degrees to radians and radians to degrees:

Get the bounding box for a list of points:

Test if a point is within a given bounding box:

Simplify a list of points returns an object with parameters points and ratio, which is the ratio simplified length / original length: