source: osm/applications/editors/josm/plugins/seachart/src/render/Renderer.java@ 35788

Last change on this file since 35788 was 35788, checked in by malcolmh, 4 years ago

jchart enhancements

File size: 43.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package render;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Font;
7import java.awt.Graphics2D;
8import java.awt.Rectangle;
9import java.awt.RenderingHints;
10import java.awt.TexturePaint;
11import java.awt.font.FontRenderContext;
12import java.awt.font.GlyphVector;
13import java.awt.geom.AffineTransform;
14import java.awt.geom.Arc2D;
15import java.awt.geom.Ellipse2D;
16import java.awt.geom.GeneralPath;
17import java.awt.geom.Line2D;
18import java.awt.geom.Path2D;
19import java.awt.geom.Point2D;
20import java.awt.geom.Rectangle2D;
21import java.awt.geom.RoundRectangle2D;
22import java.awt.image.AffineTransformOp;
23import java.awt.image.BufferedImage;
24import java.util.ArrayList;
25
26import s57.S57map;
27import s57.S57map.GeomIterator;
28import s57.S57map.Pflag;
29import s57.S57map.Snode;
30import s57.S57val.UniHLU;
31import symbols.Areas;
32import symbols.Symbols;
33import symbols.Symbols.Caption;
34import symbols.Symbols.Delta;
35import symbols.Symbols.Form;
36import symbols.Symbols.Handle;
37import symbols.Symbols.Instr;
38import symbols.Symbols.LineStyle;
39import symbols.Symbols.Scheme;
40import symbols.Symbols.SubSymbol;
41import symbols.Symbols.Symbol;
42
43/**
44 * @author Malcolm Herring
45 */
46public final class Renderer {
47 private Renderer() {
48 // Hide default constructor for utilities classes
49 }
50
51 public static final double[] symbolScale = {
52 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.61, 0.372, 0.227, 0.138, 0.0843, 0.0514, 0.0313, 0.0191, 0.0117, 0.007};
53
54 public enum LabelStyle { NONE, RRCT, RECT, ELPS, CIRC, VCLR, PCLR, HCLR }
55
56 static ChartContext context;
57 static S57map map;
58 static double sScale;
59 static Graphics2D g2;
60 static int zoom;
61
62 public static void reRender(Graphics2D g, Rectangle rect, int z, double factor, S57map m, ChartContext c) {
63 g2 = g;
64 zoom = z;
65 context = c;
66 map = m;
67 sScale = symbolScale[zoom] * factor;
68 if (map != null) {
69 if (context.clip()) {
70 Point2D tl = context.getPoint(new Snode(map.bounds.maxlat, map.bounds.minlon));
71 Point2D br = context.getPoint(new Snode(map.bounds.minlat, map.bounds.maxlon));
72 g2.clip(new Rectangle2D.Double(tl.getX(), tl.getY(), (br.getX() - tl.getX()), (br.getY() - tl.getY())));
73 }
74 g2.setBackground(context.background(map));
75 g2.clearRect(rect.x, rect.y, rect.width, rect.height);
76 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
77 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
78 g2.setStroke(new BasicStroke(0, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
79 do {} while (!Rules.rules());
80 }
81 if ((context.grid() > 0) && (map != null)) {
82 rose();
83 grid();
84 }
85 }
86
87 public static void symbol(Symbol symbol) {
88 Point2D point = context.getPoint(Rules.feature.geom.centre);
89 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, null);
90 }
91
92 public static void symbol(Symbol symbol, Scheme scheme) {
93 Point2D point = context.getPoint(Rules.feature.geom.centre);
94 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, null);
95 }
96
97 public static void symbol(Symbol symbol, Delta delta) {
98 Point2D point = context.getPoint(Rules.feature.geom.centre);
99 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, delta);
100 }
101
102 public static void symbol(Symbol symbol, double scale, Delta delta) {
103 Point2D point = context.getPoint(Rules.feature.geom.centre);
104 Symbols.drawSymbol(g2, symbol, (sScale * scale), point.getX(), point.getY(), null, delta);
105 }
106
107 public static void symbol(Symbol symbol, Scheme scheme, Delta delta) {
108 Point2D point = context.getPoint(Rules.feature.geom.centre);
109 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, delta);
110 }
111
112 public static void symbol(Symbol symbol, double scale, Scheme scheme) {
113 Point2D point = context.getPoint(Rules.feature.geom.centre);
114 Symbols.drawSymbol(g2, symbol, (sScale * scale), point.getX(), point.getY(), scheme, null);
115 }
116
117 public static void colLetters(ArrayList<?> cols) {
118 String str = "";
119 for (int i = 0; (i < cols.size()) && (i < 4); i++) {
120 str = str.concat(Rules.colourLetters.get(cols.get(i)));
121 }
122 labelText(str, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, 40)));
123 }
124
125 public static void cluster(ArrayList<Symbol> symbols) {
126 Rectangle2D.Double bbox = null;
127 if (symbols.size() > 4) {
128 for (Instr instr : symbols.get(0)) {
129 if (instr.type == Form.BBOX) {
130 bbox = (Rectangle2D.Double) instr.params;
131 break;
132 }
133 }
134 if (bbox == null) return;
135 }
136 switch (symbols.size()) {
137 case 1:
138 symbol(symbols.get(0), new Delta(Handle.CC, new AffineTransform()));
139 break;
140 case 2:
141 symbol(symbols.get(0), new Delta(Handle.RC, new AffineTransform()));
142 symbol(symbols.get(1), new Delta(Handle.LC, new AffineTransform()));
143 break;
144 case 3:
145 symbol(symbols.get(0), new Delta(Handle.BC, new AffineTransform()));
146 symbol(symbols.get(1), new Delta(Handle.TR, new AffineTransform()));
147 symbol(symbols.get(2), new Delta(Handle.TL, new AffineTransform()));
148 break;
149 case 4:
150 symbol(symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
151 symbol(symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
152 symbol(symbols.get(2), new Delta(Handle.TR, new AffineTransform()));
153 symbol(symbols.get(3), new Delta(Handle.TL, new AffineTransform()));
154 break;
155 case 5:
156 symbol(symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
157 symbol(symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
158 symbol(symbols.get(2), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
159 symbol(symbols.get(3), new Delta(Handle.TC, new AffineTransform()));
160 symbol(symbols.get(4), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
161 break;
162 case 6:
163 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
164 symbol(symbols.get(1), new Delta(Handle.BC, new AffineTransform()));
165 symbol(symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
166 symbol(symbols.get(3), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
167 symbol(symbols.get(4), new Delta(Handle.TC, new AffineTransform()));
168 symbol(symbols.get(5), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
169 break;
170 case 7:
171 symbol(symbols.get(0), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
172 symbol(symbols.get(1), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
173 symbol(symbols.get(2), new Delta(Handle.CC, new AffineTransform()));
174 symbol(symbols.get(3), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
175 symbol(symbols.get(4), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
176 symbol(symbols.get(5), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
177 symbol(symbols.get(6), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
178 break;
179 case 8:
180 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
181 symbol(symbols.get(1), new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
182 symbol(symbols.get(2), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
183 symbol(symbols.get(3), new Delta(Handle.CC, new AffineTransform()));
184 symbol(symbols.get(4), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
185 symbol(symbols.get(5), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
186 symbol(symbols.get(6), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
187 symbol(symbols.get(7), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
188 break;
189 case 9:
190 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, -bbox.height/2)));
191 symbol(symbols.get(1), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
192 symbol(symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, -bbox.height/2)));
193 symbol(symbols.get(3), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
194 symbol(symbols.get(4), new Delta(Handle.CC, new AffineTransform()));
195 symbol(symbols.get(5), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
196 symbol(symbols.get(6), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
197 symbol(symbols.get(7), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
198 symbol(symbols.get(8), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
199 break;
200 }
201 }
202
203 private static Rectangle2D.Double symbolSize(Symbol symbol) {
204 Symbol ssymb = symbol;
205 while (ssymb != null) {
206 for (Instr item : symbol) {
207 if (item.type == Form.BBOX) {
208 return (Rectangle2D.Double) item.params;
209 }
210 if (item.type == Form.SYMB) {
211 ssymb = ((SubSymbol) item.params).instr;
212 break;
213 }
214 }
215 if (ssymb == symbol)
216 break;
217 }
218 return null;
219 }
220
221 public static void lineSymbols(Symbol prisymb, double space, Symbol secsymb, Symbol tersymb, int ratio, Color col) {
222 if ((Rules.feature.geom.prim == Pflag.NOSP) || (Rules.feature.geom.prim == Pflag.POINT))
223 return;
224 Rectangle2D.Double prect = symbolSize(prisymb);
225 Rectangle2D.Double srect = symbolSize(secsymb);
226 Rectangle2D.Double trect = symbolSize(tersymb);
227 if (srect == null)
228 ratio = 0;
229 if (prect != null) {
230 double psize = Math.abs(prect.getY()) * sScale;
231 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
232 double tsize = (trect != null) ? Math.abs(srect.getY()) * sScale : 0;
233 Point2D prev = new Point2D.Double();
234 Point2D next = new Point2D.Double();
235 Point2D curr = new Point2D.Double();
236 Point2D succ = new Point2D.Double();
237 boolean gap = true;
238 boolean piv = false;
239 double len = 0;
240 double angle = 0;
241 int stcount = ratio;
242 boolean stflag = false;
243 Symbol symbol = prisymb;
244 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
245 while (git.hasComp()) {
246 git.nextComp();
247 boolean first = true;
248 while (git.hasEdge()) {
249 git.nextEdge();
250 while (git.hasNode()) {
251 Snode node = git.next();
252 if (node == null) continue;
253 prev = next;
254 next = context.getPoint(node);
255 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
256 piv = true;
257 if (first) {
258 curr = succ = next;
259 gap = (space > 0);
260 stcount = ratio - 1;
261 symbol = prisymb;
262 len = gap ? psize * space * 0.5 : psize;
263 first = false;
264 } else {
265 while (curr.distance(next) >= len) {
266 if (piv) {
267 double rem = len;
268 double s = prev.distance(next);
269 double p = curr.distance(prev);
270 if ((s > 0) && (p > 0)) {
271 double n = curr.distance(next);
272 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
273 double phi = Math.asin(p / len * Math.sin(theta));
274 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
275 }
276 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
277 piv = false;
278 } else {
279 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
280 }
281 if (!gap) {
282 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Scheme(col),
283 new Delta(Handle.BC, AffineTransform.getRotateInstance(
284 Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))));
285 }
286 if (space > 0)
287 gap = !gap;
288 curr = succ;
289 len = gap ? (psize * space) : (--stcount == 0) ? (stflag ? tsize : ssize) : psize;
290 if (stcount == 0) {
291 symbol = stflag ? tersymb : secsymb;
292 if (trect != null)
293 stflag = !stflag;
294 stcount = ratio;
295 } else {
296 symbol = prisymb;
297 }
298 }
299 }
300 }
301 }
302 }
303 }
304 }
305
306 public static void lineVector(LineStyle style) {
307 Path2D.Double p = new Path2D.Double();
308 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
309 Point2D point;
310 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
311 while (git.hasComp()) {
312 git.nextComp();
313 boolean first = true;
314 while (git.hasEdge()) {
315 git.nextEdge();
316 point = context.getPoint(git.next());
317 if (first) {
318 p.moveTo(point.getX(), point.getY());
319 first = false;
320 } else {
321 p.lineTo(point.getX(), point.getY());
322 }
323 while (git.hasNode()) {
324 Snode node = git.next();
325 if (node == null) continue;
326 point = context.getPoint(node);
327 p.lineTo(point.getX(), point.getY());
328 }
329 }
330 }
331 if ((style.fill != null) && (Rules.feature.geom.prim == Pflag.AREA)) {
332 g2.setPaint(style.fill);
333 g2.fill(p);
334 }
335 if (style.line != null) {
336 if (style.dash != null) {
337 float[] dash = new float[style.dash.length];
338 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
339 for (int i = 0; i < style.dash.length; i++) {
340 dash[i] *= (float) sScale;
341 }
342 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
343 } else {
344 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
345 }
346 g2.setPaint(style.line);
347 g2.draw(p);
348 }
349 }
350
351 public static void grid() {
352 if ((context.grid() > 0) && (map != null)) {
353 LineStyle style = new LineStyle(Color.black, (float)2.0);
354 Point2D point = context.getPoint(new Snode(map.bounds.minlat, map.bounds.maxlon));
355 double ratio = point.getX() / point.getY();
356 double nspan = 60 * Math.toDegrees(map.bounds.maxlon - map.bounds.minlon) / (context.grid() * (ratio > 1.0 ? ratio : 1.0));
357 double mult = 1.0;
358 boolean ndiv = false;
359 if (nspan < 1.0) {
360 do {
361 nspan *= 10.0;
362 mult *= 10.0;
363 } while (nspan < 1.0);
364 } else if (nspan > 10.0){
365 do {
366 nspan /= 10.0;
367 mult /= 10.0;
368 } while (nspan > 10.0);
369 }
370 if (nspan < 2.0) nspan = 1.0;
371 else if (nspan < 5.0) nspan = 2.0;
372 else {
373 nspan = 5.0;
374 ndiv = true;
375 }
376 nspan = nspan / mult / 60.0;
377 double left = Math.toDegrees(map.bounds.minlon) + 180.0;
378 left = Math.ceil(left / nspan);
379 left = Math.toRadians((left * nspan) - 180.0);
380 double tspan = 60 * Math.toDegrees(map.bounds.maxlat - map.bounds.minlat) / (context.grid() / (ratio < 1.0 ? ratio : 1.0));
381 mult = 1.0;
382 boolean tdiv = false;
383 if (tspan < 1.0) {
384 do {
385 tspan *= 10.0;
386 mult *= 10.0;
387 } while (tspan < 1.0);
388 } else if (tspan > 10.0){
389 do {
390 tspan /= 10.0;
391 mult /= 10.0;
392 } while (tspan > 10.0);
393 }
394 if (tspan < 2.0) tspan = 1.0;
395 else if (tspan < 5.0) tspan = 2.0;
396 else {
397 tspan = 5.0;
398 tdiv = true;
399 }
400 tspan = tspan / mult / 60.0;
401 double bottom = Math.toDegrees(map.bounds.minlat) + 90.0;
402 bottom = Math.ceil(bottom / tspan);
403 bottom = Math.toRadians((bottom * tspan) - 90.0);
404 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
405 Path2D.Double p = new Path2D.Double();
406 for (double lon = left; lon < map.bounds.maxlon; lon += Math.toRadians(nspan)) {
407 point = context.getPoint(new Snode(map.bounds.maxlat, lon));
408 p.moveTo(point.getX(), point.getY());
409 point = context.getPoint(new Snode(map.bounds.minlat, lon));
410 p.lineTo(point.getX(), point.getY());
411 for (int i = 1; i < 10; i++) {
412 int grad = (i % (ndiv ? 2 : 5)) == 0 ? 2 : 1;
413 point = context.getPoint(new Snode(map.bounds.maxlat, lon + (i * (nspan / 10))));
414 p.moveTo(point.getX(), point.getY());
415 point = context.getPoint(new Snode(map.bounds.maxlat - (grad * (tspan / 10)), lon + (i * (nspan / 10))));
416 p.lineTo(point.getX(), point.getY());
417 point = context.getPoint(new Snode(map.bounds.minlat, lon + (i * (nspan / 10))));
418 p.moveTo(point.getX(), point.getY());
419 point = context.getPoint(new Snode(map.bounds.minlat + (grad * (tspan / 10)), lon + (i * (nspan / 10))));
420 p.lineTo(point.getX(), point.getY());
421 }
422 double deg = Math.toDegrees(lon);
423 String ew = (deg < -0.001) ? "W" : (deg > 0.001) ? "E" : "";
424 deg = Math.abs(deg);
425 String dstr = String.format("%03d°", (int)Math.floor(deg));
426 double min = (deg - Math.floor(deg)) * 60.0;
427 String mstr = String.format("%05.2f'%s", min, ew);
428 Symbol label = new Symbol();
429 if (point.getX() > 600.0) {
430 label.add(new Instr(Form.TEXT, new Caption(dstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BR, AffineTransform.getTranslateInstance(-10, -20)))));
431 label.add(new Instr(Form.TEXT, new Caption(mstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BL, AffineTransform.getTranslateInstance(20, 0)))));
432 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, null);
433 }
434 }
435 g2.setPaint(style.line);
436 g2.draw(p);
437 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
438 p = new Path2D.Double();
439 for (double lat = bottom; lat < map.bounds.maxlat; lat += Math.toRadians(tspan)) {
440 point = context.getPoint(new Snode(lat, map.bounds.maxlon));
441 p.moveTo(point.getX(), point.getY());
442 point = context.getPoint(new Snode(lat, map.bounds.minlon));
443 p.lineTo(point.getX(), point.getY());
444 double deg = Math.toDegrees(lat);
445 String ns = (deg < -0.001) ? "S" : (deg > 0.001) ? "N" : "";
446 deg = Math.abs(deg);
447 String dstr = String.format("%02d°%s", (int)Math.floor(deg), ns);
448 double min = (deg - Math.floor(deg)) * 60.0;
449 String mstr = String.format("%05.2f'", min);
450 Symbol label = new Symbol();
451 if (point.getY() < (context.getPoint(new Snode(map.bounds.minlat, map.bounds.minlon)).getY() - 200.0)) {
452 label.add(new Instr(Form.TEXT, new Caption(dstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BL, AffineTransform.getTranslateInstance(10, -10)))));
453 label.add(new Instr(Form.TEXT, new Caption(mstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, 50)))));
454 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, null);
455 }
456 }
457 g2.setPaint(style.line);
458 g2.draw(p);
459 Symbol legend = new Symbol();
460 legend.add(new Instr(Form.BBOX, new Rectangle2D.Double(0, 0, 600, 250)));
461 Path2D.Double path = new Path2D.Double(); path.moveTo(0, 0); path.lineTo(600, 0); path.lineTo(600, 250); path.lineTo(0, 250); path.closePath();
462 legend.add(new Instr(Form.FILL, Color.white));
463 legend.add(new Instr(Form.PGON, path));
464 legend.add(new Instr(Form.TEXT, new Caption("Mercator Projection", new Font("Arial", Font.PLAIN, 50), Color.black, new Delta(Handle.BC, AffineTransform.getTranslateInstance(300, 60)))));
465 legend.add(new Instr(Form.TEXT, new Caption("© OpenStreetMap contributors", new Font("Arial", Font.PLAIN, 30), Color.black, new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, 40)))));
466 legend.add(new Instr(Form.TEXT, new Caption("EMODnet Bathymetry Consortium (2018)", new Font("Arial", Font.PLAIN, 30), Color.black, new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, 40)))));
467 legend.add(new Instr(Form.TEXT, new Caption("EMODnet Digital Bathymetry (DTM)", new Font("Arial", Font.PLAIN, 30), Color.black, new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, 40)))));
468 legend.add(new Instr(Form.TEXT, new Caption("http://doi.org/10.12770/18ff0d48-b203-4a65-94a9-5fd8b0ec35f6", new Font("Arial", Font.PLAIN, 20), Color.black, new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, 35)))));
469 point = context.getPoint(new Snode(map.bounds.minlat, map.bounds.minlon));
470 Symbols.drawSymbol(g2, legend, sScale, point.getX(), point.getY(), null, new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, 0)));
471 }
472 }
473
474 public static void rose() {
475 LineStyle style = new LineStyle(Color.black, (float)2.0);
476 Point2D point = context.getPoint(new Snode(Math.toRadians(53.91649), Math.toRadians(-0.16141)));
477 g2.setPaint(Color.white);
478 g2.fill(new Ellipse2D.Double(point.getX() - 30, point.getY() - 30, 60, 60));
479 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
480 Path2D.Double p = new Path2D.Double();
481 p.moveTo(point.getX() - 30, point.getY());p.lineTo(point.getX() + 30, point.getY());
482 p.moveTo(point.getX(), point.getY() - 30);p.lineTo(point.getX(), point.getY() + 30);
483 for (int i = 0; i < 360; i++) {
484 double inner = ((i % 10) == 0) ? 0.92 : ((i % 5) == 0) ? 0.96 : 0.98;
485 double xouter = 1750 * Math.sin(Math.toRadians(i));
486 double youter = -1750 * Math.cos(Math.toRadians(i));
487 p.moveTo(point.getX() + xouter, point.getY() + youter);
488 p.lineTo(point.getX() + (inner * xouter), point.getY() + (inner * youter));
489 if ((i % 10) == 0) {
490 Handle h = Handle.BC;
491 if ((i > 0) && (i <= 90)) h = Handle.BL;
492 else if (i == 90) h = Handle.LC;
493 else if ((i > 90) && (i < 180)) h = Handle.LC;
494 else if (i == 180) h = Handle.CC;
495 else if ((i > 180) && (i <= 270)) h = Handle.RC;
496 else if (i > 270) h = Handle.BR;
497 Symbol value = new Symbol();
498 value.add(new Instr(Form.BBOX, new Rectangle2D.Double(0, 0, 60, 30)));
499 value.add(new Instr(Form.TEXT, new Caption(String.format("%03d", i), new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.CC, AffineTransform.getTranslateInstance(30, 25)))));
500 Symbols.drawSymbol(g2, value, sScale, point.getX(), point.getY(), null, new Delta(h, AffineTransform.getTranslateInstance(1.02*xouter/sScale, 1.02*youter/sScale)));
501 }
502 }
503 g2.setPaint(style.line);
504 g2.draw(p);
505 }
506
507 public static void lineCircle(LineStyle style, double radius, UniHLU units) {
508 switch (units) {
509 case HLU_FEET:
510 radius /= 6076;
511 break;
512 case HLU_KMTR:
513 radius /= 1.852;
514 break;
515 case HLU_HMTR:
516 radius /= 18.52;
517 break;
518 case HLU_SMIL:
519 radius /= 1.15078;
520 break;
521 case HLU_NMIL:
522 break;
523 default:
524 radius /= 1852;
525 break;
526 }
527 radius *= context.mile(Rules.feature);
528 Symbol circle = new Symbol();
529 if (style.fill != null) {
530 circle.add(new Instr(Form.FILL, style.fill));
531 circle.add(new Instr(Form.RSHP, new Ellipse2D.Double(-radius, -radius, radius*2, radius*2)));
532 }
533 circle.add(new Instr(Form.FILL, style.line));
534 circle.add(new Instr(Form.STRK, new BasicStroke(style.width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, style.dash, 0)));
535 circle.add(new Instr(Form.ELPS, new Ellipse2D.Double(-radius, -radius, radius*2, radius*2)));
536 Point2D point = context.getPoint(Rules.feature.geom.centre);
537 Symbols.drawSymbol(g2, circle, 1, point.getX(), point.getY(), null, null);
538 }
539
540 public static void fillPattern(BufferedImage image) {
541 Path2D.Double p = new Path2D.Double();
542 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
543 Point2D point;
544 switch (Rules.feature.geom.prim) {
545 case POINT:
546 point = context.getPoint(Rules.feature.geom.centre);
547 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
548 (int) (point.getX() - (50 * sScale)), (int) (point.getY() - (50 * sScale)));
549 break;
550 case AREA:
551 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
552 while (git.hasComp()) {
553 git.nextComp();
554 boolean newComp = true;
555 while (git.hasEdge()) {
556 git.nextEdge();
557 point = context.getPoint(git.next());
558 if (newComp) {
559 p.moveTo(point.getX(), point.getY());
560 newComp = false;
561 } else {
562 p.lineTo(point.getX(), point.getY());
563 }
564 while (git.hasNode()) {
565 Snode node = git.next();
566 if (node == null) continue;
567 point = context.getPoint(node);
568 p.lineTo(point.getX(), point.getY());
569 }
570 }
571 }
572 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int) (300 * sScale), 1 + (int) (300 * sScale))));
573 g2.fill(p);
574 break;
575 default:
576 break;
577 }
578 }
579
580 public static void labelText(String str, Font font, Color tc) {
581 labelText(str, font, tc, LabelStyle.NONE, null, null, null);
582 }
583
584 public static void labelText(String str, Font font, Color tc, Delta delta) {
585 labelText(str, font, tc, LabelStyle.NONE, null, null, delta);
586 }
587
588 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg) {
589 labelText(str, font, tc, style, fg, null, null);
590 }
591
592 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Color bg) {
593 labelText(str, font, tc, style, fg, bg, null);
594 }
595
596 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Delta delta) {
597 labelText(str, font, tc, style, fg, null, delta);
598 }
599
600 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Color bg, Delta delta) {
601 if (delta == null) delta = new Delta(Handle.CC);
602 if (bg == null) bg = new Color(0x00000000, true);
603 if (str == null || str.isEmpty()) str = " ";
604 FontRenderContext frc = g2.getFontRenderContext();
605 GlyphVector gv = font.deriveFont((float) font.getSize()).createGlyphVector(frc, str.equals(" ") ? "M" : str);
606 Rectangle2D bounds = gv.getVisualBounds();
607 double width = bounds.getWidth();
608 double height = bounds.getHeight();
609 Symbol label = new Symbol();
610 double lx, ly, tx, ty;
611 switch (style) {
612 case RRCT:
613 width += height * 1.0;
614 height *= 1.5;
615 if (width < height) width = height;
616 lx = -width / 2;
617 ly = -height / 2;
618 tx = lx + (height * 0.34);
619 ty = ly + (height * 0.17);
620 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
621 label.add(new Instr(Form.FILL, bg));
622 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
623 label.add(new Instr(Form.FILL, fg));
624 label.add(new Instr(Form.STRK, new BasicStroke(1 + (int) (height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
625 label.add(new Instr(Form.RRCT, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
626 break;
627 case VCLR:
628 width += height * 1.0;
629 height *= 2.0;
630 if (width < height) width = height;
631 lx = -width / 2;
632 ly = -height / 2;
633 tx = lx + (height * 0.27);
634 ty = ly + (height * 0.25);
635 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
636 label.add(new Instr(Form.FILL, bg));
637 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
638 label.add(new Instr(Form.FILL, fg));
639 int sw = 1 + (int) (height/10);
640 double po = sw / 2;
641 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
642 Path2D.Double p = new Path2D.Double(); p.moveTo(-height*0.2, -ly-po);
643 p.lineTo(height*0.2, -ly-po); p.moveTo(0, -ly-po); p.lineTo(0, -ly-po-(height*0.15));
644 p.moveTo(-height*0.2, ly+po); p.lineTo((height*0.2), ly+po); p.moveTo(0, ly+po); p.lineTo(0, ly+po+(height*0.15));
645 label.add(new Instr(Form.PLIN, p));
646 break;
647 case PCLR:
648 width += height * 1.0;
649 height *= 2.0;
650 if (width < height) width = height;
651 lx = -width / 2;
652 ly = -height / 2;
653 tx = lx + (height * 0.27);
654 ty = ly + (height * 0.25);
655 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
656 label.add(new Instr(Form.FILL, bg));
657 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
658 label.add(new Instr(Form.FILL, fg));
659 sw = 1 + (int) (height/10);
660 po = sw / 2;
661 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
662 p = new Path2D.Double();
663 p.moveTo(-height*0.2, -ly-po);
664 p.lineTo(height*0.2, -ly-po);
665 p.moveTo(0, -ly-po);
666 p.lineTo(0, -ly-po-(height*0.15));
667 p.moveTo(-height*0.2, ly+po);
668 p.lineTo((height*0.2), ly+po);
669 p.moveTo(0, ly+po);
670 p.lineTo(0, ly+po+(height*0.15));
671 label.add(new Instr(Form.PLIN, p));
672 label.add(new Instr(Form.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null,
673 new Delta(Handle.CC, new AffineTransform(0, -1, 1, 0, -width/2, 0)))));
674 label.add(new Instr(Form.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null,
675 new Delta(Handle.CC, new AffineTransform(0, -1, 1, 0, width/2, 0)))));
676 break;
677 case HCLR:
678 width += height * 1.5;
679 height *= 1.5;
680 if (width < height) width = height;
681 lx = -width / 2;
682 ly = -height / 2;
683 tx = lx + (height * 0.5);
684 ty = ly + (height * 0.17);
685 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
686 label.add(new Instr(Form.FILL, bg));
687 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
688 label.add(new Instr(Form.FILL, fg));
689 sw = 1 + (int) (height/10);
690 double vo = height / 4;
691 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
692 p = new Path2D.Double();
693 p.moveTo(-width*0.4-sw, -ly-vo);
694 p.lineTo(-width*0.4-sw, ly+vo);
695 p.moveTo(-width*0.4-sw, 0);
696 p.lineTo(-width*0.4+sw, 0);
697 p.moveTo(width*0.4+sw, -ly-vo);
698 p.lineTo(width*0.4+sw, ly+vo);
699 p.moveTo(width*0.4-sw, 0);
700 p.lineTo(width*0.4+sw, 0);
701 label.add(new Instr(Form.PLIN, p));
702 break;
703 default:
704 lx = -width / 2;
705 ly = -height / 2;
706 tx = lx;
707 ty = ly;
708 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
709 break;
710 }
711 label.add(new Instr(Form.TEXT, new Caption(str, font, tc, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
712 Point2D point = context.getPoint(Rules.feature.geom.centre);
713 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
714 }
715
716 public static void lineText(String str, Font font, Color colour, double dy) {
717 if (!str.isEmpty()) {
718 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
719 g2.setPaint(colour);
720 FontRenderContext frc = g2.getFontRenderContext();
721 GlyphVector gv = font.deriveFont(font.getSize2D() * (float) sScale).createGlyphVector(frc, str);
722 double width = gv.getVisualBounds().getWidth();
723 double height = gv.getVisualBounds().getHeight();
724 double offset = (Rules.feature.geom.length * context.mile(Rules.feature) - width) / 2;
725 if (offset > 0) {
726 Point2D before = null;
727 Point2D after = null;
728 ArrayList<Point2D> between = new ArrayList<>();
729 Point2D prev = null;
730 Point2D next = null;
731 double length = 0;
732 double lb = 0;
733 double la = 0;
734 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
735 if (git.hasComp()) {
736 git.nextComp();
737 while (git.hasEdge()) {
738 git.nextEdge();
739 while (git.hasNode()) {
740 Snode node = git.next();
741 if (node == null)
742 continue;
743 prev = next;
744 next = context.getPoint(node);
745 if (prev != null)
746 length += Math.sqrt(Math.pow((next.getX() - prev.getX()), 2) + Math.pow((next.getY() - prev.getY()), 2));
747 if (length < offset) {
748 before = next;
749 lb = la = length;
750 } else if (after == null) {
751 if (length > (offset + width)) {
752 after = next;
753 la = length;
754 break;
755 } else {
756 between.add(next);
757 }
758 }
759 }
760 if (after != null)
761 break;
762 }
763 }
764 if (after != null) {
765 double angle = Math.atan2((after.getY() - before.getY()), (after.getX() - before.getX()));
766 double rotate = Math.abs(angle) < (Math.PI / 2) ? angle : angle + Math.PI;
767 Point2D mid = new Point2D.Double((before.getX() + after.getX()) / 2, (before.getY() + after.getY()) / 2);
768 Point2D centre = context.getPoint(Rules.feature.geom.centre);
769 AffineTransform pos = AffineTransform.getTranslateInstance(-dy * Math.sin(rotate), dy * Math.cos(rotate));
770 pos.rotate(rotate);
771 pos.translate((mid.getX() - centre.getX()), (mid.getY() - centre.getY()));
772 Symbol label = new Symbol();
773 label.add(new Instr(Form.BBOX, new Rectangle2D.Double((-width / 2), -height, width, height)));
774 label.add(new Instr(Form.TEXT, new Caption(str, font, colour, new Delta(Handle.BC))));
775 Symbols.drawSymbol(g2, label, sScale, centre.getX(), centre.getY(), null, new Delta(Handle.BC, pos));
776 }
777 }
778 }
779 }
780
781 public static void lightSector(Color col1, Color col2, double radius, double s1, double s2, Double dir, String str) {
782 double mid = (((s1 + s2) / 2) + (s1 > s2 ? 180 : 0)) % 360;
783 g2.setStroke(new BasicStroke((float) (3.0 * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1,
784 new float[] {20 * (float) sScale, 20 * (float) sScale}, 0));
785 g2.setPaint(Color.black);
786 Point2D.Double centre = (Point2D.Double) context.getPoint(Rules.feature.geom.centre);
787 double radial = radius * context.mile(Rules.feature);
788 if (dir != null) {
789 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(dir)),
790 centre.y + radial * Math.cos(Math.toRadians(dir))));
791 } else {
792 if ((s1 != 0.0) || (s2 != 360.0)) {
793 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(s1)),
794 centre.y + radial * Math.cos(Math.toRadians(s1))));
795 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(s2)),
796 centre.y + radial * Math.cos(Math.toRadians(s2))));
797 }
798 }
799 double arcWidth = 10.0 * sScale;
800 g2.setStroke(new BasicStroke((float) arcWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1));
801 g2.setPaint(col1);
802 g2.draw(new Arc2D.Double(centre.x - radial, centre.y - radial, 2 * radial, 2 * radial, -(s1 + 90),
803 ((s1 < s2) ? (s1 - s2) : (s1 - s2 - 360)), Arc2D.OPEN));
804 if (col2 != null) {
805 g2.setPaint(col2);
806 g2.draw(new Arc2D.Double(centre.x - radial + arcWidth, centre.y - radial + arcWidth, 2 * (radial - arcWidth),
807 2 * (radial - arcWidth), -(s1 + 90), ((s1 < s2) ? (s1 - s2) : (s1 - s2 - 360)), Arc2D.OPEN));
808 }
809 if (str != null && !str.isEmpty()) {
810 Font font = new Font("Arial", Font.PLAIN, 40);
811 double arc = (s2 > s1) ? (s2 - s1) : (s2 - s1 + 360);
812 double awidth = (Math.toRadians(arc) * radial);
813 boolean hand = ((mid > 270) || (mid < 90));
814 double phi = Math.toRadians(mid);
815 radial += 30 * sScale;
816 AffineTransform at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale);
817 if ((font.getSize() * sScale * str.length()) < awidth) {
818 at.rotate(Math.toRadians(mid + (hand ? 0 : 180)));
819 labelText(str, font, Color.black, new Delta(Handle.CC, at));
820 } else if ((font.getSize() * sScale) < awidth) {
821 hand = (mid < 180);
822 at.rotate(Math.toRadians(mid + (hand ? -90 : 90)));
823 labelText(str, font, Color.black, hand ? new Delta(Handle.RC, at) : new Delta(Handle.LC, at));
824 }
825 if (dir != null) {
826 font = new Font("Arial", Font.PLAIN, 30);
827 str = dir + "°";
828 hand = (dir > 180);
829 phi = Math.toRadians(dir + (hand ? -0.5 : 0.5));
830 radial -= 70 * sScale;
831 at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale);
832 at.rotate(Math.toRadians(dir + (hand ? 90 : -90)));
833 labelText(str, font, Color.black, hand ? new Delta(Handle.BR, at) : new Delta(Handle.BL, at));
834 }
835 }
836 }
837
838 public static void rasterPixel(double size, Color col) {
839 double s = Rules.feature.geom.centre.lat - (size / 2.0);
840 double w = Rules.feature.geom.centre.lon - (size / 2.0);
841 double n = Rules.feature.geom.centre.lat + (size / 2.0);
842 double e = Rules.feature.geom.centre.lon + (size / 2.0);
843 Point2D sw = context.getPoint(new Snode(s, w));
844 Point2D nw = context.getPoint(new Snode(n, w));
845 Point2D ne = context.getPoint(new Snode(n, e));
846 Point2D se = context.getPoint(new Snode(s, e));
847 Symbol pixel = new Symbol();
848 Path2D.Double path = new Path2D.Double(); path.moveTo(sw.getX(), sw.getY()); path.lineTo(nw.getX(), nw.getY());
849 path.lineTo(ne.getX(), ne.getY()); path.lineTo(se.getX(), se.getY()); path.closePath();
850 pixel.add(new Instr(Form.FILL, col));
851 pixel.add(new Instr(Form.PGON, path));
852 Symbols.drawSymbol(g2, pixel, 1.0, 0, 0, null, null);
853 }
854}
Note: See TracBrowser for help on using the repository browser.