1

Suppose I'm interested in the latitude of Los Angeles and I do the following:

In[1]:= c1 = CityData[{"LosAngeles", "California", "UnitedStates"}];
In[2]:= Latitude[c1]
Out[2]= Quantity[34.0194, "AngularDegrees"]

So the latitude is a Quantity! And if I get the string value I get:

In[4]:= ToString[Latitude[c1]]
Out[4]= "34.0194 degrees"

This seems a little at odds with the documentation where the examples seem to show a plain number being output.

Quantities are all well and good until I want to, say, use the Google Directions Api:

In[6]:= address = 
 ToString@StringForm[
   "http://maps.googleapis.com/maps/api/directions/json?origin=`1`,`2`", Latitude[c1], Longitude[c1]]
Out[6]= "http://maps.googleapis.com/maps/api/directions/json?origin=34.0194 degrees,-118.411 degrees"

That's not at all what I want!

I suppose I can get the magnitude of the Quantity to get something more like what I want:

In[7]:= address = 
 ToString@StringForm[
   "http://maps.googleapis.com/maps/api/directions/json?origin=`1`,`2`", QuantityMagnitude[Latitude[c1]], QuantityMagnitude[Longitude[c1]]]

Out[7]= "http://maps.googleapis.com/maps/api/directions/json?origin=34.0194,-118.411"

But that's beginning to feel like I'm straining and probably not doing it right. Is there a better way of dealing with these units?

  • 1
    have you tried Latitude[c1][[1]] and Longitude[c1][[1]]? – soandos Jan 01 '15 at 00:58
  • The discrepancy between your result and the documentation is probably because those functions didn't return units in previous versions of Mathematica. Re. your question: Try using QuantityMagnitude, for example QuantityMagnitude@Latitude[c1]. – C. E. Jan 01 '15 at 01:11
  • That produces the right result, of course. Maybe that's the expert's idiom. I'm such a beginner at Mathematica that I'm using more explicit forms so that I know what the code is saying. I was also hoping it was "better code". Even in this form, however, I'm still picking the magnitude out of the Quantity. – BigBaaadBob Jan 01 '15 at 01:13
  • @Pickett, the change to having Latitude return a Quantity seems like it would have broken existing code that was producing strings from values. That's kinda why I'm wondering if there isn't a backwards-compatibility feature or something. – BigBaaadBob Jan 01 '15 at 01:21
  • 3
    Using SetSystemOptions["DataOptions" -> "ReturnQuantities" -> False] you can get back the old behaviour (trick from the 'Possible Issues' section of DateDifference). – Sjoerd C. de Vries Jan 01 '15 at 13:10
  • I disagree that this is a duplicate of that question in any meaningful sense. Yes, this is a function that works differently in 10 than 9, but it doesn't really answer my "is there a better way" question. Incidentally, setting "ReturnQuantities"->False doesn't seem to change anything. `In[12]:= SystemOptions["DataOptions"]

    Out[12]= {"DataOptions" -> {"ReturnEntities" -> True, "ReturnQuantities" -> False, "UseDataWrappers" -> True}} In[13]:= Latitude[c1]

    Out[13]= Quantity[34.0194, "AngularDegrees"]`. Maybe there isn't a better way?

    – BigBaaadBob Jan 06 '15 at 03:58

0 Answers0