I am trying to find the next closest city to city 1 and want to use a certain radius to check what is the closest city I'm trying to create an algorithm for a TSP. Does anyone know how to implement the GeoDistance feature with a radius feature?


(* sets all locations equal to cities *)
cities = {GeoPosition[{18.96`, 72.82`}], 
GeoPosition[{12.97`, 77.56`}], GeoPosition[{27.19`, 78.01`}], 
GeoPosition[{19.89`, 75.32000000000001`}], 
GeoPosition[{24.85`, 79.93`}], 
GeoPosition[{32.71`, 74.85000000000001`}], 
GeoPosition[{15.42`, 73.78`}], GeoPosition[{26.92`, 75.8`}], 
GeoPosition[{24.580000000000002`, 73.69`}], 
GeoPosition[{26.92`, 70.9`}], GeoPosition[{34.17`, 77.58`}], 
GeoPosition[{32.27`, 77.17`}], GeoPosition[{31.1033`, 77.1722`}], 
GeoPosition[{27.34`, 88.61`}], GeoPosition[{27.05`, 88.26`}], 
GeoPosition[{8.078`, 77.541`}], GeoPosition[{29.98`, 78.16`}], 
GeoPosition[{29.38`, 79.45`}], GeoPosition[{28.6`, 77.22`}], 
GeoPosition[{30.34`, 78.05`}], GeoPosition[{31.64`, 74.87`}], 
GeoPosition[{27.1`, 77.67`}], 
GeoPosition[{17.400000000000002`, 78.48`}], 
GeoPosition[{12.88`, 74.84`}], GeoPosition[{25.57`, 91.87`}], 
GeoPosition[{17.73`, 83.3`}], 
GeoPosition[{30.73`, 79.07000000000001`}], 
GeoPosition[{13.63`, 79.41`}], GeoPosition[{10.23`, 77.48`}], 
GeoPosition[{22.57`, 88.36`}], GeoPosition[{18.98`, 73.27`}], 
GeoPosition[{25.32`, 83.01`}], GeoPosition[{17.92`, 73.67`}], 
GeoPosition[{24.71`, 84.98`}], GeoPosition[{26.85`, 80.92`}], 
GeoPosition[{9.92`, 78.12`}], GeoPosition[{23.17`, 79.94`}], 
GeoPosition[{24.6`, 72.7`}], 
GeoPosition[{29.400000000000002`, 79.12`}], 
GeoPosition[{11.411746`, 76.69466`}], GeoPosition[{8.51`, 76.95`}],
GeoPosition[{13.09`, 80.27`}], GeoPosition[{12.3, 76.6}], 
GeoPosition[{13.21, 75.99}], GeoPosition[{9.61, 77.15}], 
GeoPosition[{10.08, 77.0597}], GeoPosition[{15.33, 76.46}], 
GeoPosition[{12.62, 80.1994}], GeoPosition[{26.35, 92.67}], 
GeoPosition[{11.58, 75.59}]};
  • $\begingroup$ Try GeoNearest["City", origin, {1, distance}]. Or, if you have a list of cities you want to search, GeoNearest[list, origin, {1, distance}] $\endgroup$ Commented Apr 22, 2015 at 17:13
  • $\begingroup$ Use GeoPosition on your list of cities as input to Nearest, to get a NearestFunction. You can then apply that to individual cities to get neighbors within some radius. $\endgroup$ Commented Apr 22, 2015 at 17:25
  • $\begingroup$ @DanielLichtblau I already have my cites in GeoPosition could you show me an example? $\endgroup$
    – Sarah
    Commented Apr 22, 2015 at 17:28
  • $\begingroup$ @Sarah do you want to find the closest city, or whether a city exists within a certain radius as your title suggests? $\endgroup$ Commented Apr 22, 2015 at 17:31
  • 1
    $\begingroup$ @Mr.Wizard They're definitely about the same thing, but I don't think they're duplicates. That one asked about finding all distances, this one is about finding the nearest point. $\endgroup$ Commented Apr 22, 2015 at 17:41

1 Answer 1


First we define the city to search from:

city1 = Entity["City", {"Champaign", "Illinois", "UnitedStates"}];

(You can enter the Entity object by typing Ctrl+= champaign Enter.)

We then search for the nearest city using GeoNearest:

GeoNearest["City", city1, {1, Quantity[10, "Kilometers"]}]

The option 1 means that we should return at most 1 match.

Of course, the return value in this case is simply:

{Entity["City", {"Champaign", "Illinois", "UnitedStates"}]}

That is, the closest city to Champaign is Champaign. D'oh! We can fix this by excluding city1 explicitly:

GeoNearest[Except[city1, "City"], city1, 1]

The output is now:

{Entity["City", {"Savoy", "Illinois", "UnitedStates"}]}

(If we don't put 1 we get both Savoy and Urbana, since they both touch Champaign and their distance is zero.)

If you have a specific list of cities you want to search through, we can specify that list instead of "City":

cityList = GeoEntities[Entity["AdministrativeDivision", {"Virginia", "UnitedStates"}], "City"];
GeoNearest[cityList, city1, 1]

This simply returns {Entity["City", {"Ewing", "Virginia", "UnitedStates"}]}.

  • $\begingroup$ What if I want to exclude the first city? Do you do the similar except command you did earlier? $\endgroup$
    – Sarah
    Commented Apr 22, 2015 at 17:33
  • $\begingroup$ You can use DeleteCases[cityList, city1]. I don't think the Except query syntax will work with a list. $\endgroup$ Commented Apr 22, 2015 at 17:35
  • $\begingroup$ Within my CityList, do I have to go through each GeoPosition and define it as City1, City2,.... etc. ? $\endgroup$
    – Sarah
    Commented Apr 22, 2015 at 17:49
  • $\begingroup$ I got it to work ,once I defined city 1. GeoNearest[DeleteCases[CityList, city1], city1, 1] thanks, now I have to create a loop so it iterates and finds the nearest city to the city that was nearest to city 1. $\endgroup$
    – Sarah
    Commented Apr 22, 2015 at 17:57
  • $\begingroup$ @Sarah In this case I put the city you are searching from into a seprate variable, city1. If, for example, you want to find the nearest city to the first one in CityList, you'd do something like GeoNearest[Rest[CityList], First[CityList]]. The specifics of how you handle this list manipulation are out of scope for this question. $\endgroup$ Commented Apr 22, 2015 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.