+ Reply to Thread
Results 1 to 12 of 12

Finding closest location to a point, as well as the next two closest.

  1. #1
    Registered User
    Join Date
    11-04-2021
    Location
    USA
    MS-Off Ver
    365
    Posts
    13

    Finding closest location to a point, as well as the next two closest.

    Hello everyone, I've got a data set of temperature recording stations, their lat / lon, and a list of zip codes with their corresponding geographic center lat / lon. I've got a function that will find the closest station for a given zip code, but for a number of reasons the closest may not be the best choice, so I would like to also return the second and third closest stations as well. I don't intend for this data to really be interacted with by anyone using the full workbook so I don't want to set it up as a pivot and make everyone manually transcribe information over.
    I've got a nice function that when I give it lat / lon values from my zip code list will search through the list of stations and show the nearest, now I'd just like to somehow show the next two closest.

    Please Login or Register  to view this content.
    *Note: the zip code data was removed and just shown as the locations on the few station locations included so the formulas evaluate correctly.
    Attached Files Attached Files
    Last edited by SolarDesign; 11-10-2021 at 07:32 PM.

  2. #2
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Finding closest location to a point, as well as the next two closest.

    see attachment (same formula)
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert
    Join Date
    10-15-2018
    Location
    MA, USA
    MS-Off Ver
    2010, 2019
    Posts
    1,616

    Re: Finding closest location to a point, as well as the next two closest.

    Changes to your existing formula are highlighted in red:

    Change the highlighted 2 to 3 to get the 3rd closest

    It's the same formula change essentially as Bsalv, except I leave the layout to you.

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Geoff

    Did I help significantly? If you wish, click on * Add Reputation to say thanks.
    If your problem has been resolved please select ?Solved? from the Thread Tools menu

  4. #4
    Registered User
    Join Date
    11-04-2021
    Location
    USA
    MS-Off Ver
    365
    Posts
    13

    Re: Finding closest location to a point, as well as the next two closest.

    Thank you for the responses, now I'm just working out how to calculate the distance between the lat/lon points to show how far the stations are from the given point.

  5. #5
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Finding closest location to a point, as well as the next two closest.

    distance = my column Y ?

  6. #6
    Registered User
    Join Date
    11-04-2021
    Location
    USA
    MS-Off Ver
    365
    Posts
    13

    Re: Finding closest location to a point, as well as the next two closest.

    Quote Originally Posted by bsalv View Post
    distance = my column Y ?
    When I adapted your setup to my full set the distances in that column weren't accurate, so I tried tweaking that, and also using
    Please Login or Register  to view this content.
    (I tried as both the converted radian values, and the decimal values already present, which either showed thousands of miles of difference, or about a mile apart...)
    From an Excel tips post that this forum won't allow me to link to that can be found searching for "Calculating the Distance between Points"

    No matter how I set it up the distances are always way off for some reason.

    Here's 3 stations with their coordinates and known distances using what you put together on the previous worksheet plugged into the table.
    Attached Files Attached Files

  7. #7
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2405
    Posts
    13,424

    Re: Finding closest location to a point, as well as the next two closest.

    Quote Originally Posted by bsalv View Post
    see attachment (same formula)
    Sorry for off-topic interjection:

    Although there is no official rule regarding this behavior, we request that wherever possible both the question AND the answer be provided in substantive detail here within the thread. An attached workbook is an excellent aid for posing a question and offering a solution, but solely doing that with no in thread explanation makes it difficult for researchers to understand or consider the Q & A of this thread without downloading what may be a pointless doc to them, if they can do that at all. Doing that also hides the content from search engines so others may never benefit from this.

    I'm sure you understand, and we look forward to seeing you post your formulas/macros in your posts for the searching benefit of all.

    Thanks again for all your hard work here!
    Dave

  8. #8
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Finding closest location to a point, as well as the next two closest.

    link https://www.contextures.com/excellat...tude.html#code
    an UDF to calculate the distance.
    the lat and lon in the table are the columns "Lat" and "Lon x(-1)", my error.
    When using this UDF the distances are almost correct.

  9. #9
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Finding closest location to a point, as well as the next two closest.

    i couldn't add the attachment in the previous reaction (?)
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-04-2021
    Location
    USA
    MS-Off Ver
    365
    Posts
    13

    Re: Finding closest location to a point, as well as the next two closest.

    Worked out great! Thanks!

  11. #11
    Forum Expert
    Join Date
    10-15-2018
    Location
    MA, USA
    MS-Off Ver
    2010, 2019
    Posts
    1,616

    Re: Finding closest location to a point, as well as the next two closest.

    In post #6 you were attempting a distance formula that looks a little like the "Haversine" formula, but without luck.

    In case you are still interested in a formula based approach, I added a Haversine implementation to bsalv's post #9 in workbook cell Q4:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Here is how it compares with bsalv's Vincenti implementation:

    distance.png

    The Haversine model, per Wikipedia, can have errors of up to 0.5% as it approximates the earth's shape to a sphere, but maybe that is good enough for your purposes?

    On the other hand, the Vincenti approach is millimeter-accurate relative to the standard WGS-84 oblate spheroid model

    Attached is bsalv's post #9 workbook with the Haversine formula in Col-Q.

    Oh, and thanks for the rep!
    Attached Files Attached Files
    Last edited by GeoffW283; 11-11-2021 at 12:29 PM. Reason: Changed "The Vincenti model, per Wikipedia . . ." to The Haversine model, per Wikipedia . . .

  12. #12
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Finding closest location to a point, as well as the next two closest.

    the method used in the UDF is https://en.wikipedia.org/wiki/Vincenty%27s_formulae
    The first (direct) method computes the location of a point that is a given distance and azimuth (direction) from another point. The second (inverse) method computes the geographical distance and azimuth between two given points. They have been widely used in geodesy because they are accurate to within 0.5 mm (0.020 in) on the Earth ellipsoid.
    So I also think the small formula from GeoffW283 is good enough to make your ranking, you don't need the exact distance with high precision.
    Last edited by bsalv; 11-11-2021 at 02:44 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 11
    Last Post: 06-05-2023, 01:36 AM
  2. X and Y coordinates, locating closest Point to point distance.
    By Erav in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-13-2020, 04:02 PM
  3. [SOLVED] Finding Closest Point and Distance
    By batman07 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-25-2017, 08:06 AM
  4. [SOLVED] Retrieving the Closest Larger / Closest Smaller Values from a List
    By ChipsSlave in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-31-2017, 08:38 AM
  5. Geofencing Lat / Longs (Finding Closest Point) in Excel
    By mattkunkle in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-19-2013, 02:05 PM
  6. [SOLVED] get 10 Closest Larger / Closest Smaller Values from a List
    By Auni in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-19-2013, 04:07 AM
  7. Replies: 0
    Last Post: 08-25-2005, 02:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1