I have developed a solution to identify geographic regions bordering a region in this post (94723).
Can this functionality be made to execute any faster? Find which entities in a set border other entities. For example, counties that border a neighbouring state (New York state to Pennsylvania counties, West Virginia state to Ohio counties, etc.), Gulf of Mexico to USA state counties, ect. The current execute takes over 40+ minutes for Gulf of Mexico to USA state counties. Can the run time be reduced by a factor of 10 or more.
geoBorderingEntities[region_, entities_List?VectorQ,
geoPadding_?QuantityQ] :=
Module[{borderBox, borderPoly, boxIntersections, intersections,
positions, borderPoints},
borderBox =
Rectangle @@ (GeoBoundingBox[region,
geoPadding] /. {GeoPosition :> Identity});
borderPoly = region["Polygon"] /. {GeoPosition :> Identity};
boxIntersections =
ParallelMap[
RegionIntersection[
borderBox,
Rectangle @@ (GeoBoundingBox[#,
geoPadding] /. {GeoPosition :> Identity})] &,
entities, 1];
(* Faster to split them *)
intersections = ParallelMap[
Function[{index},
With[{boxToPoly =
Evaluate@
RegionIntersection[boxIntersections[[index]], borderPoly]},
If[Head[boxToPoly] === EmptyRegion,
boxToPoly,
RegionIntersection[
boxToPoly, (entities[[index]][
"Polygon"] /. {GeoPosition :> Identity})]
]
]
],
Range[Length@entities], {1}];
positions =
Position[intersections, x_ /; ! (Head@x === EmptyRegion), {1},
Heads -> False];
Extract[entities, positions]]
However, this takes too long (about 40+ minutes) to complete for larger problems. For example:
gulf = OceanData["GulfMexico"];
usStates =
EntityValue[
Entity["AdministrativeDivision", {EntityProperty[
"AdministrativeDivision", "ParentRegion"] ->
CountryData["UnitedStates"]}], "Entities"];
(* Get bordering US states *)
borderStates = geoBorderingEntities[gulf, usStates, Quantity[5, "Kilometers"]];
(* Get counties of bordering US states *)
borderStatesAdminDivs =
EntityValue[
Entity["AdministrativeDivision", {EntityProperty[
"AdministrativeDivision", "ParentRegion"] -> #}],
"Entities"] & /@ borderStates ;
(* Get counties bordering Gulf of Mexico *)
borderCounties = geoBorderingEntities[gulf, #, Quantity[5, "Kilometers"]] & /@ borderStatesAdminDivs ;
(* Show *)
GeoGraphics[{
{EdgeForm[Black], White, Sequence @@ (Polygon[#] & /@ borderStatesAdminDivs)},
{Red, gulf["Polygon"]},
{EdgeForm[Gray], Blue, GeoStyling[Opacity[1]], Tooltip[Polygon[#], #["Name"]] & /@ Flatten[borderCounties]}},
GeoBackground -> None]
How can I speed up the region intersections to find polygons in entities that are touching the region polygon?


Polygonwith this rule//. {Polygon -> Identity, {{x__}} -> {x}}and set the compile parameter as{x, _Real, 2}but it still says that the values passed in are not of rank 2. Let us hope this catches the eye of someone more experienced with compile. – Edmund Sep 28 '15 at 16:51