0
$\begingroup$
x = RandomChoice[CountryData[]]

Here I have defined x to be a random country.

citynames = Dynamic[p = CityData[{Large, Last[x]}]]

Then I defined the large cities with p.

t = Dynamic[InputForm[p]]

This converts all of them into entities as such:

Entity["City", {"Baghdad", "Baghdad", "Iraq"}], 
Entity["City", {"AlMawsil", "Nineveh", "Iraq"}], 
Entity["City", {"AlBasrah", "Basra", "Iraq"}], 
Entity["City", {"Irbil", "Arbil", "Iraq"}], 
Entity["City", {"Kirkuk", "AtTamim", "Iraq"}], 
Entity["City", {"Sulaymaniyah", "Sulaymaniyah", "Iraq"}], 
Entity["City", {"AnNajaf", "AnNajaf", "Iraq"}], 
Entity["City", {"Karbala", "Karbala", "Iraq"}], 
Entity["City", {"AnNasiriyah", "DhiQar", "Iraq"}], 
Entity["City", {"AlAmarah", "Maysan", "Iraq"}], 
Entity["City", {"AdDiwaniyah", "AlQadisiyah", "Iraq"}], 
Entity["City", {"AlKut", "Wasit", "Iraq"}], 
Entity["City", {"AlHillah", "Babil", "Iraq"}], 
Entity["City", {"ArRamadi", "AlAnbar", "Iraq"}], 
Entity["City", {"AlFallujah", "AlAnbar", "Iraq"}], 
Entity["City", {"AbuGhraib", "Baghdad", "Iraq"}], 
Entity["City", {"TallAfar", "Nineveh", "Iraq"}], 
Entity["City", {"Samarra", "SalahAdDin", "Iraq"}], 
Entity["City", {"AsSamawah", "AlMuthanna", "Iraq"}], 
Entity["City", {"Baqubah", "Diyala", "Iraq"}], 
Entity["City", {"Dahuk", "Dahuk", "Iraq"}], 
Entity["City", {"AlKufah", "AnNajaf", "Iraq"}], 
Entity["City", {"AzZubayr", "Basra", "Iraq"}], 
Entity["City", {"SattAlArab", "Basra", "Iraq"}], 
Entity["City", {"AlFaw", "Basra", "Iraq"}]}]

Now I'd like to map all of these entities into

CityData[*entity here*,"Coordinates"] and CityData[*entity here*, "Name"]

Map doesn't seem to work. I don't know how to format it properly.

$\endgroup$
  • 1
    $\begingroup$ I am confused by your use of Dynamicand InputForm here. However, you should be able to use CityData[#, "Coordinates"]& /@ yourListofEntities. $\endgroup$ – MarcoB Dec 20 '19 at 17:56
  • $\begingroup$ The Dynamic is for something else that I didn't include. $\endgroup$ – Hajar Zaid Dec 20 '19 at 18:25
  • $\begingroup$ I tried it but I am getting "Missing[Not Available]" $\endgroup$ – Hajar Zaid Dec 20 '19 at 18:45
  • $\begingroup$ That's not what I get. Here is a small example: CityData[#, "Coordinates"] & /@ {Entity["City", {"Baghdad", "Baghdad", "Iraq"}], Entity["City", {"AlMawsil", "Nineveh", "Iraq"}]}. This returns a list of coordinates, one for each city. $\endgroup$ – MarcoB Dec 20 '19 at 18:47
  • $\begingroup$ You would want to clean up your code, remove the parts that do not matter (e.g. the Dynamic etc) and show exactly what code you are running that gives you trouble. For instance, your current code does not produce the list you show, so it is hard to try and fix it because we are probably not working with the actual code you have. $\endgroup$ – MarcoB Dec 20 '19 at 18:48
1
$\begingroup$

First, a minor point. I would use RandomEntity instead of RandomChoice (RandomEntity will be much faster for entity types with a large number of entities, e.g., "Star")

entity = RandomEntity["Country"]

Entity["Country", "IvoryCoast"]

Then, you can get the cities with:

cities = CityData[{Large, CanonicalName @ entity}]

{Entity["City", {"Abidjan", "Lagunes", "IvoryCoast"}], Entity["City", {"Bouake", "ValleeDuBandama", "IvoryCoast"}], Entity["City", {"Daloa", "HautSassandra", "IvoryCoast"}], Entity["City", {"SanPedro", "BasSassandra", "IvoryCoast"}], Entity["City", {"Yamoussoukro", "Lacs", "IvoryCoast"}], Entity["City", {"Korhogo", "Savanes", "IvoryCoast"}], Entity["City", {"Man", "DixHuitMontagnes", "IvoryCoast"}], Entity["City", {"Divo", "SudBandama", "IvoryCoast"}], Entity["City", {"Gagnoa", "Fromager", "IvoryCoast"}], Entity["City", {"Abengourou", "MoyenComoe", "IvoryCoast"}], Entity["City", {"Anyama", "Lagunes", "IvoryCoast"}]}

Finally, to obtain the coordinates and names you can use EntityValue:

EntityValue[cities, {"Coordinates", "Name"}]

{{{5.33, -4.03}, "Abidjan"}, {{7.69, -5.03}, "Bouake"}, {{6.89, -6.45}, "Daloa"}, {{4.77, -6.64}, "San\[Hyphen]Pedro"}, {{6.82, -5.28}, "Yamoussoukro"}, {{9.45, -5.65}, "Korhogo"}, {{7.4, -7.55}, "Man"}, {{5.85, -5.37}, "Divo"}, {{6.15, -5.88}, "Gagnoa"}, {{6.73, -3.49}, "Abengourou"}, {{5.5, -4.05}, "Anyama"}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hello! Thank you very much! Question: I wanted to make a tooltip to highlight the name coordinates and names of cities. How would I do it. I have this so far: Manipulate[Button["Random", entity = RandomEntity["Country"]], Dynamic[DynamicGeoGraphics[ Tooltip[GeoMarker[cities], EntityValue[cities, "Name"]]]]] $\endgroup$ – Hajar Zaid Dec 20 '19 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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