Message Boards Message Boards


Random Cities with a Non-Missing Property

Posted 1 year ago
2 Replies
3 Total Likes

I would like to fetch four random city entities from the curated database that have a value in a specified property (not Missing[]). The end result would look something like this if I used the property "Area":

{{"Los Angeles", Quantity[468.6701619038, ("Miles")^2]},{"Miami", Quantity[35.871043820, ("Miles")^2]},{"Twin Falls", Quantity[18.103053374, ("Miles")^2]},{"Canton", Quantity[25.462368239, ("Miles")^2]}}

So far everything I've tried has been too slow to be useful, on the order of 20 minutes per query. I admit though that I don't understand EntityClass, EntityFunction, etc. very well. The problem seems to be in the large size of the database that contains the "city" entities and the relatively few entities that have values for most of the properties. Usable times for my purpose would be less than 15 seconds or so.

Any help would be appreciated.


2 Replies

How about this?

   EntityClass["City",  "Area" -> Quantity[Interval[{0, Infinity}], "Miles"*"Miles"]], 4] //
  EntityValue[#, {"Area"}, "EntityAssociation"] & //AbsoluteTiming

 <|Entity["City", {"Westminster", "Colorado", "UnitedStates"}] -> {Quantity[31.550416264, ("Miles")^2]}, 
  Entity["City", {"Belvue", "Kansas", "UnitedStates"}] -> {Quantity[0.115757288, ("Miles")^2]}, 
  Entity["City", {"Lewisville", "Indiana", "UnitedStates"}] -> {Quantity[0.253491511, ("Miles")^2]}, 
  Entity["City", {"Lehighton", "Pennsylvania", "UnitedStates"}] -> {Quantity[1.623076949,("Miles")^2]}|>}

Awesome, thank you. I was getting the syntax wrong. The Interval[{0,Infinity}] is the part I left out. I had tried >=0. Your solution not only solves my short-term problem, but also opens up a direction in my Wolfram programming that I had been avoiding because I couldn't make it work.

Thanks again, Rohit.


Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract