Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/GridCell.java @ 540:80630520e25a
merged gnv-artifacts/0.4
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:49 +0200 |
parents | 4e347624ee7c |
children | b248531fa20b |
comparison
equal
deleted
inserted
replaced
415:9f4a0b990d27 | 540:80630520e25a |
---|---|
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 import org.apache.log4j.Logger; | |
20 | |
21 /** | |
22 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | |
23 */ | |
24 public class GridCell | |
25 implements Serializable | |
26 { | |
27 private static Logger log = Logger.getLogger(GridCell.class); | |
28 | |
29 public static final class CellFinder | |
30 implements ItemVisitor | |
31 { | |
32 public GridCell found; | |
33 | |
34 protected Point point; | |
35 | |
36 public CellFinder() { | |
37 } | |
38 | |
39 public void prepare(Coordinate center) { | |
40 found = null; | |
41 point = GEOMETRY_FACTORY.createPoint(center); | |
42 } | |
43 | |
44 public void visitItem(Object item) { | |
45 if (found == null) { | |
46 GridCell cell = (GridCell)item; | |
47 if (cell.contains(point)) { | |
48 found = cell; | |
49 } | |
50 } | |
51 } | |
52 } // class CellFinder | |
53 | |
54 public Point2d p1; | |
55 public Point2d p2; | |
56 public Point2d p3; | |
57 public Point2d p4; | |
58 | |
59 protected Polygon polygon; | |
60 | |
61 public static final GeometryFactory GEOMETRY_FACTORY | |
62 = new GeometryFactory(); | |
63 | |
64 public GridCell() { | |
65 } | |
66 | |
67 public GridCell(Point2d p1, Point2d p2, Point2d p3, Point2d p4) { | |
68 this.p1 = p1; | |
69 this.p2 = p2; | |
70 this.p3 = p3; | |
71 this.p4 = p4; | |
72 createPolygon(); | |
73 } | |
74 | |
75 protected void createPolygon() { | |
76 LinearRing shell = GEOMETRY_FACTORY | |
77 .createLinearRing(new Coordinate [] { p1, p2, p3, p4, p1 }); | |
78 polygon = GEOMETRY_FACTORY.createPolygon(shell, null); | |
79 } | |
80 | |
81 public Envelope getEnvelope() { | |
82 return polygon.getEnvelopeInternal(); | |
83 } | |
84 | |
85 public boolean contains(Geometry coord) { | |
86 return polygon.contains(coord); | |
87 } | |
88 | |
89 public static List<GridCell> pointsToGridCells( | |
90 List<? extends Point2d> points | |
91 ) { | |
92 return pointsToGridCells(points, null); | |
93 } | |
94 | |
95 public static List<GridCell> pointsToGridCells( | |
96 List<? extends Point2d> points, | |
97 Envelope relevantArea | |
98 ) { | |
99 int minI = Integer.MAX_VALUE; | |
100 int maxI = Integer.MIN_VALUE; | |
101 int minJ = Integer.MAX_VALUE; | |
102 int maxJ = Integer.MIN_VALUE; | |
103 | |
104 HashMap<Integer, HashMap<Integer, Point2d>> rows = | |
105 new HashMap<Integer, HashMap<Integer, Point2d>>(); | |
106 | |
107 int culled = 0; | |
108 | |
109 for (Point2d p: points) { | |
110 | |
111 if (relevantArea != null && !relevantArea.contains(p.x, p.y)) { | |
112 ++culled; | |
113 continue; | |
114 } | |
115 | |
116 if (p.i < minI) minI = p.i; | |
117 if (p.i > maxI) maxI = p.i; | |
118 if (p.j < minJ) minJ = p.j; | |
119 if (p.j > maxJ) maxJ = p.j; | |
120 | |
121 HashMap<Integer, Point2d> row = rows.get(p.i); | |
122 | |
123 if (row == null) { | |
124 rows.put(p.i, row = new HashMap<Integer, Point2d>()); | |
125 } | |
126 | |
127 row.put(p.j, p); | |
128 } | |
129 | |
130 ArrayList<GridCell> cells = new ArrayList<GridCell>(points.size()); | |
131 | |
132 for (int i = minI + 1; i <= maxI; ++i) { | |
133 HashMap<Integer, Point2d> row1 = rows.get(i-1); | |
134 HashMap<Integer, Point2d> row2 = rows.get(i); | |
135 if (row1 != null && row2 != null) { | |
136 for (int j = minJ + 1; j < maxJ; ++j) { | |
137 Point2d p1 = row1.get(j-1); | |
138 Point2d p2 = row1.get(j); | |
139 Point2d p3 = row2.get(j); | |
140 Point2d p4 = row2.get(j-1); | |
141 | |
142 if (p1 != null && p2 != null && p3 != null && p4 != null) { | |
143 cells.add(new GridCell(p1, p2, p3, p4)); | |
144 } | |
145 } // for all columns in row | |
146 } | |
147 } // for all rows | |
148 | |
149 if (log.isDebugEnabled()) { | |
150 log.debug("culled points: " + culled); | |
151 log.debug("min/max i: " + minI + " / " + maxI); | |
152 log.debug("min/max j: " + minJ + " / " + maxJ); | |
153 log.debug("cells found: " + cells.size()); | |
154 } | |
155 | |
156 return cells; | |
157 } | |
158 } | |
159 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |