Index: /applications/editors/josm/plugins/smed2/jrender/src/jrender/Jrender.java
===================================================================
--- /applications/editors/josm/plugins/smed2/jrender/src/jrender/Jrender.java	(revision 30403)
+++ /applications/editors/josm/plugins/smed2/jrender/src/jrender/Jrender.java	(revision 30404)
@@ -28,11 +28,11 @@
 	
 	public static void main(String[] args) throws IOException {
-		if (args.length < 5) {
-			System.err.println("Usage: java -jar jrender.jar osm_file minlat, minlon, maxlat, maxlon");
+		if (args.length < 6) {
+			System.err.println("Usage: java -jar jrender.jar osm_file tile_directory minlat, minlon, maxlat, maxlon");
 			System.exit(-1);
 		}
 		
-		MapBB bb = new MapBB(Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]), Double.parseDouble(args[4]));
-		Tilegen.tileMap(Extract.extractData(args[0], bb), bb);
+		MapBB bb = new MapBB(Double.parseDouble(args[2]), Double.parseDouble(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]));
+		Tilegen.tileMap(Extract.extractData(args[0], bb), bb, args[1]);
 
 		System.err.println("Finished");
Index: /applications/editors/josm/plugins/smed2/jrender/src/jrender/Tilegen.java
===================================================================
--- /applications/editors/josm/plugins/smed2/jrender/src/jrender/Tilegen.java	(revision 30403)
+++ /applications/editors/josm/plugins/smed2/jrender/src/jrender/Tilegen.java	(revision 30404)
@@ -21,6 +21,4 @@
 import javax.imageio.ImageIO;
 
-import jrender.Jrender.MapBB;
-
 import org.apache.batik.dom.GenericDOMImplementation;
 import org.apache.batik.svggen.SVGGraphics2D;
@@ -28,170 +26,12 @@
 import org.w3c.dom.Document;
 
+import jrender.Jrender.MapBB;
 import render.MapContext;
 import render.Renderer;
 import s57.S57map;
