I have a Users table that contains latitude and longitude attribute for every user. So I need to calculate the distance between two users in AQL Query.
I have done the same in Orientdb with the below query.
var laltitude = CURRENT_USER_laltitude;
var longitude = CURRENT_USER_longitude;
var query = "select distance(latitude, longitude,"+laltitude+","+longitude+") as distance from users";
First, create a js file distance.js (or whatever you want to name it) and put below code as below.
/* distance.js */
'use strict';
function gdistance(latitude1, longitude1, latitude2, longitude2, radius) {
if (!latitude1 || !longitude1 || !latitude2 || !longitude2) {
return null;
};
var lat1 = Number(latitude1), lon1 = Number(longitude1);
var lat2 = Number(latitude2), lon2 = Number(longitude2);
radius = (radius === undefined) ? 6371e3 : Number(radius);
var R = radius;
var φ1 = (lat1 * Math.PI / 180), λ1 = (lon1 * Math.PI / 180);
var φ2 = (lat2 * Math.PI / 180), λ2 = (lon2 * Math.PI / 180);
var Δφ = φ2 - φ1;
var Δλ = λ2 - λ1;
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2)
+ Math.cos(φ1) * Math.cos(φ2)
* Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Meters
var d2 = d / 1000; // Meters to KM
return d2;
}
module.exports = gdistance;
Now open Arango Console with arangosh. This will open with _system database by default. So if you have other than this database like me then use db._useDatabase("myDatabase") command to change database.
Now write below commands to add custom to your desired database.
Version 2.8
db._useDatabase("myDatabase");
var aqlfunctions = require("org/arangodb/aql/functions");
var f = require("/path/to/file/distance.js");
aqlfunctions.register("geo::gdistance", f, true)
Version 3.0+
db._useDatabase("myDatabase");
var aqlfunctions = require("#arangodb/aql/functions");
var f = require("/path/to/distance.js");
i.e.
var f = require("/home/ubuntu/distance.js");
var f = require("distance.js");
# If you want to remove this group's UDFs (User defined functions)
# aqlfunctions.unregisterGroup("geo");
aqlfunctions.register("geo::gdistance", f, true);
Now use in your AQL queries as below.
LET distance = geo::gdistance(attrbute_name.latitude, attrbute_name.longitude, #your_latitude, #your_longitude)
For more references with here.
Currently ArangoDB can only give you distances when you use the Geo index to return you the distance of your search start to the point matching your condition:
FOR doc IN WITHIN(##collection, #lat, #long, #radius, #distanceAttributeName)
RETURN doc
You could however use a user defined AQL function to extend AQL. User defined functions are implemented in Javascript, which is luckily used by Chris Veness to explain howto calculate distances
With ArangoDB 3.0 we most probably will support the arithmetic operations to calculate this in AQL.
I will edit this post with more details and examples soon.
Related
I was calculating the distance between 2 points using google earth Ruler and haversine/Vincenty great-circle distance formula BUt result is not getting the same how can I get the same result as per google earth?
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
echo 'haversineGreatCircleDistance: '.$angle * $earthRadius;
}
haversineGreatCircleDistance( 22.098299, 90.394299,26.564095, 88.408258);
function vincentyGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$lonDelta = $lonTo - $lonFrom;
$a = pow(cos($latTo) * sin($lonDelta), 2) +
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
$angle = atan2(sqrt($a), $b);
echo 'vincentyGreatCircleDistance: '.$angle * $earthRadius;
}
vincentyGreatCircleDistance( 22.098299, 90.394299,26.564095, 88.408258);
function twopoints_on_earth($latitudeFrom, $longitudeFrom,$latitudeTo, $longitudeTo)
{
$long1 = deg2rad($longitudeFrom);
$long2 = deg2rad($longitudeTo);
$lat1 = deg2rad($latitudeFrom);
$lat2 = deg2rad($latitudeTo);
//Haversine Formula
$dlong = $long2 - $long1;
$dlati = $lat2 - $lat1;
$val = pow(sin($dlati/2),2)+cos($lat1)*cos($lat2)*pow(sin($dlong/2),2);
$res = 2 * asin(sqrt($val));
$radius = 3958.756;
return (($res*$radius)*1609.34);
}
// latitude and longitude of Two Points
$latitudeFrom = 22.098299 ;
$longitudeFrom = 90.394299;
$latitudeTo = 26.564095;
$longitudeTo = 88.408258;
// Distance between Mumbai and New York
print_r(twopoints_on_earth( $latitudeFrom, $longitudeFrom,$latitudeTo, $longitudeTo));
I have a RigidBody Node (3D), and I want to get, in global coordinates, the position of "target", given:
an angle "rot" (up is Y)
a Vector = 5 * Vector.FORWARD
It looks very easy.... first translate, then rotate, or if it's not working, first rotate, then rotate.
A to B -> If I only apply the translation, it works, no problem:
target = global_transform.translated(5 * Vector3.FORWARD).origin
But if I want B to C, it fails: it looks like it rotate around world origin
I tried many things during last few hours and read so many articles on Transform and now, I'm giving up. So... How would you do it?
We will define a position relative to your RigidBody(3D) by polar coordinates (namely a "rot" angle and a distance, I'll call it "d") on the XZ plane.
To be clear, we are going to define it in local space of the RigidBody(3D), and then move it to global coordinates.
We can do it using transforms. Which you would do if you need to preserve the rotation. Although it requires a little more work. Or, you can do it Using vectors only. Which is easier.
Below are the different approaches to do this.
Using Transforms
Local transform
We define combine the rotation and translation transformation like this:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
By the way, the order does not matter when using these methods. The result is this either way:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot) * Transform.IDENTITY.translated(d * Vector3.FORWARD)
Which gives a rotation around the origin at a given distance. However, if you do this:
var t = Transform.IDENTITY.translated(d * Vector3.FORWARD) * Transform.IDENTITY.rotated(Vector3.UP, rot)
You get a transform that is displaced forward, and then rotates in position.
Global transform
For the global transform, we can use the fact that this should be relative to the RidigBody(3D), and we have the global transform of it:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
var global_t = global_transform * t
Be aware that this will apply rotation, scaling, and so on. If you only wanted the global translation, you can do it like this:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
var global_t = Transform.IDENTITY.translated(global_transform.origin) * t
And no, t.translated(global_transform.origin) is not the same thing. When you use translated it is in the local coordinates of the transform on which you are calling it.
Local position
As you know, we can use the origin of the transform:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
var v = t.origin
Global position
Either you can use to_global:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
var v = t.origin
var global_v = to_global(v)
Or we can take origin from the global transform:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
var global_t = global_transform * t
var global_v = global_t.origin
If you only want the global translation, you can add global_transform.origin:
var t = Transform.IDENTITY.rotated(Vector3.UP, rot).translated(d * Vector3.FORWARD)
var v = t.origin
var global_v = global_transform.origin + v
Using Vectors Only
Local position
If you only care about the position, we can do this without the transforms altogether:
var v = (d * Vector3.FORWARD).rotated(Vector3.UP, rot)
Global position
And, of course, you can use to_global:
var v = (d * Vector3.FORWARD).rotated(Vector3.UP, rot)
var global_v = to_global(v)
And again, if you only want the global translation, you can add global_transform.origin:
var v = (d * Vector3.FORWARD).rotated(Vector3.UP, rot)
var global_v = global_transform.origing + v
After 1 more hour, I finally found something, but it's kind of ugly:
target = to_global(to_local(transform.translated(3 * Vector3.FORWARD).origin).rotated(Vector3.UP, rot))
If you have something better, please add your answer!
I'm trying to draw an SVG path in a PDF using itextsharp v5.
The approach I am following is roughly this:
Reading the SVG path from the SVG file (Svg.SvgPath)
Getting the list of segments from the path ({Svg.Pathing.SvgPathSegmentList})
Creating an iTextSharp PdfAnnotation and associate a PdfAppearance to it
Drawing each segment in the SvgPathSegmentList using the corresponding PdfContentByte method ( for SvgLineSegment I use PdfContentByte.LineTo, for SvgCubicCurveSegment I use PdfContentByte.CurveTo )
For most of the SvgPathSegments types, there is a clear mapping between values in the SvgPathSegments and the arguments in the PdfContentByte method. A few examples:
SvgMoveToSegment has the attribute End which is the target point (X, Y) and the PdfContentByte.MoveTo takes two parameters: X, Y
SvgLineSegment, very similar to the Move. It has the Target End and the PdfContentByte.LineTo takes two parameters X and Y and draws a line from the current position to the target point.
app.MoveTo(segment.Start.X, segment.Start.Y);
SvgCubicCurveSegment has all you need to create a Bezier curve (The Start point, the End point, and the first and second control point). With this I use PdfContentByte.CurveTo and get a curve in the PDF that looks exactly as it looks in the SVG editor.
var cubicCurve = (Svg.Pathing.SvgCubicCurveSegment)segment;
app.CurveTo(
cubicCurve.FirstControlPoint.X, cubicCurve.FirstControlPoint.Y,
cubicCurve.SecondControlPoint.X, cubicCurve.SecondControlPoint.Y,
cubicCurve.End.X, cubicCurve.End.Y);
The problem I have is with the ARC ("A" command in the SVG, SvgArcSegment)
The SvgArcSegment has the following values:
Angle
Start (X, Y)
End (X, Y)
RadiusX
RadiusY
Start
Sweep
On the other hand, PdfContentByte.Arc method expect:
X1, X2, Y1, Y2
StartAngle,
Extent
As per the itextsharp documentation, Arc draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2 starting (counter-clockwise) at StartAngle degrees and covering extent degrees. I.e. startAng=0 and extent=180 yield an openside-down semi-circle inscribed in the rectangle.
My question is: How to "map" the values in the SvgArcSegment created from the SVG A command into the arguments that PdfContentByte.Arc method expects.
I know that the Start and End values are indeed the origin and target of the curve I want, but no clue what RadiusX and RadiusY mean.
As #RobertLongson pointed in his comment, what I needed was to convert from Center to Endpoint Parametrization.
I'm posting my own C# implementation of the algorithm documented in the SVG documentation, just in case someone else needs it.
public static SvgCenterParameters EndPointToCenterParametrization(Svg.Pathing.SvgArcSegment arc)
{
//// Conversion from endpoint to center parameterization as in SVG Implementation Notes:
//// https://www.w3.org/TR/SVG11/implnote.html#ArcConversionEndpointToCenter
var sinA = Math.Sin(arc.Angle);
var cosA = Math.Cos(arc.Angle);
//// Large arc flag
var fA = arc.Size == Svg.Pathing.SvgArcSize.Large ? 1 : 0;
//// Sweep flag
var fS = arc.Sweep == Svg.Pathing.SvgArcSweep.Positive ? 1 : 0;
var radiusX = arc.RadiusX;
var radiusY = arc.RadiusY;
var x1 = arc.Start.X;
var y1 = arc.Start.Y;
var x2 = arc.End.X;
var y2 = arc.End.Y;
/*
*
* Step 1: Compute (x1′, y1′)
*
*/
//// Median between Start and End
var midPointX = (x1 - x2) / 2;
var midPointY = (y1 - y2) / 2;
var x1p = (cosA * midPointX) + (sinA * midPointY);
var y1p = (cosA * midPointY) - (sinA * midPointX);
/*
*
* Step 2: Compute (cx′, cy′)
*
*/
var rxry_2 = Math.Pow(radiusX, 2) * Math.Pow(radiusY, 2);
var rxy1p_2 = Math.Pow(radiusX, 2) * Math.Pow(y1p, 2);
var ryx1p_2 = Math.Pow(radiusY, 2) * Math.Pow(x1p, 2);
var sqrt = Math.Sqrt(Math.Abs(rxry_2 - rxy1p_2 - ryx1p_2) / (rxy1p_2 + ryx1p_2));
if (fA == fS)
{
sqrt = -sqrt;
}
var cXP = sqrt * (radiusX * y1p / radiusY);
var cYP = sqrt * -(radiusY * x1p / radiusX);
/*
*
* Step 3: Compute (cx, cy) from (cx′, cy′)
*
*/
var cX = (cosA * cXP) - (sinA * cYP) + ((x1 + x2) / 2);
var cY = (sinA * cXP) + (cosA * cYP) + ((y1 + y2) / 2);
/*
*
* Step 4: Compute θ1 and Δθ
*
*/
var x1pcxp_rx = (float)(x1p - cXP) / radiusX;
var y1pcyp_ry = (float)(y1p - cYP) / radiusY;
Vector2 vector1 = new Vector2(1f, 0f);
Vector2 vector2 = new Vector2(x1pcxp_rx, y1pcyp_ry);
var angle = Math.Acos(((vector1.x * vector2.x) + (vector1.y * vector2.y)) / (Math.Sqrt((vector1.x * vector1.x) + (vector1.y * vector1.y)) * Math.Sqrt((vector2.x * vector2.x) + (vector2.y * vector2.y)))) * (180 / Math.PI);
if (((vector1.x * vector2.y) - (vector1.y * vector2.x)) < 0)
{
angle = angle * -1;
}
var vector3 = new Vector2(x1pcxp_rx, y1pcyp_ry);
var vector4 = new Vector2((float)(-x1p - cXP) / radiusX, (float)(-y1p - cYP) / radiusY);
var extent = (Math.Acos(((vector3.x * vector4.x) + (vector3.y * vector4.y)) / Math.Sqrt((vector3.x * vector3.x) + (vector3.y * vector3.y)) * Math.Sqrt((vector4.x * vector4.x) + (vector4.y * vector4.y))) * (180 / Math.PI)) % 360;
if (((vector3.x * vector4.y) - (vector3.y * vector4.x)) < 0)
{
extent = extent * -1;
}
if (fS == 1 && extent < 0)
{
extent = extent + 360;
}
if (fS == 0 && extent > 0)
{
extent = extent - 360;
}
var rectLL_X = cX - radiusX;
var rectLL_Y = cY - radiusY;
var rectUR_X = cX + radiusX;
var rectUR_Y = cY + radiusY;
return new SvgCenterParameters
{
LlX = (float)rectLL_X,
LlY = (float)rectLL_Y,
UrX = (float)rectUR_X,
UrY = (float)rectUR_Y,
Angle = (float)angle,
Extent = (float)extent
};
}
I am using the following code to get the locations for the nearest clinics in kms the code works sweet. But what I cant understand is how to get around the parse object only returning 100 objects so I guess my question should be how do i return just a subset that match the current long and lat of the clinics.
I call the below functions in my viewdIdLoadMethod
List<Clinics> _clicics;
_clicics =GetAllNearestFamousPlaces (54.269412, -0.93399086);
public List<Clinics> GetAllNearestFamousPlaces(double currentLatitude,double currentLongitude)
{
List<Clinics> Caldistance = new List<Clinics>();
var query = ParseObject.GetQuery("clinics");
query.FindAsync().ContinueWith(t =>
{
IEnumerable<ParseObject> results = t.Result;
foreach (var obj in results)
{
double distance = Distance(currentLatitude, currentLongitude, obj.Get<double>("lat"), obj.Get<double>("long"));
if (distance < 25) //nearbyplaces which are within 25 kms
{
Clinics dist = new Clinics();
dist.Name = obj.Get<string>("Name");
dist.Latitute = obj.Get<double>("lat");
dist.Longitude =obj.Get<double>("long");
Caldistance.Add(dist);
}
}
});
return Caldistance;
}
private double Distance(double lat1, double lon1, double lat2, double lon2)
{
double theta = lon1 - lon2;
double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
dist = Math.Acos(dist);
dist = rad2deg(dist);
dist = (dist * 60 * 1.1515) / 0.6213711922; //miles to kms
return (dist);
}
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad)
{
return (rad * 180.0 / Math.PI);
}
This may not be syntactically correct - I don't actually use Parse so I'm guessing based on their docs
// assume your point of origin is 54.269412, -0.93399086
// each degree of lat/long is **roughly** 100 km so we'll fudge and +- .5 to narrow down the
// list of clinics
double lat = 54.269412;
double lng = -0.93399086;
double minLong = lng - 0.5;
double maxLong = lng + 0.5;
double minLat = lat - 0.5;
double maxLat = lat + 0.5;
var query = from clinic in ParseObject.GetQuery("clinics")
where clinic.Get<double>("lat") >= minLat
and clinic.Get<double>("lat") <= maxLat
and clinic.Get<double>("long") >= minLat
and clinic.Get<double>("long") <= maxLat
select clinic;
// 1000 is the max we can request at a time
query = query.Limit(1000);
// now execute your query to get the results, and then use your Distance() function to calculate
// the precise distance and remove results that are to far away, etc
I would like all the zip codes which are within a 5 miles distance of a zip code input by the user.
See you have the centre coordinates for the circle and radius of the circle say 5 miles, now for every cordinate point in database, check the distance between Centre and that point by applying distance formula and check if it is less than radius,
In short,
C = point that is the center of the area
P = point we want to check for being inside the area
R = radius of the area
P is inside the area if
||P-C|| <= R
EDIT-
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}