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

@kevinulrich/greatcircle

v2.0.0

Published

A JavaScript toolbox for working with geographical entities

Downloads

12

Readme

greatCircle

A JavaScript toolbox for working with geographical entities. Currently supports single points, calculating their distance and sorting them by distance. Ideas for future expansion:

  • Calculating Bearing between points
  • Taking into account differences in altitude
  • Calculating sunrise and sunset at a given point in time for a geographical position

Build Status codecov Maintainability

npm install @kevinulrich/greatcircle

Example usage

This library ships different modules for working with geographical positions. Currently these are: Point, Vector and PointList

Working with Points

The point constructor accepts an options object, no options actually have to be given. Missing latitude or longitude will default to 0, missing payload will default to an empty object. The payload can be any valid JavaScript object, string or number for example.

var myPoint = new greatCircle.Point({
	latitude: 50.0104469, 
	longitude: 8.7194302, 
	payload: 'Stangenpyramide'
});

You can make greatCircle calculate a vector between points by using the getVectorFrom or getVectorTo methods.

var myVector = myPoint.getVectorTo(anotherPoint)

greatCircle will help you validate geographical positions. You can simply input your latitude and longitude and it will not only check for valid numbers but also if the given latitude or longitude is within -180 and 180 degrees.

var myPoint = new greatCircle.Point({
	latitude: 564.564, 
	longitude: 2, 
	payload: 'Bad Point'
});

console.log(myPoint.isValid());

// Will output false

Working with Vectors

The vector constructor takes two arguments for a starting and an end point. These must be valid Point objects. The order of start and end is very important as a vector is directional. As of 2.0, the library is only able to calculate the distance of a vector so the order is not important right now, but will become so once bearing and other features come into play.

var myVector = new greatCircle.Vector(myPoint, anotherPoint);

You can not easily calculate the length of the vector by getting the distance. getDistance will return a number in meters.

myVector.getDistance();

Working with PointLists

A pointlist is an aggregate of multiple points and can be used to determine the closest or fartherst point from another or even sort all points by their distance to a given reference point.

var myList = new greatCircle.PointList();

myList.addPoint(myPoint);
myList.addPoint(anotherPoint);

console.log(myList.count());
// Will output 2

myList.getClosestPointTo(referencePoint);
myList.getFarthestPointFrom(referencePoint);

To sort all points within the list by their distance to or from another point you can use the sortByDistanceTo method. It will accept two parameters: The reference point to be used and a sorting parameter which accepts asc or desc, default is asc;

myList.sortByDistanceTo(referencePoint, 'desc');

To retrieve the contained points, the PointList implements iteration as well as a toArray method.

var sortedPoints = myList.toArray();

for (let point of myList) {
	console.log(point);
}

// Will output all contained points by their distance in reference to the point and sorting given 
// by a previous call to ```sortByDistanceTo```.