4
$\begingroup$

I have the following codes:-

First@GeoNearest["AdministrativeDivision", GeoPosition[{38.6, -90.2}]]
First@GeoNearest["AdministrativeDivision", GeoPosition[{32.3, -106.8}]]

Output:-

Entity["AdministrativeDivision", {"StLouisIndependentCity", "Missouri", "UnitedStates"}]
Entity["AdministrativeDivision", {"DonaAnaCounty", "NewMexico", "UnitedStates"}]

In fact, my desired output are the input-string StLouisIndependentCity and DonaAnaCounty. I tried to use Entity["AdministrativeDivision", {"StLouisIndependentCity", "Missouri", "UnitedStates"}]["Name"] (similar to Dona), but the output are the name-string St. Louis (independent city) and Doña Ana County, which are not what I want.

How can I get it done? Many thanks!

$\endgroup$
3
$\begingroup$

You can use any method that you would normally use to extract data from an expression. Consider that you had this expression:

{"AdministrativeDivision", {"StLouisIndependentCity", "Missouri", "UnitedStates"}}

How would you extract "StLouisIndependentCity" from this? Then do the same for your entity. Here are two suggestions:

entity = Entity["AdministrativeDivision", {"StLouisIndependentCity", "Missouri", "UnitedStates"}];
entity[[2,1]]

"StLouisIndependentCity"

Another one:

getIdentifier[Entity["AdministrativeDivision", {id_, _, _}]] := id
getIdentifier[entity]

"StLouisIndependentCity"

$\endgroup$
5
$\begingroup$

You might want to look at the CanonicalName accessor.

entity = First@GeoNearest["AdministrativeDivision", GeoPosition[{38.6, -90.2}]]
(* Entity["AdministrativeDivision", {"StLouisIndependentCity", "Missouri", "UnitedStates"}] *)

CanonicalName[entity]
(* {"StLouisIndependentCity", "Missouri", "UnitedStates"} *)

name[entity_Entity] :=
  Replace[CanonicalName[entity], {en_String, ___} :> en];
name[entity]
(* StLouisIndependentCity *)
$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.