This is current method to find the maximal EuclideanDistance in American

 EuclideanDistance[x, y], 
 {x ∈ BoundaryDiscretizeGraphics[CountryData["UnitedStates", "Polygon"]],
  y ∈ BoundaryDiscretizeGraphics[CountryData["UnitedStates", "Polygon"]]}

{57.8918, {x -> {-124.732, 48.381}, y -> {-66.9498, 44.8179}}}

But as we know, EuclideanDistance is not suitable to calculate the geodesic distance. So I change it.

Failure one

 QuantityMagnitude[GeoDistance[x, y]], {x ∈ CountryData["UnitedStates", "Polygon"], 
  y ∈ CountryData["UnitedStates", "Polygon"]}]

It will give a promp that we cannot set a constraint for x and y by here.

Failure two

region = TransformedRegion[
    CountryData["UnitedStates", "Polygon"]], {#2, #1} &];

geoDist = 
   NMaximize[QuantityMagnitude[GeoDistance[x, y]], {x ∈ region, y ∈ region}]

It will give some error informations and a result:

{3892.01, {x -> {46.3815, -122.129}, y -> {42.9557, -72.4605}}}

But after I visualize the results, I don't think it is right:

GeoGraphics[{Entity["Country", "UnitedStates"], 
  Arrow@GeoPath[GeoPosition /@ Values[Last[geoDist]]], Red, 
  PointSize[Large], Point[GeoPosition /@ Values[Last[geoDist]]]}]

Those places point by red arrow should have larger distance obviously. And we will also fail to calculate the TravelDistance by this method.

travelDist = 
  QuantityMagnitude[TravelDistance[{GeoPosition[x], GeoPosition[y]}]], 
  {x ∈ region, y ∈ region}]

Actually this error information same to the above expression ...QuantityMagnitude[GeoDistance[x, y]]....So any workarounds can calculate GeoDistance and TravelDistance?

  • $\begingroup$ How does the last example fail? What errors do you get in your attempts? $\endgroup$
    – MarcoB
    Commented Mar 4, 2017 at 15:31
  • $\begingroup$ @MarcoB Same to the last second example..I should post these error information? $\endgroup$
    – yode
    Commented Mar 4, 2017 at 15:41
  • $\begingroup$ Yes you should. For instance, in your errors it seems that the calls to TravelDistance fail, even before passing a result to QuantityMagnitude. That's a good starting point to debug your code: you want to make sure that you can get the calls to TravelDistance to work consistently first. Besides, if travel distance requires an external connection, wouldn't the minimization be horribly slow? $\endgroup$
    – MarcoB
    Commented Mar 4, 2017 at 19:24

1 Answer 1


Don't transform the region, transform the arguments to GeoDistance instead. Also, account for the possibility that NMaximize might feed GeoDistance invalid arguments:

gd[x:{__Real}, y:{__Real}] := Quiet @ Check[
    QuantityMagnitude @ GeoDistance[Reverse@x, Reverse@y],

Then, your basic code works:

region = BoundaryDiscretizeGraphics[CountryData["UnitedStates","Polygon"]];

geoDist = NMaximize[gd[x,y], {x\[Element]region,y\[Element]region}]

{2872.96, {x -> {-124.732, 48.381}, y -> {-80.3997, 25.2389}}}

    Arrow @ GeoPath[GeoPosition/@Reverse/@Values[Last[geoDist]]],
    Red, PointSize[Large],
    Point /@ GeoPosition /@ Reverse /@ Values[Last[geoDist]]}

enter image description here

  • $\begingroup$ How about TravelDistance?I cannot success to do it by your method. $\endgroup$
    – yode
    Commented Mar 5, 2017 at 19:02
  • $\begingroup$ TravelDistance is a service (so it will be slow) and it is not defined for all geopositions in the US. This makes it hard to use in NMaximize. $\endgroup$
    – Carl Woll
    Commented Mar 6, 2017 at 6:08

Your Answer

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

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