How do you generate a RandomPoint in a geographic region?

state = Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}];
GeoGraphics[{EdgeForm[Red], Opacity[0.1], Polygon[state]}]

This produces the polygon outline of the state. I would like to get a random point (location coordinates) in the geographic region. I tried -


but this gives the error

RandomPoint::creg: The first argument Polygon[Illinois, United States] is expected to be a parameter-free region. >>

How do you use RandomPoint for geographic regions?

  • 2
    $\begingroup$ GeoPosition[ RandomPoint[ EntityValue[state, EntityProperty["AdministrativeDivision", "Polygon"]] /. GeoPosition -> Identity]] $\endgroup$ – ilian Aug 18 '15 at 22:30
  • $\begingroup$ @ilian So, it seems that Polygon is doing something special with a geographic object. Thanks for the workaround. $\endgroup$ – Edmund Aug 18 '15 at 22:48
  • $\begingroup$ @ilian I had been puzzled by this as well. Is the usage you suggest reported anywhere in the docs? If not, it would be great if you could convert your comment into an answer and possibly expand upon what is going on here. $\endgroup$ – MarcoB Aug 18 '15 at 22:55
  • $\begingroup$ @MarcoB Converted into an answer, though this usage is just a kludge. $\endgroup$ – ilian Aug 18 '15 at 23:18

The polygon of interest is

state = Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}];
(polygon = state["Polygon"]) // Short

(* Polygon[GeoPosition[{{{36.9821, -89.1329}, <<187>> ,{36.9821, -89.1329}}}]] *)

however that expression is not a valid region


 (* False *)

because the argument of Polygon[] is not a list of point coordinates. Nevertheless RandomPoint can be used after GeoPosition is stripped

RandomPoint[polygon /. GeoPosition -> Identity]

(* {39.7605, -89.8648} *)

Arguably the region functionality could be extended to support GeoPositions, but as of now this is not implemented.

| improve this answer | |
  • $\begingroup$ Just a side note: this distribution is a bit non-uniform on the Earth surface. It is uniform only in equal-area projections (e.g. Gall–Peters projection). $\endgroup$ – ybeltukov Nov 29 '15 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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