| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo; |
| 27 |
|
| 28 |
import java.awt.*; |
| 29 |
import java.awt.geom.AffineTransform; |
| 30 |
import java.awt.geom.GeneralPath; |
| 31 |
import java.awt.geom.Rectangle2D; |
| 32 |
import java.awt.image.BufferedImage; |
| 33 |
import java.awt.image.Raster; |
| 34 |
import java.util.ArrayList; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class KMLRenderer implements Lattice { |
| 40 |
|
| 41 |
BufferedImage image; |
| 42 |
int[][] lattice; |
| 43 |
Rectangle2D bounds; |
| 44 |
|
| 45 |
java.util.List<AbstractPolygon2D> polygons; |
| 46 |
java.util.List<Shape> shapes; |
| 47 |
|
| 48 |
ViewTransform viewTransform; |
| 49 |
|
| 50 |
Color shapeColor; |
| 51 |
Color background; |
| 52 |
|
| 53 |
public KMLRenderer(String kmlFileName, Color shapeColor, Color background) { |
| 54 |
|
| 55 |
polygons = Polygon2D.readKMLFile(kmlFileName); |
| 56 |
|
| 57 |
this.shapeColor = shapeColor; |
| 58 |
this.background = background; |
| 59 |
|
| 60 |
System.out.println("Read " + polygons.size() + " polygons"); |
| 61 |
|
| 62 |
System.out.println("Converting polygons to shapes"); |
| 63 |
|
| 64 |
double minX = Double.MAX_VALUE, maxX = -Double.MAX_VALUE, minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE; |
| 65 |
|
| 66 |
shapes = new ArrayList<Shape>(); |
| 67 |
|
| 68 |
for (AbstractPolygon2D p : polygons) { |
| 69 |
|
| 70 |
Shape shape = p.getShape(); |
| 71 |
|
| 72 |
bounds = shape.getBounds(); |
| 73 |
|
| 74 |
if (bounds.getMinX() < minX) minX = bounds.getMinX(); |
| 75 |
if (bounds.getMaxX() > maxX) maxX = bounds.getMaxX(); |
| 76 |
if (bounds.getMinY() < minY) minY = bounds.getMinY(); |
| 77 |
if (bounds.getMaxY() > maxY) maxY = bounds.getMaxY(); |
| 78 |
|
| 79 |
shapes.add(shape); |
| 80 |
System.out.print("."); |
| 81 |
System.out.flush(); |
| 82 |
} |
| 83 |
|
| 84 |
bounds = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); |
| 85 |
|
| 86 |
System.out.println(); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public Rectangle2D getBounds() { |
| 91 |
return bounds; |
| 92 |
} |
| 93 |
|
| 94 |
public BufferedImage render(int size) { |
| 95 |
int width; |
| 96 |
int height; |
| 97 |
if (bounds.getHeight() > bounds.getWidth()) { |
| 98 |
height = size; |
| 99 |
width = (int) (height * bounds.getWidth() / bounds.getHeight()); |
| 100 |
} else { |
| 101 |
width = size; |
| 102 |
height = (int) (width * bounds.getHeight() / bounds.getWidth()); |
| 103 |
} |
| 104 |
return render(width, height); |
| 105 |
} |
| 106 |
|
| 107 |
public BufferedImage render(int width, int height) { |
| 108 |
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 109 |
render(image); |
| 110 |
|
| 111 |
Raster raster = image.getData(); |
| 112 |
|
| 113 |
lattice = new int[width][height]; |
| 114 |
int[] pixel = new int[4]; |
| 115 |
for (int i = 0; i < width; i++) { |
| 116 |
for (int j = 0; j < height; j++) { |
| 117 |
raster.getPixel(i, j, pixel); |
| 118 |
if (colorDistanceSquared(pixel, shapeColor) < colorDistanceSquared(pixel, background)) { |
| 119 |
lattice[i][j] = 1; |
| 120 |
} else { |
| 121 |
lattice[i][j] = 0; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return image; |
| 127 |
} |
| 128 |
|
| 129 |
private double colorDistanceSquared(int[] pixel, Color color) { |
| 130 |
|
| 131 |
double[] argb = new double[4]; |
| 132 |
|
| 133 |
argb[0] = Math.abs(pixel[0] - color.getAlpha()); |
| 134 |
argb[1] = Math.abs(pixel[1] - color.getRed()); |
| 135 |
argb[2] = Math.abs(pixel[2] - color.getGreen()); |
| 136 |
argb[3] = Math.abs(pixel[3] - color.getBlue()); |
| 137 |
|
| 138 |
double dist = 0; |
| 139 |
for (double a : argb) { |
| 140 |
dist += a * a; |
| 141 |
} |
| 142 |
return dist; |
| 143 |
} |
| 144 |
|
| 145 |
public void render(BufferedImage image) { |
| 146 |
Graphics2D g2d = image.createGraphics(); |
| 147 |
|
| 148 |
g2d.setColor(background); |
| 149 |
g2d.fillRect(0, 0, image.getWidth(), image.getHeight()); |
| 150 |
|
| 151 |
viewTransform = new ViewTransform(bounds, image.getWidth(), image.getHeight()); |
| 152 |
|
| 153 |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 154 |
|
| 155 |
g2d.setColor(shapeColor); |
| 156 |
AffineTransform transform = viewTransform.getTransform(); |
| 157 |
for (Shape s : shapes) { |
| 158 |
GeneralPath path = new GeneralPath(s); |
| 159 |
path.transform(transform); |
| 160 |
g2d.fill(path); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public int latticeWidth() { |
| 165 |
return lattice.length; |
| 166 |
} |
| 167 |
|
| 168 |
public int latticeHeight() { |
| 169 |
return lattice[0].length; |
| 170 |
} |
| 171 |
|
| 172 |
public void setState(int i, int j, int state) { |
| 173 |
lattice[i][j] = state; |
| 174 |
} |
| 175 |
|
| 176 |
public int getState(int i, int j) { |
| 177 |
return lattice[i][j]; |
| 178 |
} |
| 179 |
|
| 180 |
public void paintLattice(Graphics g) { |
| 181 |
g.drawImage(image, 0, 0, null); |
| 182 |
} |
| 183 |
|
| 184 |
public void setBounds(Rectangle2D bounds) { |
| 185 |
this.bounds = bounds; |
| 186 |
} |
| 187 |
} |