# Self loop on CityData graph?

Why does NearestNeighborGraph[ CityData[{Large, "Colorado", "UnitedStates"}], 3, DistanceFunction -> GeoDistance, DirectedEdges -> False, VertexLabels -> "Name"] create a self loop on the graph? Is there a way to get rid of it ?

GeoDistance is computing minimal distances between the polygons of the cities, and some cities are contiguous, i.e. their polygons are touching, so the result is zero. See the non-diagonal zeros in

TableForm[GeoDistance[cities, cities], TableHeadings -> {cities, cities}]


You can compute distances among centered points instead of polygons using this alternative distance function:

DistanceFunction -> Function[GeoDistance[GeoPosition[#1], GeoPosition[#2]]]


Then this is the result:

• Thanks a lot @jose! Jan 19 at 16:37
• Why does WeatherData[{"Buckeye", "Arizona", "UnitedStates"}, "Temperature", "NonMetricValue"] this fail? @jose I am trying to create a city graph with weather data as feature. Jan 19 at 20:50
• WeatherData is based on weather stations. In a case like this you can first find the nearest station and then check its temperature. Something like: WeatherData[ WeatherData[{"Buckeye", "Arizona", "UnitedStates"}], "Temperature", "NonMetricValue"]. This should have worked automatically.
– jose
Jan 19 at 22:42
• For bunch of US Cities, I am getting Missing["NotAvailable"] for example WeatherData[WeatherData["Miami"], "Temperature", "NonMetricValue"]..why is that so? Is there a way to fix it? Jan 20 at 7:01
• Actually this WeatherData["Miami", "Temperature", "NonMetricValue"] works instead of WeatherData[WeatherData["Miami"], "Temperature", "NonMetricValue"] Jan 20 at 17:21