Calculate angle between 2 geographic coordinates in MATLAB - matlab

I'm trying to calculate the angle between 2 geographic (Latitude,Longitude) points in MATLAB. The points are:
(-65.226,125.5) and (-65.236,125.433).
I used the MATLAB function, azimuth, as:
azimuth(-65.226,125.5,-65.236,125.433)
I convert the result to radians, and plotting this using quiver, I get the following plot:
I want the red vector to point from the top right dot to the bottom left dot.
The points are at fairly high latitude (~65S), and the separation of the points is low (about 0.1 degrees). Thus, I can't really understand how the curvature of the earth could affect the azimuth prediction that much..
Does anyone have any experience with azimuth in MATLAB, or have a better suggestion to calculating the angle between the coordinate pairs?
Thanks!

Here you can detailed information and formulae on how to find angle between two latitude-longitude points.

Related

Question about Matlab's aerospace toolbox's gravitysphericalharmonic library

I'm currently trying to learn how to use the gravity models that Matlab's 'gravitysphericalharmonics' library has. In this documentation: https://www.mathworks.com/help/aerotbx/ug/gravitysphericalharmonic.html#mw_3bdd1e99-46be-4fd5-8b7c-6dd710546616
It gives us two examples:
Calculate the gravity in the x-axis at the equator on the surface of Earth. This example uses the default 120 degree model of EGM2008 with default warning actions.
gx = gravitysphericalharmonic([-6378.137e3 0 0])
Calculate the gravity at 25,000 m over the south pole of Earth.
[gx, gy, gz] = gravitysphericalharmonic([0 0 -6381.751e3],'EGM96','Error')
Here's my question, how did they get -6381.751e3 for the second question if they're just finding the gravity 25,000m above the surface of Earth? If Earth's radius is ~6378.137e3 meters, then calculating the gravity at 25,000m above Earth would just be the radius + 25,000 meters right?
Lastly, I wanted to just ask someone with more knowledge about this module why it was -6378.137e3 m for the first question. If we're taking the coordinate from just the surface of Earth, wouldn't that number have to be positive?
Sorry if this doesn't make any sense, I'm new to this stuff and I really want to learn! Thanks!
The first example uses a 3D position vector, in a coordinate system with its origin at the center of the planet (Earth). The position vector[-6378.137e3 0 0] is on the equator, as is [6378.137e3 0 0] (they are on opposite sides of the world). Any other position vector with magnitude 6378.137e3 and a Z component (the third number) of 0 would also be on the equator.
The Earth is not a perfect sphere; it is an oblate spheroid. The radius at the equator is 6378.137 km, but the radius at the poles is 6356.752 km. Their documentation is not great here, but they have arrived at the position vector in the second example by taking this polar radius and increasing the magnitude of the vector by 15 km. The Z component is negative because this example is at the South Pole; if the Z component were positive, it would be at the North Pole. Because it is negative, they have to add -15 km to get to a point that is 15 km above the surface (but if you were looking at a globe, and holding the globe so that north is up, that point would be below the globe).
They have probably used examples with negative values in the position vector precisely to ensure that people ask themselves the kinds of questions that you are asking.
The aerospace toolbox also provides a function ecef2lla to convert these position vectors into latitude, longitude, and altitude, which may help you understand them.

Ellipse distance metric for DBSCAN clustering

I am using the DBSCAN algorithm to determine clusters in a data set obtained by an automotive radar. The paper "Grid-Based DBSCAN for Clustering Extended Objects in Radar Data" from Dominik Kellner, Jens Klappstein and Klaus Dietmayer (link below) proposes a Grid-Based DBSCAN method. Therefore, the search radius epsilon variates in azimuth direction depending on the range. The radius in range direction stays constant. The normal DBSCAN is using Euclidean distance metric to determine the epsilon-neighbourhood where the search radius is the same in both directions. I cannot find out how to have an ellipse-search instead of a circular.
Do you know a distance metric that is working elliptical? Or, can you provide me with a short code that solves my problem? I am using MATLAB but the code can be in your prefered language.
Let's give an example so we talk about the same:
Consider a cartesian coordinate system with range in meters plotted against azimuth angle in degrees. The search distance in the range direction should be three meters (or possible observation points) in both directions from a centre point. In azimuth direction, the search radius should be five points in both directions.
If you cannot think of an elliptical solution, maybe a linear works as well.
Thank you for your help.
https://www.researchgate.net/profile/Dominik_Kellner2/publication/261127945_Grid-based_DBSCAN_for_clustering_extended_objects_in_radar_data/links/57742a7708aead7ba06e60b5.pdf

Calculate angle of matrix with topography for each triangle

I have a matrix with data of a topography, let's say several hills. I want to have information of the angle of each data point to the vertical line. Here are two examples:
If I consider a place near the foot of the hill that is totally flat, I have a degree of 90° (90° to the vertical line).
If I am at the steepest point of the hill, I have a lower angle of let's say 50°.
To compute this I guess I have to to connect all the topography data so that (at least) three near pixels form triangle. After that I have to compute the angle(s) of this triangle.
Can I use a existing algorithm?
If your heightmap is a matrix A then you could approximate the the gradient components of every inner point (without the egdes) by
Xgrad = (A(2:end-1,3:end)-A(2:end-1,1:end-2))/2;
Ygrad = (A(3:end,2:end-1)-A(1:end-2,2:end-1))/2;
The angulars will then be
deg = (pi/2 - atan(sqrt(Xgrad.^2 + Ygrad.^2),1))/pi*180;
Depending on your heightmap, differentiating numerically can produce "fuzzy" results. Maybe you have to do some blur-filtering in order to produce smoother gradients.

