Index: build.xml
===================================================================
--- build.xml	(revision 3777)
+++ build.xml	(working copy)
@@ -233,7 +233,7 @@
 		<javac srcdir="${src}" destdir="${build.classes}" encoding="utf-8" debug="true" includeantruntime="false">
 			<include name="**/*.java" />
 			<classpath refid="main"/>
-			<exclude name="**/optional/*.java"/>
+			<exclude name="**/dem/optional/*.java"/>
 		</javac>
 	</target>
 
Index: src/uk/me/parabola/mkgmap/build/LayerFilterChain.java
===================================================================
--- src/uk/me/parabola/mkgmap/build/LayerFilterChain.java	(revision 3777)
+++ src/uk/me/parabola/mkgmap/build/LayerFilterChain.java	(working copy)
@@ -25,6 +25,8 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.geotools.util.UnsupportedImplementationException;
+
 /**
  * This calls all the filters that are applied to an element as it is added to
  * the map at a particular level.
@@ -53,6 +55,9 @@
 		if (position >= nfilters)
 			return;
 		
+		if (position < 0) {
+			long dd = 4;
+		}
 		MapFilter f = filters.get(position++);
 		f.doFilter(element, this);
 		// maintain chain position for repeated calls in the split filters 
@@ -79,4 +84,13 @@
 		filter.init(config);
 		filters.add(filter);
 	}
+	
+	@Override
+	public void resetPos(MapFilter f) {
+		position = 0;
+		while (f != filters.get(position)) {
+			position++;
+		}
+	}
+	
 }
Index: src/uk/me/parabola/mkgmap/build/MapArea.java
===================================================================
--- src/uk/me/parabola/mkgmap/build/MapArea.java	(revision 3777)
+++ src/uk/me/parabola/mkgmap/build/MapArea.java	(working copy)
@@ -21,21 +21,18 @@
 import java.util.List;
 
 import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
-
-import uk.me.parabola.imgfmt.Utils;
-// import uk.me.parabola.util.Java2DConverter;
-import uk.me.parabola.util.ShapeSplitter;
 import uk.me.parabola.imgfmt.app.Area;
 import uk.me.parabola.imgfmt.app.Coord;
+import uk.me.parabola.imgfmt.app.net.RoadNetwork;
 import uk.me.parabola.imgfmt.app.trergn.Overview;
 import uk.me.parabola.log.Logger;
 import uk.me.parabola.mkgmap.filters.FilterConfig;
 import uk.me.parabola.mkgmap.filters.LineSizeSplitterFilter;
 import uk.me.parabola.mkgmap.filters.LineSplitterFilter;
+import uk.me.parabola.mkgmap.filters.MapFilter;
 import uk.me.parabola.mkgmap.filters.MapFilterChain;
 import uk.me.parabola.mkgmap.filters.PolygonSplitterFilter;
 import uk.me.parabola.mkgmap.filters.PolygonSubdivSizeSplitterFilter;
-import uk.me.parabola.mkgmap.filters.ShapeMergeFilter;
 import uk.me.parabola.mkgmap.general.MapDataSource;
 import uk.me.parabola.mkgmap.general.MapElement;
 import uk.me.parabola.mkgmap.general.MapLine;
@@ -42,7 +39,8 @@
 import uk.me.parabola.mkgmap.general.MapPoint;
 import uk.me.parabola.mkgmap.general.MapRoad;
 import uk.me.parabola.mkgmap.general.MapShape;
-import uk.me.parabola.imgfmt.app.net.RoadNetwork;
+// import uk.me.parabola.util.Java2DConverter;
+import uk.me.parabola.util.ShapeSplitter;
 
 /**
  * A sub area of the map.  We have to divide the map up into areas to meet the
@@ -129,6 +127,11 @@
 				MapShape shape = (MapShape) element;
 				addShape(shape);
 			}
+
+			@Override
+			public void resetPos(MapFilter f) {
+				throw new UnsupportedOperationException("");
+			}
 		};
 
 		PolygonSubdivSizeSplitterFilter filter = new PolygonSubdivSizeSplitterFilter();
@@ -156,6 +159,12 @@
 				MapLine line = (MapLine) element;
 				addLine(line);
 			}
+
+			@Override
+			public void resetPos(MapFilter f) {
+				// TODO Auto-generated method stub
+				
+			}
 		};
 
 		LineSizeSplitterFilter filter = new LineSizeSplitterFilter();
Index: src/uk/me/parabola/mkgmap/build/MapBuilder.java
===================================================================
--- src/uk/me/parabola/mkgmap/build/MapBuilder.java	(revision 3777)
+++ src/uk/me/parabola/mkgmap/build/MapBuilder.java	(working copy)
@@ -73,7 +73,7 @@
 import uk.me.parabola.mkgmap.filters.LineSplitterFilter;
 import uk.me.parabola.mkgmap.filters.MapFilter;
 import uk.me.parabola.mkgmap.filters.MapFilterChain;
-import uk.me.parabola.mkgmap.filters.PolygonSplitterFilter;
+import uk.me.parabola.mkgmap.filters.PolygonSplitIfNeededFilter;
 import uk.me.parabola.mkgmap.filters.RemoveEmpty;
 import uk.me.parabola.mkgmap.filters.RemoveObsoletePointsFilter;
 import uk.me.parabola.mkgmap.filters.RoundCoordsFilter;
@@ -1138,6 +1138,7 @@
 		preserveHorizontalAndVerticalLines(res, shapes);
 		
 		LayerFilterChain filters = new LayerFilterChain(config);
+		filters.addFilter(new PolygonSplitIfNeededFilter());
 		if (enableLineCleanFilters && (res < 24)) {
 			filters.addFilter(new RoundCoordsFilter());
 			int sizefilterVal =  getMinSizePolygonForResolution(res);
@@ -1148,7 +1149,6 @@
 			if(reducePointErrorPolygon > 0)
 				filters.addFilter(new DouglasPeuckerFilter(reducePointErrorPolygon));
 		}
-		filters.addFilter(new PolygonSplitterFilter());
 		filters.addFilter(new RemoveEmpty());
 		filters.addFilter(new RemoveObsoletePointsFilter());
 		filters.addFilter(new LinePreparerFilter(div));
@@ -1157,7 +1157,6 @@
 		for (MapShape shape : shapes) {
 			if (shape.getMinResolution() > res || shape.getMaxResolution() < res)
 				continue;
-
 			filters.startFilter(shape);
 		}
 	}
Index: src/uk/me/parabola/mkgmap/filters/LinePreparerFilter.java
===================================================================
--- src/uk/me/parabola/mkgmap/filters/LinePreparerFilter.java	(revision 3777)
+++ src/uk/me/parabola/mkgmap/filters/LinePreparerFilter.java	(working copy)
@@ -51,6 +51,9 @@
 		MapLine line = (MapLine) element;
 
 		int numPoints = line.getPoints().size();
+		if (line instanceof MapShape && numPoints >= PolygonSplitterFilter.MAX_POINT_IN_ELEMENT)
+			throw new MustSplitException();
+
 		boolean first = true;
 		int minPointsRequired = (element instanceof MapShape) ? 3:2;
 		if (minPointsRequired == 3 && line.getPoints().get(0).equals(line.getPoints().get(numPoints-1)))
Index: src/uk/me/parabola/mkgmap/filters/MapFilterChain.java
===================================================================
--- src/uk/me/parabola/mkgmap/filters/MapFilterChain.java	(revision 3777)
+++ src/uk/me/parabola/mkgmap/filters/MapFilterChain.java	(working copy)
@@ -37,4 +37,10 @@
 	 */
 	public void doFilter(MapElement element);
 
