Ignore:
Timestamp:
2016-03-05T11:40:02+01:00 (9 years ago)
Author:
malcolmh
Message:

[SeaChart] improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/seachart/jrender/src/jrender/Jrender.java

    r32090 r32095  
    2727import javax.imageio.ImageIO;
    2828
    29 import org.apache.batik.dom.GenericDOMImplementation;
    30 import org.apache.batik.svggen.SVGGraphics2D;
    31 import org.w3c.dom.DOMImplementation;
    32 import org.w3c.dom.Document;
    33 
    3429import s57.S57map;
    3530import s57.S57osm;
     
    5651         
    5752          public Context () {
    58                         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;
    59                         mile = 768 / ((Math.toDegrees(map.bounds.maxlat) - Math.toDegrees(map.bounds.minlat)) * 60);
     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);
    6055          }
    6156         
    6257                public Point2D getPoint(Snode coord) {
    63                         double x = (Math.toDegrees(coord.lon) - Math.toDegrees(map.bounds.minlon)) * 256.0 * 2048.0 / 180.0;
    64                         double y = ((1.0 - Math.log(Math.tan(coord.lat) + 1.0 / Math.cos(coord.lat)) / Math.PI) / 2.0 * 256.0 * 4096.0) - top;
     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;
    6560                        return new Point2D.Double(x, y);
    6661                }
     
    8378        }
    8479       
    85         static final boolean test = true;
    86        
    87         public static void tileMap(String idir, int zoom) throws IOException {
    88                 BufferedImage img;
    89                 context = new Context();
    90 
    91                 int border = 256 / (int)Math.pow(2, (12 - zoom));
    92                 int size = 256;
    93                 for (int i = 0; i < (12 - zoom); i++) size *= 2;
    94                 Rectangle rect = new Rectangle((size + (2 * border)), (size + (2 * border)));
    95                 img = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB);
    96                 Renderer.reRender(img.createGraphics(), rect, zoom, 1.0, map, context);
    97                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
    98                 ImageIO.write(img, "png", bos);
    99                 empty = bos.size();
    100 
    101                 if (test) {
    102                         for (int z = 12; z <= 18; z++) {
    103                                 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    104                                 Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
    105                                 SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    106                                 svgGenerator.clearRect(rect.x, rect.y, rect.width, rect.height);
    107                                 svgGenerator.setSVGCanvasSize(rect.getSize());
    108                                 svgGenerator.setClip(rect.x, rect.y, rect.width, rect.height);
    109                                 svgGenerator.translate(-border, -border);
    110                                 Renderer.reRender(svgGenerator, rect, z, 1.0, map, context);
    111                                 svgGenerator.stream(dstdir + "tst_" + z + "-" + xtile + "-" + ytile + ".svg");
    112                         }
    113                 } else {
    114                         tile(zoom, 1, 0, 0);
    115                 }
    116         }
    117        
    118         static void tile(int zoom, int s, int xn, int yn) throws IOException {
    119                 int scale = (int) Math.pow(2, zoom - 12);
    120                 int xdir = (scale * xtile) + xn;
    121                 int ynam = (scale * ytile) + yn;
     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;
    12285                BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
    12386                Graphics2D g2 = img.createGraphics();
    12487                g2.scale(s, s);
    125                 g2.translate(-(256 + (xn * 256 / s)), -(256 + (yn * 256 / s)));
    126                 Renderer.reRender(g2, new Rectangle(256, 256), zoom, 1, map, context);
     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);
    12790                ByteArrayOutputStream bos = new ByteArrayOutputStream();
    12891                ImageIO.write(img, "png", bos);
    12992                if (bos.size() > empty) {
    130                         String dstnam = dstdir + zoom + "/" + xdir + "/" + ynam + ".png";
     93                        String dstnam = dstdir + z + "/" + xdir + "/" + ynam + ".png";
    13194                        deletes.remove(dstnam);
    132                         send.add("put " + dstnam + " tiles/" + zoom + "/" + xdir + "/" + ynam + ".png");
    133                         File ofile = new File(dstdir + "/" + zoom + "/" + xdir + "/");
     95                        send.add("put " + dstnam + " tiles/" + z + "/" + xdir + "/" + ynam + ".png");
     96                        File ofile = new File(dstdir + "/" + z + "/" + xdir + "/");
    13497                        ofile.mkdirs();
    135                         FileOutputStream fos = new FileOutputStream(dstdir + "/" + zoom + "/" + xdir + "/" + ynam + ".png");
     98                        FileOutputStream fos = new FileOutputStream(dstdir + "/" + z + "/" + xdir + "/" + ynam + ".png");
    13699                        bos.writeTo(fos);
    137100                        fos.close();
    138                         if (send.size() > 10) {
    139                                 PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xdir + "-" + ynam + ".send", "UTF-8");
     101                        if (send.size() > 20) {
     102                                PrintWriter writer = new PrintWriter(srcdir + z + "-" + xdir + "-" + ynam + ".send", "UTF-8");
    140103                                for (String str : send) {
    141104                                        writer.println(str);
     
    145108                        }
    146109                }
    147                 if ((zoom >= 12) && (zoom < 18) && ((zoom < 16) || (bos.size() > empty))) {
     110                if ((z >= 12) && (z < 18) && ((z < 16) || (bos.size() > empty))) {
    148111                        for (int x = 0; x < 2; x++) {
    149112                                for (int y = 0; y < 2; y++) {
    150                                         tile((zoom + 1), (s * 2), (xn * 2 + x), (yn * 2 + y));
     113                                        tile((z + 1), (s * 2), (xn * 2 + x), (yn * 2 + y));
    151114                                }
    152115                        }
     
    181144                send = new ArrayList<String>();
    182145                deletes = new HashMap<String, Boolean>();
    183                 BufferedReader in = new BufferedReader(new FileReader(srcdir + zoom + "-" + xtile + "-" + ytile + ".osm"));
     146                BufferedReader in = new BufferedReader(new FileReader(srcdir + xtile + "-" + ytile + "-" + zoom + ".osm"));
    184147                map = new S57map(true);
    185148                S57osm.OSMmap(in, map, false);
    186149                in.close();
     150                context = new Context();
     151                ByteArrayOutputStream bos = new ByteArrayOutputStream();
     152                ImageIO.write(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), "png", bos);
     153                empty = bos.size();
    187154                if (zoom == 12) {
    188155                        clean(12, 0, 0);
    189156                }
    190                 tileMap(dstdir, zoom);
    191 //              if ((send.size() + deletes.size()) > 0) {
    192 //                      PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".send", "UTF-8");
    193 //                      for (String str : send) {
    194 //                              writer.println(str);
    195 //                      }
    196 //                      for (String del : deletes.keySet()) {
    197 //                              writer.println("rm " + del);
    198 //                      }
    199 //                      writer.close();
    200 //              }
     157                tile(zoom, 1, 0, 0);
     158                if (send.size() > 0) {
     159                        PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".send", "UTF-8");
     160                        for (String str : send) {
     161                                writer.println(str);
     162                        }
     163                        writer.close();
     164                }
     165                if (deletes.size() > 0) {
     166                        PrintWriter writer = new PrintWriter(srcdir + zoom + "-" + xtile + "-" + ytile + ".delete", "UTF-8");
     167                        for (String del : deletes.keySet()) {
     168                                writer.println("rm " + del);
     169                        }
     170                        writer.close();
     171                }
    201172                System.exit(0);
    202173        }
Note: See TracChangeset for help on using the changeset viewer.