|
| 1 | +package javaxt.cartography; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import java.awt.image.*; |
| 6 | +import java.awt.*; |
| 7 | +import javax.imageio.*; |
| 8 | +import javax.imageio.stream.*; |
| 9 | +import javaxt.geospatial.geometry.Point; |
| 10 | + |
| 11 | +//****************************************************************************** |
| 12 | +//** CreateMap Class - By Peter Borissow |
| 13 | +//****************************************************************************** |
| 14 | +/** |
| 15 | + * Enter class description |
| 16 | + * |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +public class RasterMap { |
| 20 | + |
| 21 | + private double ULx = 0; |
| 22 | + private double ULy = 0; |
| 23 | + private double resX = 1; |
| 24 | + private double resY = 1; |
| 25 | + |
| 26 | + |
| 27 | + private BufferedImage bufferedImage = null; |
| 28 | + Graphics2D g2d = null; |
| 29 | + |
| 30 | + //************************************************************************** |
| 31 | + //** Creates a new instance of CreateMap |
| 32 | + //************************************************************************** |
| 33 | + |
| 34 | + public RasterMap(double minX, double minY, |
| 35 | + double maxX, double maxY, |
| 36 | + int width, int height){ |
| 37 | + |
| 38 | + //Create BufferedImage |
| 39 | + this.bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| 40 | + |
| 41 | + //Initialize Variables |
| 42 | + init(minX, minY, maxX, maxY, bufferedImage); |
| 43 | + |
| 44 | + //Set default background and line color |
| 45 | + g2d.setColor(Color.WHITE); |
| 46 | + g2d.fillRect(0,0,width,height); |
| 47 | + g2d.setColor(Color.BLACK); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + //************************************************************************** |
| 52 | + //** Creates a new instance of CreateMap |
| 53 | + //************************************************************************** |
| 54 | + |
| 55 | + public RasterMap(double minX, double minY, |
| 56 | + double maxX, double maxY, |
| 57 | + BufferedImage bufferedImage){ |
| 58 | + |
| 59 | + this.bufferedImage = bufferedImage; |
| 60 | + |
| 61 | + //Initialize Variables |
| 62 | + init(minX, minY, maxX, maxY, bufferedImage); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + //************************************************************************** |
| 68 | + //** Init |
| 69 | + //************************************************************************** |
| 70 | + |
| 71 | + private void init(double minX, double minY, double maxX, double maxY, |
| 72 | + BufferedImage bufferedImage){ |
| 73 | + |
| 74 | + //Validate Coordinates |
| 75 | + if (validate(minX, minY, maxX, maxY)==false) return; //error? |
| 76 | + |
| 77 | + //Get Width/Height |
| 78 | + int width = bufferedImage.getWidth(); |
| 79 | + int height = bufferedImage.getHeight(); |
| 80 | + |
| 81 | + //Update min/max coordinates |
| 82 | + minX = x(minX); |
| 83 | + minY = y(minY); |
| 84 | + maxX = x(maxX); |
| 85 | + maxY = y(maxY); |
| 86 | + |
| 87 | + |
| 88 | + //Update Local Variables using updated values |
| 89 | + this.ULx = minX; |
| 90 | + this.ULy = maxY; |
| 91 | + |
| 92 | + |
| 93 | + //Compute pixelsPerDeg |
| 94 | + this.resX = ((double) width) / (maxX-minX); |
| 95 | + this.resY = ((double) height) / (minY-maxY);//(maxY-minY); |
| 96 | + //System.out.println("pixelsPerDeg = " + resX + ", " + resY); |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + //Instantiate 2D Drawing Class |
| 102 | + this.g2d = bufferedImage.createGraphics(); |
| 103 | + g2d.setColor(Color.BLACK); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + //************************************************************************** |
| 108 | + //** Validate |
| 109 | + //************************************************************************** |
| 110 | + /** Used to validate coordinates used to invoke this class */ |
| 111 | + |
| 112 | + private boolean validate(double minX, double minY, double maxX, double maxY){ |
| 113 | + if (minX > maxX || minY > maxY) return false; |
| 114 | + if (minX < -180 || maxX < -180 || maxX > 180 || minX > 180) return false; |
| 115 | + if (minY < -90 || maxY < -90 || maxY > 90 || minY > 90) return false; |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + //************************************************************************** |
| 120 | + //** X |
| 121 | + //************************************************************************** |
| 122 | + /** Used to convert longitude to pixel coordinates */ |
| 123 | + |
| 124 | + private double x(double pt){ |
| 125 | + pt += 180; |
| 126 | + double x = (pt - ULx) * resX; |
| 127 | + //System.out.println("X = " + x); |
| 128 | + return x; |
| 129 | + } |
| 130 | + |
| 131 | + private double x(String pt){ |
| 132 | + return x(cdbl(pt)); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + //************************************************************************** |
| 137 | + //** Y |
| 138 | + //************************************************************************** |
| 139 | + /** Used to convert latitude to pixel coordinates */ |
| 140 | + |
| 141 | + private double y(double pt){ |
| 142 | + |
| 143 | + //System.out.print("Y = " + pt + " ("); |
| 144 | + pt = -pt; |
| 145 | + if (pt<=0) pt = 90 + -pt; |
| 146 | + else pt = 90 - pt; |
| 147 | + |
| 148 | + pt = 180-pt; |
| 149 | + //System.out.print(pt + ")" + " ("); |
| 150 | + |
| 151 | + |
| 152 | + double y = (pt - ULy) * resY; |
| 153 | + |
| 154 | + if (cint(y)==0 || cint(y)==-0) y = 0; |
| 155 | + //else y = -y; |
| 156 | + |
| 157 | + //System.out.println(y + ")"); |
| 158 | + return y; |
| 159 | + } |
| 160 | + |
| 161 | + private double y(String pt){ |
| 162 | + return y(cdbl(pt)); |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + public void AddGraticule(double SpacingInDegrees){ |
| 167 | + |
| 168 | + int spacing = 15; //degrees |
| 169 | + |
| 170 | + for (int i=0; i<=360; i++){ |
| 171 | + int x = -(180-i); |
| 172 | + Insert(x + ",90 " + x + ",-90",null); |
| 173 | + i+=spacing-1; |
| 174 | + } |
| 175 | + |
| 176 | + for (int i=0; i<=90; i++){ |
| 177 | + int y = i; |
| 178 | + Insert("-180," + y + " 180," + y,null); |
| 179 | + Insert("-180," + -y + " 180," + -y,null); |
| 180 | + i+=spacing-1; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + //************************************************************************** |
| 185 | + //** Insert |
| 186 | + //************************************************************************** |
| 187 | + /** Used to insert a "feature" into the map */ |
| 188 | + |
| 189 | + public void Insert(Point[] Coordinates, Style style){ |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + //************************************************************************** |
| 194 | + //** Insert |
| 195 | + //************************************************************************** |
| 196 | + /** Used to insert a "feature" into the map */ |
| 197 | + public void Insert(String Coordinates, Style style){ |
| 198 | + String cs = ","; |
| 199 | + String ts = " "; |
| 200 | + Insert(Coordinates, style, cs, ts); |
| 201 | + } |
| 202 | + |
| 203 | + public void Insert(String Coordinates, Style style, String cs, String ts){ |
| 204 | + if (Coordinates==null) return; |
| 205 | + |
| 206 | + Coordinates = Coordinates.trim(); |
| 207 | + |
| 208 | + String[] coords = Coordinates.split(ts); |
| 209 | + |
| 210 | + int numPoints = coords.length; |
| 211 | + int[] x = new int[numPoints]; |
| 212 | + int[] y = new int[numPoints]; |
| 213 | + for (int i=0; i<numPoints; i++){ |
| 214 | + String[] pt = coords[i].split(cs); |
| 215 | + x[i] = cint(x(pt[0])); |
| 216 | + y[i] = cint(y(pt[1])); |
| 217 | + } |
| 218 | + |
| 219 | + if (coords.length==1){ |
| 220 | + g2d.fillOval(cint(x(x[0])), cint(y(y[0])), 5, 5); |
| 221 | + } |
| 222 | + else{ |
| 223 | + if (coords[0].equals(coords[coords.length-1])){ |
| 224 | + g2d.drawPolygon(x, y, numPoints); |
| 225 | + } |
| 226 | + else{ |
| 227 | + g2d.drawPolyline(x, y, numPoints); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + //************************************************************************** |
| 234 | + //** getBufferedImage |
| 235 | + //************************************************************************** |
| 236 | + |
| 237 | + public BufferedImage getBufferedImage(double minX, double minY, |
| 238 | + double maxX, double maxY, |
| 239 | + int width, int height){ |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | + minX = x(minX); |
| 244 | + minY = y(minY); |
| 245 | + maxX = x(maxX); |
| 246 | + maxY = y(maxY); |
| 247 | + |
| 248 | + int x = cint(minX); |
| 249 | + int y = cint(maxY); |
| 250 | + int w = cint(maxX)-x; |
| 251 | + int h = cint(minY)-y; |
| 252 | + |
| 253 | + //Crop the Map |
| 254 | + BufferedImage croppedImage = bufferedImage.getSubimage(x,y,w,h); |
| 255 | + |
| 256 | + return croppedImage; |
| 257 | +/* |
| 258 | + //Resize Cropped Map |
| 259 | + java.awt.Image scaledImage = croppedImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); |
| 260 | + BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 261 | + Graphics2D g2d = outputImage.createGraphics(); |
| 262 | + g2d.drawImage(scaledImage, 0, 0, null); |
| 263 | + g2d.dispose(); |
| 264 | + |
| 265 | + //Return Output |
| 266 | + return outputImage; |
| 267 | + */ |
| 268 | + } |
| 269 | + |
| 270 | + //************************************************************************** |
| 271 | + //** Save |
| 272 | + //************************************************************************** |
| 273 | + /** Used to save the map to an image file */ |
| 274 | + |
| 275 | + public void Save(String PathToFile){ |
| 276 | + try{ |
| 277 | + //g2d.dispose(); |
| 278 | + |
| 279 | + File File = new File(PathToFile); |
| 280 | + File.getParentFile().mkdirs(); |
| 281 | + String FileName = File.getName(); |
| 282 | + String FileExt = FileName.substring(FileName.lastIndexOf(".")+1,FileName.length()); |
| 283 | + RenderedImage rendImage = bufferedImage; |
| 284 | + ImageIO.write(rendImage,FileExt,File); |
| 285 | + } |
| 286 | + catch (Exception e){ |
| 287 | + //System.out.println(e.toString()); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + |
| 292 | + public Style getStyle(){ |
| 293 | + return new Style(g2d); |
| 294 | + } |
| 295 | + |
| 296 | + public BufferedImage getImage(){ |
| 297 | + return bufferedImage; |
| 298 | + } |
| 299 | + |
| 300 | + |
| 301 | + //************************************************************************** |
| 302 | + //** String Conversions |
| 303 | + //************************************************************************** |
| 304 | + private int cint(String str){return Integer.valueOf(str).intValue(); } |
| 305 | + private int cint(Double d){ return (int)Math.round(d); } |
| 306 | + private double cdbl(String str){return Double.valueOf(str).doubleValue(); } |
| 307 | +} |
0 commit comments