I have two points making a line, I want to see if a point is ruffly on the line. To do this I need to create a bounding box/ a rectangle which has a width D outwards/ perpendicular to the line. I was following this website for guidance but seem to have implemented it wrongly. Any help would be appreciated.
private static bool IsInside(GPS Point1, GPS Point2)
{
GPS VectorV = new GPS()
{
Longitude = Point1.Longitude - Point2.Longitude,
Latitude = Point1.Latitude - Point2.Latitude
};
GPS VectorW = new GPS()
{
Longitude = -1 / VectorV.Longitude,
Latitude = -1 / VectorV.Latitude
};
double W = Math.Sqrt(Convert.ToDouble(VectorW.Latitude * VectorW.Latitude) + Convert.ToDouble(VectorW.Longitude * VectorW.Longitude));
GPS NewVector = new GPS()
{
Longitude = Convert.ToDecimal(Convert.ToDouble(VectorW.Longitude) / W),
Latitude = Convert.ToDecimal(Convert.ToDouble(VectorW.Latitude) / W),
};
decimal D = 5;
GPS DisplacmentVector = new GPS()
{
Longitude = (D / 2) * NewVector.Longitude,
Latitude = (D / 2) * NewVector.Latitude
};
GPS BPoint1 = new GPS() { Longitude = Point1.Longitude + DisplacmentVector.Longitude, Latitude = Point1.Latitude + DisplacmentVector.Latitude };
GPS BPoint2 = new GPS() { Longitude = Point1.Longitude - DisplacmentVector.Longitude, Latitude = Point1.Latitude - DisplacmentVector.Latitude };
GPS BPoint3 = new GPS() { Longitude = Point2.Longitude + DisplacmentVector.Longitude, Latitude = Point2.Latitude + DisplacmentVector.Latitude };
GPS BPoint4 = new GPS() { Longitude = Point2.Longitude - DisplacmentVector.Longitude, Latitude = Point2.Latitude - DisplacmentVector.Latitude };
}
public partial class GPS
{
public decimal Longitude { get; set; }
public decimal Latitude { get; set; }
public GPS() { }
public GPS(decimal longitude, decimal latitude) {
Longitude = longitude;
Latitude = latitude;
}
}
When you have a vector v= A to B = {xb-xa, yb-ya} = {vx, vy} there are two directions for its perpendicular: one heading right of A->B, the orther heading left of A->B.
One perpendicular vector is w1= {-vy, vx} (towards left), the other is w2= {vy, -vx} (towards right). Note than w1 = -w2.
To get the four corners you may use w1 or w2 or both. It's a matter of the used signs and the order of resultant points.
Let's use w1. Your definition is wrong. Use this instead:
GPS VectorW = new GPS()
{
Longitude = - VectorV.Latitude,
Latitude = VectorV.Longitude
};
You are overcomplicating things. There are 3 cases for the line:
Line parallel with x axis (y coordinate of both points are equal), easy to calculate
Line parallel with y axis (x coordinate of both points are equal), easy to calculate
for the rest you need dx and dy:
cos(θ) = dx / (D / 2) => dx = cos(θ) * (D / 2)
sin(θ) = dy / (D / 2) => dy = sin(θ) * (D / 2)
θ = 90 - φ
tan(φ) = a / b => φ = atan(a / b)
Thats it.
In the end I decided to go for a different approach and instead found the distance away from the line the point was. It follows the same logic that it must be X distance away from the line.
For guidance I followed/ used this code.
Related
I'm writing an app that calculates covered distance in real time. I'm doing this by comparing my actual postion to the previous one and count the distance between them (using the Haversine formula) and sum everything up. This gives me some result, the distance is growing everything seems to be working. BUT the problem is with the accuracy. I tested this app on a route that is ~15km long many times and the covered distance is always bigger than my car's counter says. The difference is ALWAYS different - from 500m to even 2km.
This is my Geolocator object:
Geolocator gl = new Geolocator() {
DesiredAccuracy = PositionAccuracy.High, MovementThreshold = 20, ReportInterval = 50
};
In the constructor I declare that when the position is changed, "OnPositionChanged" method should be fired and also the method to find my actual location:
gl.PositionChanged += OnPositionChanged;
setMyLocation();
This is the "OnPositionChanged()" method:
async private void OnPositionChanged(Geolocator sender, PositionChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
setMyLocation();
});
}
This is the setMyLocation() method:
private async void setMyLocation()
{
try
{
p = new Position();
location = await gl.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(5));
p.Latitude = location.Coordinate.Point.Position.Latitude;
p.Longitude = location.Coordinate.Point.Position.Longitude;
obla = location.Coordinate.Point.Position.Latitude;
oblo = location.Coordinate.Point.Position.Longitude;
if (prev_location.Latitude == 0)
{
prev_location = p;
}
positions.Add(new BasicGeoposition() {
Latitude = location.Coordinate.Latitude,
Longitude = location.Coordinate.Longitude
});
myMap.Children.Remove(myCircle);
myCircle = new Ellipse();
myCircle.Fill = new SolidColorBrush(Colors.Green);
myCircle.Height = 17;
myCircle.Width = 17;
myCircle.Opacity = 50;
myMap.Children.Add(myCircle);
MapControl.SetLocation(myCircle, location.Coordinate.Point);
MapControl.SetNormalizedAnchorPoint(myCircle, new Point(0, 0));
routeKM += (Distance(p, prev_location));
Distance_txt.Text = routeKM.ToString("f2") + " km";
prev_location = p;
}
catch
{
}
}
This is my double (routeKM) counted with the Haversine formula:
public double Distance(Position pos1, Position pos2)
{
var R = 6371d; // Radius of the earth in km
var dLat = Deg2Rad(pos2.Latitude - pos1.Latitude); // deg2rad below
var dLon = Deg2Rad(pos2.Longitude - pos1.Longitude);
var a = Math.Sin(dLat / 2d) * Math.Sin(dLat / 2d) +
Math.Cos(Deg2Rad(pos1.Latitude)) * Math.Cos(Deg2Rad(pos2.Latitude)) *
Math.Sin(dLon / 2d) * Math.Sin(dLon / 2d);
var c = 2d * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1d - a));
var d = R * c; // Distance in km
return d;
}
double Deg2Rad(double deg)
{
return deg * (Math.PI / 180d);
}
So my question is: how to improve the accuracy? I'm not happy that my car's counter shows even 2km less than my app.
By doing DesiredAccuracy = PositionAccuracy.High you already suggested Windows Phone OS to look for highest accuracy.
I think you should change your distance finding logic and see if it works.
Use GeoCoordinate.GetDistanceTo as suggested in following answer.
https://stackoverflow.com/a/6366657/744616
I am trying to implement a function that will give me the GEO location (Lat,Long) given 3 GEO reference points and radius away from each point.
The signature for the function I'm looking for is:
public static GeoLocation Triangle(GeoLocation pos1, double r1, GeoLocation pos2,
double r2, GeoLocation pos3, double r3)
As example, 3 friends meet up somewhere secret. Each one can only tell me where he/she lives (GeoLocation = lat,long) and how far they are meeting from their house (r = radius). Given 3 such reference points (from all 3 friends), I should have sufficient information to calculate this secret meeting point as a GeoLocation.
This problem is very similar to the mobile / towers problem where you triangulate a mobile by measuring individual signal strengths from a few towers.
I have tried to find formulas online for quite some time now, which is why I'm posting my question here on Stack Overflow.
I will appreciate it if you could help me fill in the formula (Triangle method) - Thanks.
Code I have so far:
public class GeoLocation
{
private double _latitude;
private double _longitude;
public GeoLocation(double latitude, double longitude)
{
this._latitude = latitude;
this._longitude = longitude;
}
//Tested and working!
public double DistanceToKm(GeoLocation loc)
{
double lat1, lon1, lat2, lon2;
lat1 = this._latitude;
lon1 = this._longitude;
lat2 = loc._latitude;
lon2 = loc._longitude;
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;
}
}
Code which I think is not needed, but for what it's worth:
public static Coords ToCoord(GeoLocation pos)
{
var x = Math.Cos(pos._longitude) * Math.Cos(pos._latitude);
var y = Math.Sin( pos._longitude) * Math.Cos(pos._latitude);
var z = Math.Sin(pos._latitude);
return new Coords(x,y,z);
}
class Coords
{
public double x;
public double y;
public double z;
public Coords(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
Seems this is the solution after all.
https://gis.stackexchange.com/questions/66/trilateration-using-3-latitude-and-longitude-points-and-3-distances
... far more complicated than school geometry #DrKoch
Here is the Python solution:
yC = earthR *(math.cos(math.radians(LatC)) * math.sin(math.radians(LonC)))
zC = earthR *(math.sin(math.radians(LatC)))
P1 = array([xA, yA, zA])
P2 = array([xB, yB, zB])
P3 = array([xC, yC, zC])
#from wikipedia
#transform to get circle 1 at origin
#transform to get circle 2 on x axis
ex = (P2 - P1)/(numpy.linalg.norm(P2 - P1))
i = dot(ex, P3 - P1)
ey = (P3 - P1 - i*ex)/(numpy.linalg.norm(P3 - P1 - i*ex))
ez = numpy.cross(ex,ey)
d = numpy.linalg.norm(P2 - P1)
j = dot(ey, P3 - P1)
#from wikipedia
#plug and chug using above values
x = (pow(DistA,2) - pow(DistB,2) + pow(d,2))/(2*d)
y = ((pow(DistA,2) - pow(DistC,2) + pow(i,2) + pow(j,2))/(2*j)) - ((i/j)*x)
# only one case shown here
z = sqrt(pow(DistA,2) - pow(x,2) - pow(y,2))
#triPt is an array with ECEF x,y,z of trilateration point
triPt = P1 + x*ex + y*ey + z*ez
#convert back to lat/long from ECEF
#convert to degrees
lat = math.degrees(math.asin(triPt[2] / earthR))
lon = math.degrees(math.atan2(triPt[1],triPt[0]))
print lat, lon`
I am calculating center Lat/Lng fron list of available Lat/Lng using C# and rendering on OpenLayer Map.
I observed the calculation of getting center lat/lng will give me slightly difference in lat/lng. I am referring this link for the calculation
Calculate the center point of multiple latitude/longitude coordinate pairs.
C# Code:
static void Main(string[] args)
{
List<GeoCoordinate> listCoordinate = new List<GeoCoordinate>();
listCoordinate.Add(new GeoCoordinate() { Latitude = 22.9833, Longitude = 72.5000 }); //Sarkhej
listCoordinate.Add(new GeoCoordinate() { Latitude = 18.9750, Longitude = 72.8258 }); //Mumbai
listCoordinate.Add(new GeoCoordinate() { Latitude = 22.3000, Longitude = 73.2003 }); //Vadodara
listCoordinate.Add(new GeoCoordinate() { Latitude = 26.9260, Longitude = 75.8235 }); //Jaipur
listCoordinate.Add(new GeoCoordinate() { Latitude = 28.6100, Longitude = 77.2300 }); //Delhi
listCoordinate.Add(new GeoCoordinate() { Latitude = 22.3000, Longitude = 70.7833 }); //Rajkot
GeoCoordinate centerCoordinate = GetCentralGeoCoordinate(listCoordinate); //Output (Latitude:23.696708071960074, Longitude:73.681549202080149)
Console.WriteLine("Lat:" + centerCoordinate.Latitude + ",Lon:" + centerCoordinate.Longitude);
Console.ReadKey();
}
public static GeoCoordinate GetCentralGeoCoordinate(List<GeoCoordinate> geoCoordinates)
{
if (geoCoordinates.Count == 1)
{
return geoCoordinates.Single();
}
double x = 0, y = 0, z = 0;
foreach (var geoCoordinate in geoCoordinates)
{
var latitude = geoCoordinate.Latitude * Math.PI / 180;
var longitude = geoCoordinate.Longitude * Math.PI / 180;
x += Math.Cos(latitude) * Math.Cos(longitude);
y += Math.Cos(latitude) * Math.Sin(longitude);
z += Math.Sin(latitude);
}
var total = geoCoordinates.Count;
x = x / total;
y = y / total;
z = z / total;
var centralLongitude = Math.Atan2(y, x);
var centralSquareRoot = Math.Sqrt(x * x + y * y);
var centralLatitude = Math.Atan2(z, centralSquareRoot);
return new GeoCoordinate(centralLatitude * 180 / Math.PI, centralLongitude * 180 / Math.PI);
}
Javascrip Code:
var arrLonLat = [
{'Lon' : 72.5000, 'Lat' : 22.9833},
{'Lon' : 72.8258, 'Lat' : 18.9750},
{'Lon' : 73.2003, 'Lat' : 22.3000},
{'Lon' : 75.8235, 'Lat' : 26.9260},
{'Lon' : 77.2300, 'Lat' : 28.6100},
{'Lon' : 70.7833, 'Lat' : 22.3000}];
var centerLonLat = getCenterLonLat(arrLonLat);
var lonLatSarkhej = new OpenLayers.LonLat(arrLonLat[0].Lon,arrLonLat[0].Lat).transform(epsg4326,projectTo);
var lonLatMumbai = new OpenLayers.LonLat(arrLonLat[1].Lon,arrLonLat[1].Lat).transform(epsg4326,projectTo);
var lonLatVadodara = new OpenLayers.LonLat(arrLonLat[2].Lon,arrLonLat[2].Lat).transform(epsg4326,projectTo);
var lonLatJaipur = new OpenLayers.LonLat(arrLonLat[3].Lon,arrLonLat[3].Lat).transform(epsg4326,projectTo);
var lonLatDelhi = new OpenLayers.LonLat(arrLonLat[4].Lon,arrLonLat[4].Lat).transform(epsg4326,projectTo);
var lonLatRajkot = new OpenLayers.LonLat(arrLonLat[5].Lon,arrLonLat[5].Lat).transform(epsg4326,projectTo);
//Center Point of Average Markers
var lonLatCenter = new OpenLayers.LonLat(73.681549202080149,23.696708071960074).transform(epsg4326,projectTo);
var markers = new OpenLayers.Layer.Markers("Markers");
map.addLayer(markers);
var size = new OpenLayers.Size(24,24);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('icon/Marker-Pink.png', size, offset);
var iconCenter = new OpenLayers.Icon('icon/Marker-Green.png', size, offset);
markers.addMarker(new OpenLayers.Marker(lonLatSarkhej,icon)); //Sarkhej
markers.addMarker(new OpenLayers.Marker(lonLatMumbai,icon.clone())); //Mumbai
markers.addMarker(new OpenLayers.Marker(lonLatVadodara,icon.clone())); //Vadodara
markers.addMarker(new OpenLayers.Marker(lonLatJaipur,icon.clone())); //Jaipur
markers.addMarker(new OpenLayers.Marker(lonLatDelhi,icon.clone())); //Delhi
markers.addMarker(new OpenLayers.Marker(lonLatRajkot,icon.clone())); //Rajkot
I am rendering 6 different location with Pink marker, and center with Green marker.
Please see below image for more clarification.
Now have drawn box to get idea about the center marker (Green) which is not actually center. I think it should be positioned on Green dot which is crossed by green Horizontal and Vertical line.
Can anybody let me know, does my center point is calculated correctly
or not?
Why it is not display at center of the box?
I have also added Ruler for calculation of center point.
Please help me to find the actual solution, please do let me know if you need more details for the same.
The correct center depends actually on the definition for center and there are several possibilites:
One might calculate the smallest rectangle containing all points and
use the center of this rectangle as you did with your green lines
and green point. (This is unusual)
One might ask which point is closest to all other points, i.e.
find a center point so that all distances between this center and
all other points are smallest (abs-norm, used for practical
problems)
One might ask which center point results in the least error, i.e.
the smallest (sum of) quadratic distances to all other points
(used ins math, statistics, etc)
You see, depending on the definition, you'll have to use a different algorithm and will arrive on a different center point.
The algorithm you show in your question seems to calculate (2.)
How to deal with big, WGS84 points coordinates?
What I think, I can do, is to translate world (WGS84) points, to points on screen in pixels. Is it a good way? It still doesn't work corectly because big zoom is needed, and I would have to change unit from meters to milimeters (but how? just multiplying the x, and y of points?).
Here's a very simplistic approach for this mapping problem. Geographers will probably cry about this, but it works quite well in practice as long as the coordinates are below about 70° latitude and the size of the window is not too big. Also, don't try to directly map large objects (such as very long lines) only using their start and end points.
public PointF GeoCoordToPixel(IGeographicPosition geoPos)
{
double tempLong = geoPos.Longitude;
if (tempLong > CenterPos.Longitude && (tempLong - CenterPos.Longitude) > 180)
{
// the position is to the left, over the antimeridian
tempLong = tempLong - 360;
}
if (tempLong < CenterPos.Longitude && (CenterPos.Longitude - tempLong) > 180)
{
// the position is to the right, over the antimeridian
tempLong = tempLong + 360;
}
PointF pt = new PointF(
(float)((tempLong - LongitudeOfOrigin) / LongitudeIncrement),
(float)((-geoPos.Latitude + LatitudeOfOrigin) / LatitudeIncrement));
return pt;
}
with CenterPos = Center of window; LatituteOfOrigin / LongitudeOfOrigin = Top left position of window; LongitudeIncrement / LatitudeIncrement = Scale of view. Their relation is:
LatitudeOfOrigin = CenterPos.Latitude + (m_drawingBuffer.Height / 2.0 * LatitudeIncrement);
LongitudeOfOrigin = CenterPos.Longitude - (m_drawingBuffer.Width / 2.0 * LongitudeIncrement);
and the inverse:
public CGeographicPosition PixelToGeoCoord(PointF pt)
{
double latitude = -(pt.Y * LatitudeIncrement) + LatitudeOfOrigin;
double longitude = (pt.X * LongitudeIncrement) + LongitudeOfOrigin;
if (longitude > 180)
{
longitude -= 360;
}
return (new CGeographicPosition(latitude, longitude, 0));
}
Not really difficult, is it?
I have a list of coordinates which will be considered as a center point of the map.
I need to calculate bounds of the map at zoom level 16 using only C# (no javascript no map object).
Is this possible?
Please advise.
Here is something I wanted. I have not written this class personal and found in somewhere here and I don't remember so the credit goes X person! Provided Latitude and Longitude and a radius in KM gives a bounding box. Somewhat what GMap gives on viewport Bounds.
public class GlobalMercator
{
public class MapPoint
{
public double Longitude { get; set; } // In Degrees
public double Latitude { get; set; } // In Degrees
}
public class BoundingBox
{
public MapPoint MinPoint { get; set; }
public MapPoint MaxPoint { get; set; }
}
// Semi-axes of WGS-84 geoidal reference
private const double WGS84_a = 6378137.0; // Major semiaxis [m]
private const double WGS84_b = 6356752.3; // Minor semiaxis [m]
// 'halfSideInKm' is the half length of the bounding box you want in kilometers.
public static BoundingBox GetBoundingBox(MapPoint point, double halfSideInKm)
{
// Bounding box surrounding the point at given coordinates,
// assuming local approximation of Earth surface as a sphere
// of radius given by WGS84
var lat = Deg2rad(point.Latitude);
var lon = Deg2rad(point.Longitude);
var halfSide = 1000 * halfSideInKm;
// Radius of Earth at given latitude
var radius = WGS84EarthRadius(lat);
// Radius of the parallel at given latitude
var pradius = radius * Math.Cos(lat);
var latMin = lat - halfSide / radius;
var latMax = lat + halfSide / radius;
var lonMin = lon - halfSide / pradius;
var lonMax = lon + halfSide / pradius;
return new BoundingBox
{
MinPoint = new MapPoint { Latitude = Rad2deg(latMin), Longitude = Rad2deg(lonMin) },
MaxPoint = new MapPoint { Latitude = Rad2deg(latMax), Longitude = Rad2deg(lonMax) }
};
}
// degrees to radians
private static double Deg2rad(double degrees)
{
return Math.PI * degrees / 180.0;
}
// radians to degrees
private static double Rad2deg(double radians)
{
return 180.0 * radians / Math.PI;
}
// Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
private static double WGS84EarthRadius(double lat)
{
// http://en.wikipedia.org/wiki/Earth_radius
var An = WGS84_a * WGS84_a * Math.Cos(lat);
var Bn = WGS84_b * WGS84_b * Math.Sin(lat);
var Ad = WGS84_a * Math.Cos(lat);
var Bd = WGS84_b * Math.Sin(lat);
return Math.Sqrt((An * An + Bn * Bn) / (Ad * Ad + Bd * Bd));
}
}