How do I find the nearest city out of a list of cities?

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?

Thanks

(* 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}]};

• Try GeoNearest["City", origin, {1, distance}]. Or, if you have a list of cities you want to search, GeoNearest[list, origin, {1, distance}] Commented Apr 22, 2015 at 17:13
• 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. Commented Apr 22, 2015 at 17:25
• @DanielLichtblau I already have my cites in GeoPosition could you show me an example? Commented Apr 22, 2015 at 17:28
• @Sarah do you want to find the closest city, or whether a city exists within a certain radius as your title suggests? Commented Apr 22, 2015 at 17:31
• @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. Commented Apr 22, 2015 at 17:41

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"}]}.

• What if I want to exclude the first city? Do you do the similar except command you did earlier? Commented Apr 22, 2015 at 17:33
• You can use DeleteCases[cityList, city1]. I don't think the Except query syntax will work with a list. Commented Apr 22, 2015 at 17:35
• Within my CityList, do I have to go through each GeoPosition and define it as City1, City2,.... etc. ? Commented Apr 22, 2015 at 17:49
• 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. Commented Apr 22, 2015 at 17:57
• @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. Commented Apr 22, 2015 at 18:10