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.BasicStroke;
|
---|
13 | import java.awt.Graphics2D;
|
---|
14 | import java.awt.RenderingHints;
|
---|
15 | import java.awt.geom.GeneralPath;
|
---|
16 | import java.awt.geom.Path2D;
|
---|
17 | import java.awt.geom.Point2D;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.HashMap;
|
---|
20 |
|
---|
21 | import s57.S57att.Att;
|
---|
22 | import s57.S57obj.Obj;
|
---|
23 | import s57.S57val.*;
|
---|
24 | import s57.S57val;
|
---|
25 | import seamap.SeaMap;
|
---|
26 | import seamap.SeaMap.*;
|
---|
27 | import symbols.Symbols;
|
---|
28 | import symbols.Symbols.*;
|
---|
29 |
|
---|
30 | public class Renderer {
|
---|
31 |
|
---|
32 | static MapHelper helper;
|
---|
33 | static SeaMap map;
|
---|
34 | static double sScale;
|
---|
35 | static double tScale;
|
---|
36 | static Graphics2D g2;
|
---|
37 | static int zoom;
|
---|
38 |
|
---|
39 | public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapHelper h) {
|
---|
40 | g2 = g;
|
---|
41 | zoom = z;
|
---|
42 | helper = h;
|
---|
43 | map = m;
|
---|
44 | sScale = Symbols.symbolScale[zoom]*factor;
|
---|
45 | tScale = Symbols.textScale[zoom]*factor;
|
---|
46 | if (map != null) {
|
---|
47 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
48 | g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
|
---|
49 | Rules.rules(map, zoom);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static AttMap getAtts(Feature feature, Obj obj, int idx) {
|
---|
54 | HashMap<Integer, AttMap> objs = feature.objs.get(obj);
|
---|
55 | if (objs == null) return null;
|
---|
56 | else return objs.get(idx);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static Object getAttVal(Feature feature, Obj obj, int idx, Att att) {
|
---|
60 | AttMap atts = getAtts(feature, obj, idx);
|
---|
61 | if (atts == null) return S57val.nullVal(att);
|
---|
62 | else {
|
---|
63 | AttItem item = atts.get(att);
|
---|
64 | if (item == null) return S57val.nullVal(att);
|
---|
65 | return item.val;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static double calcArea(Feature feature) {
|
---|
70 | if (feature.flag == Fflag.AREA) {
|
---|
71 | ArrayList<Long> way = map.ways.get(feature.refs);
|
---|
72 | Coord coord = map.nodes.get(way.get(0));
|
---|
73 | double llon = coord.lon;
|
---|
74 | double llat = coord.lat;
|
---|
75 | double sigma = 0.0;
|
---|
76 | for (long node : way) {
|
---|
77 | coord = map.nodes.get(node);
|
---|
78 | double lat = coord.lat;
|
---|
79 | double lon = coord.lon;
|
---|
80 | sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
|
---|
81 | llon = lon;
|
---|
82 | llat = lat;
|
---|
83 | }
|
---|
84 | return Math.abs(sigma) * 3444 * 3444 / 2.0;
|
---|
85 | }
|
---|
86 | return 0.0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static Coord findCentroid(Feature feature) {
|
---|
90 | Coord coord;
|
---|
91 | ArrayList<Long> way = map.ways.get(feature.refs);
|
---|
92 | switch (feature.flag) {
|
---|
93 | case NODE:
|
---|
94 | return map.nodes.get(feature.refs);
|
---|
95 | case WAY:
|
---|
96 | coord = map.nodes.get(way.get(1));
|
---|
97 | break;
|
---|
98 | case AREA:
|
---|
99 | default:
|
---|
100 | coord = map.nodes.get(way.get(0));
|
---|
101 | }
|
---|
102 | double slat = 0.0;
|
---|
103 | double slon = 0.0;
|
---|
104 | double sarc = 0.0;
|
---|
105 | double llat = coord.lat;
|
---|
106 | double llon = coord.lon;
|
---|
107 | for (long node : way) {
|
---|
108 | coord = map.nodes.get(node);
|
---|
109 | double lon = coord.lon;
|
---|
110 | double lat = coord.lat;
|
---|
111 | double arc = (Math.acos(Math.cos(lon-llon) * Math.cos(lat-llat)));
|
---|
112 | slat += (lat * arc);
|
---|
113 | slon += (lon * arc);
|
---|
114 | sarc += arc;
|
---|
115 | llat = lat;
|
---|
116 | llon = lon;
|
---|
117 | }
|
---|
118 | return map.new Coord((sarc > 0.0 ? slat/sarc : 0.0), (sarc > 0.0 ? slon/sarc : 0.0));
|
---|
119 | }
|
---|
120 |
|
---|
121 | public static void symbol(Feature feature, Symbol symbol, Obj obj, Delta delta) {
|
---|
122 | Point2D point = helper.getPoint(findCentroid(feature));
|
---|
123 | if (obj == null) {
|
---|
124 | Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, null);
|
---|
125 | } else {
|
---|
126 | ArrayList<ColCOL> colours = (ArrayList<ColCOL>) getAttVal(feature, obj, 0, Att.COLOUR);
|
---|
127 | ArrayList<ColPAT> pattern = (ArrayList<ColPAT>) getAttVal(feature, obj, 0, Att.COLPAT);
|
---|
128 | Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, new Scheme(pattern, colours));
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static void lineSymbols(Feature feature, Symbol prisymb, double space, Symbol secsymb, int ratio) {
|
---|
133 | if (feature.flag != Fflag.NODE) {
|
---|
134 | ArrayList<Long> way = map.ways.get(feature.refs);
|
---|
135 | for (long node : way) {
|
---|
136 | Point2D point = helper.getPoint(map.nodes.get(node));
|
---|
137 |
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static void lineVector (Feature feature, LineStyle style) {
|
---|
143 | if (feature.flag != Fflag.NODE) {
|
---|
144 | ArrayList<Long> nodes = map.ways.get(feature.refs);
|
---|
145 | Path2D.Double p = new Path2D.Double();
|
---|
146 | p.setWindingRule(GeneralPath.WIND_NON_ZERO);
|
---|
147 | boolean first = true;
|
---|
148 | for (long node : nodes) {
|
---|
149 | Point2D point = helper.getPoint(map.nodes.get(node));
|
---|
150 | if (first) {
|
---|
151 | p.moveTo(point.getX(), point.getY());
|
---|
152 | first = false;
|
---|
153 | } else {
|
---|
154 | p.lineTo(point.getX(), point.getY());
|
---|
155 | }
|
---|
156 | }
|
---|
157 | if (style.line != null) {
|
---|
158 | if (style.dash != null) {
|
---|
159 | float[] dash = new float[style.dash.length];
|
---|
160 | System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
|
---|
161 | for (int i = 0; i < style.dash.length; i++) {
|
---|
162 | dash[i] *= (float) (sScale);
|
---|
163 | }
|
---|
164 | g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
|
---|
165 | } else {
|
---|
166 | g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
|
---|
167 | }
|
---|
168 | g2.setPaint(style.line);
|
---|
169 | g2.draw(p);
|
---|
170 | }
|
---|
171 | if (style.fill != null) {
|
---|
172 | g2.setPaint(style.fill);
|
---|
173 | g2.fill(p);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | public static void labelText (Feature feature, String str, TextStyle style, Delta delta) {
|
---|
179 |
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static void lineText (Feature feature, String str, TextStyle style, double offset, Delta delta) {
|
---|
183 |
|
---|
184 | }
|
---|
185 | }
|
---|