1 | /* Copyright 2014 Malcolm Herring
|
---|
2 | *
|
---|
3 | * This is free software: you can redistribute it and/or modify
|
---|
4 | * it under the terms of the GNU General Public License as published by
|
---|
5 | * the Free Software Foundation, version 3 of the License.
|
---|
6 | *
|
---|
7 | * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
|
---|
8 | */
|
---|
9 |
|
---|
10 | package jrender;
|
---|
11 |
|
---|
12 | import java.awt.Dimension;
|
---|
13 | import java.awt.Graphics2D;
|
---|
14 | import java.awt.geom.Point2D;
|
---|
15 | import java.awt.image.BufferedImage;
|
---|
16 | import java.io.ByteArrayOutputStream;
|
---|
17 | import java.io.FileOutputStream;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.util.ArrayList;
|
---|
20 |
|
---|
21 | import javax.imageio.ImageIO;
|
---|
22 |
|
---|
23 | import jrender.Jrender.MapBB;
|
---|
24 |
|
---|
25 | import org.apache.batik.dom.GenericDOMImplementation;
|
---|
26 | import org.apache.batik.svggen.SVGGraphics2D;
|
---|
27 | import org.w3c.dom.DOMImplementation;
|
---|
28 | import org.w3c.dom.Document;
|
---|
29 |
|
---|
30 | import render.MapContext;
|
---|
31 | import render.Renderer;
|
---|
32 | import s57.S57map;
|
---|
33 | import s57.S57map.Feature;
|
---|
34 | import s57.S57map.Snode;
|
---|
35 |
|
---|
36 | public class Tilegen {
|
---|
37 |
|
---|
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 | static class Context implements MapContext {
|
---|
197 |
|
---|
198 | static double minlat = 0;
|
---|
199 | static double minlon = 0;
|
---|
200 | static double maxlat = 0;
|
---|
201 | static double maxlon = 0;
|
---|
202 | static double top = 0;
|
---|
203 | static double mile = 0;
|
---|
204 |
|
---|
205 | public Context (double nt, double nn, double xt, double xn) {
|
---|
206 | minlat = nt;
|
---|
207 | minlon = nn;
|
---|
208 | maxlat = xt;
|
---|
209 | maxlon = xn;
|
---|
210 | top = (1.0 - Math.log(Math.tan(Math.toRadians(maxlat)) + 1.0 / Math.cos(Math.toRadians(maxlat))) / Math.PI) / 2.0 * 256.0 * 4096.0;
|
---|
211 | mile = 768 / ((maxlat - minlat) * 60);
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public Point2D getPoint(Snode coord) {
|
---|
216 | double x = (Math.toDegrees(coord.lon) - minlon) * 256.0 * 2048.0 / 180.0;
|
---|
217 | 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;
|
---|
218 | return new Point2D.Double(x, y);
|
---|
219 | }
|
---|
220 |
|
---|
221 | @Override
|
---|
222 | public double mile(Feature feature) {
|
---|
223 | return mile;
|
---|
224 | }
|
---|
225 |
|
---|
226 | }
|
---|
227 | }
|
---|