1 | /* Copyright 2013 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 seamap;
|
---|
11 |
|
---|
12 | import java.awt.Graphics2D;
|
---|
13 | import java.awt.RenderingHints;
|
---|
14 | import java.awt.geom.Point2D;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.HashMap;
|
---|
17 |
|
---|
18 | import s57.S57att.Att;
|
---|
19 | import s57.S57obj.Obj;
|
---|
20 | import s57.S57val.ColCOL;
|
---|
21 | import s57.S57val.*;
|
---|
22 | import s57.S57val;
|
---|
23 | import seamap.SeaMap.*;
|
---|
24 | import symbols.Symbols;
|
---|
25 | import symbols.Symbols.*;
|
---|
26 |
|
---|
27 | public class Renderer {
|
---|
28 |
|
---|
29 | static MapHelper helper;
|
---|
30 | static SeaMap map;
|
---|
31 | static double sScale;
|
---|
32 | static double tScale;
|
---|
33 | static Graphics2D g2;
|
---|
34 |
|
---|
35 | public static void reRender(Graphics2D g, int zoom, double factor, SeaMap m, MapHelper h) {
|
---|
36 | g2 = g;
|
---|
37 | helper = h;
|
---|
38 | map = m;
|
---|
39 | sScale = Symbols.symbolScale[zoom]*factor;
|
---|
40 | tScale = Symbols.textScale[zoom]*factor;
|
---|
41 | if (map != null) {
|
---|
42 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
43 | g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
|
---|
44 | Rules.rules(map, zoom);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static AttMap getAtts(Feature feature, Obj obj, int idx) {
|
---|
49 | HashMap<Integer, AttMap> objs = feature.objs.get(obj);
|
---|
50 | if (objs == null) return null;
|
---|
51 | else return objs.get(idx);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static Object getAttVal(Feature feature, Obj obj, int idx, Att att) {
|
---|
55 | AttMap atts = getAtts(feature, obj, idx);
|
---|
56 | if (atts == null) return S57val.nullVal(att);
|
---|
57 | else {
|
---|
58 | AttItem item = atts.get(att);
|
---|
59 | if (item == null) return S57val.nullVal(att);
|
---|
60 | return item.val;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static double calcArea(Feature feature) {
|
---|
65 | if (feature.flag == Fflag.AREA) {
|
---|
66 | ArrayList<Long> way = map.ways.get(feature.refs);
|
---|
67 | Coord coord = map.nodes.get(way.get(0));
|
---|
68 | double llat = coord.lat;
|
---|
69 | double llon = coord.lon;
|
---|
70 | double area = 0.0;
|
---|
71 | for (long node : way) {
|
---|
72 | coord = map.nodes.get(node);
|
---|
73 | area += ((llon * coord.lat) - (llat * coord.lon));
|
---|
74 | llat = coord.lat;
|
---|
75 | llon = coord.lon;
|
---|
76 | }
|
---|
77 | return Math.abs(area) / 2.0 * 60.0 * 60.0;
|
---|
78 | }
|
---|
79 | return 0.0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static Coord findCentroid(Feature feature) {
|
---|
83 | Coord coord;
|
---|
84 | ArrayList<Long> way = map.ways.get(feature.refs);
|
---|
85 | switch (feature.flag) {
|
---|
86 | case NODE:
|
---|
87 | return map.nodes.get(feature.refs);
|
---|
88 | case WAY:
|
---|
89 | coord = map.nodes.get(way.get(1));
|
---|
90 | break;
|
---|
91 | case AREA:
|
---|
92 | default:
|
---|
93 | coord = map.nodes.get(way.get(0));
|
---|
94 | }
|
---|
95 | double slat = 0.0;
|
---|
96 | double slon = 0.0;
|
---|
97 | double sarc = 0.0;
|
---|
98 | double llat = coord.lat;
|
---|
99 | double llon = coord.lon;
|
---|
100 | for (long node : way) {
|
---|
101 | coord = map.nodes.get(node);
|
---|
102 | double lon = coord.lon;
|
---|
103 | double lat = coord.lat;
|
---|
104 | double arc = Math.sqrt(Math.pow((lon-llon), 2) + Math.pow((lat-llat), 2));
|
---|
105 | slat += (lat * arc);
|
---|
106 | slon += (lon * arc);
|
---|
107 | sarc += arc;
|
---|
108 | llat = lat;
|
---|
109 | llon = lon;
|
---|
110 | }
|
---|
111 | return map.new Coord((sarc > 0.0 ? slat/sarc : 0.0), (sarc > 0.0 ? slon/sarc : 0.0));
|
---|
112 | }
|
---|
113 |
|
---|
114 | public static void symbol(Feature feature, ArrayList<Instr> symbol, Obj obj) {
|
---|
115 | Point2D point = helper.getPoint(findCentroid(feature));
|
---|
116 | ArrayList<ColCOL> colours = (ArrayList<ColCOL>) getAttVal(feature, obj, 0, Att.COLOUR);
|
---|
117 | ArrayList<ColPAT> pattern = (ArrayList<ColPAT>) getAttVal(feature, obj, 0, Att.COLPAT);
|
---|
118 | Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, new Scheme(pattern, colours));
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|