source: osm/applications/editors/josm/plugins/smed2/src/seamap/Renderer.java@ 29215

Last change on this file since 29215 was 29215, checked in by malcolmh, 12 years ago

save

File size: 9.4 KB
Line 
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
10package seamap;
11
12import java.awt.BasicStroke;
13import java.awt.Color;
14import java.awt.Font;
15import java.awt.Graphics2D;
16import java.awt.Rectangle;
17import java.awt.RenderingHints;
18import java.awt.geom.AffineTransform;
19import java.awt.geom.GeneralPath;
20import java.awt.geom.Path2D;
21import java.awt.geom.Point2D;
22import java.util.ArrayList;
23import java.util.HashMap;
24
25import s57.S57att.Att;
26import s57.S57obj.Obj;
27import s57.S57val.*;
28import s57.S57val;
29import seamap.SeaMap;
30import seamap.SeaMap.*;
31import symbols.Symbols;
32import symbols.Symbols.*;
33
34public class Renderer {
35
36 static MapHelper helper;
37 static SeaMap map;
38 static double sScale;
39 static double tScale;
40 static Graphics2D g2;
41 static int zoom;
42
43 public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapHelper h) {
44 g2 = g;
45 zoom = z;
46 helper = h;
47 map = m;
48 sScale = Symbols.symbolScale[zoom]*factor;
49 tScale = Symbols.textScale[zoom]*factor;
50 if (map != null) {
51 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
52 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
53 Rules.rules(map, zoom);
54 }
55 }
56
57 public static AttMap getAtts(Feature feature, Obj obj, int idx) {
58 HashMap<Integer, AttMap> objs = feature.objs.get(obj);
59 if (objs == null) return null;
60 else return objs.get(idx);
61 }
62
63 public static Object getAttVal(Feature feature, Obj obj, int idx, Att att) {
64 AttMap atts = getAtts(feature, obj, idx);
65 if (atts == null) return S57val.nullVal(att);
66 else {
67 AttItem item = atts.get(att);
68 if (item == null) return S57val.nullVal(att);
69 return item.val;
70 }
71 }
72
73 public static double signedArea(Feature feature) {
74 if (feature.flag == Fflag.AREA) {
75 ArrayList<Long> way;
76 if (map.mpolys.containsKey(feature.refs)) {
77 way = map.ways.get(map.mpolys.get(feature.refs));
78 } else {
79 way = map.ways.get(feature.refs);
80 }
81 Coord coord = map.nodes.get(way.get(0));
82 double llon = coord.lon;
83 double llat = coord.lat;
84 double sigma = 0.0;
85 for (long node : way) {
86 coord = map.nodes.get(node);
87 double lat = coord.lat;
88 double lon = coord.lon;
89 sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
90 llon = lon;
91 llat = lat;
92 }
93 return sigma;
94 }
95 return 0;
96 }
97
98 public boolean handOfArea(Feature feature) {
99 return (signedArea(feature) < 0);
100 }
101
102 public static double calcArea(Feature feature) {
103 return Math.abs(signedArea(feature)) * 3444 * 3444 / 2.0;
104 }
105
106 public static Coord findCentroid(Feature feature) {
107 Coord coord;
108 ArrayList<Long> way;
109 if (map.mpolys.containsKey(feature.refs)) {
110 way = map.ways.get(map.mpolys.get(feature.refs).get(0));
111 } else {
112 way = map.ways.get(feature.refs);
113 }
114 switch (feature.flag) {
115 case NODE:
116 return map.nodes.get(feature.refs);
117 case LINE:
118 coord = map.nodes.get(way.get(1));
119 break;
120 case AREA:
121 default:
122 coord = map.nodes.get(way.get(0));
123 }
124 double slat = 0.0;
125 double slon = 0.0;
126 double sarc = 0.0;
127 double llat = coord.lat;
128 double llon = coord.lon;
129 for (long node : way) {
130 coord = map.nodes.get(node);
131 double lon = coord.lon;
132 double lat = coord.lat;
133 double arc = (Math.acos(Math.cos(lon-llon) * Math.cos(lat-llat)));
134 slat += (lat * arc);
135 slon += (lon * arc);
136 sarc += arc;
137 llat = lat;
138 llon = lon;
139 }
140 return map.new Coord((sarc > 0.0 ? slat/sarc : 0.0), (sarc > 0.0 ? slon/sarc : 0.0));
141 }
142
143 public static void symbol(Feature feature, Symbol symbol, Obj obj, Delta delta) {
144 Point2D point = helper.getPoint(findCentroid(feature));
145 if (obj == null) {
146 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, null);
147 } else {
148 ArrayList<ColCOL> colours = (ArrayList<ColCOL>) getAttVal(feature, obj, 0, Att.COLOUR);
149 ArrayList<ColPAT> pattern = (ArrayList<ColPAT>) getAttVal(feature, obj, 0, Att.COLPAT);
150 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, new Scheme(pattern, colours));
151 }
152 }
153
154 private static Rectangle symbolSize(Symbol symbol) {
155 Symbol ssymb = symbol;
156 while (ssymb != null) {
157 for (Instr item : symbol) {
158 if (item.type == Prim.BBOX) {
159 return (Rectangle) item.params;
160 }
161 if (item.type == Prim.SYMB) {
162 ssymb = ((SubSymbol)item.params).instr;
163 break;
164 }
165 }
166 if (ssymb == symbol)
167 break;
168 }
169 return null;
170 }
171
172 public static void lineSymbols(Feature feature, Symbol prisymb, double space, Symbol secsymb, int ratio) {
173 if (feature.flag != Fflag.NODE) {
174 Rectangle prect = symbolSize(prisymb);
175 Rectangle srect = symbolSize(secsymb);
176 if (srect == null)
177 ratio = 0;
178 if (prect != null) {
179 ArrayList<Long> ways = new ArrayList<Long>();
180 double psize = Math.abs(prect.getY()) * sScale;
181 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
182 if (map.outers.containsKey(feature.refs)) {
183 ways.addAll(map.mpolys.get(map.outers.get(feature.refs)));
184 } else {
185 if (map.mpolys.containsKey(feature.refs)) {
186 ways.addAll(map.mpolys.get(feature.refs));
187 } else {
188 ways.add(feature.refs);
189 }
190 }
191 Point2D prev = new Point2D.Double();
192 Point2D next = new Point2D.Double();
193 Point2D curr = new Point2D.Double();
194 Point2D succ = new Point2D.Double();
195 boolean gap = true;
196 boolean piv = false;
197 double len = 0;
198 double angle = 0;
199 int scount = ratio;
200 Symbol symbol = prisymb;
201 for (long way : ways) {
202 boolean first = true;
203 for (long node : map.ways.get(way)) {
204 prev = next;
205 next = helper.getPoint(map.nodes.get(node));
206 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
207 piv = true;
208 if (first) {
209 curr = succ = next;
210 gap = (space > 0);
211 scount = ratio;
212 symbol = prisymb;
213 len = gap ? psize * space * 0.5 : psize;
214 first = false;
215 } else {
216 while (curr.distance(next) >= len) {
217 if (piv) {
218 double rem = len;
219 double s = prev.distance(next);
220 double p = curr.distance(prev);
221 if ((s > 0) && (p > 0)) {
222 double n = curr.distance(next);
223 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
224 double phi = Math.asin(p / len * Math.sin(theta));
225 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
226 }
227 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
228 piv = false;
229 } else {
230 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
231 }
232 if (!gap) {
233 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(),
234 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX()) + Math.toRadians(90)))), null);
235 }
236 if (space > 0) gap = !gap;
237 curr = succ;
238 len = gap ? (psize * space) : (--scount == 0) ? ssize : psize;
239 if (scount == 0) {
240 symbol = secsymb;
241 scount = ratio;
242 } else {
243 symbol = prisymb;
244 }
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252
253 public static void lineVector (Feature feature, LineStyle style) {
254 if (feature.flag != Fflag.NODE) {
255 ArrayList<Long> ways = new ArrayList<Long>();
256 if (map.outers.containsKey(feature.refs)) {
257 ways.addAll(map.mpolys.get(map.outers.get(feature.refs)));
258 } else {
259 if (map.mpolys.containsKey(feature.refs)) {
260 ways.addAll(map.mpolys.get(feature.refs));
261 } else {
262 ways.add(feature.refs);
263 }
264 }
265 Path2D.Double p = new Path2D.Double();
266 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
267 for (long way : ways) {
268 boolean first = true;
269 for (long node : map.ways.get(way)) {
270 Point2D point = helper.getPoint(map.nodes.get(node));
271 if (first) {
272 p.moveTo(point.getX(), point.getY());
273 first = false;
274 } else {
275 p.lineTo(point.getX(), point.getY());
276 }
277 }
278 }
279 if (style.line != null) {
280 if (style.dash != null) {
281 float[] dash = new float[style.dash.length];
282 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
283 for (int i = 0; i < style.dash.length; i++) {
284 dash[i] *= (float) sScale;
285 }
286 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
287 } else {
288 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
289 }
290 g2.setPaint(style.line);
291 g2.draw(p);
292 }
293 if (style.fill != null) {
294 g2.setPaint(style.fill);
295 g2.fill(p);
296 }
297 }
298 }
299
300 public static void labelText (Feature feature, String str, Font font, Color colour, Delta delta) {
301 Symbol label = new Symbol();
302 label.add(new Instr(Prim.TEXT, new Caption(str, font, colour, (delta == null) ? new Delta(Handle.CC, null) : delta)));
303 Point2D point = helper.getPoint(findCentroid(feature));
304 Symbols.drawSymbol(g2, label, tScale, point.getX(), point.getY(), null, null);
305 }
306
307 public static void lineText (Feature feature, String str, Font font, double offset, double dy) {
308
309 }
310}
Note: See TracBrowser for help on using the repository browser.