6

According to the V10 documentation on $GeoLocation,

\$GeoLocationCity gives the city entity for the current setting for \$GeoLocation

And since this is essentially all the documentation has to say about it, I thought that if I were to change the value of $GeoLocation, the value of $GeoLocationCity would change as well. I have verified that this true when it is done at top level, but I get unexpected and undesirable results when I change $GeoLocation within the scope of a Block.

Here is where Mathematica thinks I am at the start of a session.

$GeoLocationCity // FullForm
Entity["City", List["FarmingtonHills", "Michigan", "UnitedStates"]]

Now I want to pretend, temporarily, I am in London.

Block[{$GeoLocation = GeoPosition @ CityData["London", "Coordinates"]}, 
  Column @ {Here, $GeoLocationCity // FullForm}]
GeoPosition[{51.5, -0.116667}]
Entity["City", List["FarmingtonHills", "Michigan", "UnitedStates"]]

It didn't work. Although Here reflects the assignment to $GeoLocation in the first argument of Block, $GeoLocationCity seems oblivious to that assignment.

What's going on here? Why doesn't the Block work as I expected it to?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257

1 Answers1

4

Suspecting the odd behavior of $GeoLocationCity is caused by some sort of caching or memoization mechanism, I did a little spelunking.


Code in this section must be evaluated with a fresh kernel.

Attributes[$GeoLocationCity] = {Protected};
?? $GeoLocationCity

geo-loc-city-1

?? System`Dump`GeoLocationDump`setGeoLocationCity

set-geo-loc-city

 $GeoLocationCity // FullForm
Entity["City", List["FarmingtonHills", "Michigan", "UnitedStates"]]
?? $GeoLocationCity

geo-loc-city-2


My spelunking shows that $GeoLocationCity looks at $GeoLocation only at first time it is evaluated, after which it is memoized. So a solution (or is it a work-around?) is to explicitly call the undocumented function $GeoLocationCity calls the first time it is evaluated.

Block[{$GeoLocation = GeoPosition@CityData["London", "Coordinates"], 
   $GeoLocationCity},
  System`Dump`GeoLocationDump`setGeoLocationCity[]; 
  Column @ {Here, $GeoLocationCity // FullForm}]
GeoPosition[{51.5, -0.116667}]
Entity["City", List["London", "GreaterLondon", "UnitedKingdom"]]

The global value $GeoLocationCity has not been disturbed.

$GeoLocationCity // FullForm
Entity["City", List["FarmingtonHills", "Michigan", "UnitedStates"]]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257