Arrange the vertices of a 3D convex polygonal plane in counter clockwise direction in MATLAB

I have a convex polygon in 3D. For simplicity, let it be a square with vertices, (0,0,0),(1,1,0),(1,1,1),(0,0,1).. I need to arrange these vertices in counter clockwise order. I found a solution here. It is suggested to determine the angle at the center of the polygon and sort them. I am not clear how is that going to work. Does anyone have a solution? I need a solution which is robust and even works when the vertices get very close.
A sample MATLAB code would be much appreciated!
This is actually quite a tedious problem so instead of actually doing it I am just going to explain how I would do it. First find the equation of the plane (you only need to use 3 points for this) and then find your rotation matrix. Then find your vectors in your new rotated space. After that is all said and done find which quadrant your point is in and if n > 1 in a particular quadrant then you must find the angle of each point (theta = arctan(y/x)). Then simply sort each quadrant by their angle (arguably you can just do separation by pi instead of quadrants (sort the points into when the y-component (post-rotation) is greater than zero).
Sorry I don't have time to actually test this but give it a go and feel free to post your code and I can help debug it if you like.
Luckily you have a convex polygon, so you can use the angle trick: find a point in the interior (e.g., find the midpoint of two non-adjacent points), and draw vectors to all the vertices. Choose one vector as a base, calculate the angles to the other vectors and order them. You can calculate the angles using the dot product: A · B = A B cos θ = |A||B| cos θ.
Below are the steps I followed.
The 3D planar polygon can be rotated to 2D plane using the known formulas. Use the one under the section Rotation matrix from axis and angle.
Then as indicated by #Glenn, an internal points needs to be calculated to find the angles. I take that internal point as the mean of the vertex locations.
Using the x-axis as the reference axis, the angle, on a 0 to 2pi scale, for each vertex can be calculated using atan2 function as explained here.
The non-negative angle measured counterclockwise from vector a to vector b, in the range [0,2pi], if a = [x1,y1] and b = [x2,y2], is given by:
angle = mod(atan2(y2-y1,x2-x1),2*pi);
Finally, sort the angles, [~,XI] = sort(angle);.
It's a long time since I used this, so I might be wrong, but I believe the command convhull does what you need - it returns the convex hull of a set of points (which, since you say your points are a convex set, should be the set of points themselves), arranged in counter-clockwise order.
Note that MathWorks recently delivered a new class DelaunayTri which is intended to superseded the functionality of convhull and other older computational geometry stuff. I believe it's more accurate, especially when the points get very close together. However I haven't tried it.
Hope that helps!
So here's another answer if you want to use convhull. Easily project your polygon into an axes plane by setting one coordinate zero. For example, in (0,0,0),(1,1,0),(1,1,1),(0,0,1) set y=0 to get (0,0),(1,0),(1,1),(0,1). Now your problem is 2D.
You might have to do some work to pick the right coordinate if your polygon's plane is orthogonal to some axis, if it is, pick that axis. The criterion is to make sure that your projected points don't end up on a line.

Dividing a geographic region

I have a certain geographic region defined by the bottom left and top right coordinates. How can I divide this region into areas of 20x20km. I mean in practial the shape of the earth is not flat it's round. The bounding box is just an approximation. It's not even rectangular in actual sense. It's just an assumption. Lets say the bottomleft coordinate is given by x1,y1 and the topright coordinate is given by x2,y2, the length of x1 to x2 at y1 is different than that of the length between x1 to x2 at y2. How can I overcome this issue
Actually, I have to create a spatial meshgrid for this region using matlab's meshgrid function. So that the grids are of area 20x20km.
meshgrid(x1:deltaY:x2,y1:deltaX:y2)
As you can see I can have only one deltaX and one deltaY. I want to choose deltaX and deltaY such that the increments create grid of size 20x20km. However this deltaX and deltaY are supposed to vary based upon the location. Any suggestions?
I mean lets say deltaX=del1. Then distance between points (x1,y1) to (x1,y1+del1) is 20km. BUt when I measure the distance between points (x2,y1) to (x2, y1_del1) the distance is < 20km. The meshgrid function above does creates mesh. But the distances are not consistent. Any ideas how to overcome this issue?
Bear in mind that 20km on the surface of the earth is a REALLY short distance, about .01 radians - so the area you're looking at would be approximated as flat for anything non-scientific. Assuming it is scientific...
To get something other than monotonic steps in meshgrid you should create a function which takes as its input your desired (x,y) and maps it relative to (x_0,y_0) and (x_max,y_max) in your units of choice. Here's an inline function demonstrating the idea of using a function for meshgrid steps
step=inline('log10(x)');
[x,y]=meshgrid(step(1:10),step(1:10));
image(255*x.*y)
colormap(gray(255))
So how do you determine what the function should be? That's hard for us to answer exactly without a little more information about what your data set looks like, how you're interacting with it, and what your accuracy requirements are. If you have access to the actual location at every point, you should vary one dimension at a time (if your data grid is aligned with your latitude grid, for example) and use a curve fit with model selection techniques (akaike/bayes criterion) to find the best function for your data.