]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/sg/MapScaleNode.java
Lots of changes to district stuff
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / sg / MapScaleNode.java
1 package org.simantics.maps.sg;
2
3 import java.awt.AlphaComposite;
4 import java.awt.BasicStroke;
5 import java.awt.Color;
6 import java.awt.Font;
7 import java.awt.FontMetrics;
8 import java.awt.Graphics2D;
9 import java.awt.geom.AffineTransform;
10 import java.awt.geom.Point2D;
11 import java.awt.geom.Rectangle2D;
12 import java.util.Locale;
13
14 import org.geotools.referencing.CRS;
15 import org.geotools.referencing.GeodeticCalculator;
16 import org.opengis.referencing.FactoryException;
17 import org.opengis.referencing.crs.CoordinateReferenceSystem;
18 import org.simantics.scenegraph.g2d.G2DNode;
19 import org.simantics.scenegraph.utils.GridUtils;
20
21 public class MapScaleNode extends G2DNode {
22
23     private static final long serialVersionUID = -2738682328944298290L;
24     
25     private static final Color GRAY              = new Color(100, 100, 100);
26
27     protected Boolean          enabled           = true;
28
29     protected double           gridSize          = 1.0;
30
31     private double scale;
32
33     @Override
34     public void render(Graphics2D g) {
35         if (!enabled)
36             return;
37
38         AffineTransform tr = g.getTransform();
39         double scaleX = Math.abs(tr.getScaleX());
40         double scaleY = Math.abs(tr.getScaleY());
41         if (scaleX <= 0 || scaleY <= 0) {
42             // Make sure that we don't end up in an eternal loop below.
43             return;
44         }
45         double offsetX = tr.getTranslateX();
46         double offsetY = tr.getTranslateY();
47         g.setTransform(new AffineTransform());
48
49         Font rulerFont = new Font("Tahoma", Font.PLAIN, 9);
50
51         //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
52         g.setStroke(new BasicStroke(1));
53         g.setColor(new Color(0.9f, 0.9f, 0.9f, 0.75f));
54
55         Rectangle2D bounds = g.getClipBounds();
56         if(bounds == null) return; // FIXME
57
58         double previousText = -100;
59         
60         double minY = bounds.getMaxY() - 30;
61         
62         double scaleRight = bounds.getMaxX() - 30;
63         
64         double meterPerPixel = getMeterPerPixel(scaleRight - offsetX, minY - offsetY, scaleX, scaleY);
65         
66         double pixels = 0;
67         double value = 0;
68         for (int i = 0; i < SCALE_VALUES.length; i++) {
69             value = SCALE_VALUES[i];
70             pixels = value / meterPerPixel;
71             if (pixels > 100) {
72                 break;
73             }
74         }
75         
76         
77         double newScaleLeft = scaleRight - pixels;
78         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
79         Rectangle2D vertical = new Rectangle2D.Double(newScaleLeft, bounds.getMaxY() - 30, pixels, 20);
80         g.fill(vertical);
81         
82         g.setColor(GRAY);
83         g.setFont(rulerFont);
84         
85         
86         // stepX and stepY should be something between 50 and 100
87         double stepX = 50;
88
89         stepX = GridUtils.limitedEvenGridSpacing(stepX, scaleX, 100, gridSize, true);
90         //while(stepX * scaleX > 100) stepX /= 2;
91         //while(stepY * scaleY > 100) stepY /= 2;
92
93         while(stepX * scaleX < 50) stepX *= 2;
94
95         stepX *= scaleX;
96         
97         double gap = scaleRight -newScaleLeft;
98         
99         stepX = gap / 2 - 0.00001;
100         
101         // Horizontal ruler
102         double label = 0;
103         for(double x = newScaleLeft; x < scaleRight; x += stepX) {
104             String str = formatValue(label * meterPerPixel);
105             FontMetrics fm = g.getFontMetrics();
106             Rectangle2D r = fm.getStringBounds(str, g);
107             if((x - r.getWidth() / 2) > previousText) {
108                 g.setColor(Color.BLACK);
109                 g.drawString(str, (int)(x-r.getWidth()/2), (int)(minY+1+r.getHeight()));
110                 previousText = x+r.getWidth()/2+stepX/4;
111             }
112
113             g.setColor(GRAY);
114             g.drawLine((int)x, (int)minY+12, (int)x, (int)minY+19);
115             if (x + 0.1 < scaleRight) {
116                 if(stepX/5 > 2) {
117                     for(double x2 = x+stepX/5; x2 < x+stepX; x2+=stepX/5) {
118                         if(x2 > 20) {
119                             g.drawLine((int)x2, (int)minY+15, (int)x2, (int)minY+19);
120                         }
121                     }
122                     for(double x2 = x+stepX/10; x2 < x+stepX; x2+=stepX/5) {
123                         if(x2 > 20) {
124                             g.drawLine((int)x2, (int)minY+17, (int)x2, (int)minY+19);
125                         }
126                     }
127                 }
128             }
129             label += stepX;
130         }
131
132         g.setTransform(tr);
133     }
134
135     @Override
136     public Rectangle2D getBoundsInLocal() {
137         return null;
138     }
139     
140     private static final transient int    MAX_DIGITS = 0;
141     private static final transient double EPSILON    = 0.01;
142     private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 2);
143     private static final transient String[] SI_UNIT_LARGE_PREFIXES = { "m", "km" };
144     
145     private static final transient double[] SCALE_VALUES = { 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 50000000 };
146     
147     public static String formatValue(double value) {
148         int magnitude = (int) Math.round(Math.log10(value));
149         //System.out.println("magnitude: " + magnitude + ", " + value);
150         int allowedDecimals = MAX_DIGITS;
151         allowedDecimals -= Math.abs(magnitude);
152         if (allowedDecimals < 0)
153             allowedDecimals = 0;
154
155         String valueStr = String.format(Locale.US, "%." + allowedDecimals + "f", value);
156         if (allowedDecimals > 0) {
157             for (int trunc = valueStr.length() - 1; trunc > 0; --trunc) {
158                 char ch = valueStr.charAt(trunc);
159                 if (ch == '.') {
160                     valueStr = valueStr.substring(0, trunc);
161                     break;
162                 }
163                 if (valueStr.charAt(trunc) != '0') {
164                     valueStr = valueStr.substring(0, trunc + 1);
165                     break;
166                 }
167             }
168             if (Math.abs(value) + EPSILON > TRIM_THRESHOLD_MAX_VALUE) {
169                 // Cut anything beyond a possible decimal dot out since they
170                 // should not show anyway. This is a complete hack that tries to
171                 // circumvent floating-point inaccuracy problems.
172                 int dotIndex = valueStr.lastIndexOf('.');
173                 if (dotIndex > -1) {
174                     valueStr = valueStr.substring(0, dotIndex);
175                 }
176             }
177         }
178
179         if (valueStr.equals("-0"))
180             valueStr = "0";
181         
182 //        if (!valueStr.equals("0")) {
183 //            double trimValue = value;
184 //            for (int i = 0; i < SI_UNIT_LARGE_PREFIXES.length; i++) {
185 //                double trim = trimValue / 1000;
186 //                if (Math.abs(trim)-EPSILON < TRIM_THRESHOLD_MAX_VALUE) {
187 //                    valueStr = valueStr.substring(0, valueStr.length() - (i + 1) * 3);
188 //                    valueStr += SI_UNIT_LARGE_PREFIXES[i];
189 //                    break;
190 //                }
191 //                trimValue = trim;
192 //            }
193 //        }
194
195         return valueStr;
196     }
197
198     public void setEnabled(boolean enabled) {
199         this.enabled = enabled;
200     }
201
202     public void setScale(double scale) {
203         this.scale = scale;
204     }
205     
206     @Override
207     public void init() {
208         try {
209             EPSG4326 = CRS.decode("EPSG:4326");
210             calculator = new GeodeticCalculator(EPSG4326);
211         } catch (FactoryException e) {
212             e.printStackTrace();
213         }
214         super.init();
215     }
216     
217     GeodeticCalculator calculator; 
218     CoordinateReferenceSystem EPSG4326;
219     
220     public Point2D scaleLeftmostPoint(double startLon, double startLat, double distance, double azimuth) {
221         GeodeticCalculator calculator = new GeodeticCalculator();
222         calculator.setStartingGeographicPoint(startLon, startLat);
223         calculator.setDirection(azimuth, distance);
224         return calculator.getDestinationGeographicPoint();
225     }
226     
227     public double getMeterPerPixel(double screenX, double screenY, double scaleX, double scaleY) {
228         double startLon = (screenX / scaleX) / scale;
229         double val = (screenY / scaleY) / scale;
230         val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
231         double startLat = val;
232         double endLon = ((screenX + 1) / scaleX) / scale;
233         double endLat = val;
234         
235         calculator.setStartingGeographicPoint(startLon, startLat);
236         calculator.setDestinationGeographicPoint(endLon, endLat);
237         double distance = calculator.getOrthodromicDistance();
238         
239         return distance;
240     }
241
242 }