-1
$\begingroup$

I am wondering how to extract entities such as states and cities from TravelDistanceData. For example, given the following example from the documentation, how could I extract the states the route goes through:

td = TravelDirections[{Entity[
    "City", {"NewYork", "NewYork", "UnitedStates"}], 
   Entity["City", {"Miami", "Florida", "UnitedStates"}]}]

My goal is to find a way to figure out the Country Chains Wolfram Challenge.

$\endgroup$
4
  • $\begingroup$ Possibly a better way: precompute the border graph from EntityValue["Country", EntityProperty["Country", "BorderingCountries"], "Dataset"] and set up a ShortestPathFunction with FindShortestPath and then query that repeatedly $\endgroup$
    – b3m2a1
    Dec 5, 2021 at 15:27
  • $\begingroup$ how would I set the ShortestPathFunction? $\endgroup$ Dec 5, 2021 at 22:10
  • $\begingroup$ I tried FindShortestPath[Graph@Normal@data,country1,country2] where data is the dataset, but I got an error. $\endgroup$ Dec 5, 2021 at 22:11
  • $\begingroup$ A) you need to fix the dataset to work with Graph and b) you should read the docs on FindShortestPath because it’s documented there and then come back if you can’t figure it out $\endgroup$
    – b3m2a1
    Dec 5, 2021 at 23:30

2 Answers 2

1
$\begingroup$
ds = TravelDirections[{Entity[
      "City", {"NewYork", "NewYork", "UnitedStates"}], 
     Entity["City", {"Miami", "Florida", "UnitedStates"}]}]["Dataset"];

states = DeleteDuplicates[
  GeoNearest["USState", #] & /@ Normal[ds[All, "StartingPoint"]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ I added some more ideas to my question. $\endgroup$ Dec 4, 2021 at 20:29
1
$\begingroup$
td = TravelDirections[{Entity[
    "City", {"NewYork", "NewYork", "UnitedStates"}], 
   Entity["City", {"Miami", "Florida", "UnitedStates"}]}]

td["Properties"]
{"Dataset", "ManeuverGrid", "TravelPath", "TravelDistance", \
"TravelTime"}

To plot the path:

tpath = td["TravelPath"]
GeoGraphics[tpath]

enter image description here


First attempt using Travel directions is not very satisfying and completely misses a lot of states as it is highway-centric.

Union@TextCases[Normal@td["Dataset"][All, "Description"], 
   "AdministrativeDivision"] /. {} -> Nothing
{{"Florida"}, {"New Hampshire"}, {"New Jersey"}, {"Rhode Island"}, \
{"Staten Island"}}

A much better approach is to figure out the geo-positions in column 5:

locs = Normal@td["Dataset"][All, "StartingPoint"]

Union@Normal@(GeoIdentify["USState", #] & /@ locs) /. {} -> Nothing

enter image description here

$\endgroup$
2
  • $\begingroup$ How would I adapt it to run fast enough to be done in 3000 milliseconds for the Wolfram Challenge Country Chains? $\endgroup$ Dec 4, 2021 at 20:26
  • $\begingroup$ I don't know the answer to that. $\endgroup$
    – Syed
    Dec 4, 2021 at 20:32

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.