# 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!
– sra
Commented Jan 19, 2021 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.
– sra
Commented Jan 19, 2021 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
Commented Jan 19, 2021 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?
– sra
Commented Jan 20, 2021 at 7:01
• Actually this WeatherData["Miami", "Temperature", "NonMetricValue"] works instead of WeatherData[WeatherData["Miami"], "Temperature", "NonMetricValue"]
– sra
Commented Jan 20, 2021 at 17:21