-import s57.S57map.Feature;
-import s57.S57map.Snode;
+import s57.S57map.*;
 
 public class Tilegen {
 
-	public static void tileMap(ArrayList<String> buf, MapBB bb) throws IOException {
-		Context context;
-		String k = "";
-		String v = "";
-		
-		double lat = 0;
-		double lon = 0;
-		long id = 0;
-
-		BufferedImage img;
-		Graphics2D g2;
-		boolean inOsm = false;
-		boolean inNode = false;
-		boolean inWay = false;
-		boolean inRel = false;
-		
-		context = new Context(bb.minlat, bb.minlon, bb.maxlat, bb.maxlon);
-		S57map map = null;
-
-		for (String ln : buf) {
-			if (inOsm) {
-				if ((inNode || inWay || inRel) && (ln.contains("<tag"))) {
-					k = v = "";
-					String[] token = ln.split("k=");
-					k = token[1].split("[\"\']")[1];
-					token = token[1].split("v=");
-					v = token[1].split("[\"\']")[1];
-					if (!k.isEmpty() && !v.isEmpty()) {
-						map.addTag(k, v);
-					}
-				}
-				if (inNode) {
-					if (ln.contains("</node")) {
-						inNode = false;
-						map.tagsDone(id);
-					}
-				} else if (ln.contains("<node")) {
-					for (String token : ln.split("[ ]+")) {
-						if (token.matches("^id=.+")) {
-							id = Long.parseLong(token.split("[\"\']")[1]);
-						} else if (token.matches("^lat=.+")) {
-							lat = Double.parseDouble(token.split("[\"\']")[1]);
-						} else if (token.matches("^lon=.+")) {
-							lon = Double.parseDouble(token.split("[\"\']")[1]);
-						}
-					}
-					map.addNode(id, lat, lon);
-					if (ln.contains("/>")) {
-						map.tagsDone(id);
-					} else {
-						inNode = true;
-					}
-				} else if (inWay) {
-					if (ln.contains("<nd")) {
-						long ref = 0;
-						for (String token : ln.split("[ ]+")) {
-							if (token.matches("^ref=.+")) {
-								ref = Long.parseLong(token.split("[\"\']")[1]);
-							}
-						}
-						map.addToEdge(ref);
-					}
-					if (ln.contains("</way")) {
-						inWay = false;
-						map.tagsDone(id);
-					}
-				} else if (ln.contains("<way")) {
-					for (String token : ln.split("[ ]+")) {
-						if (token.matches("^id=.+")) {
-							id = Long.parseLong(token.split("[\"\']")[1]);
-						}
-					}
-					map.addEdge(id);
-					if (ln.contains("/>")) {
-						map.tagsDone(0);
-					} else {
-						inWay = true;
-					}
-				} else if (ln.contains("</osm")) {
-					inOsm = false;
-					break;
-				} else if (inRel) {
-					if (ln.contains("<member")) {
-						String type = "";
-						String role = "";
-						long ref = 0;
-						for (String token : ln.split("[ ]+")) {
-							if (token.matches("^ref=.+")) {
-								ref = Long.parseLong(token.split("[\"\']")[1]);
-							} else if (token.matches("^type=.+")) {
-								type = (token.split("[\"\']")[1]);
-							} else if (token.matches("^role=.+")) {
-								role = (token.split("[\"\']")[1]);
-							}
-						}
-						if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
-							map.addToArea(ref, role.equals("outer"));
-					}
-					if (ln.contains("</relation")) {
-						inRel = false;
-						map.tagsDone(id);
-					}
-				} else if (ln.contains("<relation")) {
-					for (String token : ln.split("[ ]+")) {
-						if (token.matches("^id=.+")) {
-							id = Long.parseLong(token.split("[\"\']")[1]);
-						}
-					}
-					map.addArea(id);
-					if (ln.contains("/>")) {
-						map.tagsDone(id);
-					} else {
-						inRel = true;
-					}
-				}
-			} else if (ln.contains("<osm")) {
-				inOsm = true;
-				map = new S57map();
-			}
-		}
-		
-		img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
-		Renderer.reRender(img.createGraphics(), 12, 1, map, context);
-		ByteArrayOutputStream bos = new ByteArrayOutputStream();
-		ImageIO.write(img, "png", bos);
-		int empty = bos.size();
-		for (int s = 1, z = 12; z <= 18; s *= 2, z++) {
-			for (int x = 0; x < s; x++) {
-				for (int y = 0; y < s; y++) {
-					img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
-					g2 = img.createGraphics();
-					g2.scale(s, s);
-					g2.translate(-(256 + (x * 256 / s)), -(256 + (y * 256 / s)));
-					Renderer.reRender(g2, z, 1, map, context);
-					bos = new ByteArrayOutputStream();
-					ImageIO.write(img, "png", bos);
-					if (bos.size() > empty) {
-						FileOutputStream fos = new FileOutputStream("/Users/mherring/boatsw/oseam/josm/plugins/smed2/jrender/tst/tst" + z + "_" + x + "_" + y + ".png");
-						bos.writeTo(fos);
-						fos.close();
-					}
-				}
-			}
-		}
-
-		for (int z = 12; z <= 18; z++) {
-			DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
-			Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
-			SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
-			svgGenerator.setSVGCanvasSize(new Dimension(256, 256));
-			svgGenerator.setClip(0, 0, 256, 256);
-			svgGenerator.translate(-256, -256);
-			Renderer.reRender(svgGenerator, z, 1, map, context);
-			svgGenerator.stream("/Users/mherring/boatsw/oseam/josm/plugins/smed2/jrender/tst/tst_" + z + ".svg");
-		}
-
-	}
-	
 	static class Context implements MapContext {
 		
@@ -223,5 +63,170 @@
 			return mile;
 		}
-		
 	}
