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

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

save

File size: 3.7 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.Graphics2D;
13import java.awt.RenderingHints;
14import java.awt.geom.Point2D;
15import java.util.ArrayList;
16import java.util.HashMap;
17
18import s57.S57att.Att;
19import s57.S57obj.Obj;
20import s57.S57val.ColCOL;
21import s57.S57val.*;
22import s57.S57val;
23import seamap.SeaMap.*;
24import symbols.Symbols;
25import symbols.Symbols.*;
26
27public 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 llon = Math.toRadians(coord.lon);
69 double llat = Math.toRadians(coord.lat);
70 double sigma = 0.0;
71 for (long node : way) {
72 coord = map.nodes.get(node);
73 double lat = Math.toRadians(coord.lat);
74 double lon = Math.toRadians(coord.lon);
75 sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
76 llon = lon;
77 llat = lat;
78 }
79 return Math.abs(sigma) / 2.0 * 3444 * 3444;
80 }
81 return 0.0;
82 }
83
84 public static Coord findCentroid(Feature feature) {
85 double tst = calcArea(feature);
86 Coord coord;
87 ArrayList<Long> way = map.ways.get(feature.refs);
88 switch (feature.flag) {
89 case NODE:
90 return map.nodes.get(feature.refs);
91 case WAY:
92 coord = map.nodes.get(way.get(1));
93 break;
94 case AREA:
95 default:
96 coord = map.nodes.get(way.get(0));
97 }
98 double slat = 0.0;
99 double slon = 0.0;
100 double sarc = 0.0;
101 double llat = coord.lat;
102 double llon = coord.lon;
103 for (long node : way) {
104 coord = map.nodes.get(node);
105 double lon = coord.lon;
106 double lat = coord.lat;
107 double arc = (Math.acos(Math.cos(Math.toRadians(lon-llon)) * Math.cos(Math.toRadians(lat-llat))));
108 slat += (lat * arc);
109 slon += (lon * arc);
110 sarc += arc;
111 llat = lat;
112 llon = lon;
113 }
114 return map.new Coord((sarc > 0.0 ? slat/sarc : 0.0), (sarc > 0.0 ? slon/sarc : 0.0));
115 }
116
117 public static void symbol(Feature feature, Symbol symbol, Obj obj, Delta delta) {
118 Point2D point = helper.getPoint(findCentroid(feature));
119 ArrayList<ColCOL> colours = (ArrayList<ColCOL>) getAttVal(feature, obj, 0, Att.COLOUR);
120 ArrayList<ColPAT> pattern = (ArrayList<ColPAT>) getAttVal(feature, obj, 0, Att.COLPAT);
121 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, new Scheme(pattern, colours));
122 }
123
124}
Note: See TracBrowser for help on using the repository browser.