H3Lib/Extensions/GeoFenceExtensions.cs
/// <summary>
/// Create a bounding box from a simple polygon loop
/// </summary>
/// <remarks>
/// Known limitations:
/// - Does not support polygons with two adjacent points > 180 degrees of
///   longitude apart. These will be interpreted as crossing the antimeridian.
/// - Does not currently support polygons containing a pole.
/// </remarks>
/// <param name="loop">Loop of coordinates</param>
/// <returns>output box</returns>
Create a bounding box from a simple polygon loop
Create a bounding box from a simple polygon loop
public static BBox ToBBox(this GeoFence loop)
        {
            if (loop.IsEmpty)
            {
                return new BBox(0, 0, 0, 0);
            }
            var box = new BBox(
                               -decimal.MaxValue,
                               decimal.MaxValue,
                               -decimal.MaxValue,
                               decimal.MaxValue);
            decimal minPosLon = decimal.MaxValue;
            decimal maxNegLon = -decimal.MaxValue;
            bool isTransmeridian = false;
            decimal lat;
            decimal lon;
            GeoCoord coord;
            GeoCoord next;
            int loopIndex = -1;
            while (true)
            {
                if (++loopIndex >= loop.NumVerts)
                {
                    break;
                }
                coord = loop.Verts[loopIndex];
                next = loop.Verts[(loopIndex + 1) % loop.NumVerts];
                lat = coord.Latitude;
                lon = coord.Longitude;
                if (lat < box.South)
                {
                    box = box.ReplaceSouth(lat);
                }
                if (lon < box.West)
                {
                    box = box.ReplaceWest(lon);
                }
                if (lat > box.North)
                {
                    box = box.ReplaceNorth(lat);
                }
                if (lon > box.East)
                {
                    box = box.ReplaceEast(lon);
                }
                if (lon > 0 && lon < minPosLon)
                {
                    minPosLon = lon;
                }
                if (lon < 0 && lon > maxNegLon)
                {
                    maxNegLon = lon;
                }
                if (Math.Abs(lon - next.Longitude) > Constants.H3.M_PI)
                {
                    isTransmeridian = true;
                }
            }
            if (isTransmeridian)
            {
                box = box.ReplaceEW(maxNegLon, minPosLon);
            }
            return box;
        }
public static BBox ToBBox ( this GeoFence loop ) { if ( loop . IsEmpty ) { return new BBox ( 0 , 0 , 0 , 0 ) ; } var box = new BBox ( - decimal . MaxValue , decimal . MaxValue , - decimal . MaxValue , decimal . MaxValue ) ; decimal minPosLon = decimal . MaxValue ; decimal maxNegLon = - decimal . MaxValue ; bool isTransmeridian = false ; decimal lat ; decimal lon ; GeoCoord coord ; GeoCoord next ; int loopIndex = - 1 ; while ( true ) { if ( ++ loopIndex >= loop . NumVerts ) { break ; } coord = loop . Verts [ loopIndex ] ; next = loop . Verts [ ( loopIndex + 1 ) % loop . NumVerts ] ; lat = coord . Latitude ; lon = coord . Longitude ; if ( lat < box . South ) { box = box . ReplaceSouth ( lat ) ; } if ( lon < box . West ) { box = box . ReplaceWest ( lon ) ; } if ( lat > box . North ) { box = box . ReplaceNorth ( lat ) ; } if ( lon > box . East ) { box = box . ReplaceEast ( lon ) ; } if ( lon > 0 && lon < minPosLon ) { minPosLon = lon ; } if ( lon < 0 && lon > maxNegLon ) { maxNegLon = lon ; } if ( Math . Abs ( lon - next . Longitude ) > Constants . H3 . M_PI ) { isTransmeridian = true ; } } if ( isTransmeridian ) { box = box . ReplaceEW ( maxNegLon , minPosLon ) ; } return box ; }
Create a bounding box from a simple polygon loop
Create a bounding box from a simple polygon loop
// INIT_ITERATION => INIT_ITERATION_GEOFENCE // INIT_ITERATION END // ITERATE(loop, coord, next) => ITERATE_GEOFENCE(geofence, vertexA, vertexB) // ITERATE END // Save the min positive and max negative longitude for // use in the transmeridian case // check for arcs > 180 degrees longitude, flagging as transmeridian // Swap east and west if transmeridian
None
GeoFence
this
loop
Loop of coordinates
Loop of coordinates
remarks
Known limitations:
Does not support polygons with two adjacent points > 180 degrees of
longitude apart. These will be interpreted as crossing the antimeridian.
Does not currently support polygons containing a pole.
Known limitations : Does not support polygons with two adjacent points > 180 degrees of longitude apart . These will be interpreted as crossing the antimeridian . Does not currently support polygons containing a pole .