Пример #1
0
 public static string ToWkt(Polygon2 <double> geom)
 {
     if (geom != null)
     {
         Ring2 <double> cur;
         StringBuilder  sb = new StringBuilder();
         sb.Append("POLYGON(");
         for (uint j = 0; j < geom.SectionCount; j++)
         {
             cur = geom[j];
             sb.Append('(');
             for (uint i = 0; i < cur.VertexCount; i++)
             {
                 sb.Append(cur[i].X);
                 sb.Append(' ');
                 sb.Append(cur[i].Y);
                 sb.Append(',');
             }
             sb[sb.Length - 1] = ')';
             sb.Append(',');
         }
         sb[sb.Length - 1] = ')';
         return(sb.ToString());
     }
     return(string.Empty);
 }
Пример #2
0
        public static bool Contains(Polygon2 <double> outer, Polygon2 <double> inner)
        {
            if (outer == null || inner == null)
            {
                return(false);
            }

            if (outer.HasHoles)
            {
                if (RingUtils.Contains(outer.OuterRing, inner.OuterRing))
                {
                    for (int i = 0; i < outer.InnerRings.Rings.Length; i++)
                    {
                        if (RingUtils.Contains(outer.InnerRings.Rings[i], inner.OuterRing))
                        {
                            return(false); //in a hole
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(RingUtils.Contains(outer.OuterRing, inner.OuterRing));
            }
        }
Пример #3
0
        //point is inside the ring - not on the edge
        public static bool Contains(Polygon2 <double> ring, Point2 <double> pt)
        {
            if (ring == null || pt == null)
            {
                return(false);
            }

            if (ring.HasHoles)
            {
                if (RingUtils.Contains(ring.OuterRing, pt))
                {
                    for (int i = 0; i < ring.InnerRings.Rings.Length; i++)
                    {
                        if (RingUtils.Contains(ring.InnerRings.Rings[i], pt))
                        {
                            return(false); //in a hole
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(RingUtils.Contains(ring.OuterRing, pt));
            }
        }
Пример #4
0
 public Point2 <T> this[uint part, uint section, uint index]
 {
     get
     {
         Polygon2 <T> pt = this.Polygons[part];
         return(pt[section, index]);
     }
 }
Пример #5
0
 public Polygon2(Polygon2 <T> cloned)
 {
     if (cloned == null)
     {
         throw new ArgumentNullException();
     }
     this.OuterRing  = cloned.OuterRing;
     this.InnerRings = cloned.InnerRings;
 }
Пример #6
0
        //is the "inner" (ring) inside the "outer" (ring)
        //matches check for polygon holes, so single shared points on edge are ok, they share no area nor segment
        public static bool ContainsByArea(Polygon2 <double> outer, Polygon2 <double> inner)
        {
            if (outer == null || inner == null)
            {
                return(false);
            }

            return(RingUtils.ContainsByArea(outer.OuterRing, inner.OuterRing));
        }
Пример #7
0
        //is the "inner" (point) inside or on the "outer" (ring)
        //point is in or on the ring
        public static bool ContainsByArea(Polygon2 <double> ring, Point2 <double> pt)
        {
            if (ring == null || pt == null)
            {
                return(false);
            }

            return(RingUtils.ContainsByArea(ring.OuterRing, pt));
        }
Пример #8
0
 public int CompareTo(Polygon2 <T> other)
 {
     if (other == null)
     {
         return(1);
     }
     if (object.ReferenceEquals(this, other))
     {
         return(0);
     }
     if (object.ReferenceEquals(this.OuterRing, other.OuterRing))
     {
         return(0);
     }
     return(this.Envelope.CompareTo(other.Envelope));
 }
Пример #9
0
 public PolygonBag2 <T> ConstructPolygonBag(IList <Polygon2 <T> > parts)
 {
     if (parts != null && parts.Count > 0)
     {
         Polygon2 <T>[] tmp = new Polygon2 <T> [parts.Count];
         for (int i = 0; i < tmp.Length; i++)
         {
             if (parts[i] == null)
             {
                 return(null);
             }
             tmp[i] = parts[i];
         }
         return(new PolygonBag2 <T>(tmp));
     }
     return(null);
 }
Пример #10
0
 public override bool Equals(object obj)
 {
     if (object.ReferenceEquals(null, obj))
     {
         return(false);
     }
     if (object.ReferenceEquals(this, obj))
     {
         return(true);
     }
     if (obj is Polygon2 <T> )
     {
         Polygon2 <T> other = obj as Polygon2 <T>;
         if (other != null)
         {
             return(this.Equals(other));
         }
     }
     return(false);
 }
Пример #11
0
        public static JObject ToGeoJson(Polygon2 <double> geom)
        {
            if (geom == null)
            {
                return((JObject)null);
            }
            JObject jobject = new JObject();

            jobject.Add("type", (JToken) new JValue("Polygon"));
            JArray jarray1 = new JArray();
            JArray jarray2 = new JArray();

            jarray1.Add(jarray2); //for the outer ring
            foreach (Point2 <double> coordinate in geom.OuterRing)
            {
                jarray2.Add((JToken) new JArray()
                {
                    (JToken)coordinate.X,
                    (JToken)coordinate.Y
                }
                            );
            }
            if (geom.HasHoles)
            {
                foreach (Ring2 <double> linearRing in geom.InnerRings)
                {
                    jarray2 = new JArray();
                    foreach (Point2 <double> coordinate in linearRing)
                    {
                        jarray2.Add((JToken) new JArray()
                        {
                            (JToken)coordinate.X,
                            (JToken)coordinate.Y
                        });
                    }
                    jarray1.Add((JToken)jarray2);
                }
            }
            jobject.Add("coordinates", (JToken)jarray1);
            return(jobject);
        }
Пример #12
0
 public static PolygonBag2 <double> BuildMultiPolygon(PositionSet ps)
 {
     if (ps != null && ps.Positions.Count > 0)
     {
         List <Polygon2 <double> > list = new List <Polygon2 <double> >();
         foreach (Position position in ps.Positions)
         {
             Polygon2 <double> polygon = BuildPolygon(position as PositionSet);
             if (polygon == null)
             {
                 return(null);
             }
             list.Add(polygon);
         }
         if (list.Count > 0)
         {
             return(factory.ConstructPolygonBag(list));
         }
     }
     return(null);
 }
Пример #13
0
 public override PolygonSet2 <double> ConstructPolygonSet(Polygon2 <double>[] parts)
 {
     if (parts == null)
     {
         return(null);
     }
     Ring2 <double>[] shells = new Ring2 <double> [parts.Length];
     for (int i = 0; i < parts.Length; i++)
     {
         Polygon2 <double> curA = parts[i];
         if (curA == null)
         {
             return(null);
         }
         shells[i] = curA.OuterRing;
     }
     if (PlanarGraphUtils.ValidRingSet(shells))
     {
         return(new PolygonSet2 <double>(parts));
     }
     return(null);
 }
Пример #14
0
 public bool Equals(Polygon2 <T> other)
 {
     if (other == null)
     {
         return(false);
     }
     if (object.ReferenceEquals(this, other))
     {
         return(true);
     }
     if (object.ReferenceEquals(this.OuterRing, other.OuterRing))
     {
         if (this.InnerRings == null)
         {
             return(other.InnerRings == null);
         }
         if (other.InnerRings == null)
         {
             return(false);
         }
         if (object.ReferenceEquals(this.InnerRings, other.InnerRings))
         {
             return(true);
         }
     }
     if (this.VertexCount.Equals(other.VertexCount) && this.Envelope.Equals(other.Envelope))
     {
         if (this.OuterRing.Equals(other.OuterRing))
         {
             if (this.InnerRings == null) //same number of vertices and outer rings are equal -- must both have no inner rings
             {
                 return(true);
             }
             return(this.InnerRings.Equals(other.InnerRings));
         }
     }
     return(false);
 }
Пример #15
0
        public static Polygon2 <double> Simplify(double minSegmentLength, Polygon2 <double> poly)
        {
            if (poly == null)
            {
                return(null);
            }

            Ring2 <double> outer = RingUtils.Simplify(minSegmentLength, poly.OuterRing);

            if (outer == null)
            {
                return(null);
            }

            if (poly.HasHoles)
            {
                List <Ring2 <double> > inners = new List <Ring2 <double> >();
                Ring2 <double>         curInner;
                double distSum;
                double minDist = 3.0 * minSegmentLength;
                for (int i = 0; i < poly.InnerRings.Rings.Length; i++)
                {
                    curInner = poly.InnerRings.Rings[i];

                    //quick reject if the ring is "small"
                    distSum = 0;
                    for (uint j = 1; j < curInner.VertexCount; j++)
                    {
                        distSum += SegmentUtils.Length(curInner[j - 1], curInner[j]);
                        if (distSum >= minDist) //quick exit for big rings
                        {
                            break;
                        }
                    }
                    distSum += SegmentUtils.Length(curInner[curInner.VertexCount - 1], curInner[0]);
                    if (distSum <= minDist)
                    {
                        continue; //can't exist as a ring with that small a boundary
                    }
                    curInner = RingUtils.Simplify(minSegmentLength, curInner);
                    if (curInner != null)
                    {
                        inners.Add(curInner);
                    }
                }

                if (inners.Count < 1)
                {
                    return(outer); //no holes came through
                }
                RingSet2 <double> rs = poly.Factory.ConstructRingSet(inners);
                if (rs == null)
                {
                    return(null);
                }
                return(poly.Factory.ConstructPolygon(outer, rs));
            }
            else
            {
                return(outer);
            }
        }