--- SubgraphDepthLocater.cpp.orig	2007-02-21 05:14:20.000000000 -0500
+++ SubgraphDepthLocater.cpp	2007-07-23 02:05:37.000000000 -0400
@@ -35,7 +35,7 @@
 #include <geos/geomgraph/Position.h>
 
 #ifndef GEOS_DEBUG
-#define GEOS_DEBUG 0
+#define GEOS_DEBUG 2
 #endif
 
 using namespace std;
@@ -52,7 +52,6 @@
  * for its sides.
  */
 class DepthSegment {
-
 private:
 
 	geom::LineSegment& upwardSeg;
@@ -133,7 +132,13 @@
 	{
 		assert(first);
 		assert(second);
+#if GEOS_DEBUG
+		cerr<<" SubgraphDepthLocater::DepthSegmentLessThen "<<
+			first->upwardSeg<<":"<<(first->leftDepth)<<"  "<<
+			second->upwardSeg<<":"<<(second->leftDepth)<<" == "<<first->compareTo(*second)<<endl;
+#endif
 		if (first->compareTo(*second)<0) return true;
+		else if ((first->compareTo(*second)==0)&&(first->leftDepth <= second->leftDepth)) return true;
 		else return false;
 	}
 };
@@ -151,6 +156,17 @@
 	if (stabbedSegments.size()==0) return 0;
 
 	sort(stabbedSegments.begin(), stabbedSegments.end(), DepthSegmentLessThen());
+#if GEOS_DEBUG
+	cerr<<" SubgraphDepthLocater::getDepth()"<<endl<<"   found these stabbed segments"<<endl;
+
+	for (int i=0; i < stabbedSegments.size(); i++)
+	{
+		DepthSegment *dss=stabbedSegments[i];
+		cerr<<"    ("<<i<<": "<<dss->upwardSeg<<" "<<dss->leftDepth<<")"<<endl;
+	}
+
+	cerr<<endl;
+#endif
 
 	DepthSegment *ds=stabbedSegments[0];
 	int ret = ds->leftDepth;
@@ -240,7 +256,7 @@
 		seg.p0=pts->getAt(i);
 		seg.p1=pts->getAt(i + 1);
 #if GEOS_DEBUG
-		cerr << " SubgraphDepthLocater::findStabbedSegments: segment " << i
+		cerr << " SubgraphDepthLocater::findStabbedSegments:  evaluate segment " << i
 			<< " (" << seg << ") ";
 #endif
 
@@ -248,6 +264,10 @@
 		const Coordinate *low=&(pts->getAt(i));
 		const Coordinate *high=&(pts->getAt(i+1));
 		const Coordinate *swap=NULL;
+#if GEOS_DEBUG
+		cerr << " SubgraphDepthLocater::findStabbedSegments:  evaluate segment " << i
+			<< " (" << low->toString() << " " << high->toString() << ") ";
+#endif
 
 #endif
 
@@ -341,7 +361,7 @@
 #endif
 
 #if GEOS_DEBUG
-	cerr<<" depth: "<<depth<<endl;
+	cerr<<"     depth: "<<depth<<"  right:"<<dirEdge->getDepth(Position::RIGHT)<<", left:"<<dirEdge->getDepth(Position::LEFT)<<" swapped:"<<(swap!=NULL)<<endl;
 #endif
 
 #ifdef SKIP_LS
@@ -349,6 +369,11 @@
 		seg.p1 = *high;
 #endif
 
+#if GEOS_DEBUG
+	cerr<<"     push back: ("<<seg<<" "<<depth<<")"<<endl;
+#endif
+
+// need to make a copy of seg as part of the DepthSegment constructor not just pass a pointer
 		DepthSegment *ds=new DepthSegment(seg, depth);
 		stabbedSegments.push_back(ds);
 	}

