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
RevLine 
[29157]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
[29199]12import java.awt.BasicStroke;
[29202]13import java.awt.Color;
14import java.awt.Font;
[29174]15import java.awt.Graphics2D;
[29202]16import java.awt.Rectangle;
[29174]17import java.awt.RenderingHints;
[29204]18import java.awt.geom.AffineTransform;
[29199]19import java.awt.geom.GeneralPath;
20import java.awt.geom.Path2D;
[29174]21import java.awt.geom.Point2D;
22import java.util.ArrayList;
[29175]23import java.util.HashMap;
[29157]24
[29175]25import s57.S57att.Att;
26import s57.S57obj.Obj;
27import s57.S57val.*;
28import s57.S57val;
[29199]29import seamap.SeaMap;
[29174]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;
[29199]41 static int zoom;
[29174]42
[29199]43 public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapHelper h) {
[29174]44 g2 = g;
[29199]45 zoom = z;
[29174]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);
[29184]53 Rules.rules(map, zoom);
[29174]54 }
55 }
56
[29184]57 public static AttMap getAtts(Feature feature, Obj obj, int idx) {
58 HashMap<Integer, AttMap> objs = feature.objs.get(obj);
[29175]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) {
[29184]64 AttMap atts = getAtts(feature, obj, idx);
[29185]65 if (atts == null) return S57val.nullVal(att);
[29175]66 else {
67 AttItem item = atts.get(att);
68 if (item == null) return S57val.nullVal(att);
69 return item.val;
70 }
71 }
[29184]72
[29215]73 public static double signedArea(Feature feature) {
[29185]74 if (feature.flag == Fflag.AREA) {
[29202]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 }
[29185]81 Coord coord = map.nodes.get(way.get(0));
[29198]82 double llon = coord.lon;
83 double llat = coord.lat;
[29189]84 double sigma = 0.0;
[29185]85 for (long node : way) {
86 coord = map.nodes.get(node);
[29198]87 double lat = coord.lat;
88 double lon = coord.lon;
[29189]89 sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
90 llon = lon;
91 llat = lat;
[29185]92 }
[29215]93 return sigma;
[29185]94 }
[29215]95 return 0;
[29185]96 }
97
[29215]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
[29184]106 public static Coord findCentroid(Feature feature) {
[29185]107 Coord coord;
[29202]108 ArrayList<Long> way;
109 if (map.mpolys.containsKey(feature.refs)) {
[29206]110 way = map.ways.get(map.mpolys.get(feature.refs).get(0));
[29202]111 } else {
112 way = map.ways.get(feature.refs);
113 }
[29185]114 switch (feature.flag) {
115 case NODE:
116 return map.nodes.get(feature.refs);
[29206]117 case LINE:
[29185]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;
[29184]125 double slon = 0.0;
126 double sarc = 0.0;
[29185]127 double llat = coord.lat;
128 double llon = coord.lon;
[29184]129 for (long node : way) {
[29185]130 coord = map.nodes.get(node);
131 double lon = coord.lon;
132 double lat = coord.lat;
[29198]133 double arc = (Math.acos(Math.cos(lon-llon) * Math.cos(lat-llat)));
[29185]134 slat += (lat * arc);
[29184]135 slon += (lon * arc);
136 sarc += arc;
[29185]137 llat = lat;
[29184]138 llon = lon;
139 }
140 return map.new Coord((sarc > 0.0 ? slat/sarc : 0.0), (sarc > 0.0 ? slon/sarc : 0.0));
141 }
[29175]142
[29186]143 public static void symbol(Feature feature, Symbol symbol, Obj obj, Delta delta) {
[29184]144 Point2D point = helper.getPoint(findCentroid(feature));
[29199]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 }
[29174]152 }
153
[29204]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) {
[29206]162 ssymb = ((SubSymbol)item.params).instr;
[29204]163 break;
164 }
165 }
166 if (ssymb == symbol)
167 break;
168 }
169 return null;
170 }
171
[29198]172 public static void lineSymbols(Feature feature, Symbol prisymb, double space, Symbol secsymb, int ratio) {
173 if (feature.flag != Fflag.NODE) {
[29204]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) {
[29205]218 double rem = len;
219 double s = prev.distance(next);
220 double p = curr.distance(prev);
[29207]221 if ((s > 0) && (p > 0)) {
[29205]222 double n = curr.distance(next);
[29207]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);
[29205]226 }
227 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
[29204]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) {
[29205]233 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(),
[29207]234 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX()) + Math.toRadians(90)))), null);
[29204]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 }
[29198]249 }
250 }
251 }
[29204]252
[29198]253 public static void lineVector (Feature feature, LineStyle style) {
[29199]254 if (feature.flag != Fflag.NODE) {
[29200]255 ArrayList<Long> ways = new ArrayList<Long>();
[29202]256 if (map.outers.containsKey(feature.refs)) {
257 ways.addAll(map.mpolys.get(map.outers.get(feature.refs)));
[29200]258 } else {
[29202]259 if (map.mpolys.containsKey(feature.refs)) {
260 ways.addAll(map.mpolys.get(feature.refs));
261 } else {
262 ways.add(feature.refs);
263 }
[29200]264 }
[29199]265 Path2D.Double p = new Path2D.Double();
[29200]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 }
[29199]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++) {
[29202]284 dash[i] *= (float) sScale;
[29199]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 }
[29198]298 }
299
[29204]300 public static void labelText (Feature feature, String str, Font font, Color colour, Delta delta) {
[29202]301 Symbol label = new Symbol();
[29206]302 label.add(new Instr(Prim.TEXT, new Caption(str, font, colour, (delta == null) ? new Delta(Handle.CC, null) : delta)));
[29202]303 Point2D point = helper.getPoint(findCentroid(feature));
[29206]304 Symbols.drawSymbol(g2, label, tScale, point.getX(), point.getY(), null, null);
[29198]305 }
306
[29202]307 public static void lineText (Feature feature, String str, Font font, double offset, double dy) {
[29198]308
309 }
[29157]310}
Note: See TracBrowser for help on using the repository browser.