Index: applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
===================================================================
--- applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java	(revision 24060)
+++ applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java	(revision 24061)
@@ -53,4 +53,7 @@
 
 	public void filterByColor(Color color) {
+
+		int rgb = color.getRGB() & 0xffffff;
+
 		List<LayerContents> newLayers = new ArrayList<LayerContents>();
 		for(LayerContents l: this.layers) {
@@ -58,9 +61,10 @@
 			boolean good = false;
 
-			if (l.info.fill != null && l.info.fill.equals(color)) {
+
+			if (l.info.fill != null && (l.info.fill.getRGB() & 0xffffff) == rgb) {
 				good = true;
 			}
 
-			if (l.info.stroke != null && l.info.stroke.equals(color)) {
+			if (l.info.stroke != null && (l.info.stroke.getRGB() & 0xffffff) == rgb) {
 				good = true;
 			}
@@ -277,72 +281,157 @@
 	 */
 	private void concatenatePaths(LayerContents layer) {
-		Map<Point2D, PdfPath> pathEndpoints = new HashMap<Point2D, PdfPath>();
+		Map<Point2D, List<PdfPath>> pathEndpoints = new HashMap<Point2D, List<PdfPath>>();
 		Set<PdfPath> mergedPaths = new HashSet<PdfPath>();
-
+		List<PdfPath> newPaths = new ArrayList<PdfPath>();
+
+		//fill pathEndpoints map
 		for(PdfPath pp: layer.paths){
-
-			PdfPath path = pp;
+			if (pp.isClosed()) {
+				newPaths.add(pp);
+				continue;
+			}
+
+			List<PdfPath> paths = pathEndpoints.get(pp.firstPoint());
+			if (paths == null){
+				paths = new ArrayList<PdfPath>(2);
+				pathEndpoints.put(pp.firstPoint(), paths);
+			}
+			paths.add(pp);
+
+			paths = pathEndpoints.get(pp.lastPoint());
+			if (paths == null){
+				paths = new ArrayList<PdfPath>(2);
+				pathEndpoints.put(pp.lastPoint(), paths);
+			}
+			paths.add(pp);
+		}
+
+		List<PdfPath> pathChain = new ArrayList<PdfPath>(2);
+		Set<Point2D> pointsInPath = new HashSet<Point2D>();
+
+		//join the paths
+		for(PdfPath pp: layer.paths) {
+
+			if (pp.isClosed() || mergedPaths.contains(pp)) {
+				continue;
+			}
+
 			boolean changed = true;
 
-			while (changed && !path.isClosed()) {
-				changed  = false;
-
-				if (pathEndpoints.containsKey(path.firstPoint())) {
-
-					PdfPath p1 = pathEndpoints.get(path.firstPoint());
-
-					if (this.isSubpathOf(p1, path)){
-						continue;
+			PdfPath firstPath = pp;
+			PdfPath lastPath = pp;
+			Point2D firstPoint = pp.firstPoint();
+			Point2D lastPoint = pp.lastPoint();
+
+
+			pathChain.clear();
+			pathChain.add(pp);
+			pointsInPath.clear();
+			pointsInPath.add(firstPoint);
+			pointsInPath.add(lastPoint);
+
+			//process last point
+			while (changed && firstPoint != lastPoint) {
+				changed = false;
+
+				List<PdfPath> adjacentPaths = pathEndpoints.get(lastPoint);
+				PdfPath nextPath = findNextPath(adjacentPaths, lastPath);
+
+				if (nextPath != null) {
+					Point2D nextPoint = nextPath.getOtherEnd(lastPoint);
+
+					lastPoint = nextPoint;
+					lastPath = nextPath;
+					pathChain.add(lastPath);
+
+					if (!pointsInPath.contains(lastPoint)) {
+						pointsInPath.add(lastPoint);
+						changed = true;
 					}
-
-					pathEndpoints.remove(p1.firstPoint());
-					pathEndpoints.remove(p1.lastPoint());
-
-					List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points);
-
-					if (newNodes == null)
+					else
 					{
-						int a = 10;
-						a++;
+						//closed path found
+						//remove excess segments from start of chain
+						while (lastPoint != firstPoint) {
+							PdfPath pathToRemove = pathChain.get(0);
+							firstPoint = pathToRemove.getOtherEnd(firstPoint);
+						}
+
+						changed = false;
 					}
-
-					path.points = newNodes;
-					mergedPaths.add(p1);
-					changed = true;
 				}
-
-				if (pathEndpoints.containsKey(path.lastPoint())) {
-					PdfPath p1 = pathEndpoints.get(path.lastPoint());
-
-					if (this.isSubpathOf(p1, path)){
-						continue;
+			}
+
+
+			//process first point
+			changed = true;
+			while (changed && firstPoint != lastPoint) {
+				changed = false;
+
+				List<PdfPath> adjacentPaths = pathEndpoints.get(firstPoint);
+				PdfPath nextPath = findNextPath(adjacentPaths, firstPath);
+
+				if (nextPath != null) {
+					Point2D nextPoint = nextPath.getOtherEnd(firstPoint);
+
+					firstPoint = nextPoint;
+					firstPath = nextPath;
+					pathChain.add(0, firstPath);
+
+					if (!pointsInPath.contains(nextPoint)) {
+						pointsInPath.add(firstPoint);
+						changed = true;
 					}
-
-					pathEndpoints.remove(p1.firstPoint());
-					pathEndpoints.remove(p1.lastPoint());
-
-					List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points);
-					path.points = newNodes;
-					mergedPaths.add(p1);
-					changed = true;
+					else
+					{
+						//closed path found
+						//remove excess segments from end of chain
+						while (lastPoint != firstPoint) {
+							PdfPath pathToRemove = pathChain.get(pathChain.size() - 1);
+							lastPoint = pathToRemove.getOtherEnd(lastPoint);
+						}
+
+						changed = false;
+					}
 				}
 			}
 
-			if (!path.isClosed()){
-				pathEndpoints.put(path.firstPoint(), path);
-				pathEndpoints.put(path.lastPoint(), path);
-			}
-		}
-
-		List<PdfPath> resultPaths = new ArrayList<PdfPath>();
-
-		for(PdfPath path: layer.paths) {
-			if (!mergedPaths.contains(path)){
-				resultPaths.add(path);
-			}
-		}
-
-		layer.paths = resultPaths;
-	}
+			//construct path
+
+			//remove from map
+			for (PdfPath path: pathChain) {
+				pathEndpoints.get(path.firstPoint()).remove(path);
+				pathEndpoints.get(path.lastPoint()).remove(path);
+			}
+
+			PdfPath path = pathChain.get(0);
+
+			for (int pos = 1; pos < pathChain.size(); pos ++) {
+				path.points = tryMergeNodeLists(path.points, pathChain.get(pos).points);
+
+				if (path.points == null) {
+					throw new RuntimeException();
+				}
+
+				mergedPaths.add(pathChain.get(pos));
+			}
+
+			newPaths.add(path);
+		}
+
+		layer.paths = newPaths;
+	}
+
+	private PdfPath findNextPath(List<PdfPath> adjacentPaths, PdfPath firstPath) {
+		for (int pos = 0; pos < adjacentPaths.size(); pos ++) {
+			PdfPath p = adjacentPaths.get(pos);
+			if (p != firstPath && !isSubpathOf(firstPath, p)){
+				return p;
+			}
+		}
+
+		return null;
+	}
+
 
 	/**
Index: applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfPath.java
===================================================================
--- applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfPath.java	(revision 24060)
+++ applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfPath.java	(revision 24061)
@@ -7,33 +7,46 @@
 	public List<Point2D> points;
 	public double length;
-	
+
 	LayerContents layer;
 	public int nr;
-	
-	
+
+
 	public PdfPath(List<Point2D> nodes) {
-		points = nodes;		
+		points = nodes;
 	}
 
-	public boolean isClosed() {		
-		return points.size() > 1 && points.get(0) == points.get(points.size() - 1); 
+	public boolean isClosed() {
+		return points.size() > 1 && points.get(0) == points.get(points.size() - 1);
 	}
 
 	public Point2D firstPoint() {
-		return points.get(0);	
+		return points.get(0);
 	}
 
 	public Point2D lastPoint() {
-		return points.get(points.size() - 1);	
+		return points.get(points.size() - 1);
 	}
-	
+
 	public void calculateLength() {
 		double len = 0;
-		
+
 		for(int pos =1; pos < points.size(); pos ++) {
 			len += points.get(pos).distance(points.get(pos -1));
 		}
-		
+
 		this.length = len;
 	}
+
+	public Point2D getOtherEnd(Point2D endPoint) {
+		if (this.firstPoint() == endPoint) {
+			return this.lastPoint();
+		}
+
+		if (this.lastPoint() == endPoint) {
+			return this.firstPoint();
+		}
+
+		throw new RuntimeException("Unexpected point");
+
+	}
 }
