root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / randomVector / RandomGeometryUtilities.java @ 59
History | View | Annotate | Download (8.05 KB)
1 |
/*
|
---|---|
2 |
* OrbisGIS is a GIS application dedicated to scientific spatial simulation.
|
3 |
* This cross-platform GIS is developed at French IRSTV institute and is able
|
4 |
* to manipulate and create vector and raster spatial information. OrbisGIS
|
5 |
* is distributed under GPL 3 license. It is produced by the geo-informatic team of
|
6 |
* the IRSTV Institute <http://www.irstv.cnrs.fr/>, CNRS FR 2488:
|
7 |
* Erwan BOCHER, scientific researcher,
|
8 |
* Thomas LEDUC, scientific researcher,
|
9 |
* Fernando GONZALEZ CORTES, computer engineer.
|
10 |
*
|
11 |
* Copyright (C) 2007 Erwan BOCHER, Fernando GONZALEZ CORTES, Thomas LEDUC
|
12 |
*
|
13 |
* This file is part of OrbisGIS.
|
14 |
*
|
15 |
* OrbisGIS is free software: you can redistribute it and/or modify
|
16 |
* it under the terms of the GNU General Public License as published by
|
17 |
* the Free Software Foundation, either version 3 of the License, or
|
18 |
* (at your option) any later version.
|
19 |
*
|
20 |
* OrbisGIS is distributed in the hope that it will be useful,
|
21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23 |
* GNU General Public License for more details.
|
24 |
*
|
25 |
* You should have received a copy of the GNU General Public License
|
26 |
* along with OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
|
27 |
*
|
28 |
* For more information, please consult:
|
29 |
* <http://orbisgis.cerma.archi.fr/>
|
30 |
* <http://sourcesup.cru.fr/projects/orbisgis/>
|
31 |
*
|
32 |
* or contact directly:
|
33 |
* erwan.bocher _at_ ec-nantes.fr
|
34 |
* fergonco _at_ gmail.com
|
35 |
* thomas.leduc _at_ cerma.archi.fr
|
36 |
*/
|
37 |
package es.unex.sextante.vectorTools.randomVector; |
38 |
|
39 |
import java.util.Random; |
40 |
import java.util.SortedSet; |
41 |
import java.util.TreeSet; |
42 |
|
43 |
import com.vividsolutions.jts.geom.Coordinate; |
44 |
import com.vividsolutions.jts.geom.CoordinateList; |
45 |
import com.vividsolutions.jts.geom.Envelope; |
46 |
import com.vividsolutions.jts.geom.Geometry; |
47 |
import com.vividsolutions.jts.geom.GeometryFactory; |
48 |
import com.vividsolutions.jts.geom.LineSegment; |
49 |
import com.vividsolutions.jts.geom.LineString; |
50 |
import com.vividsolutions.jts.geom.LinearRing; |
51 |
import com.vividsolutions.jts.geom.Point; |
52 |
import com.vividsolutions.jts.geom.Polygon; |
53 |
|
54 |
public class RandomGeometryUtilities { |
55 |
private static final Random RAND = new Random(); |
56 |
private static final GeometryFactory gf = new GeometryFactory(); |
57 |
private final int maxNodesPerLineMinus3; |
58 |
private final int maxHolesPerPolygon; |
59 |
|
60 |
|
61 |
public RandomGeometryUtilities() {
|
62 |
this(12, 4); |
63 |
} |
64 |
|
65 |
|
66 |
public RandomGeometryUtilities(final int maxNodesPerLineMinus3, |
67 |
final int maxHolesPerPolygon) { |
68 |
this.maxNodesPerLineMinus3 = maxNodesPerLineMinus3;
|
69 |
this.maxHolesPerPolygon = maxHolesPerPolygon;
|
70 |
} |
71 |
|
72 |
|
73 |
private int getNumberOfNodesPerLine() { |
74 |
// In a LinearRing number of points must be 0 or >3
|
75 |
return RAND.nextInt(maxNodesPerLineMinus3) + 3; |
76 |
} |
77 |
|
78 |
|
79 |
private int getNumberOfHolesPerPolygon() { |
80 |
return RAND.nextInt(maxHolesPerPolygon);
|
81 |
} |
82 |
|
83 |
|
84 |
public Coordinate nextCoordinate() {
|
85 |
return new Coordinate(RAND.nextLong(), RAND.nextLong(), RAND.nextLong()); |
86 |
} |
87 |
|
88 |
|
89 |
public Coordinate nextCoordinate(final Envelope envelope) { |
90 |
return new Coordinate(RAND.nextInt((int) envelope.getWidth()) + envelope.getMinX(), |
91 |
RAND.nextInt((int) envelope.getHeight()) + envelope.getMinY(), RAND.nextLong());
|
92 |
} |
93 |
|
94 |
|
95 |
public Coordinate[] nextCoordinates(final int n) { |
96 |
final Coordinate[] result = new Coordinate[n]; |
97 |
for (int i = 0; i < n; i++) { |
98 |
result[i] = nextCoordinate(); |
99 |
} |
100 |
return result;
|
101 |
} |
102 |
|
103 |
|
104 |
public Point nextPoint() { |
105 |
return gf.createPoint(nextCoordinate());
|
106 |
} |
107 |
|
108 |
|
109 |
public Point nextPoint(final Envelope envelope) { |
110 |
return gf.createPoint(nextCoordinate(envelope));
|
111 |
} |
112 |
|
113 |
|
114 |
public Point[] nextPoints(final int n) { |
115 |
final Point[] result = new Point[n]; |
116 |
for (int i = 0; i < n; i++) { |
117 |
result[i] = nextPoint(); |
118 |
} |
119 |
return result;
|
120 |
} |
121 |
|
122 |
|
123 |
public LineSegment nextLineSegment() {
|
124 |
return new LineSegment(nextCoordinate(), nextCoordinate()); |
125 |
} |
126 |
|
127 |
|
128 |
public LineSegment[] nextLineSegments(final int n) { |
129 |
final LineSegment[] result = new LineSegment[n]; |
130 |
for (int i = 0; i < n; i++) { |
131 |
result[i] = nextLineSegment(); |
132 |
} |
133 |
return result;
|
134 |
} |
135 |
|
136 |
|
137 |
public LineString nextLineString() {
|
138 |
final int n = getNumberOfNodesPerLine(); |
139 |
final SortedSet<Coordinate> nodes = new TreeSet<Coordinate>(); |
140 |
while (n > nodes.size()) {
|
141 |
nodes.add(nextCoordinate()); |
142 |
} |
143 |
return gf.createLineString(nodes.toArray(new Coordinate[0])); |
144 |
} |
145 |
|
146 |
|
147 |
public LineString nextLineString(final Envelope envelope) { |
148 |
final int n = getNumberOfNodesPerLine(); |
149 |
final SortedSet<Coordinate> nodes = new TreeSet<Coordinate>(); |
150 |
while (n > nodes.size()) {
|
151 |
nodes.add(nextCoordinate(envelope)); |
152 |
} |
153 |
return gf.createLineString(nodes.toArray(new Coordinate[0])); |
154 |
} |
155 |
|
156 |
|
157 |
public LineString[] nextLineStrings(final int n) { |
158 |
final LineString[] result = new LineString[n]; |
159 |
for (int i = 0; i < n; i++) { |
160 |
result[i] = nextLineString(); |
161 |
} |
162 |
return result;
|
163 |
} |
164 |
|
165 |
|
166 |
public LinearRing nextLinearRing() {
|
167 |
LinearRing result; |
168 |
do {
|
169 |
final CoordinateList cl = new CoordinateList(nextLineString().getCoordinates()); |
170 |
cl.closeRing(); |
171 |
result = gf.createLinearRing(cl.toCoordinateArray()); |
172 |
} |
173 |
while (!result.isValid());
|
174 |
return result;
|
175 |
} |
176 |
|
177 |
|
178 |
public LinearRing nextLinearRing(final Envelope envelope) { |
179 |
LinearRing result; |
180 |
do {
|
181 |
final CoordinateList cl = new CoordinateList(nextLineString(envelope).getCoordinates()); |
182 |
cl.closeRing(); |
183 |
result = gf.createLinearRing(cl.toCoordinateArray()); |
184 |
} |
185 |
while (!result.isValid());
|
186 |
return result;
|
187 |
} |
188 |
|
189 |
|
190 |
public LinearRing[] nextLinearRings(final int n) { |
191 |
final LinearRing[] result = new LinearRing[n]; |
192 |
for (int i = 0; i < n; i++) { |
193 |
result[i] = nextLinearRing(); |
194 |
} |
195 |
return result;
|
196 |
} |
197 |
|
198 |
|
199 |
public Polygon nextNoHolePolygon() { |
200 |
return gf.createPolygon(nextLinearRing(), null); |
201 |
} |
202 |
|
203 |
|
204 |
public Polygon nextNoHolePolygon(final Envelope envelope) { |
205 |
return gf.createPolygon(nextLinearRing(envelope), null); |
206 |
} |
207 |
|
208 |
|
209 |
public Polygon[] nextNoHolePolygons(final int n) { |
210 |
final Polygon[] result = new Polygon[n]; |
211 |
for (int i = 0; i < n; i++) { |
212 |
result[i] = nextNoHolePolygon(); |
213 |
} |
214 |
return result;
|
215 |
} |
216 |
|
217 |
|
218 |
public Polygon nextPolygon() { |
219 |
final LinearRing shell = nextLinearRing();
|
220 |
final int nbHoles = 1; // getNumberOfHolesPerPolygon(); |
221 |
final LinearRing[] holes = new LinearRing[nbHoles]; |
222 |
for (int i = 0; i < nbHoles; i++) { |
223 |
do {
|
224 |
holes[i] = nextLinearRing(shell.getEnvelopeInternal()); |
225 |
} |
226 |
while (!shell.contains(holes[i]));
|
227 |
} |
228 |
return gf.createPolygon(shell, holes);
|
229 |
} |
230 |
|
231 |
|
232 |
//
|
233 |
// public Polygon[] nextPolygons(final int n) {
|
234 |
// final Polygon[] result = new Polygon[n];
|
235 |
// for (int i = 0; i < n; i++) {
|
236 |
// result[i] = nextPolygon();
|
237 |
// }
|
238 |
// return result;
|
239 |
// }
|
240 |
|
241 |
public Geometry nextGeometry() {
|
242 |
switch (RAND.nextInt(4)) { |
243 |
case 0: |
244 |
return nextPoint();
|
245 |
case 1: |
246 |
return nextLineString();
|
247 |
case 2: |
248 |
return nextLinearRing();
|
249 |
case 3: |
250 |
return nextNoHolePolygon();
|
251 |
// return nextPolygon();
|
252 |
} |
253 |
throw new RuntimeException("Unreachable code"); |
254 |
} |
255 |
|
256 |
|
257 |
public Geometry nextGeometry(final Envelope env) { |
258 |
switch (RAND.nextInt(4)) { |
259 |
case 0: |
260 |
return nextPoint(env);
|
261 |
case 1: |
262 |
return nextLineString(env);
|
263 |
case 2: |
264 |
return nextLinearRing(env);
|
265 |
case 3: |
266 |
return nextNoHolePolygon(env);
|
267 |
// return nextPolygon();
|
268 |
} |
269 |
throw new RuntimeException("Unreachable code"); |
270 |
} |
271 |
|
272 |
|
273 |
public Geometry[] nextGeometries(final int n) { |
274 |
final Geometry[] result = new Geometry[n]; |
275 |
for (int i = 0; i < n; i++) { |
276 |
result[i] = nextGeometry(); |
277 |
} |
278 |
return result;
|
279 |
} |
280 |
} |