001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.plugin.scalebar;
034:
035: import java.util.Collection;
036: import java.util.Collections;
037: import java.util.Iterator;
038:
039: import com.vividsolutions.jts.util.Assert;
040: import com.vividsolutions.jump.util.MathUtil;
041:
042: /**
043: * Chooses a good size for the scale-bar increments.
044: */
045: public class IncrementChooser {
046: public IncrementChooser() {
047: }
048:
049: /**
050: * @param displayWidth in model-space coordinates.
051: * @return a Quantity whose value will be a multiple of 10
052: */
053: public RoundQuantity chooseGoodIncrement(Collection units,
054: double idealIncrement) {
055: return goodIncrement(goodUnit(units, idealIncrement),
056: idealIncrement);
057: }
058:
059: /**
060: * @return the Unit that is the fewest orders of magnitude away from the ideal
061: * increment, preferably smaller than the ideal increment.
062: */
063: private Unit goodUnit(Collection units, double idealIncrement) {
064: Unit goodUnit = (Unit) Collections.min(units);
065:
066: for (Iterator i = units.iterator(); i.hasNext();) {
067: Unit candidateUnit = (Unit) i.next();
068:
069: if (candidateUnit.getModelValue() > idealIncrement) {
070: continue;
071: }
072:
073: if (distance(candidateUnit.getModelValue(), idealIncrement) < distance(
074: goodUnit.getModelValue(), idealIncrement)) {
075: goodUnit = candidateUnit;
076: }
077: }
078:
079: return goodUnit;
080: }
081:
082: private double distance(double a, double b) {
083: return Math.abs(MathUtil.orderOfMagnitude(a)
084: - MathUtil.orderOfMagnitude(b));
085: }
086:
087: /**
088: * @return an amount of the form 1 x 10^n, 2 x 10^n or 5 x 10^n that is closest to the
089: * ideal increment without exceeding it.
090: */
091: private RoundQuantity goodIncrement(Unit unit, double idealIncrement) {
092: RoundQuantity mantissa1Candidate = new RoundQuantity(1,
093: (int) Math.floor(MathUtil
094: .orderOfMagnitude(idealIncrement)
095: - MathUtil.orderOfMagnitude(unit
096: .getModelValue())), unit);
097: // MD - hack to get around Nan exception
098: if (Double.isNaN(idealIncrement))
099: idealIncrement = mantissa1Candidate.getModelValue();
100: Assert.isTrue(
101: mantissa1Candidate.getModelValue() <= idealIncrement,
102: "unit=" + unit.getModelValue() + ", ideal increment="
103: + idealIncrement);
104:
105: RoundQuantity mantissa2Candidate = new RoundQuantity(2,
106: mantissa1Candidate.getExponent(), unit);
107: RoundQuantity mantissa5Candidate = new RoundQuantity(5,
108: mantissa1Candidate.getExponent(), unit);
109:
110: if (mantissa5Candidate.getModelValue() <= idealIncrement) {
111: return mantissa5Candidate;
112: }
113:
114: if (mantissa2Candidate.getModelValue() <= idealIncrement) {
115: return mantissa2Candidate;
116: }
117:
118: return mantissa1Candidate;
119: }
120:
121: }
|