comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/GridCell.java @ 807:a645bd23c1c8

Added more javadoc. Removed trailing whitespace. gnv-artifacts/trunk@889 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 08 Apr 2010 15:24:45 +0000
parents d766fe2d917a
children 22c18083225e
comparison
equal deleted inserted replaced
806:2cea76f1112e 807:a645bd23c1c8
19 import java.util.List; 19 import java.util.List;
20 20
21 import org.apache.log4j.Logger; 21 import org.apache.log4j.Logger;
22 22
23 /** 23 /**
24 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> 24 * Spans a rectangle of points to be used in spatial indexing.
25 *
26 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
25 */ 27 */
26 public class GridCell 28 public class GridCell
27 implements Serializable 29 implements Serializable
28 { 30 {
29 private static Logger log = Logger.getLogger(GridCell.class); 31 private static Logger log = Logger.getLogger(GridCell.class);
30 32
33 /**
34 * Callback for {@link com.vividsolutions.jts.index.SpatialIndex}
35 * to find a GridCell which contains a given point.
36 */
31 public static final class CellFinder 37 public static final class CellFinder
32 implements ItemVisitor 38 implements ItemVisitor
33 { 39 {
40 /**
41 * Stores the found GridCell.
42 */
34 public GridCell found; 43 public GridCell found;
35 44
45 /**
46 * The query point.
47 */
36 protected Point point; 48 protected Point point;
37 49
50 /**
51 * Default constructor.
52 */
38 public CellFinder() { 53 public CellFinder() {
39 } 54 }
40 55
56 /**
57 * Prepares a spatial index lookup.
58 * @param center The query point.
59 */
41 public void prepare(Coordinate center) { 60 public void prepare(Coordinate center) {
42 found = null; 61 found = null;
43 point = GEOMETRY_FACTORY.createPoint(center); 62 point = GEOMETRY_FACTORY.createPoint(center);
44 } 63 }
45 64
65 /**
66 * Called by the spatial index as a 2nd order filter.
67 * @param item The GridCell to test.
68 */
46 public void visitItem(Object item) { 69 public void visitItem(Object item) {
47 if (found == null) { 70 if (found == null) {
48 GridCell cell = (GridCell)item; 71 GridCell cell = (GridCell)item;
49 if (cell.contains(point)) { 72 if (cell.contains(point)) {
50 found = cell; 73 found = cell;
51 } 74 }
52 } 75 }
53 } 76 }
54 } // class CellFinder 77 } // class CellFinder
55 78
79 /**
80 * The first point.
81 */
56 public Point2d p1; 82 public Point2d p1;
83 /**
84 * The second point.
85 */
57 public Point2d p2; 86 public Point2d p2;
87 /**
88 * The third point.
89 */
58 public Point2d p3; 90 public Point2d p3;
91 /**
92 * The fourth point.
93 */
59 public Point2d p4; 94 public Point2d p4;
60 95
96 /**
97 * Polygon created from the four points.
98 */
61 protected Polygon polygon; 99 protected Polygon polygon;
62 100
101 /**
102 * Geometry factory to create the query points and the polygons.
103 */
63 public static final GeometryFactory GEOMETRY_FACTORY 104 public static final GeometryFactory GEOMETRY_FACTORY
64 = new GeometryFactory(); 105 = new GeometryFactory();
65 106
107 /**
108 * Default constructor.
109 */
66 public GridCell() { 110 public GridCell() {
67 } 111 }
68 112
113 /**
114 * Constructor to create a GridCell out of four given points..
115 * @param p1 The first point.
116 * @param p2 The second point.
117 * @param p3 The thrid point.
118 * @param p4 The fourth point.
119 */
69 public GridCell(Point2d p1, Point2d p2, Point2d p3, Point2d p4) { 120 public GridCell(Point2d p1, Point2d p2, Point2d p3, Point2d p4) {
70 this.p1 = p1; 121 this.p1 = p1;
71 this.p2 = p2; 122 this.p2 = p2;
72 this.p3 = p3; 123 this.p3 = p3;
73 this.p4 = p4; 124 this.p4 = p4;
74 createPolygon(); 125 createPolygon();
75 } 126 }
76 127
128 /**
129 * Creates the polygon from the four points.
130 */
77 protected void createPolygon() { 131 protected void createPolygon() {
78 Coordinate [] coords = new Coordinate [] { p1, p2, p3, p4, p1 }; 132 Coordinate [] coords = new Coordinate [] { p1, p2, p3, p4, p1 };
79 if (!CGAlgorithms.isCCW(coords)) { 133 if (!CGAlgorithms.isCCW(coords)) {
80 for (int i = 0, j = coords.length-1; i < j; ++i, --j) { 134 for (int i = 0, j = coords.length-1; i < j; ++i, --j) {
81 Coordinate c = coords[i]; 135 Coordinate c = coords[i];
91 } 145 }
92 146
93 polygon = GEOMETRY_FACTORY.createPolygon(shell, null); 147 polygon = GEOMETRY_FACTORY.createPolygon(shell, null);
94 } 148 }
95 149
150 /**
151 * Returns the envelope of the four point polygon.
152 * @return
153 */
96 public Envelope getEnvelope() { 154 public Envelope getEnvelope() {
97 return polygon.getEnvelopeInternal(); 155 return polygon.getEnvelopeInternal();
98 } 156 }
99 157
158 /**
159 * Test if a given point is inside this grid cell.
160 * @param coord
161 * @return
162 */
100 public boolean contains(Geometry coord) { 163 public boolean contains(Geometry coord) {
101 return polygon.contains(coord); 164 return polygon.contains(coord);
102 } 165 }
103 166
167 /**
168 * Converts a list of points to a list of grid cells.
169 * @param points This list of points.
170 * @return This list of grid cells.
171 */
104 public static List<GridCell> pointsToGridCells( 172 public static List<GridCell> pointsToGridCells(
105 List<? extends Point2d> points 173 List<? extends Point2d> points
106 ) { 174 ) {
107 return pointsToGridCells(points, null); 175 return pointsToGridCells(points, null);
108 } 176 }
109 177
178 /**
179 * Converts a list of points to a list of grid cells.
180 * Points that are not in a relevant area are ignored.
181 * @param points The list of points.
182 * @param relevantArea The relevant area.
183 * @return The list of grid cells.
184 */
110 public static List<GridCell> pointsToGridCells( 185 public static List<GridCell> pointsToGridCells(
111 List<? extends Point2d> points, 186 List<? extends Point2d> points,
112 Envelope relevantArea 187 Envelope relevantArea
113 ) { 188 ) {
114 return pointsToGridCells(points, relevantArea, 0); 189 return pointsToGridCells(points, relevantArea, 0);
123 { +1, 0 }, // 5 198 { +1, 0 }, // 5
124 { +1, -1 }, // 6 199 { +1, -1 }, // 6
125 { 0, -1 } // 7 200 { 0, -1 } // 7
126 }; 201 };
127 202
203 /**
204 * Generate points by extrapolating border points.
205 * @param rows (i, j) indexed map of points.
206 * @param minI min known i.
207 * @param maxI max known i.
208 * @param minJ min known j.
209 * @param maxJ max known j.
210 * @param rounds Deternine how many extra rings should be generated.
211 * @param relevantArea The relevant area.
212 * @return number of newly generated points.
213 */
128 public static int extrapolate( 214 public static int extrapolate(
129 HashMap<Integer, HashMap<Integer, Point2d>> rows, 215 HashMap<Integer, HashMap<Integer, Point2d>> rows,
130 int minI, int maxI, 216 int minI, int maxI,
131 int minJ, int maxJ, 217 int minJ, int maxJ,
132 int rounds, 218 int rounds,
210 } // for all rounds 296 } // for all rounds
211 297
212 return total; 298 return total;
213 } 299 }
214 300
301 /**
302 * Converts a list of points to a list of grid cells.
303 * @param points The list of points.
304 * @param relevantArea The relevant area. If a point is
305 * not inside this area it is ignored during the build process.
306 * @param extrapolationRounds Number of extra point rings.
307 * 0 = no extrpolation.
308 * @return The list of grid cells.
309 */
215 public static List<GridCell> pointsToGridCells( 310 public static List<GridCell> pointsToGridCells(
216 List<? extends Point2d> points, 311 List<? extends Point2d> points,
217 Envelope relevantArea, 312 Envelope relevantArea,
218 int extrapolationRounds 313 int extrapolationRounds
219 ) { 314 ) {

http://dive4elements.wald.intevation.org