Revision 31988
trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/core/GeometryUtilities.java | ||
---|---|---|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
6 | 6 |
* of the Valencian Government (CIT) |
7 |
*
|
|
7 |
* |
|
8 | 8 |
* This program is free software; you can redistribute it and/or |
9 | 9 |
* modify it under the terms of the GNU General Public License |
10 | 10 |
* as published by the Free Software Foundation; either version 2 |
11 | 11 |
* of the License, or (at your option) any later version. |
12 |
*
|
|
12 |
* |
|
13 | 13 |
* This program is distributed in the hope that it will be useful, |
14 | 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | 16 |
* GNU General Public License for more details. |
17 |
*
|
|
17 |
* |
|
18 | 18 |
* You should have received a copy of the GNU General Public License |
19 | 19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
21 | 21 |
* MA 02110-1301, USA. |
22 |
*
|
|
22 |
* |
|
23 | 23 |
*/ |
24 | 24 |
|
25 | 25 |
import java.awt.geom.PathIterator; |
... | ... | |
83 | 83 |
|
84 | 84 |
/** |
85 | 85 |
* Calculates the length of a polygon (perimeter) or a line. |
86 |
*
|
|
86 |
* |
|
87 | 87 |
* @param vp |
88 | 88 |
* @param geom |
89 | 89 |
* @return |
... | ... | |
116 | 116 |
* multipolygon or multilyne. Each element of the array list is an array |
117 | 117 |
* (Double[][]) containing the X and Y coordinates for each polygon |
118 | 118 |
* part.</p> |
119 |
*
|
|
119 |
* |
|
120 | 120 |
* <p>I think it wll be clearer with an example: |
121 |
*
|
|
121 |
* |
|
122 | 122 |
* <pre>ArrayList parts = GeometryUtilities.getParts(geom); |
123 |
* // xs will contain all the X coordinates from the first polygon part
|
|
123 |
* // xs will contain all the X coordinates from the first polygon part |
|
124 | 124 |
* Double[] xs = parts.get(0)[0]; |
125 | 125 |
* // ys will contain all the Y coordinates from the first polygon part |
126 | 126 |
* Double[] ys = parts.get(0)[1]; |
127 |
*
|
|
127 |
* |
|
128 | 128 |
* ... do something with 'xs' and 'ys' ... |
129 |
*
|
|
130 |
* // xs will now contain all the X coordinates from the second polygon part
|
|
129 |
* |
|
130 |
* // xs will now contain all the X coordinates from the second polygon part |
|
131 | 131 |
* xs = parts.get(1)[0]; |
132 | 132 |
* // ys will now contain all the Y coordinates from the second polygon part |
133 | 133 |
* ys = parts.get(1)[1]; |
... | ... | |
136 | 136 |
* parts.get(3), etc... of course, we should check how many parts the polygon |
137 | 137 |
* really has. |
138 | 138 |
* </pre> |
139 |
*
|
|
140 |
*
|
|
139 |
* |
|
140 |
* |
|
141 | 141 |
* @param geometry |
142 | 142 |
* @return |
143 | 143 |
*/ |
... | ... | |
153 | 153 |
int numParts = 0; |
154 | 154 |
theIterator = geometry.getPathIterator(null, FConverter.FLATNESS); //, flatness); |
155 | 155 |
boolean isClosed = false; |
156 |
double firstX=0; |
|
157 |
double firstY=0; |
|
156 | 158 |
while (!theIterator.isDone()) { |
157 | 159 |
theType = theIterator.currentSegment(theData); |
158 | 160 |
|
159 | 161 |
switch (theType) { |
160 | 162 |
case PathIterator.SEG_MOVETO: |
161 | 163 |
if (numParts==0){ |
164 |
firstX=theData[0]; |
|
165 |
firstY=theData[1]; |
|
162 | 166 |
xs.add(new Double(theData[0])); |
163 | 167 |
ys.add(new Double(theData[1])); |
164 | 168 |
}else{ |
... | ... | |
169 | 173 |
xs.clear(); |
170 | 174 |
ys.clear(); |
171 | 175 |
} |
176 |
firstX=theData[0]; |
|
177 |
firstY=theData[1]; |
|
172 | 178 |
xs.add(new Double(theData[0])); |
173 | 179 |
ys.add(new Double(theData[1])); |
174 | 180 |
} |
... | ... | |
184 | 190 |
isClosed=true; |
185 | 191 |
xs.add(new Double(theData[0])); |
186 | 192 |
ys.add(new Double(theData[1])); |
193 |
xs.add(new Double(firstX)); |
|
194 |
ys.add(new Double(firstY)); |
|
187 | 195 |
Double[] x = (Double[]) xs.toArray(new Double[0]); |
188 | 196 |
Double[] y = (Double[]) ys.toArray(new Double[0]); |
189 | 197 |
parts.add(new Double[][] { x, y }); |
... | ... | |
223 | 231 |
/** |
224 | 232 |
* Returns the area of a polygon, whose coordinates are defined |
225 | 233 |
* in a geographic (latitude/longitude) CRS. |
226 |
*
|
|
234 |
* |
|
227 | 235 |
* @param xs |
228 | 236 |
* @param ys |
229 | 237 |
* @return |
... | ... | |
281 | 289 |
int currentType; |
282 | 290 |
boolean closed = false; |
283 | 291 |
long numPoints = 0; |
284 |
|
|
292 |
|
|
285 | 293 |
while (! it.isDone()) { |
286 | 294 |
currentType = it.currentSegment(newCoords); |
287 | 295 |
|
... | ... | |
333 | 341 |
|
334 | 342 |
/** |
335 | 343 |
* Creates a polyline 2D from an array of points |
336 |
*
|
|
344 |
* |
|
337 | 345 |
* @param points |
338 | 346 |
* @return |
339 | 347 |
*/ |
340 | 348 |
public static IGeometry getPolyLine2D(FPoint2D[] points) { |
341 | 349 |
if (points.length < 2) |
342 | 350 |
return null; |
343 |
|
|
351 |
|
|
344 | 352 |
// Creates the general path of the new polyline |
345 | 353 |
GeneralPathX gPath = new GeneralPathX(); |
346 |
|
|
354 |
|
|
347 | 355 |
gPath.moveTo(points[0].getX(), points[0].getY()); |
348 |
|
|
356 |
|
|
349 | 357 |
for (int i = 1; i < points.length; i++) { |
350 | 358 |
gPath.lineTo(points[i].getX(), points[i].getY()); |
351 | 359 |
} |
352 | 360 |
|
353 | 361 |
return ShapeFactory.createPolyline2D(gPath); |
354 | 362 |
} |
355 |
|
|
363 |
|
|
356 | 364 |
/** |
357 | 365 |
* Creates a polygon 2D from an array of points. |
358 |
*
|
|
366 |
* |
|
359 | 367 |
* @param points |
360 | 368 |
* @return |
361 | 369 |
*/ |
362 | 370 |
public static IGeometry getPolygon2D(FPoint2D[] points) { |
363 | 371 |
if (points.length < 3) |
364 | 372 |
return null; |
365 |
|
|
373 |
|
|
366 | 374 |
// Creates the general path of the new polygon |
367 | 375 |
GeneralPathX gPath = new GeneralPathX(); |
368 |
|
|
376 |
|
|
369 | 377 |
gPath.moveTo(points[0].getX(), points[0].getY()); |
370 |
|
|
378 |
|
|
371 | 379 |
for (int i = 1; i < points.length; i++) { |
372 | 380 |
gPath.lineTo(points[i].getX(), points[i].getY()); |
373 | 381 |
} |
374 |
|
|
382 |
|
|
375 | 383 |
// Forces to close the polygon |
376 | 384 |
gPath.closePath(); |
377 | 385 |
|
Also available in: Unified diff