Tuesday, February 19, 2013

UnionAndSplit

public void UnionAndSplit(ref SqlGeometry rd, ref List imps, bool IntersectAtStart)
        {                       
            SqlGeometry fullrd = rd;
            Dictionary jnctdict =new Dictionary();
            if(IntersectAtStart)
                jnctdict.Add(rd.STStartPoint(), 1);
            else
                jnctdict.Add(rd.STEndPoint(), 1);
            foreach (SqlGeometry imp in imps)
            {
                if (imp.STIntersects(fullrd))
                {
                    SqlGeometry interpts = imp.STIntersection(fullrd);
                    for (int i = 0; i < interpts.STNumPoints(); i++)
                    {
                        SqlGeometry pti = interpts.STPointN(i + 1);
                        bool exist=false;
                        foreach (SqlGeometry pt in jnctdict.Keys.ToArray()) //not a good solution, but cannot think out a better one at this point
                        {
                            if (pti.STDistance(pt) < 0.00001)
                            {
                                jnctdict[pt]++;
                                exist = true;
                            }                            
                        }
                        if (!exist)
                            jnctdict.Add(pti, 1);
                    }
                }
                fullrd=fullrd.STUnion(imp);
            }
            if (jnctdict.Count == 1)
                return;
            imps=new List();
            foreach (SqlGeometry pt in (from v in jnctdict where v.Value > 2 select v.Key))
            {
                fullrd = fullrd.STDifference(pt.STBuffer(0.00001));
            }
            for (int i = 0; i < fullrd.STNumGeometries(); i++)
            {
                SqlGeometry rdi = fullrd.STGeometryN(i + 1);
                if (rdi.STTouches(rd))
                    rd = rdi;
                else
                    imps.Add(rdi);
            }
        }

No comments:

Post a Comment