Message Boards Message Boards

Why does GeoDistance not work on custom entities?

I'm new to Wolfram and tried to follow this great blog post from 5 years ago (Custom entities with Bostock's unemployment data and EntityStore) in which a custom EntityStore USCounty is implemented based on the EntityClass AdministrativeDivision.

Showing the entities on a map works as expected:

c1 = RandomEntity["USCounty"]
c2 = RandomEntity["USCounty"]
GeoGraphics[GeoMarker[{c1, c2}]]

GeoGraphics for USCounty works

so I tried to calculate the distance between two entities, but got this error instead:

GeoDistance[c1, c2]

GeoDistance does not work for USCounty :-(

With AdministrativeDivision it works, though:

d1 = RandomEntity["AdministrativeDivision"]
d2 = RandomEntity["AdministrativeDivision"]
GeoDistance[d1, d2]

GeoDistance for AdministrativeDivision works

Why does it not work with custom entities and how could I debug this? I was expecting that the properties Position, Latitude and Longitude should be enough for this!?

I'm not even sure what this error means!?

I'm using the Wolfram Cloud with basic plan. Any ideas?

6 Replies

Christoph: I can confirm Rohit suggested try.

enter image description here

Follow instruction from linked post on custom entities.

c1=RandomEntity["USCounty"]
c2=RandomEntity["USCounty"]
GeoGraphics[GeoMarker[{c1,c2}]]

enter image description here

GeoDistance[c1, c2]

Yields same error enter image description here

But

GeoDistance[c1["Position"], c2["Position"]]

Yields (* Quantity[270.758081689567, "Miles"] *)

POSTED BY: Hans Michel

Yes, I'm aware that this workaround works. I'm more looking for an explanation, why GeoDistance on entities isn't working as expected. And maybe an explanation what this $Failed means in the error message. What has "failed"?

Normally I would just start a debugger, go to the line of code where the error happens and then try to understand the reason. This does not seem to be possible in the Wolfram language, right?

If anyone is interested, I learned that there are debugging capabilities in the Wolfram language, but that led me into a rabbit hole I'm not sure how to escape from.

I thought a simple

Trace[GeoDistance[c1, c2]]

could show me where exactly the problem is, but it generated a trace larger than 10 MB where I can see internal GeoNearest lookups, caching, a lot of https request to dozens of wolfram backends, encoding of url characters, compresssing, client and server session information, URLs to JSP backends, etc...

I think I'll let it go for now... ;)

Hi Christoph,

Not sure how you got the GeoGraphics to work because "USCounty" is not a valid type specification for RandomEntity.

c1 = RandomEntity["USCounty"]
(* RandomEntity::ntype: USCounty is not a valid type of Entity or EntityClass. *)

This works

c1 = RandomEntity[EntityClass["AdministrativeDivision", "USCountiesAllCounties"]]
c2 = RandomEntity[EntityClass["AdministrativeDivision", "USCountiesAllCounties"]]
GeoGraphics[GeoMarker[{c1, c2}]]

However, this fails, seems like a bug to me

GeoDistance[c1, c2]

Workaround

GeoDistance[c1["Position"], c2["Position"]]

I am running "13.1.0 for Mac OS X ARM (64-bit) (June 16, 2022)"

POSTED BY: Rohit Namjoshi

Sorry, I missed the use of a custom EntityStore. The error you got suggests that position information is not available for those counties in the custom store. Try evaluating c1["Position"].

POSTED BY: Rohit Namjoshi

Position is defined:

store = EntityStore["USCounty" -> <|
     "Entities" -> countyEntities,
     "Properties" -> <|
       "ParentEntity" -> <|
         "Label" -> "Parent entity"
         |>,
       "FIPSCode" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["FIPSCode"] &),
         "Label" -> "County FIPS code"
         |>,
       "Polygon" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["Polygon"] &),
         "Label" -> "Polygon"
         |>,
       "Position" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["Position"] &),
         "Label" -> "Position"
         |>,
       "Latitude" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["Latitude"] &),
         "Label" -> "Latitude"
         |>,
       "Longitude" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["Longitude"] &),
         "Label" -> "Longitude"
         |>,
       "HasPolygon" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["HasPolygon"] &),
         "Label" -> "Has polygon?"
         |>,
       "Name" -> <|
         "DefaultFunction" -> (#["ParentEntity"]["Name"] &),
         "Label" -> "Name"
         |>
       |>,
     "EntityClasses" -> <|
       "Alaska" -> <|
         "Entities" -> ("ParentEntity" -> (MatchQ[#, 
               Entity[_, {_, "Alaska", _}]] &)),
         "Label" -> Style["Alaska", Bold]
         |>,
       "Hawaii" -> <|
         "Entities" -> ("ParentEntity" -> (MatchQ[#, Entity[_, {_, "Hawaii", _}]] &)),
         "Label" -> Style["Hawaii", Bold]
         |>,
       "Mainland" -> <|
         "Entities" -> ("ParentEntity" -> (MatchQ[#, Entity[_, {_, Except["Alaska" | "Hawaii"], _}]] &)),
         "Label" -> Style["Mainland", Bold]|>
       |>
     |>];

I'm still a learner in this Wolfram thing, but I have a background in object-oriented programming, so I'm always thinking in "interfaces" that needs to be implemented. And that is what I currently don't understand with the Wolfram knowledge framework. Why is the "interface" to do all the geo-stuff not described? Some functions obviously need "Position" (e.g. GeoGraphics), but others need more. But what is this "more"? Or is this not the idea of the framework that everyone can build own EntityStores which gives back entities that can be used together with the existing functions and other Entities?

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

Group Abstract Group Abstract