+	/**
+	 * Reset the chain so that the next call of
+	 * {@code doFilter(MapElement element)} executes the given filter (again).
+	 */
+	public void resetPos(MapFilter f);
+
 }
Index: src/uk/me/parabola/mkgmap/filters/MustSplitException.java
===================================================================
--- src/uk/me/parabola/mkgmap/filters/MustSplitException.java	(nonexistent)
+++ src/uk/me/parabola/mkgmap/filters/MustSplitException.java	(working copy)
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2017
+ * 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ * 
+ * 
+ */
+package uk.me.parabola.mkgmap.filters;
+
+/**
+ * Used to exit the program.  So that System.exit need only be called
+ * in the one place, or indeed not at all.
+ *
+ * @author Gerd Petermann
+ */
+public class MustSplitException extends RuntimeException {
+
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 3534824303375821690L;
+
+	/**
+	 * Constructs a new runtime exception to signal that the object needs a split.
+	 */
+	public MustSplitException() {
+		super();
+	}
+
+}
Index: src/uk/me/parabola/mkgmap/filters/PolygonSplitIfNeededFilter.java
===================================================================
--- src/uk/me/parabola/mkgmap/filters/PolygonSplitIfNeededFilter.java	(nonexistent)
+++ src/uk/me/parabola/mkgmap/filters/PolygonSplitIfNeededFilter.java	(working copy)
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2017 
+ * 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ * 
+ */
+package uk.me.parabola.mkgmap.filters;
+
+import uk.me.parabola.mkgmap.general.MapElement;
+import uk.me.parabola.mkgmap.general.MapShape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This filter splits a polygon if any of the subsequent filters throws a {@link MustSplitException}.
+ * This will not happen often.
+ *
+ * @author Gerd Petermann
+ */
+public class PolygonSplitIfNeededFilter extends PolygonSplitterBase implements MapFilter {
+
+	public PolygonSplitIfNeededFilter() {
+	}
+
+	/**
+	 *
+	 * @param element A map element, only polygons will be processed.
+	 * @param next	This is used to pass the possibly transformed element onward.
+	 */
+	public void doFilter(MapElement element, MapFilterChain next) {
+		assert element instanceof MapShape;
+		MapShape shape = (MapShape) element;
+
+		try {
+			next.doFilter(shape);
+		} catch (MustSplitException e) {
+			List<MapShape> outputs = new ArrayList<MapShape>();
+			split(shape,outputs);
+			for (MapShape s : outputs) {
+				next.resetPos(this); 
+				next.doFilter(s);
+			}
+		}
+	}
+}
