# How to calculate the maximal GeoDistance and TravelDistance in a geo-region

This is current method to find the maximal EuclideanDistance in American

NMaximize[
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

NMinimize[
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[
BoundaryDiscretizeGraphics[
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 =
NMaximize[
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?

• How does the last example fail? What errors do you get in your attempts? – MarcoB Mar 4 '17 at 15:31
• @MarcoB Same to the last second example..I should post these error information? – yode Mar 4 '17 at 15:41
• 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? – MarcoB Mar 4 '17 at 19:24

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],
0
]


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}}}

GeoGraphics[{
Entity["Country","UnitedStates"],
Arrow @ GeoPath[GeoPosition/@Reverse/@Values[Last[geoDist]]],
Red, PointSize[Large],
Point /@ GeoPosition /@ Reverse /@ Values[Last[geoDist]]}
] • How about TravelDistance?I cannot success to do it by your method. – yode Mar 5 '17 at 19:02
• 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. – Carl Woll Mar 6 '17 at 6:08