4
$\begingroup$

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 ?

enter image description here

$\endgroup$
0

1 Answer 1

4
$\begingroup$

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:

enter image description here

$\endgroup$
6
  • $\begingroup$ Thanks a lot @jose! $\endgroup$
    – sra
    Jan 19, 2021 at 16:37
  • $\begingroup$ Why does WeatherData[{"Buckeye", "Arizona", "UnitedStates"}, "Temperature", "NonMetricValue"] this fail? @jose I am trying to create a city graph with weather data as feature. $\endgroup$
    – sra
    Jan 19, 2021 at 20:50
  • 3
    $\begingroup$ 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. $\endgroup$
    – jose
    Jan 19, 2021 at 22:42
  • $\begingroup$ 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? $\endgroup$
    – sra
    Jan 20, 2021 at 7:01
  • $\begingroup$ Actually this WeatherData["Miami", "Temperature", "NonMetricValue"] works instead of WeatherData[WeatherData["Miami"], "Temperature", "NonMetricValue"] $\endgroup$
    – sra
    Jan 20, 2021 at 17:21

Your Answer

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

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