7
$\begingroup$

I have tried the following:

usentity = Entity["Country", "UnitedStates"];
uspoly = usentity["Polygon"];
stdpoly = uspoly /. GeoPosition -> Identity;
RegionQ[stdpoly]

[1] True

RegionMember[stdpoly, {35, -90}]

returns unevaluated.

Is there a better approach?

$\endgroup$
2
  • 6
    $\begingroup$ You're looking for GeoWithinQ. $\endgroup$
    – rcollyer
    Commented May 19, 2016 at 15:10
  • $\begingroup$ @rcollyer - perfect, just what I needed. $\endgroup$
    – JJM
    Commented May 19, 2016 at 15:14

1 Answer 1

3
$\begingroup$

If you look at the structure of the Polygon, it seems there is an extra level of {} that needs to be removed

Short@stdpoly
(* Polygon[{{{49.0021,-122.758},<<2620>>}}] *)

So you can use FlattenAt to remove that layer of brackets,

stdpoly2=FlattenAt[stdpoly,{1,1}];
Short@stdpoly
(* Polygon[{{{49.0021,-122.758},<<2620>>}}] *)

The polygons appear the same

RegionPlot /@ {stdpoly, stdpoly2}

Mathematica graphics

and now you can use RegionMember all you like

RegionMember[stdpoly2, {35, -90}]
(* True *)

There is also the fact that for some reason, geographic polygons have their coordinates reversed, as I learned here. This is what I did there,

stdpoly = Polygon @@ First@First@uspoly // Map[Reverse, #, {-2}] &;
Graphics@stdpoly
RegionMember[stdpoly, {-90, 35}]

Mathematica graphics

(* True  *)

As rcollyer points out, you can use the built-in GeoWithinQ to test this as well,

GeoWithinQ[Entity["Country", "UnitedStates"], GeoPosition[{35, -90}]]
(* True *)
$\endgroup$
1
  • $\begingroup$ JasonB, thanks! I just discovered that myself also. $\endgroup$
    – JJM
    Commented May 19, 2016 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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