| 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 dr.xml.*; |
| 29 |
import org.jdom.Element; |
| 30 |
|
| 31 |
import java.awt.geom.*; |
| 32 |
import java.util.List; |
| 33 |
import java.util.StringTokenizer; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class NewPolygon2D { |
| 41 |
|
| 42 |
public static final String POLYGON = "polygon"; |
| 43 |
public static final String CLOSED = "closed"; |
| 44 |
public static final String FILL_VALUE = "fillValue"; |
| 45 |
|
| 46 |
public NewPolygon2D(GeneralPath path) { |
| 47 |
this.path = path; |
| 48 |
} |
| 49 |
|
| 50 |
public NewPolygon2D(Element e) { |
| 51 |
|
| 52 |
List<Element> children = e.getChildren(); |
| 53 |
|
| 54 |
for (Element childElement : children) { |
| 55 |
|
| 56 |
if (childElement.getName().equals(KMLCoordinates.COORDINATES)) { |
| 57 |
|
| 58 |
String value = childElement.getTextTrim(); |
| 59 |
StringTokenizer st1 = new StringTokenizer(value, "\n"); |
| 60 |
int count = st1.countTokens(); |
| 61 |
|
| 62 |
|
| 63 |
this.path = new GeneralPath(); |
| 64 |
|
| 65 |
for (int i = 0; i < count; i++) { |
| 66 |
String line = st1.nextToken(); |
| 67 |
StringTokenizer st2 = new StringTokenizer(line, ","); |
| 68 |
if (st2.countTokens() != 3) |
| 69 |
throw new IllegalArgumentException("All KML coordinates must contain (X,Y,Z) values. Three dimensions not found in element '" + line + "'"); |
| 70 |
final float x = Float.valueOf(st2.nextToken()); |
| 71 |
final float y = Float.valueOf(st2.nextToken()); |
| 72 |
if (i == 0) { |
| 73 |
path.moveTo(x, y); |
| 74 |
} else { |
| 75 |
path.lineTo(x, y); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
break; |
| 81 |
|
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public NewPolygon2D() { |
| 87 |
path = new GeneralPath(); |
| 88 |
} |
| 89 |
|
| 90 |
public void moveTo(Point2D pt) { |
| 91 |
path.moveTo((float) pt.getX(), (float) pt.getY()); |
| 92 |
} |
| 93 |
|
| 94 |
public void lineTo(Point2D pt) { |
| 95 |
path.lineTo((float) pt.getX(), (float) pt.getY()); |
| 96 |
} |
| 97 |
|
| 98 |
public boolean contains(Point2D pt) { |
| 99 |
return path.contains(pt); |
| 100 |
} |
| 101 |
|
| 102 |
public void closePath() { |
| 103 |
path.closePath(); |
| 104 |
} |
| 105 |
|
| 106 |
public void setFillValue(double value) { |
| 107 |
fillValue = value; |
| 108 |
} |
| 109 |
|
| 110 |
public double getFillValue() { |
| 111 |
return fillValue; |
| 112 |
} |
| 113 |
|
| 114 |
public NewPolygon2D clip(Rectangle2D boundingBox) { |
| 115 |
Area thisArea = new Area(path); |
| 116 |
thisArea.intersect(new Area(boundingBox)); |
| 117 |
PathIterator iterator = thisArea.getPathIterator(null); |
| 118 |
double[] v = new double[2]; |
| 119 |
while (!iterator.isDone()) { |
| 120 |
int type = iterator.currentSegment(v); |
| 121 |
System.err.println(":" + v[0] + v[1] + "\n"); |
| 122 |
iterator.next(); |
| 123 |
} |
| 124 |
System.exit(-1); |
| 125 |
|
| 126 |
GeneralPath path = new GeneralPath(thisArea); |
| 127 |
path.closePath(); |
| 128 |
NewPolygon2D newPolygon = new NewPolygon2D(path); |
| 129 |
newPolygon.setFillValue(this.getFillValue()); |
| 130 |
return newPolygon; |
| 131 |
} |
| 132 |
|
| 133 |
public String toString() { |
| 134 |
StringBuffer sb = new StringBuffer(); |
| 135 |
sb.append("polygon("); |
| 136 |
sb.append(fillValue); |
| 137 |
sb.append(")[\n"); |
| 138 |
PathIterator iterator = path.getPathIterator(null); |
| 139 |
float[] values = new float[2]; |
| 140 |
while (!iterator.isDone()) { |
| 141 |
int type = iterator.currentSegment(values); |
| 142 |
Point2D pt = new Point2D.Double(values[0], values[1]); |
| 143 |
sb.append("\t"); |
| 144 |
sb.append(pt); |
| 145 |
sb.append("\n"); |
| 146 |
iterator.next(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
sb.append("]"); |
| 156 |
return sb.toString(); |
| 157 |
} |
| 158 |
|
| 159 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 160 |
|
| 161 |
public String getParserName() { |
| 162 |
return POLYGON; |
| 163 |
} |
| 164 |
|
| 165 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 166 |
|
| 167 |
KMLCoordinates coordinates = (KMLCoordinates) xo.getChild(KMLCoordinates.class); |
| 168 |
boolean closed = xo.getAttribute(CLOSED, false); |
| 169 |
|
| 170 |
if ((!closed && coordinates.length < 3) || |
| 171 |
(closed && coordinates.length < 4)) |
| 172 |
throw new XMLParseException("Insufficient points in polygon '" + xo.getId() + "' to define a polygon in 2D"); |
| 173 |
|
| 174 |
NewPolygon2D polygon = new NewPolygon2D(); |
| 175 |
polygon.moveTo(new Point2D.Double(coordinates.x[0], coordinates.y[0])); |
| 176 |
int length = coordinates.length; |
| 177 |
if (closed) |
| 178 |
length--; |
| 179 |
for (int i = 1; i < length; i++) |
| 180 |
polygon.lineTo(new Point2D.Double(coordinates.x[i], coordinates.y[i])); |
| 181 |
polygon.lineTo(new Point2D.Double(coordinates.x[0], coordinates.y[0])); |
| 182 |
|
| 183 |
|
| 184 |
polygon.setFillValue(xo.getAttribute(FILL_VALUE, 0.0)); |
| 185 |
|
| 186 |
return polygon; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public String getParserDescription() { |
| 194 |
return "This element represents a polygon."; |
| 195 |
} |
| 196 |
|
| 197 |
public Class getReturnType() { |
| 198 |
return Polygon2D.class; |
| 199 |
} |
| 200 |
|
| 201 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 202 |
return rules; |
| 203 |
} |
| 204 |
|
| 205 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 206 |
new ElementRule(KMLCoordinates.class), |
| 207 |
AttributeRule.newBooleanRule(CLOSED, true), |
| 208 |
AttributeRule.newDoubleRule(FILL_VALUE, true), |
| 209 |
}; |
| 210 |
}; |
| 211 |
|
| 212 |
public static void main(String[] args) { |
| 213 |
NewPolygon2D polygon = new NewPolygon2D(); |
| 214 |
polygon.moveTo(new Point2D.Double(-10, -10)); |
| 215 |
polygon.lineTo(new Point2D.Double(-10, 50)); |
| 216 |
polygon.lineTo(new Point2D.Double(10, 50)); |
| 217 |
polygon.lineTo(new Point2D.Double(10, -10)); |
| 218 |
polygon.lineTo(new Point2D.Double(-10, -10)); |
| 219 |
|
| 220 |
System.out.println(polygon); |
| 221 |
System.out.println(""); |
| 222 |
|
| 223 |
|
| 224 |
Point2D pt = new Point2D.Double(0, 0); |
| 225 |
System.out.println("polygon contains " + pt + ": " + polygon.contains(pt)); |
| 226 |
pt = new Point2D.Double(100, 100); |
| 227 |
System.out.println("polygon contains " + pt + ": " + polygon.contains(pt)); |
| 228 |
System.out.println(""); |
| 229 |
|
| 230 |
Rectangle2D boundingBox = new Rectangle2D.Double(0, 0, 100, 100); |
| 231 |
System.out.println(boundingBox); |
| 232 |
NewPolygon2D myClip = polygon.clip(boundingBox); |
| 233 |
System.out.println(myClip); |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
private double fillValue; |
| 238 |
private GeneralPath path; |
| 239 |
} |