+	
+	static Context context;
+	static S57map map;
+	static int empty;
+	static String dir;
+	
+	public static void tileMap(ArrayList<String> buf, MapBB bb, String idir) throws IOException {
+		String k = "";
+		String v = "";
+		
+		double lat = 0;
+		double lon = 0;
+		long id = 0;
+
+		BufferedImage img;
+		boolean inOsm = false;
+		boolean inNode = false;
+		boolean inWay = false;
+		boolean inRel = false;
+		
+		context = new Context(bb.minlat, bb.minlon, bb.maxlat, bb.maxlon);
+		dir = idir;
+
+		for (String ln : buf) {
+			if (inOsm) {
+				if ((inNode || inWay || inRel) && (ln.contains("<tag"))) {
+					k = v = "";
+					String[] token = ln.split("k=");
+					k = token[1].split("[\"\']")[1];
+					token = token[1].split("v=");
+					v = token[1].split("[\"\']")[1];
+					if (!k.isEmpty() && !v.isEmpty()) {
+						map.addTag(k, v);
+					}
+				}
+				if (inNode) {
+					if (ln.contains("</node")) {
+						inNode = false;
+						map.tagsDone(id);
+					}
+				} else if (ln.contains("<node")) {
+					for (String token : ln.split("[ ]+")) {
+						if (token.matches("^id=.+")) {
+							id = Long.parseLong(token.split("[\"\']")[1]);
+						} else if (token.matches("^lat=.+")) {
+							lat = Double.parseDouble(token.split("[\"\']")[1]);
+						} else if (token.matches("^lon=.+")) {
+							lon = Double.parseDouble(token.split("[\"\']")[1]);
+						}
+					}
+					map.addNode(id, lat, lon);
+					if (ln.contains("/>")) {
+						map.tagsDone(id);
+					} else {
+						inNode = true;
+					}
+				} else if (inWay) {
+					if (ln.contains("<nd")) {
+						long ref = 0;
+						for (String token : ln.split("[ ]+")) {
+							if (token.matches("^ref=.+")) {
+								ref = Long.parseLong(token.split("[\"\']")[1]);
+							}
+						}
+						map.addToEdge(ref);
+					}
+					if (ln.contains("</way")) {
+						inWay = false;
+						map.tagsDone(id);
+					}
+				} else if (ln.contains("<way")) {
+					for (String token : ln.split("[ ]+")) {
+						if (token.matches("^id=.+")) {
+							id = Long.parseLong(token.split("[\"\']")[1]);
+						}
+					}
+					map.addEdge(id);
+					if (ln.contains("/>")) {
+						map.tagsDone(0);
+					} else {
+						inWay = true;
+					}
+				} else if (ln.contains("</osm")) {
+					inOsm = false;
+					break;
+				} else if (inRel) {
+					if (ln.contains("<member")) {
+						String type = "";
+						String role = "";
+						long ref = 0;
+						for (String token : ln.split("[ ]+")) {
+							if (token.matches("^ref=.+")) {
+								ref = Long.parseLong(token.split("[\"\']")[1]);
+							} else if (token.matches("^type=.+")) {
+								type = (token.split("[\"\']")[1]);
+							} else if (token.matches("^role=.+")) {
+								role = (token.split("[\"\']")[1]);
+							}
+						}
+						if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
+							map.addToArea(ref, role.equals("outer"));
+					}
+					if (ln.contains("</relation")) {
+						inRel = false;
+						map.tagsDone(id);
+					}
+				} else if (ln.contains("<relation")) {
+					for (String token : ln.split("[ ]+")) {
+						if (token.matches("^id=.+")) {
+							id = Long.parseLong(token.split("[\"\']")[1]);
+						}
+					}
+					map.addArea(id);
+					if (ln.contains("/>")) {
+						map.tagsDone(id);
+					} else {
+						inRel = true;
+					}
+				}
+			} else if (ln.contains("<osm")) {
+				inOsm = true;
+				map = new S57map();
+			}
+		}
+		
+		img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
+		Renderer.reRender(img.createGraphics(), 12, 1, map, context);
+		ByteArrayOutputStream bos = new ByteArrayOutputStream();
+		ImageIO.write(img, "png", bos);
+		empty = bos.size();
+		tile(12, 1, 0, 0);
+
+		for (int z = 12; z <= 18; z++) {
+			DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
+			Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
+			SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
+			svgGenerator.setSVGCanvasSize(new Dimension(256, 256));
+			svgGenerator.setClip(0, 0, 256, 256);
+			svgGenerator.translate(-256, -256);
+			Renderer.reRender(svgGenerator, z, 1, map, context);
+			svgGenerator.stream(dir + "tst_" + z + ".svg");
+		}
+	}
+	
+	static void tile(int zoom, int s, int xn, int yn) throws IOException {
+		BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
+		Graphics2D g2 = img.createGraphics();
+		g2.scale(s, s);
+		g2.translate(-(256 + (xn * 256 / s)), -(256 + (yn * 256 / s)));
+		Renderer.reRender(g2, zoom, 1, map, context);
+		ByteArrayOutputStream bos = new ByteArrayOutputStream();
+		ImageIO.write(img, "png", bos);
+		if (bos.size() > empty) {
+			FileOutputStream fos = new FileOutputStream(dir + "tst" + zoom + "_" + xn + "_" + yn + ".png");
+			bos.writeTo(fos);
+			fos.close();
+		}
+		if ((zoom < 18) && ((zoom < 16) || (bos.size() > empty))) {
+			for (int x = 0; x < 2; x++) {
+				for (int y = 0; y < 2; y++) {
+					tile((zoom + 1), (s * 2), (xn * 2 + x), (yn * 2 + y));
+				}
+			}
+		}
+	}
+	
 }
