manuelbieh / geolib Goto Github PK
View Code? Open in Web Editor NEWZero dependency library to provide some basic geo functions
License: MIT License
Zero dependency library to provide some basic geo functions
License: MIT License
is there any plan to include a method that extends existing bounds to a new point similar to what google and leaflet have with ther latlngbounds.extend methods?
cheers
Qiong
Feature request to add the calculation of the straight line bearing between two points/coordinates.
Say I wanted to get, roughly, a 30km by 30km square around a given lat/long, how would I do this?
Something like geolib.travel(lat, long, 30, 'km', degrees)
where degrees is a number between 0 and 359.9999, and indicates the direction of travel..
var geolib = require('geolib');
geolib.getDistance(
{latitude: 51.5103, longitude: 7.49347},
{latitude: "51° 31' N", longitude: "7° 28' E"}
);
// when
var warper = geolib.getDistance
warper(
{latitude: 51.5103, longitude: 7.49347},
{latitude: "51° 31' N", longitude: "7° 28' E"}
);
// throw
// TypeError: Cannot read property 'coords' of undefined
// at geolib.extend.getDistance (\geolib\node_modules\geolib\dist\geolib.js:229:16)
// at Object. (\geolib\test.js:10:1)
// at Module._compile (module.js:460:26)
// at Object.Module._extensions..js (module.js:478:10)
// at Module.load (module.js:355:32)
// at Function.Module._load (module.js:310:12)
// at Function.Module.runMain (module.js:501:10)
// at startup (node.js:129:16)
// at node.js:814:3
If given a string ending with '0', geolib.useDecimal will throw an error due to the wacky checking, since the trailing 0 gets lost on the conversion to a Float and the string comparison does not match.
Example:
require("geolib").useDecimal("1.23450");
It would be great if there was a method for calculating the area of a polygon.
Hi,
I was looking for a js geo library that could also calc bearing and direction. At first your (great!) lib didn't seem to do that until I took a closer look at the example page. Seems it can indeed do so as I also found in the src file (getRhumbLineBearing, getBearing, getDirection, etc).
I would recommend documenting this so even more people will be finding a great solution in your lib!
The latest version I can get with bower is 2.0.9 but Github suggests the version is 2.0.17
bower install -save geolib#master
Works but this is not ideal incase there is breaking change later.
This lib is superb. Hands down it's super indepth but digestable, easily!
👍
Is there a python equivalent to something this powerful?
Thanks for your works!
From a fresh checkout, npm install; npm test
gives the following error:
Running "qunit" task
Running "qunit:files" (qunit) task
Verifying property qunit.files exists in config...OK
Testing geolib.test.html
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
npm ERR! weird error 8
npm ERR! not ok code 0
Are there any plans to add a function that checks if two polygons overlap?
Hey,
sorry i could not get that from the source. Does the function "isPointInside" needs to get the first coord pair as the last? So that it closes the polygon or is it smart enough to use the first as the last? :)
The library is definately lacking comprehensive unit tests. (Preferably using QUnit)
You have distance as meters everywhere in your documentation, but getCenter
returns kilometers.
[email protected]
Why does the function orderByDistance return a new array with a different shape to the original?
Convention says this method should return an array with the same contents as the source but sorted by distance.
isPointInside does not appear to work correctly when the polygon straddles the anti-meridian.
I maintain a system with lots of polygons that do straddle the antimeridian (I'm in Australia :-) so it's important to me.
see the test code below that illustrates the problem when it's run:
var geolib = require('geolib');
// -------------
// Test 1 This succeeds
// -------------
var polygon = [
{latitude: -46, longitude: 1},
{latitude: -46, longitude: 2},
{latitude: -47, longitude: 2},
{latitude: -47, longitude: 1}
];
console.log('Test 1: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:-46.5, longitude:1.5}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:-47.9, longitude:1.2}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:-46.8, longitude:1.1}, polygon))
console.log('------------');
// -------------
// Test 2 This succeeds
// -------------
var polygon = [
{latitude: 10, longitude: -10},
{latitude: 10, longitude: 10},
{latitude: 20, longitude: 10},
{latitude: 20, longitude: -10}
];
console.log('Test 2: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-5}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:15, longitude:-15}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-5}, polygon))
// ----------
// Test 3 : AntiMeridian 1: This fails
// ----------
var polygon = [
{latitude: 10, longitude: 175},
{latitude: 10, longitude: -175},
{latitude: 20, longitude: -175},
{latitude: 20, longitude: 175}
];
console.log('Test 3: Antimeridian 1: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:177}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:15, longitude:-170}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-176}, polygon))
console.log('------------');
// ----------
// Test 4: Antimeridian 2: This fails
// ----------
var polygon = [
{latitude: 10, longitude: -175},
{latitude: 10, longitude: 175},
{latitude: 20, longitude: 175},
{latitude: 20, longitude: -175}
];
console.log('Test 4: Antimeridian 2: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:177}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:15, longitude:-170}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-176}, polygon))
console.log('------------');
Hi,
first of all, you did a great job with this lib.
Thank you!
To get to the problem:
Either your doc describes the function incorrect or your implementation lacks of this functionality.
You say in your docs that it is possible to give the function an object as param. In the docs it is:
var spots = {
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
"Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619},
"London Eye": {latitude: 51.503333, longitude: -0.119722},
"Kremlin, Moscow": {latitude: 55.751667, longitude: 37.617778},
"Eiffel Tower, Paris": {latitude: 48.8583, longitude: 2.2945},
"Riksdag building, Stockholm": {latitude: 59.3275, longitude: 18.0675},
"Royal Palace, Oslo": {latitude: 59.916911, longitude: 10.727567}
}
geolib.getCenter(spots);
When I execute exactly this code I get returned "false".
A look in your geolib.js shows that you even don't support objects:
getCenter: function(coords) {
if (!coords.length) {
return false;
}
var X = 0.0;
var Y = 0.0;
...
geolib.getDistance(
{latitude: 0, longitude:179.9},
{latitude:0, longitude:1}
);
yeilds 19915057.
geolib.getDistance(
{latitude: 0, longitude:180},
{latitude:0, longitude:0});
yeilds NaN
geolib.getDistance(
{latitude: 0, longitude:-1},
{latitude:0, longitude:1});
yeilds 222639
var geolib = require('geolib');
var polygon = [
{latitude: -46, longitude: 1},
{latitude: -46, longitude: 2},
{latitude: -47, longitude: 2},
{latitude: -47, longitude: 1}
];
console.log(geolib.isPointInside({ latitude:-46, longitude:1}, polygon)) // --> false
console.log(geolib.isPointInside({ latitude:-46, longitude:2}, polygon)) // --> false
console.log(geolib.isPointInside({ latitude:-47, longitude:2}, polygon)) // --> false
console.log(geolib.isPointInside({ latitude:-47, longitude:1}, polygon)) // --> true
why get different result?
I am using geolib in a project. The getCenter() fn was not working, I followed the source code and was unable to determine why. I then tried running the basic examples from the README in the Chrome console and these returned the same errors I was receiving.
var spots = {
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
"Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619},
"London Eye": {latitude: 51.503333, longitude: -0.119722},
"Kremlin, Moscow": {latitude: 55.751667, longitude: 37.617778},
"Eiffel Tower, Paris": {latitude: 48.8583, longitude: 2.2945},
"Riksdag building, Stockholm": {latitude: 59.3275, longitude: 18.0675},
"Royal Palace, Oslo": {latitude: 59.916911, longitude: 10.727567}
}
geolib.getCenter(spots);
returns "false"
geolib.getCenter([
{latitude: 52.516272, longitude: 13.377722},
{latitude: 51.515, longitude: 7.453619},
{latitude: 51.503333, longitude: -0.119722}
])
throws error Uncaught TypeError: Cannot read property 'latitude' of undefined
I implemented a method to calculate bearing between points which could be useful. Should we add this?
// From http://www.movable-type.co.uk/scripts/latlong.html
var bearingFromPoints = function (pointA, pointB) {
var lat1 = pointA.latitude;
var lon1 = pointA.longitude;
var lat2 = pointB.latitude;
var lon2 = pointB.longitude;
// var dLat = (lat2-lat1).toRad(); // unused
var dLon = (lon2-lon1).toRad();
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x).toDeg();
return brng;
};
I'm doing this now:
$http.get('/locations').success(function(locations) {
var log = [];
angular.forEach(locations, function(item, index) {
var isPointInCircle = geolib.isPointInCircle(
{latitude: $localStorage.latitude, longitude: $localStorage.longitude},
{latitude: item.address.latitude, longitude: item.address.longitude},
(100*1000)
);
if(!isPointInCircle)
{
locations.splice(index, 1);
}
}, log);
// orderByDistance HOW?
$scope.locations = locations;
}
But I want the results ordered by Distance.. how?
When using with AngularJS
shows a NullPointerException at 116 geolib.js
// TODO: check if point is an object
if(typeof point != 'object' ) {
changed to " if(typeof point != 'object' || point == null) {" and it works just fine
Hey, loving your library, would you mind adding it to Bower?
http://twitter.github.com/bower/
All you need to do is run this command in your project root:
bower register geolib git://github.com/manuelbieh/Geolib
Then it should be possible to install with:
bower install geolib
Bower should use your package.json
to find the right file to download, but if you want to get more specific you could add this component.json
file to the root of your project:
{
"name": "geolib",
"version": "x",
"main": "./geolib.js"
}
Cheers.
I stumbled this library, but unfortunately I do not see any licence-information.
Are you just lacking one because of priority-issues of other tasks or is this intented, so this library is proprietary?
When the distance is returned in getDistance method, is it possible to also return the bearing between the two points?
I would like to say that Point 1 is 500 meters northwest of Point 2, with "northwest" being the information I want. The library could simply return the angle as an integer between 0 and 360, inclusive, and I could write my own script to correlate the angle as a string direction.
It would be nice if it was possible to use the Coordinates object returned by the geolocation API.
But since the getKey function has this requirement:
return point.hasOwnProperty(val) ? (function() { key = val; return false; }()) : true;
And the Coordinates latitude & longitude is in the prototype, I get:
Cannot read property 'latitude' of undefined
When trying to calculate a distance.
Currently I'm just using:
{
'latitude':position.coords.latitude,
'longitude':position.coords.longitude
};
But it would just be nicer if I could use the object as it were. Anyways - great library. Thanks
if you call
convertUnit('mi', 0)
it returns 8 miles
convertUnit('mi',0.00001) works fine
This is due to the rounding on the unit multiplication
The readme file states for the isPointInside
method that the polygon coords must be in correct order!
Can you please clarify what the 'correct' order is and how can I check if my coords are in the correct order.
From the README.md:
Works with:
- latitude:
latitude
,lat
, 0 (GeoJSON array)- longitude:
longitude
,lng
,lon
, 1 (GeoJSON array)
When these functions are called with an array longitude returns the first element and latitude returns the second element.
It would be great if we had something like that:
distanceToLine(point, lineStart, lineEnd) //returns distance in meters to nearest point of the line.
BTW thx for your GeoLib!!!
getDistance
begins with:
var s = this.coords(start);
var e = this.coords(end);
so if the coordinates are passed as [lng, lat]
pairs, they will be converted.
Why not doing the same for getCenter
? It should also begin with:
coords = coords.map(this.coords);
P.S. The [lng, lat]
form is much more common among those who use GeoJson.
On line 110 of geolib.js the lookup of the latitude, longitude and elevation fields on the point object is currently the following:
return point.hasOwnProperty(val) ? (function() { key = val; return false; }()) : true;
The issue I'm seeing is for some object that are created by frameworks (loopback for instance), the fields are set on the prototype and not the object itself. hasOwnProperty does not check the prototype values, and as such I'm seeing undefined exceptions being thrown when the json debugging output looks fine.
I have done a local test and changed the line to the following:
return val in point ? (function() { key = val; return false; }()) : true;
The in keyword returns the proper fields and everything works as expected.
What's the correct polygon coords order?
An updated readme would be good.
It seems that there's redundant rounding in getDistanceSimple. I think this rounding can be removed: https://github.com/manuelbieh/Geolib/blob/master/src/geolib.js#L385
It's possible I'm just missing the point, though.
Is there a way to achieve sub-meter accuracy? I'm pretty sure the raw distance has more resolution than meters before rounding. Thanks!
Hi,
Do you plan adding geohash
support (input and/or formatting)?
https://en.wikipedia.org/wiki/Geohash
The docs say that getPathLength() returns distance in KM but the result is in Meters for me. Is that a mistake in the docs?
Hello, I'm having a list of items. It are div
's with a data-lat="1234"
and data-lng="1234"
. How can I make use of orderByDistance
, that it'll order all items by distance?
This, just to help your users know where they are and thank you for getting this out!
Hi there, feature request here! :)
It would be ideal if convertUnit()
would convert other units into meters, instead of just meters into other units.
Reason being, I have an API interface that outputs distances in various desired units, and it should be able to take in distances in other units as well, with my primary logic working in meters.
Thanks for the module!
Why isn't the following working:
<input type="number" id="distance" value="10">
<input type="text" id="userLatitude" value="52.880709">
<input type="text" id="userLongitude" value="5.4560155">
<button id="search">go</button>
$(document).on('click', '#search', function(){
var userLatitude = $('#userLatitude').val();
var userLongitude = $('#userLongitude').val();
var user = { latitude: userLatitude, longitude: userLongitude };
var distance = parseInt($('#distance').val()) * 100;
console.log(distance);
$('.user').hide();
$('.user').each(function(index, value) {
var talent = $(this).data('talent');
var lat = $(this).data('latitude');
var lng = $(this).data('longitude');
var current = { latitude: lat, longitude: lng };
var calculated = geolib.isPointInCircle(
current,
user,
distance
);
if(calculated){
$(this).show();
}
console.log(calculated);
});
return false;
});
How to order this all by Distance? So the closest one shows first.
/**
* Checks if a value is in decimal format or, if neccessary, converts to decimal
*
* @param mixed Value to be checked/converted
* @return float Coordinate in decimal format
*/
useDecimal: function(value) {
value = value.toString().replace(/\s*/, '');
// looks silly but works as expected
// checks if value is in decimal format
if (!isNaN(parseFloat(value)) && parseFloat(value) == value) {
return parseFloat(value);
// checks if it's sexagesimal format (HHH° MM' SS" (NESW))
} else if (geolib.isSexagesimal(value) === true) {
return parseFloat(geolib.sexagesimal2decimal(value));
} else {
throw new Error('Unknown format.');
}
},
Seems like something happened with the latest commit because I can install previous versions but 2.0.7 fails. Did it not get updated properly in the repository?
Hi, I manually tried calculating the circle between two points with: http://www.movable-type.co.uk/scripts/latlong.html
Distance: 19.43 km
But when I'm trying this code:
var user = geolib.useDecimal({
lat: document.getElementById('userLatitude').value,
lng: document.getElementById('userLongitude').value
});
var distance = parseInt($('#rangeslider').val()) * 1000;
var current = geolib.useDecimal({
lat: $(this).data('latitude'),
lng: $(this).data('longitude')
});
var calculated = geolib.isPointInCircle(
current,
user,
distance
);
When input distance is 5 (so 5000), it already tells me the points are in the circle.
I'm getting this error when I require
geolib:
ReferenceError: geolib is not defined
at Object.<anonymous> (./node_modules/geolib/geolib.js:818:1)
Moving everything starting at line 810 so it's inside the anonymous function fixes it for me.
Hello.
First, this lib is just awesome and i wanna thank you for creating and sharing it!
Now my question, why do coords of the polygon in isPointInside do need to be in the correct order? The reason why i ask is because if i make some points on a piece of paper, there is only one way i can connect the points without a line crossing an other line.
Do they have to be in correct order because the way you wrote the function or do i oversee something and it is technically not possible? I completly understand that it might be much more work and you don`t want or have no time to do it, i am just curious.
Thanks!
Hi
thank you for this great library.
However, when I install geolib via npm the function computeDestinationPoint is not implemented. The same when I try to install it from github.
I have version 2.0.18 installed, any idea why this function is not there?
Andries
cc'ing @dandv
This could be a big misunderstanding on my part - I am still pretty new to Meteor. I am running Meteor 1.2 and have added geolib to my project via meteor add
. I can successfully access a global geolib object in my client code, but I was hoping to use it on my server with some large sets of location data - is that possible? I've tried using it in multiple ways - inside the Meteor.startup hook, inside of Meteor.methods, and geolib always shows up as an empty object {}.
Let me know if I should try anything in particular or import it in any special way, or if it simply isn't possible!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.