6
$\begingroup$

When I evaluate

CityData[All, "Coordinates"]

Mathematica returns

{Missing["NotAvailable"], Missing["NotAvailable"]}

but when I evaluate CityData[All] or CityData[{All, "USA"}], it behaves as expected, and returns a gigantic list of cities.

I'm curious to modify the neat example for FindShortestTour, to use coordinates from CityData -- yes it may take a very long time, but I'm curious to see for myself :)

My guess is that it is because not every city in the database has coordinates mapped thereto - but why is Mathematica failing this way? How can I programmatically eliminate the cities with 'bad' coordinates? Or programmatically build a list of all cities that do have coordinates?

$\endgroup$
0

2 Answers 2

11
$\begingroup$

Try this:

CityData[#, "Coordinates"] & /@ CityData[All]

also if you want the city too

{#, CityData[#, "Coordinates"]} & /@ CityData[All]

I assume that the option "Coordinates" need a specific city so you have to retrieve all the available cities in the database and then feed each one to CityData to get its coordinates.

$\endgroup$
1
  • $\begingroup$ Awesome. Exactly what I needed. $\endgroup$ Apr 16, 2013 at 3:45
2
$\begingroup$

As @Spawn1701D mentioned, you have to retrieve the data then map CityData across it. You can also just look at specific cities instead of loading every city in the world which seems like it would be unnecessary for some situations. For example to get just data about cities in New York you can do:

CityData[#, "Coordinates"] & /@ CityData[{All, "NewYork"}]

You can then try your modified FindShortestTour "neat example" on this data before extending it to much larger data.

$\endgroup$
4
  • $\begingroup$ Actually, although there are 100k+ entries for all the cityes I got them quite fast. $\endgroup$
    – Spawn1701D
    Apr 15, 2013 at 5:13
  • $\begingroup$ @Spawn1701D, I was referring to the FindShortestTour example the OP is trying to modify. $\endgroup$
    – RunnyKine
    Apr 15, 2013 at 5:49
  • $\begingroup$ I am just referring to the " instead of loading every city in the world which seems like it would take a while". I can's say anything about the FindShortestTour part. $\endgroup$
    – Spawn1701D
    Apr 15, 2013 at 5:52
  • $\begingroup$ @Spawn1701D, yes you're right it loaded fast for me too. $\endgroup$
    – RunnyKine
    Apr 15, 2013 at 5:55

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.