|
| 1 | +package geoscript.carto |
| 2 | + |
| 3 | +import geoscript.render.Map |
| 4 | +import org.geotools.renderer.lite.RendererUtilities |
| 5 | + |
| 6 | +import java.awt.Color |
| 7 | +import java.awt.Font |
| 8 | + |
| 9 | +/** |
| 10 | + * Adds scale bar to a cartographic document. |
| 11 | + * @author Jared Erickson |
| 12 | + */ |
| 13 | +class ScaleBarItem extends Item { |
| 14 | + |
| 15 | + Map map |
| 16 | + |
| 17 | + Color strokeColor = Color.BLACK |
| 18 | + |
| 19 | + Color fillColor = Color.WHITE |
| 20 | + |
| 21 | + float strokeWidth = 1 |
| 22 | + |
| 23 | + Font font = new Font("Arial", Font.PLAIN, 12) |
| 24 | + |
| 25 | + int border = 5 |
| 26 | + |
| 27 | + Units units = Units.METRIC |
| 28 | + |
| 29 | + enum Units { |
| 30 | + METRIC, |
| 31 | + US |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a scale bar from the top left with the given width and height. |
| 36 | + * @param x The number of pixels from the left |
| 37 | + * @param y The number of pixels from the top |
| 38 | + * @param width The width in pixels |
| 39 | + * @param height The height in pixels |
| 40 | + */ |
| 41 | + ScaleBarItem(int x, int y, int width, int height) { |
| 42 | + super(x, y, width, height) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Set the Map to use when calculating the map scale |
| 47 | + * @param map The Map |
| 48 | + * @return The ScaleBarItem |
| 49 | + */ |
| 50 | + ScaleBarItem map(Map map) { |
| 51 | + this.map = map |
| 52 | + this |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Set the Units (US or METRIC) to use |
| 57 | + * @param units Units (US or METRIC) |
| 58 | + * @return The ScaleBarItem |
| 59 | + */ |
| 60 | + ScaleBarItem units(Units units) { |
| 61 | + this.units = units |
| 62 | + this |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Set stroke Color |
| 67 | + * @param strokeColor The stroke Color |
| 68 | + * @return The ScaleBarItem |
| 69 | + */ |
| 70 | + ScaleBarItem strokeColor(Color strokeColor) { |
| 71 | + this.strokeColor = strokeColor |
| 72 | + this |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set the fill Color |
| 77 | + * @param fillColor The fill Color |
| 78 | + * @return The ScaleBarItem |
| 79 | + */ |
| 80 | + ScaleBarItem fillColor(Color fillColor) { |
| 81 | + this.fillColor = fillColor |
| 82 | + this |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set the stroke width |
| 87 | + * @param strokeWidth The stroke width |
| 88 | + * @return The ScaleBarItem |
| 89 | + */ |
| 90 | + ScaleBarItem strokeWidth(float strokeWidth) { |
| 91 | + this.strokeWidth = strokeWidth |
| 92 | + this |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Set the Font |
| 97 | + * @param font The Font |
| 98 | + * @return The ScaleBarItem |
| 99 | + */ |
| 100 | + ScaleBarItem font(Font font) { |
| 101 | + this.font = font |
| 102 | + this |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Set the border padding |
| 107 | + * @param border The border padding |
| 108 | + * @return The ScaleBarItem |
| 109 | + */ |
| 110 | + ScaleBarItem border(int border) { |
| 111 | + this.border = border |
| 112 | + this |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Calculate the scale bar information |
| 117 | + * @return A ScaleBarInfo instance |
| 118 | + */ |
| 119 | + ScaleBarInfo calculateScaleBarInfo() { |
| 120 | + |
| 121 | + double widthInMeters = RendererUtilities.toMeters(map.bounds.width, map.bounds.proj.crs) |
| 122 | + double widthInUnits = widthInMeters |
| 123 | + if (units == ScaleBarItem.Units.US) { |
| 124 | + widthInUnits = org.geotools.measure.Units.METRE.getConverterTo(org.geotools.measure.Units.FOOT).convert(widthInMeters) |
| 125 | + } |
| 126 | + |
| 127 | + double scaleDenominator = map.scaleDenominator |
| 128 | + double pixelsPerMeter = RendererUtilities.calculatePixelsPerMeterRatio(scaleDenominator, [:]) |
| 129 | + String unitForScaleText |
| 130 | + double pixelsPerUnit |
| 131 | + if (units == ScaleBarItem.Units.US) { |
| 132 | + if (widthInUnits > 5280) { |
| 133 | + unitForScaleText = "miles" |
| 134 | + pixelsPerUnit = pixelsPerMeter / 0.000621371 |
| 135 | + } else { |
| 136 | + unitForScaleText = "feet" |
| 137 | + pixelsPerUnit = pixelsPerMeter / 3.28084 |
| 138 | + } |
| 139 | + } else { |
| 140 | + if (widthInUnits > 1000) { |
| 141 | + unitForScaleText = "km" |
| 142 | + pixelsPerUnit = pixelsPerMeter * 1000 |
| 143 | + } else { |
| 144 | + unitForScaleText = "m" |
| 145 | + pixelsPerUnit = pixelsPerMeter |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + int scaleBarWidthInPixels = width - border * 2 |
| 150 | + double scaleBarWidthInUnits = scaleBarWidthInPixels / pixelsPerUnit |
| 151 | + |
| 152 | + double orderOfMagnitude = Math.pow(10, Math.floor(Math.log10(scaleBarWidthInUnits))) |
| 153 | + double desiredScaleBarWidthInUnits = Math.round(scaleBarWidthInUnits / orderOfMagnitude) * orderOfMagnitude |
| 154 | + double desiredScaleBarWidthInPixels = Math.round(desiredScaleBarWidthInUnits * pixelsPerUnit) |
| 155 | + |
| 156 | + new ScaleBarInfo( |
| 157 | + widthInPixels: desiredScaleBarWidthInPixels, |
| 158 | + widthInUnits: desiredScaleBarWidthInUnits, |
| 159 | + unitForScaleText: unitForScaleText |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + static class ScaleBarInfo { |
| 164 | + |
| 165 | + double widthInPixels |
| 166 | + |
| 167 | + double widthInUnits |
| 168 | + |
| 169 | + String unitForScaleText |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments