float zero_eps) {
int size = list.size();
int len1, len2;
float lat1, lon1, lat2, lon2;
OMGraphic obj;
OMGraphicList newGraphics = new OMGraphicList();
OMGraphicList plineGraphics = new OMGraphicList();
// check for non-connected polylines
System.out.println("finding polylines...");
for (int i = 0; i < size; i++) {
obj = list.getOMGraphicAt(i);
if ((obj instanceof OMPoly) && !((OMPoly) obj).isPolygon()) {
plineGraphics.addOMGraphic(obj);
} else {
newGraphics.addOMGraphic(obj);
}
}
// iterate through the polylines and join lines with common
// endpoints
size = plineGraphics.size();
OMPoly poly1, poly2;
float[] rads1, rads2, radians;
System.out.println("maybe joining " + size + " polylines...");
// nasty!: > O(n^2)
for (int i = 0; i < size; i++) {
if (i % 500 == 0) {
System.out.println("checking pline i=" + i);
}
for (int j = 0; j < size; j++) {
if (i == j) {
continue;
}
obj = plineGraphics.getOMGraphicAt(i);
if (obj instanceof SinkGraphic) {
continue;
}
poly1 = (OMPoly) obj;
rads1 = poly1.getLatLonArray();
len1 = rads1.length;
lat1 = ProjMath.radToDeg(rads1[len1 - 2]);
lon1 = ProjMath.radToDeg(rads1[len1 - 1]);
obj = plineGraphics.getOMGraphicAt(j);
if (obj instanceof SinkGraphic) {
continue;
}
poly2 = (OMPoly) obj;
rads2 = poly2.getLatLonArray();
len2 = rads2.length;
lat2 = ProjMath.radToDeg(rads2[0]);
lon2 = ProjMath.radToDeg(rads2[1]);
if (MoreMath.approximately_equal(lat1, lat2, zero_eps)
&& MoreMath.approximately_equal(lon1, lon2, zero_eps)) {
// System.out.println("joining...");
radians = new float[len1 + len2 - 2];
System.arraycopy(rads1, 0, radians, 0, len1);
System.arraycopy(rads2, 0, radians, len1 - 2, len2);
poly1.setLocation(radians, OMGraphic.RADIANS);
plineGraphics.setOMGraphicAt(SinkGraphic.getSharedInstance(),
j);
j = -1;// redo search
}
}
}
// add the joined lines back to the data set
size = plineGraphics.size();
for (int i = 0; i < size; i++) {
obj = plineGraphics.getOMGraphicAt(i);
if (obj instanceof OMPoly) {
newGraphics.addOMGraphic(obj);
}
}
return newGraphics;