Changeset 30404 in osm for applications/editors/josm/plugins/smed2/jrender
- Timestamp:
- 2014-04-17T00:37:54+02:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/smed2/jrender/src/jrender
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/smed2/jrender/src/jrender/Jrender.java
r30402 r30404 28 28 29 29 public static void main(String[] args) throws IOException { 30 if (args.length < 5) {31 System.err.println("Usage: java -jar jrender.jar osm_file minlat, minlon, maxlat, maxlon"); 30 if (args.length < 6) { 31 System.err.println("Usage: java -jar jrender.jar osm_file tile_directory minlat, minlon, maxlat, maxlon"); 32 32 System.exit(-1); 33 33 } 34 34 35 MapBB bb = new MapBB(Double.parseDouble(args[ 1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]), Double.parseDouble(args[4]));36 Tilegen.tileMap(Extract.extractData(args[0], bb), bb); 35 MapBB bb = new MapBB(Double.parseDouble(args[2]), Double.parseDouble(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5])); 36 Tilegen.tileMap(Extract.extractData(args[0], bb), bb, args[1]); 37 37 38 38 System.err.println("Finished"); -
applications/editors/josm/plugins/smed2/jrender/src/jrender/Tilegen.java
r30402 r30404 21 21 import javax.imageio.ImageIO; 22 22 23 import jrender.Jrender.MapBB;24 25 23 import org.apache.batik.dom.GenericDOMImplementation; 26 24 import org.apache.batik.svggen.SVGGraphics2D; … … 28 26 import org.w3c.dom.Document; 29 27 28 import jrender.Jrender.MapBB; 30 29 import render.MapContext; 31 30 import render.Renderer; 32 31 import s57.S57map; 33 import s57.S57map.Feature; 34 import s57.S57map.Snode; 32 import s57.S57map.*; 35 33 36 34 public class Tilegen { 37 35 38 public static void tileMap(ArrayList<String> buf, MapBB bb) throws IOException {39 Context context;40 String k = "";41 String v = "";42 43 double lat = 0;44 double lon = 0;45 long id = 0;46 47 BufferedImage img;48 Graphics2D g2;49 boolean inOsm = false;50 boolean inNode = false;51 boolean inWay = false;52 boolean inRel = false;53 54 context = new Context(bb.minlat, bb.minlon, bb.maxlat, bb.maxlon);55 S57map map = null;56 57 for (String ln : buf) {58 if (inOsm) {59 if ((inNode || inWay || inRel) && (ln.contains("<tag"))) {60 k = v = "";61 String[] token = ln.split("k=");62 k = token[1].split("[\"\']")[1];63 token = token[1].split("v=");64 v = token[1].split("[\"\']")[1];65 if (!k.isEmpty() && !v.isEmpty()) {66 map.addTag(k, v);67 }68 }69 if (inNode) {70 if (ln.contains("</node")) {71 inNode = false;72 map.tagsDone(id);73 }74 } else if (ln.contains("<node")) {75 for (String token : ln.split("[ ]+")) {76 if (token.matches("^id=.+")) {77 id = Long.parseLong(token.split("[\"\']")[1]);78 } else if (token.matches("^lat=.+")) {79 lat = Double.parseDouble(token.split("[\"\']")[1]);80 } else if (token.matches("^lon=.+")) {81 lon = Double.parseDouble(token.split("[\"\']")[1]);82 }83 }84 map.addNode(id, lat, lon);85 if (ln.contains("/>")) {86 map.tagsDone(id);87 } else {88 inNode = true;89 }90 } else if (inWay) {91 if (ln.contains("<nd")) {92 long ref = 0;93 for (String token : ln.split("[ ]+")) {94 if (token.matches("^ref=.+")) {95 ref = Long.parseLong(token.split("[\"\']")[1]);96 }97 }98 map.addToEdge(ref);99 }100 if (ln.contains("</way")) {101 inWay = false;102 map.tagsDone(id);103 }104 } else if (ln.contains("<way")) {105 for (String token : ln.split("[ ]+")) {106 if (token.matches("^id=.+")) {107 id = Long.parseLong(token.split("[\"\']")[1]);108 }109 }110 map.addEdge(id);111 if (ln.contains("/>")) {112 map.tagsDone(0);113 } else {114 inWay = true;115 }116 } else if (ln.contains("</osm")) {117 inOsm = false;118 break;119 } else if (inRel) {120 if (ln.contains("<member")) {121 String type = "";122 String role = "";123 long ref = 0;124 for (String token : ln.split("[ ]+")) {125 if (token.matches("^ref=.+")) {126 ref = Long.parseLong(token.split("[\"\']")[1]);127 } else if (token.matches("^type=.+")) {128 type = (token.split("[\"\']")[1]);129 } else if (token.matches("^role=.+")) {130 role = (token.split("[\"\']")[1]);131 }132 }133 if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))134 map.addToArea(ref, role.equals("outer"));135 }136 if (ln.contains("</relation")) {137 inRel = false;138 map.tagsDone(id);139 }140 } else if (ln.contains("<relation")) {141 for (String token : ln.split("[ ]+")) {142 if (token.matches("^id=.+")) {143 id = Long.parseLong(token.split("[\"\']")[1]);144 }145 }146 map.addArea(id);147 if (ln.contains("/>")) {148 map.tagsDone(id);149 } else {150 inRel = true;151 }152 }153 } else if (ln.contains("<osm")) {154 inOsm = true;155 map = new S57map();156 }157 }158 159 img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);160 Renderer.reRender(img.createGraphics(), 12, 1, map, context);161 ByteArrayOutputStream bos = new ByteArrayOutputStream();162 ImageIO.write(img, "png", bos);163 int empty = bos.size();164 for (int s = 1, z = 12; z <= 18; s *= 2, z++) {165 for (int x = 0; x < s; x++) {166 for (int y = 0; y < s; y++) {167 img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);168 g2 = img.createGraphics();169 g2.scale(s, s);170 g2.translate(-(256 + (x * 256 / s)), -(256 + (y * 256 / s)));171 Renderer.reRender(g2, z, 1, map, context);172 bos = new ByteArrayOutputStream();173 ImageIO.write(img, "png", bos);174 if (bos.size() > empty) {175 FileOutputStream fos = new FileOutputStream("/Users/mherring/boatsw/oseam/josm/plugins/smed2/jrender/tst/tst" + z + "_" + x + "_" + y + ".png");176 bos.writeTo(fos);177 fos.close();178 }179 }180 }181 }182 183 for (int z = 12; z <= 18; z++) {184 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();185 Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);186 SVGGraphics2D svgGenerator = new SVGGraphics2D(document);187 svgGenerator.setSVGCanvasSize(new Dimension(256, 256));188 svgGenerator.setClip(0, 0, 256, 256);189 svgGenerator.translate(-256, -256);190 Renderer.reRender(svgGenerator, z, 1, map, context);191 svgGenerator.stream("/Users/mherring/boatsw/oseam/josm/plugins/smed2/jrender/tst/tst_" + z + ".svg");192 }193 194 }195 196 36 static class Context implements MapContext { 197 37 … … 223 63 return mile; 224 64 } 225 226 65 } 66 67 static Context context; 68 static S57map map; 69 static int empty; 70 static String dir; 71 72 public static void tileMap(ArrayList<String> buf, MapBB bb, String idir) throws IOException { 73 String k = ""; 74 String v = ""; 75 76 double lat = 0; 77 double lon = 0; 78 long id = 0; 79 80 BufferedImage img; 81 boolean inOsm = false; 82 boolean inNode = false; 83 boolean inWay = false; 84 boolean inRel = false; 85 86 context = new Context(bb.minlat, bb.minlon, bb.maxlat, bb.maxlon); 87 dir = idir; 88 89 for (String ln : buf) { 90 if (inOsm) { 91 if ((inNode || inWay || inRel) && (ln.contains("<tag"))) { 92 k = v = ""; 93 String[] token = ln.split("k="); 94 k = token[1].split("[\"\']")[1]; 95 token = token[1].split("v="); 96 v = token[1].split("[\"\']")[1]; 97 if (!k.isEmpty() && !v.isEmpty()) { 98 map.addTag(k, v); 99 } 100 } 101 if (inNode) { 102 if (ln.contains("</node")) { 103 inNode = false; 104 map.tagsDone(id); 105 } 106 } else if (ln.contains("<node")) { 107 for (String token : ln.split("[ ]+")) { 108 if (token.matches("^id=.+")) { 109 id = Long.parseLong(token.split("[\"\']")[1]); 110 } else if (token.matches("^lat=.+")) { 111 lat = Double.parseDouble(token.split("[\"\']")[1]); 112 } else if (token.matches("^lon=.+")) { 113 lon = Double.parseDouble(token.split("[\"\']")[1]); 114 } 115 } 116 map.addNode(id, lat, lon); 117 if (ln.contains("/>")) { 118 map.tagsDone(id); 119 } else { 120 inNode = true; 121 } 122 } else if (inWay) { 123 if (ln.contains("<nd")) { 124 long ref = 0; 125 for (String token : ln.split("[ ]+")) { 126 if (token.matches("^ref=.+")) { 127 ref = Long.parseLong(token.split("[\"\']")[1]); 128 } 129 } 130 map.addToEdge(ref); 131 } 132 if (ln.contains("</way")) { 133 inWay = false; 134 map.tagsDone(id); 135 } 136 } else if (ln.contains("<way")) { 137 for (String token : ln.split("[ ]+")) { 138 if (token.matches("^id=.+")) { 139 id = Long.parseLong(token.split("[\"\']")[1]); 140 } 141 } 142 map.addEdge(id); 143 if (ln.contains("/>")) { 144 map.tagsDone(0); 145 } else { 146 inWay = true; 147 } 148 } else if (ln.contains("</osm")) { 149 inOsm = false; 150 break; 151 } else if (inRel) { 152 if (ln.contains("<member")) { 153 String type = ""; 154 String role = ""; 155 long ref = 0; 156 for (String token : ln.split("[ ]+")) { 157 if (token.matches("^ref=.+")) { 158 ref = Long.parseLong(token.split("[\"\']")[1]); 159 } else if (token.matches("^type=.+")) { 160 type = (token.split("[\"\']")[1]); 161 } else if (token.matches("^role=.+")) { 162 role = (token.split("[\"\']")[1]); 163 } 164 } 165 if ((role.equals("outer") || role.equals("inner")) && type.equals("way")) 166 map.addToArea(ref, role.equals("outer")); 167 } 168 if (ln.contains("</relation")) { 169 inRel = false; 170 map.tagsDone(id); 171 } 172 } else if (ln.contains("<relation")) { 173 for (String token : ln.split("[ ]+")) { 174 if (token.matches("^id=.+")) { 175 id = Long.parseLong(token.split("[\"\']")[1]); 176 } 177 } 178 map.addArea(id); 179 if (ln.contains("/>")) { 180 map.tagsDone(id); 181 } else { 182 inRel = true; 183 } 184 } 185 } else if (ln.contains("<osm")) { 186 inOsm = true; 187 map = new S57map(); 188 } 189 } 190 191 img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); 192 Renderer.reRender(img.createGraphics(), 12, 1, map, context); 193 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 194 ImageIO.write(img, "png", bos); 195 empty = bos.size(); 196 tile(12, 1, 0, 0); 197 198 for (int z = 12; z <= 18; z++) { 199 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); 200 Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null); 201 SVGGraphics2D svgGenerator = new SVGGraphics2D(document); 202 svgGenerator.setSVGCanvasSize(new Dimension(256, 256)); 203 svgGenerator.setClip(0, 0, 256, 256); 204 svgGenerator.translate(-256, -256); 205 Renderer.reRender(svgGenerator, z, 1, map, context); 206 svgGenerator.stream(dir + "tst_" + z + ".svg"); 207 } 208 } 209 210 static void tile(int zoom, int s, int xn, int yn) throws IOException { 211 BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); 212 Graphics2D g2 = img.createGraphics(); 213 g2.scale(s, s); 214 g2.translate(-(256 + (xn * 256 / s)), -(256 + (yn * 256 / s))); 215 Renderer.reRender(g2, zoom, 1, map, context); 216 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 217 ImageIO.write(img, "png", bos); 218 if (bos.size() > empty) { 219 FileOutputStream fos = new FileOutputStream(dir + "tst" + zoom + "_" + xn + "_" + yn + ".png"); 220 bos.writeTo(fos); 221 fos.close(); 222 } 223 if ((zoom < 18) && ((zoom < 16) || (bos.size() > empty))) { 224 for (int x = 0; x < 2; x++) { 225 for (int y = 0; y < 2; y++) { 226 tile((zoom + 1), (s * 2), (xn * 2 + x), (yn * 2 + y)); 227 } 228 } 229 } 230 } 231 227 232 }
Note:
See TracChangeset
for help on using the changeset viewer.