comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/GridCell.java @ 514:d9d933e06875

Fixed gnv/issue153 gnv-artifacts/trunk@608 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 18:22:11 +0000
parents
children 96a1e92e7ed2
comparison
equal deleted inserted replaced
513:ca5048e4e515 514:d9d933e06875
1 package de.intevation.gnv.math;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.Envelope;
5 import com.vividsolutions.jts.geom.Geometry;
6 import com.vividsolutions.jts.geom.GeometryFactory;
7 import com.vividsolutions.jts.geom.LinearRing;
8 import com.vividsolutions.jts.geom.Point;
9 import com.vividsolutions.jts.geom.Polygon;
10
11 import com.vividsolutions.jts.index.ItemVisitor;
12
13 import java.io.Serializable;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18
19 /**
20 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
21 */
22 public class GridCell
23 implements Serializable
24 {
25 public static final class CellFinder
26 implements ItemVisitor
27 {
28 public GridCell found;
29
30 protected Point point;
31
32 public CellFinder() {
33 }
34
35 public void prepare(Coordinate center) {
36 found = null;
37 point = GEOMETRY_FACTORY.createPoint(center);
38 }
39
40 public void visitItem(Object item) {
41 if (found == null) {
42 GridCell cell = (GridCell)item;
43 if (cell.contains(point)) {
44 found = cell;
45 }
46 }
47 }
48 } // class CellFinder
49
50 public Point2d p1;
51 public Point2d p2;
52 public Point2d p3;
53 public Point2d p4;
54
55 protected Polygon polygon;
56
57 public static final GeometryFactory GEOMETRY_FACTORY
58 = new GeometryFactory();
59
60 public GridCell() {
61 }
62
63 public GridCell(Point2d p1, Point2d p2, Point2d p3, Point2d p4) {
64 this.p1 = p1;
65 this.p2 = p2;
66 this.p3 = p3;
67 this.p4 = p4;
68 createPolygon();
69 }
70
71 protected void createPolygon() {
72 LinearRing shell = GEOMETRY_FACTORY
73 .createLinearRing(new Coordinate [] { p1, p2, p3, p4, p1 });
74 polygon = GEOMETRY_FACTORY.createPolygon(shell, null);
75 }
76
77 public Envelope getEnvelope() {
78 return polygon.getEnvelopeInternal();
79 }
80
81 public boolean contains(Geometry coord) {
82 return polygon.contains(coord);
83 }
84
85 public static List<GridCell> pointsToGridCells(
86 List<? extends Point2d> points
87 ) {
88 int minI = Integer.MAX_VALUE;
89 int maxI = Integer.MIN_VALUE;
90 int minJ = Integer.MAX_VALUE;
91 int maxJ = Integer.MIN_VALUE;
92
93 HashMap<Integer, HashMap<Integer, Point2d>> rows =
94 new HashMap<Integer, HashMap<Integer, Point2d>>();
95
96 for (Point2d p: points) {
97
98 if (p.i < minI) minI = p.i;
99 if (p.i > maxI) maxI = p.i;
100 if (p.j < minJ) minJ = p.j;
101 if (p.j > maxJ) maxJ = p.j;
102
103 HashMap<Integer, Point2d> row = rows.get(p.i);
104
105 if (row == null) {
106 rows.put(p.i, row = new HashMap<Integer, Point2d>());
107 }
108
109 row.put(p.j, p);
110 }
111
112 ArrayList<GridCell> cells = new ArrayList<GridCell>(points.size());
113
114 for (int i = minI + 1; i <= maxI; ++i) {
115 HashMap<Integer, Point2d> row1 = rows.get(i-1);
116 HashMap<Integer, Point2d> row2 = rows.get(i);
117 if (row1 != null && row2 != null) {
118 for (int j = minJ + 1; j < maxJ; ++j) {
119 Point2d p1 = row1.get(j-1);
120 Point2d p2 = row1.get(j);
121 Point2d p3 = row2.get(j);
122 Point2d p4 = row2.get(j-1);
123
124 if (p1 != null && p2 != null && p3 != null && p4 != null) {
125 cells.add(new GridCell(p1, p2, p3, p4));
126 }
127 } // for all columns in row
128 }
129 } // for all rows
130
131 return cells;
132 }
133 }
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org