Changeset 32906 in osm for applications/editors/josm/plugins/seachart/jrender
- Timestamp:
- 2016-09-03T16:18:15+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/jrender/src/jrender/Jrender.java
r32393 r32906 34 34 public class Jrender { 35 35 36 static String srcdir;37 static String dstdir;38 static int xtile;39 static int ytile;40 static int zoom;41 static ArrayList<String> send;42 static HashMap<String, Boolean> deletes;43 static Context context;44 static S57map map;45 static int empty;36 static String srcdir; 37 static String dstdir; 38 static int xtile; 39 static int ytile; 40 static int zoom; 41 static ArrayList<String> send; 42 static HashMap<String, Boolean> deletes; 43 static Context context; 44 static S57map map; 45 static int empty; 46 46 47 static class Context implements ChartContext {48 49 static double top;50 static double mile;51 52 public Context () {53 top = (1.0 - Math.log(Math.tan(map.bounds.maxlat) + 1.0 / Math.cos(map.bounds.maxlat)) / Math.PI) / 2.0 * 256.0 * 4096.0 * Math.pow(2, (zoom - 12));54 mile = (2 * ((zoom < 12) ? (256 / (int)(Math.pow(2, (11 - zoom)))) : 256) + 256) / ((Math.toDegrees(map.bounds.maxlat) - Math.toDegrees(map.bounds.minlat)) * 60);55 }56 57 public Point2D getPoint(Snode coord) {58 double x = (Math.toDegrees(coord.lon) - Math.toDegrees(map.bounds.minlon)) * 256.0 * 2048.0 * Math.pow(2, (zoom - 12)) / 180.0;59 double y = ((1.0 - Math.log(Math.tan(coord.lat) + 1.0 / Math.cos(coord.lat)) / Math.PI) / 2.0 * 256.0 * 4096.0 * Math.pow(2, (zoom - 12))) - top;60 return new Point2D.Double(x, y);61 }47 static class Context implements ChartContext { 48 49 static double top; 50 static double mile; 51 52 public Context () { 53 top = (1.0 - Math.log(Math.tan(map.bounds.maxlat) + 1.0 / Math.cos(map.bounds.maxlat)) / Math.PI) / 2.0 * 256.0 * 4096.0 * Math.pow(2, (zoom - 12)); 54 mile = (2 * ((zoom < 12) ? (256 / (int)(Math.pow(2, (11 - zoom)))) : 256) + 256) / ((Math.toDegrees(map.bounds.maxlat) - Math.toDegrees(map.bounds.minlat)) * 60); 55 } 56 57 public Point2D getPoint(Snode coord) { 58 double x = (Math.toDegrees(coord.lon) - Math.toDegrees(map.bounds.minlon)) * 256.0 * 2048.0 * Math.pow(2, (zoom - 12)) / 180.0; 59 double y = ((1.0 - Math.log(Math.tan(coord.lat) + 1.0 / Math.cos(coord.lat)) / Math.PI) / 2.0 * 256.0 * 4096.0 * Math.pow(2, (zoom - 12))) - top; 60 return new Point2D.Double(x, y); 61 } 62 62 63 public double mile(Feature feature) {64 return mile;65 }63 public double mile(Feature feature) { 64 return mile; 65 } 66 66 67 public boolean clip() {68 return false;69 }67 public boolean clip() { 68 return false; 69 } 70 70 71 public Color background(S57map map) {72 return new Color(0, true);73 }71 public Color background(S57map map) { 72 return new Color(0, true); 73 } 74 74 75 public RuleSet ruleset() {76 return RuleSet.SEAMARK;77 }78 }79 80 static void tile(int z, int s, int xn, int yn) throws IOException {81 int border = (z < 12) ? (256 / (int)(Math.pow(2, (11 - zoom)))) : 256;82 int scale = (int)Math.pow(2, z - 12);83 int xdir = (scale > 0) ? (scale * xtile) + xn : xtile;84 int ynam = (scale > 0) ? (scale * ytile) + yn : ytile;85 BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);86 Graphics2D g2 = img.createGraphics();87 g2.scale(s, s);88 g2.translate(-(border + (xn * 256 / s)), -(border + (yn * 256 / s)));89 Renderer.reRender(g2, new Rectangle(256, 256), z, 1.0 * Math.pow(2, (zoom - 12)), map, context);90 ByteArrayOutputStream bos = new ByteArrayOutputStream();91 ImageIO.write(img, "png", bos);92 if (bos.size() > empty) {93 String dstnam = dstdir + z + "/" + xdir + "/" + ynam + ".png";94 deletes.remove(dstnam);95 send.add("put " + dstnam + " tiles/" + z + "/" + xdir + "/" + ynam + ".png");96 File ofile = new File(dstdir + "/" + z + "/" + xdir + "/");97 ofile.mkdirs();98 FileOutputStream fos = new FileOutputStream(dstdir + "/" + z + "/" + xdir + "/" + ynam + ".png");99 bos.writeTo(fos);100 fos.close();101 if (send.size() > 20) {102 PrintWriter writer = new PrintWriter(srcdir + z + "-" + xdir + "-" + ynam + ".send", "UTF-8");103 for (String str : send) {104 writer.println(str);105 }106 writer.close();107 send = new ArrayList<>();108 }109 }110 if ((z >= 12) && (z < 18) && ((z < 16) || (bos.size() > empty))) {111 for (int x = 0; x < 2; x++) {112 for (int y = 0; y < 2; y++) {113 tile((z + 1), (s * 2), (xn * 2 + x), (yn * 2 + y));114 }115 }116 }117 }118 119 static void clean(int z, int xn, int yn) throws Exception {120 int scale = (int) Math.pow(2, z - 12);121 int xdir = (scale * xtile) + xn;122 int ynam = (scale * ytile) + yn;123 String delnam = dstdir + z + "/" + xdir + "/" + ynam + ".png";124 File delfile = new File(delnam);125 if (delfile.exists()) {126 deletes.put(delnam, true);127 delfile.delete();128 }129 if ((z < 18)) {130 for (int x = 0; x < 2; x++) {131 for (int y = 0; y < 2; y++) {132 clean((z + 1), (xn * 2 + x), (yn * 2 + y));133 }134 }135 }136 }137 138 public static void main(String[] args) throws Exception {139 if (args.length < 5) {140 System.err.println("Usage: java -jar jrender.jar <osm source directory> <tile directory> <zoom> <xtile> <ytile>");141 System.exit(-1);142 }143 srcdir = args[0];144 dstdir = args[1];145 zoom = Integer.parseInt(args[2]);146 xtile = Integer.parseInt(args[3]);147 ytile = Integer.parseInt(args[4]);148 send = new ArrayList<>();149 deletes = new HashMap<>();150 BufferedReader in = new BufferedReader(new FileReader(srcdir + xtile + "-" + ytile + "-" + zoom + ".osm"));151 map = new S57map(true);152 S57osm.OSMmap(in, map, false);153 in.close();154 context = new Context();155 ByteArrayOutputStream bos = new ByteArrayOutputStream();156 ImageIO.write(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), "png", bos);157 empty = bos.size();158 if (zoom == 12) {159 clean(12, 0, 0);160 }161 tile(zoom, 1, 0, 0);162 if (send.size() > 0) {163 PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".send", "UTF-8");164 for (String str : send) {165 writer.println(str);166 }167 writer.close();168 }169 if (deletes.size() > 0) {170 PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".delete", "UTF-8");171 for (String del : deletes.keySet()) {172 writer.println("rm " + del);173 }174 writer.close();175 }176 System.exit(0);177 }75 public RuleSet ruleset() { 76 return RuleSet.SEAMARK; 77 } 78 } 79 80 static void tile(int z, int s, int xn, int yn) throws IOException { 81 int border = (z < 12) ? (256 / (int)(Math.pow(2, (11 - zoom)))) : 256; 82 int scale = (int)Math.pow(2, z - 12); 83 int xdir = (scale > 0) ? (scale * xtile) + xn : xtile; 84 int ynam = (scale > 0) ? (scale * ytile) + yn : ytile; 85 BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); 86 Graphics2D g2 = img.createGraphics(); 87 g2.scale(s, s); 88 g2.translate(-(border + (xn * 256 / s)), -(border + (yn * 256 / s))); 89 Renderer.reRender(g2, new Rectangle(256, 256), z, 1.0 * Math.pow(2, (zoom - 12)), map, context); 90 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 91 ImageIO.write(img, "png", bos); 92 if (bos.size() > empty) { 93 String dstnam = dstdir + z + "/" + xdir + "/" + ynam + ".png"; 94 deletes.remove(dstnam); 95 send.add("put " + dstnam + " tiles/" + z + "/" + xdir + "/" + ynam + ".png"); 96 File ofile = new File(dstdir + "/" + z + "/" + xdir + "/"); 97 ofile.mkdirs(); 98 FileOutputStream fos = new FileOutputStream(dstdir + "/" + z + "/" + xdir + "/" + ynam + ".png"); 99 bos.writeTo(fos); 100 fos.close(); 101 if (send.size() > 20) { 102 PrintWriter writer = new PrintWriter(srcdir + z + "-" + xdir + "-" + ynam + ".send", "UTF-8"); 103 for (String str : send) { 104 writer.println(str); 105 } 106 writer.close(); 107 send = new ArrayList<>(); 108 } 109 } 110 if ((z >= 12) && (z < 18) && ((z < 16) || (bos.size() > empty))) { 111 for (int x = 0; x < 2; x++) { 112 for (int y = 0; y < 2; y++) { 113 tile((z + 1), (s * 2), (xn * 2 + x), (yn * 2 + y)); 114 } 115 } 116 } 117 } 118 119 static void clean(int z, int xn, int yn) throws Exception { 120 int scale = (int) Math.pow(2, z - 12); 121 int xdir = (scale * xtile) + xn; 122 int ynam = (scale * ytile) + yn; 123 String delnam = dstdir + z + "/" + xdir + "/" + ynam + ".png"; 124 File delfile = new File(delnam); 125 if (delfile.exists()) { 126 deletes.put(delnam, true); 127 delfile.delete(); 128 } 129 if ((z < 18)) { 130 for (int x = 0; x < 2; x++) { 131 for (int y = 0; y < 2; y++) { 132 clean((z + 1), (xn * 2 + x), (yn * 2 + y)); 133 } 134 } 135 } 136 } 137 138 public static void main(String[] args) throws Exception { 139 if (args.length < 5) { 140 System.err.println("Usage: java -jar jrender.jar <osm source directory> <tile directory> <zoom> <xtile> <ytile>"); 141 System.exit(-1); 142 } 143 srcdir = args[0]; 144 dstdir = args[1]; 145 zoom = Integer.parseInt(args[2]); 146 xtile = Integer.parseInt(args[3]); 147 ytile = Integer.parseInt(args[4]); 148 send = new ArrayList<>(); 149 deletes = new HashMap<>(); 150 BufferedReader in = new BufferedReader(new FileReader(srcdir + xtile + "-" + ytile + "-" + zoom + ".osm")); 151 map = new S57map(true); 152 S57osm.OSMmap(in, map, false); 153 in.close(); 154 context = new Context(); 155 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 156 ImageIO.write(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), "png", bos); 157 empty = bos.size(); 158 if (zoom == 12) { 159 clean(12, 0, 0); 160 } 161 tile(zoom, 1, 0, 0); 162 if (send.size() > 0) { 163 PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".send", "UTF-8"); 164 for (String str : send) { 165 writer.println(str); 166 } 167 writer.close(); 168 } 169 if (deletes.size() > 0) { 170 PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".delete", "UTF-8"); 171 for (String del : deletes.keySet()) { 172 writer.println("rm " + del); 173 } 174 writer.close(); 175 } 176 System.exit(0); 177 } 178 178 }
Note:
See TracChangeset
for help on using the changeset viewer.
