How to find the nearest points to given coordinates with MATLAB?

后端 未结 2 1494
南旧
南旧 2021-01-20 17:59

I need to solve a minimization problem with Matlab and I\'m wondering which is the easiest solution. All the potential solutions that I\'ve been thinking in require lot of p

相关标签:
2条回答
  • 2021-01-20 17:59

    This is always a fun one :)

    First off: Mohsen Nosratinia's answer is OK, as long as

    • you don't need to know the actual distance
    • you can guarantee with absolute certainty that you will never go near the polar regions
    • and will never go near the ±180° meridian

    For a given latitude, -180° and +180° longitude are actually the same point, so simply looking at differences between angles is not sufficient. This will be more of a problem in the polar regions, since large longitude differences there will have less of an impact on the actual distance.

    Spherical coordinates are very useful and practical for purposes of navigation, mapping, and that sort of thing. For spatial computations however, like the on-surface distances you are trying to compute, spherical coordinates are actually pretty cumbersome to work with.

    Although it is possible to do such calculations using the angles directly, I personally don't consider it very practical: you often have to have a strong background in spherical trigonometry, and considerable experience to know its many pitfalls -- very often there are instabilities or "special points" you need to work around (the poles, for example), quadrant ambiguities you need to consider because of trig functions you've introduced, etc.

    I've learned to do all this in university, but I also learned that the spherical trig approach often introduces complexity that mathematically speaking is not strictly required, in other words, the spherical trig is not the simplest representation of the underlying problem.

    For example, your distance problem is pretty trivial if you convert your latitudes and longitudes to 3D Cartesian X,Y,Z coordinates, and then find the distances through the simple formula

    distance (a, b) = R · arccos( a/|a| · b/|b| )

    where a and b are two such Cartesian vectors on the sphere. Note that |a| = |b| = R, with R = 6371 the radius of Earth.

    In MATLAB code:

    % Some example coordinates (degrees are assumed)
    lon = 360*rand(2030, 1354);
    lat = 180*rand(2030, 1354) - 90;
    
    % Your point of interest
    P = [4, 54];
    
    % Radius of Earth
    RE = 6371;
    
    % Convert the array of lat/lon coordinates to Cartesian vectors
    % NOTE: sph2cart expects radians
    % NOTE: use radius 1, so we don't have to normalize the vectors
    [X,Y,Z] = sph2cart( lon*pi/180,  lat*pi/180, 1);
    
    % Same for your point of interest    
    [xP,yP,zP] = sph2cart(P(1)*pi/180, P(2)*pi/180, 1);
    
    % The minimum distance, and the linear index where that distance was found
    % NOTE: force the dot product into the interval [-1 +1]. This prevents 
    % slight overshoots due to numerical artifacts
    dotProd = xP*X(:) + yP*Y(:) + zP*Z(:);
    [minDist, index] = min( RE*acos( min(max(-1,dotProd),1) ) );
    
    % Convert that linear index to 2D subscripts
    [ii,jj] = ind2sub(size(lon), index)
    

    If you insist on skipping the conversion to Cartesian and use lat/lon directly, you'll have to use the Haversine formula, as outlined on this website for example, which is also the method used by distance() from the mapping toolbox.

    Now, all of this is valid for the whole Earth, provided you find the smooth spherical Earth accurate enough an approximation. If you want to include the Earth's oblateness or some higher order shape model (or God forbid, distances including terrain), you need to do far more complicated stuff. But I don't think that is your goal here :)

    PS - I wouldn't be surprised that if you would write everything out that I did, you'll probably re-discover the Haversine formula. I just prefer to be able to calculate something as simple as distances along the sphere from first principles alone, rather than from some black box formula you had implanted in your head sometime long ago :)

    0 讨论(0)
  • 2021-01-20 18:07

    Let Lat and Long denote latitude and longitude matrices, then

    dist2=sum(bsxfun(@minus, cat(3,A,B), cat(3,Lat,Long)).^2,3);
    [I,J]=find(dist2==min(dist2(:)));
    

    I and J contain the indices in A and B that correspond to nearest point. Note that if there are multiple answers, I and J will not be scalar values, but vectors.

    0 讨论(0)
提交回复
热议问题