[OSM-dev] [RFC] Beautify scale bar

Jan-Benedict Glaw jbglaw at lug-owl.de
Sat May 13 21:45:20 BST 2006


On Sat, 2006-05-13 18:00:42 +0200, Jan-Benedict Glaw <jbglaw at lug-owl.de> wrote:
> On Sat, 2006-05-13 14:48:01 +0100, SteveC <steve at asklater.com> wrote:
>   * round
>   * fabs
>   * log

I found out that I need to "import" the java.lang.StrictMath class and
that I can use these functions (due to the fact that these are static
I seem to not need to get a new instance of that class) as eg.
StrictMath.round().

> The idea is to break the expected bar length down to a 10'th
> logarithm wrt. the scale, subtracting the round()ed value and
> comparing the result (-1 <= x <= 1) with the log()s of 1, 2.5 and 5
> (and 1/1, 1/2.5 and 1/5.)  That way, we can decide on a proper value
> which needs an additional factor of 10^x. The bar can then be scaled
> properly.

I've implemented this and it *seems* to work. Unfortunately, it seems
to introduce something like a warning:

    [javac] Compiling 1 source file to /home/jbglaw/vax-linux/scm/cvs-repos/osm/svn.openstreetmap.
org/java/build
    [javac] Note: /home/jbglaw/vax-linux/scm/cvs-repos/osm/svn.openstreetmap.org/java/src/org/open
streetmap/processing/OsmApplet.java uses unchecked or unsafe operations.

which comes from:

                String meters = "" + StrictMath.round(factor[used_factor]
                                     * StrictMath.pow(10.0, exponent)) + "m";

I don't know how to fix that, a Java guy should have a look at it.

Here's the patch:

Index: src/org/openstreetmap/processing/OsmApplet.java
===================================================================
--- src/org/openstreetmap/processing/OsmApplet.java	(revision 1021)
+++ src/org/openstreetmap/processing/OsmApplet.java	(working copy)
@@ -83,6 +83,7 @@
 
 package org.openstreetmap.processing;
 
+import java.lang.StrictMath;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -385,13 +386,39 @@
 	}
 
 	private void draw_scale_bar() {
+		int num_factors = 3;
+		double factor[] = { 1.0f, 2.5f, 5.0f, };
+		int exponent = 0;
+		int used_factor = 0;
+		double remains = 1.0f;
+		int i;
+		int min_length = getWidth() / 6;
+		int bar_length;
 		int dist_bottom = 70;
 		int dist_right = 20;
-		int bar_length = getWidth() / 5;
 		int ending_bar_length = getHeight() / 30;
+
+		/* Find the nearest factor */
+		for (i = 0; i < num_factors; i++) {
+			double rest;
+			double log_value;
+
+			log_value = StrictMath.log10(1000.0f * tiles.kilometersPerPixel()
+					  * min_length / factor[i]);
+			if ((rest = log_value - StrictMath.floor(log_value)) < remains) {
+				remains = rest;
+				used_factor = i;
+				exponent = (int)StrictMath.floor(log_value);
+			}
+		}
+
+		/* Calculate the exact bar length */
+		bar_length = (int) (factor[used_factor] * StrictMath.pow(10.0, exponent)
+				    / (1000.0f * tiles.kilometersPerPixel()));
+
 		fill(0);
-    strokeWeight(10);
-    textSize(10);
+		strokeWeight(2);
+		textSize(10);
 		pushMatrix();
 		/* Horizonthal bar */
 		line(getWidth() - bar_length - dist_right, getHeight() - dist_bottom,
@@ -403,10 +430,12 @@
 		line(getWidth() - dist_right, getHeight() - dist_bottom + ending_bar_length / 2,
 		     getWidth() - dist_right, getHeight() - dist_bottom - ending_bar_length / 2);
 
-		String meters = "" + (tiles.kilometersPerPixel() * bar_length * 1000.0f);
+		/* Print the numeric scale value */
+		String meters = "" + StrictMath.round(factor[used_factor]
+				     * StrictMath.pow(10.0, exponent)) + "m";
 		translate(getWidth() - dist_right - bar_length + (bar_length - textWidth(meters))/2,
 			  getHeight() - dist_bottom + 5);
-    text(meters);
+		text(meters);
 		popMatrix();
 	}
 

MfG, JBG

-- 
Jan-Benedict Glaw       jbglaw at lug-owl.de    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 für einen Freien Staat voll Freier Bürger"  | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20060513/b16cd671/attachment.pgp>


More information about the dev mailing list