Revision 30300
branches/v2_0_0_prep/extensions/org.gvsig.oracle/src/org/gvsig/fmap/dal/store/oracle/OracleUtils.java | ||
---|---|---|
42 | 42 |
*/ |
43 | 43 |
package org.gvsig.fmap.dal.store.oracle; |
44 | 44 |
|
45 |
import java.awt.Shape; |
|
46 | 45 |
import java.awt.geom.PathIterator; |
47 | 46 |
import java.awt.geom.Point2D; |
48 | 47 |
import java.awt.geom.Rectangle2D; |
... | ... | |
61 | 60 |
import java.util.ArrayList; |
62 | 61 |
import java.util.HashMap; |
63 | 62 |
import java.util.Iterator; |
63 |
import java.util.List; |
|
64 | 64 |
import java.util.Random; |
65 | 65 |
|
66 | 66 |
import oracle.sql.ARRAY; |
... | ... | |
73 | 73 |
import org.gvsig.fmap.geom.Geometry; |
74 | 74 |
import org.gvsig.fmap.geom.GeometryLocator; |
75 | 75 |
import org.gvsig.fmap.geom.GeometryManager; |
76 |
import org.gvsig.fmap.geom.aggregate.impl.MultiCurve2DZ; |
|
76 | 77 |
import org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D; |
77 | 78 |
import org.gvsig.fmap.geom.aggregate.impl.MultiPoint2DZ; |
78 | 79 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
80 |
import org.gvsig.fmap.geom.primitive.Curve; |
|
79 | 81 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
82 |
import org.gvsig.fmap.geom.primitive.Point; |
|
80 | 83 |
import org.gvsig.fmap.geom.primitive.Surface; |
84 |
import org.gvsig.fmap.geom.primitive.impl.Circle2D; |
|
85 |
import org.gvsig.fmap.geom.primitive.impl.Curve2DZ; |
|
81 | 86 |
import org.gvsig.fmap.geom.primitive.impl.Point2DZ; |
87 |
import org.gvsig.fmap.geom.primitive.impl.Surface2DZ; |
|
82 | 88 |
import org.gvsig.oracle.utils.LineString3D; |
83 | 89 |
|
84 |
|
|
85 | 90 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
86 | 91 |
import com.vividsolutions.jts.geom.Coordinate; |
87 | 92 |
import com.vividsolutions.jts.geom.CoordinateArrays; |
... | ... | |
91 | 96 |
import com.vividsolutions.jts.geom.LinearRing; |
92 | 97 |
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence; |
93 | 98 |
|
94 |
|
|
95 | 99 |
/** |
96 | 100 |
* Utility class with static methods. |
97 |
* |
|
101 |
*
|
|
98 | 102 |
* @author jldominguez |
99 |
* |
|
103 |
*
|
|
100 | 104 |
*/ |
101 | 105 |
public class OracleUtils { |
102 |
private static Logger logger = Logger.getLogger(OracleSpatialUtils.class.getName());
|
|
103 |
private static double FLATNESS = 0.8;
|
|
104 |
private static GeometryFactory geomFactory = new GeometryFactory();
|
|
105 |
private static final double IRRELEVANT_DISTANCE = 0.00000001;
|
|
106 |
private static Random rnd = new Random();
|
|
107 |
private static DecimalFormat df = new DecimalFormat();
|
|
108 |
private static DecimalFormatSymbols dfs = new DecimalFormatSymbols();
|
|
109 |
|
|
106 |
private static Logger logger = Logger
|
|
107 |
.getLogger(OracleUtils.class.getName());
|
|
108 |
private static double FLATNESS = 0.8;
|
|
109 |
private static GeometryFactory geomFactory = new GeometryFactory();
|
|
110 |
private static final double IRRELEVANT_DISTANCE = 0.00000001;
|
|
111 |
private static Random rnd = new Random();
|
|
112 |
private static DecimalFormat df = new DecimalFormat();
|
|
113 |
private static DecimalFormatSymbols dfs = new DecimalFormatSymbols(); |
|
110 | 114 |
|
111 |
/** |
|
112 |
* COnstructs a geometry from a file that contains a vertex per line: |
|
113 |
* |
|
114 |
* x1 y1 z1 |
|
115 |
* x2 y2 z2 |
|
116 |
* ... |
|
117 |
* |
|
118 |
* @param filepath vertices text file path |
|
119 |
* @param polygon whether it is a polygon or not |
|
120 |
* @return the created geometry |
|
121 |
*/ |
|
122 |
public static Geometry readGeometry3D(URL filepath, boolean polygon) { |
|
123 |
|
|
124 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
125 |
|
|
126 |
GeneralPathX resp = new GeneralPathX(); |
|
127 |
File file = new File(filepath.getFile()); |
|
128 |
ArrayList z = new ArrayList(); |
|
115 |
/** |
|
116 |
* COnstructs a geometry from a file that contains a vertex per line: |
|
117 |
* |
|
118 |
* x1 y1 z1 x2 y2 z2 ... |
|
119 |
* |
|
120 |
* @param filepath |
|
121 |
* vertices text file path |
|
122 |
* @param polygon |
|
123 |
* whether it is a polygon or not |
|
124 |
* @return the created geometry |
|
125 |
* @throws CreateGeometryException |
|
126 |
*/ |
|
127 |
public static Geometry readGeometry3D(URL filepath, boolean polygon) |
|
128 |
throws CreateGeometryException { |
|
129 | 129 |
|
130 |
try { |
|
131 |
FileReader fr = new FileReader(file); |
|
132 |
BufferedReader br = new BufferedReader(fr); |
|
133 |
double[] coords = new double[3]; |
|
130 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
134 | 131 |
|
135 |
boolean move = true; |
|
132 |
GeneralPathX gpx = new GeneralPathX(); |
|
133 |
File file = new File(filepath.getFile()); |
|
134 |
List<Double> z = new ArrayList<Double>(); |
|
136 | 135 |
|
137 |
String line = br.readLine(); |
|
136 |
try { |
|
137 |
FileReader fr = new FileReader(file); |
|
138 |
BufferedReader br = new BufferedReader(fr); |
|
139 |
double[] coords = new double[3]; |
|
138 | 140 |
|
139 |
while (line != null) { |
|
140 |
coords = parseLine(line); |
|
141 |
boolean move = true; |
|
141 | 142 |
|
142 |
if (line.length() == 0) { |
|
143 |
move = true; |
|
144 |
} |
|
145 |
else { |
|
146 |
if (move) { |
|
147 |
resp.moveTo(coords[0], coords[1]); |
|
148 |
z.add(new Double(coords[2])); |
|
149 |
} |
|
150 |
else { |
|
151 |
resp.lineTo(coords[0], coords[1]); |
|
152 |
z.add(new Double(coords[2])); |
|
153 |
} |
|
143 |
String line = br.readLine(); |
|
154 | 144 |
|
155 |
move = false;
|
|
156 |
}
|
|
145 |
while (line != null) {
|
|
146 |
coords = parseLine(line);
|
|
157 | 147 |
|
158 |
line = br.readLine(); |
|
159 |
} |
|
160 |
} |
|
161 |
catch (Exception ex) { |
|
162 |
logger.error("While creating GeneralPathX: " + |
|
163 |
ex.getMessage()); |
|
148 |
if (line.length() == 0) { |
|
149 |
move = true; |
|
150 |
} else { |
|
151 |
if (move) { |
|
152 |
gpx.moveTo(coords[0], coords[1]); |
|
153 |
z.add(new Double(coords[2])); |
|
154 |
} else { |
|
155 |
gpx.lineTo(coords[0], coords[1]); |
|
156 |
z.add(new Double(coords[2])); |
|
157 |
} |
|
164 | 158 |
|
165 |
return null;
|
|
166 |
}
|
|
159 |
move = false;
|
|
160 |
}
|
|
167 | 161 |
|
168 |
double[] zz = new double[z.size()]; |
|
162 |
line = br.readLine(); |
|
163 |
} |
|
164 |
} catch (Exception ex) { |
|
165 |
logger.error("While creating GeneralPathX: " + ex.getMessage()); |
|
166 |
return null; |
|
167 |
} |
|
169 | 168 |
|
170 |
for (int i = 0; i < z.size(); i++) { |
|
171 |
zz[i] = ((Double) z.get(i)).doubleValue(); |
|
172 |
} |
|
169 |
if (polygon) { |
|
170 |
Surface surface = (Surface) geomManager.create( |
|
171 |
Geometry.TYPES.SURFACE, Geometry.SUBTYPES.GEOM2DZ); |
|
172 |
surface.setGeneralPath(gpx); |
|
173 |
for (int i = 0; i < z.size(); i++) { |
|
174 |
surface |
|
175 |
.setCoordinateAt(i, 3, ((Double) z.get(i)) |
|
176 |
.doubleValue()); |
|
177 |
} |
|
178 |
return surface; |
|
179 |
} else { |
|
180 |
Curve curve = (Curve) geomManager.create(Geometry.TYPES.CURVE, |
|
181 |
Geometry.SUBTYPES.GEOM2DZ); |
|
182 |
curve.setGeneralPath(gpx); |
|
183 |
for (int i = 0; i < z.size(); i++) { |
|
184 |
curve.setCoordinateAt(i, 3, ((Double) z.get(i)).doubleValue()); |
|
185 |
} |
|
186 |
return curve; |
|
187 |
} |
|
188 |
} |
|
173 | 189 |
|
174 |
if (polygon) { |
|
175 |
Surface surface = (Surface) geomManager.create(Geometry.TYPES.SURFACE, Geometry.SUBTYPES.GEOM2DZ); |
|
176 |
surface. |
|
177 |
return .createSurface(resp, org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2DZ).createPolygon3D(resp, zz); |
|
178 |
} |
|
179 |
else { |
|
180 |
return ShapeFactory.createPolyline3D(resp, zz); |
|
181 |
} |
|
182 |
} |
|
190 |
/** |
|
191 |
* |
|
192 |
* @param line |
|
193 |
* @return |
|
194 |
*/ |
|
195 |
private static double[] parseLine(String line) { |
|
196 |
String[] sep = line.split(" "); |
|
197 |
double[] resp = new double[3]; |
|
183 | 198 |
|
184 |
private static double[] parseLine(String line) { |
|
185 |
String[] sep = line.split(" "); |
|
186 |
double[] resp = new double[3]; |
|
199 |
for (int i = 0; i < 3; i++) |
|
200 |
resp[i] = 0.0; |
|
187 | 201 |
|
188 |
for (int i = 0; i < 3; i++) |
|
189 |
resp[i] = 0.0; |
|
202 |
try { |
|
203 |
resp[0] = Double.parseDouble(sep[0]); |
|
204 |
} catch (Exception ex) { |
|
205 |
} |
|
190 | 206 |
|
191 |
try {
|
|
192 |
resp[0] = Double.parseDouble(sep[0]);
|
|
193 |
}
|
|
194 |
catch (Exception ex) {
|
|
195 |
}
|
|
207 |
if (sep.length > 1) {
|
|
208 |
try {
|
|
209 |
resp[1] = Double.parseDouble(sep[1]);
|
|
210 |
} catch (Exception ex) {
|
|
211 |
}
|
|
196 | 212 |
|
197 |
if (sep.length > 1) { |
|
198 |
try { |
|
199 |
resp[1] = Double.parseDouble(sep[1]); |
|
200 |
} |
|
201 |
catch (Exception ex) { |
|
202 |
} |
|
213 |
if (sep.length > 2) { |
|
214 |
try { |
|
215 |
resp[2] = Double.parseDouble(sep[2]); |
|
216 |
} catch (Exception ex) { |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
203 | 220 |
|
204 |
if (sep.length > 2) { |
|
205 |
try { |
|
206 |
resp[2] = Double.parseDouble(sep[2]); |
|
207 |
} |
|
208 |
catch (Exception ex) { |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
221 |
return resp; |
|
222 |
} |
|
212 | 223 |
|
213 |
return resp; |
|
214 |
} |
|
224 |
/** |
|
225 |
* |
|
226 |
* @param mp2d |
|
227 |
* @param c |
|
228 |
* @param srid |
|
229 |
* @param hasSrid |
|
230 |
* @return |
|
231 |
* @throws SQLException |
|
232 |
*/ |
|
233 |
private static STRUCT multiPoint2DToStruct(MultiPoint2D mp2d, Connection c, |
|
234 |
int srid, boolean hasSrid) throws SQLException { |
|
235 |
int np = mp2d.getNumgeometries(); |
|
236 |
boolean threed = (mp2d instanceof MultiPoint2DZ); |
|
237 |
int gtype = 2005; |
|
238 |
int dim = 2; |
|
239 |
MultiPoint2DZ mp3d = null; |
|
215 | 240 |
|
216 |
|
|
241 |
if (threed) { |
|
242 |
gtype = 3005; |
|
243 |
dim = 3; |
|
244 |
mp3d = (MultiPoint2DZ) mp2d; |
|
245 |
} |
|
217 | 246 |
|
218 |
private static STRUCT multiPoint2DToStruct(MultiPoint2D mp2d, |
|
219 |
Connection c, int srid, boolean hasSrid) throws SQLException { |
|
220 |
int np = mp2d.getNumPoints(); |
|
221 |
boolean threed = (mp2d instanceof MultiPoint2DZ); |
|
222 |
int gtype = 2005; |
|
223 |
int dim = 2; |
|
224 |
MultiPoint2DZ mp3d = null; |
|
247 |
NUMBER[] indices = new NUMBER[3]; |
|
248 |
indices[0] = new NUMBER(1); |
|
249 |
indices[1] = new NUMBER(1); |
|
250 |
indices[2] = new NUMBER(np); |
|
225 | 251 |
|
226 |
if (threed) { |
|
227 |
gtype = 3005; |
|
228 |
dim = 3; |
|
229 |
mp3d = (MultiPoint2DZ) mp2d; |
|
230 |
} |
|
252 |
NUMBER[] ords = new NUMBER[dim * np]; |
|
231 | 253 |
|
232 |
NUMBER[] indices = new NUMBER[3]; |
|
233 |
indices[0] = new NUMBER(1); |
|
234 |
indices[1] = new NUMBER(1); |
|
235 |
indices[2] = new NUMBER(np); |
|
254 |
for (int i = 0; i < np; i++) { |
|
255 |
ords[dim * i] = new NUMBER(mp2d.getPoint(i).getX()); |
|
256 |
ords[(dim * i) + 1] = new NUMBER(mp2d.getPoint(i).getY()); |
|
236 | 257 |
|
237 |
NUMBER[] ords = new NUMBER[dim * np]; |
|
258 |
if (threed) { |
|
259 |
ords[(dim * i) + 2] = new NUMBER(mp3d.getZs()[i]); |
|
260 |
} |
|
261 |
} |
|
238 | 262 |
|
239 |
for (int i = 0; i < np; i++) { |
|
240 |
ords[dim * i] = new NUMBER(mp2d.getPoint(i).getX()); |
|
241 |
ords[(dim * i) + 1] = new NUMBER(mp2d.getPoint(i).getY()); |
|
263 |
STRUCT resp; |
|
264 |
StructDescriptor dsc = StructDescriptor.createDescriptor( |
|
265 |
"MDSYS.SDO_GEOMETRY", c); |
|
266 |
Object[] obj = new Object[5]; |
|
267 |
obj[0] = new NUMBER(gtype); |
|
242 | 268 |
|
243 |
if (threed) { |
|
244 |
ords[(dim * i) + 2] = new NUMBER(mp3d.getZs()[i]); |
|
245 |
} |
|
246 |
} |
|
269 |
if (hasSrid) { |
|
270 |
obj[1] = new NUMBER(srid); |
|
271 |
} else { // , boolean hasSrid |
|
272 |
obj[1] = null; |
|
273 |
} |
|
247 | 274 |
|
248 |
STRUCT resp; |
|
249 |
StructDescriptor dsc = StructDescriptor.createDescriptor("MDSYS.SDO_GEOMETRY", |
|
250 |
((ConnectionJDBC)c).getConnection()); |
|
251 |
Object[] obj = new Object[5]; |
|
252 |
obj[0] = new NUMBER(gtype); |
|
275 |
obj[2] = null; |
|
276 |
obj[3] = indices; |
|
277 |
obj[4] = ords; |
|
278 |
resp = new STRUCT(dsc, c, obj); |
|
253 | 279 |
|
254 |
if (hasSrid) { |
|
255 |
obj[1] = new NUMBER(srid); |
|
256 |
} |
|
257 |
else { // , boolean hasSrid |
|
258 |
obj[1] = null; |
|
259 |
} |
|
280 |
return resp; |
|
281 |
} |
|
260 | 282 |
|
261 |
obj[2] = null; |
|
262 |
obj[3] = indices; |
|
263 |
obj[4] = ords; |
|
264 |
resp = new STRUCT(dsc, ((ConnectionJDBC)c).getConnection(), obj); |
|
283 |
/** |
|
284 |
* |
|
285 |
* @param fcirc |
|
286 |
* @param srid |
|
287 |
* @param _conn |
|
288 |
* @param hasSrid |
|
289 |
* @return |
|
290 |
* @throws SQLException |
|
291 |
*/ |
|
292 |
private static STRUCT getCircleAsStruct(Circle2D fcirc, int srid, |
|
293 |
Connection _conn, boolean hasSrid) throws SQLException { |
|
294 |
int geotype = 2003; |
|
295 |
NUMBER[] indices = new NUMBER[3]; |
|
296 |
indices[0] = new NUMBER(1); |
|
297 |
indices[1] = new NUMBER(1003); |
|
298 |
indices[2] = new NUMBER(4); |
|
265 | 299 |
|
266 |
return resp; |
|
267 |
} |
|
300 |
NUMBER[] ords = new NUMBER[6]; |
|
301 |
Coordinate[] three_points = getThreePointsOfCircumference(fcirc |
|
302 |
.getCenter(), fcirc.getRadious()); |
|
268 | 303 |
|
269 |
private static STRUCT getCircleAsStruct(FCircle2D fcirc, int srid, |
|
270 |
Connection _conn, boolean hasSrid) throws SQLException { |
|
271 |
int geotype = 2003; |
|
272 |
NUMBER[] indices = new NUMBER[3]; |
|
273 |
indices[0] = new NUMBER(1); |
|
274 |
indices[1] = new NUMBER(1003); |
|
275 |
indices[2] = new NUMBER(4); |
|
304 |
for (int i = 0; i < three_points.length; i++) { |
|
305 |
ords[i * 2] = new NUMBER(three_points[i].x); |
|
306 |
ords[(i * 2) + 1] = new NUMBER(three_points[i].y); |
|
307 |
} |
|
276 | 308 |
|
277 |
NUMBER[] ords = new NUMBER[6]; |
|
278 |
Coordinate[] three_points = getThreePointsOfCircumference(fcirc.getCenter(), |
|
279 |
fcirc.getRadio()); |
|
309 |
STRUCT resp; |
|
310 |
StructDescriptor dsc = StructDescriptor.createDescriptor( |
|
311 |
"MDSYS.SDO_GEOMETRY", _conn); |
|
312 |
Object[] obj = new Object[5]; |
|
313 |
obj[0] = new NUMBER(geotype); |
|
280 | 314 |
|
281 |
for (int i = 0; i < three_points.length; i++) { |
|
282 |
ords[i * 2] = new NUMBER(three_points[i].x); |
|
283 |
ords[(i * 2) + 1] = new NUMBER(three_points[i].y); |
|
284 |
} |
|
315 |
if (hasSrid) { |
|
316 |
obj[1] = new NUMBER(srid); |
|
317 |
} else { |
|
318 |
obj[1] = null; |
|
319 |
} |
|
285 | 320 |
|
286 |
STRUCT resp; |
|
287 |
StructDescriptor dsc = StructDescriptor.createDescriptor("MDSYS.SDO_GEOMETRY", |
|
288 |
((ConnectionJDBC)_conn).getConnection()); |
|
289 |
Object[] obj = new Object[5]; |
|
290 |
obj[0] = new NUMBER(geotype); |
|
321 |
obj[2] = null; |
|
322 |
obj[3] = indices; |
|
323 |
obj[4] = ords; |
|
324 |
resp = new STRUCT(dsc, _conn, obj); |
|
291 | 325 |
|
292 |
if (hasSrid) { |
|
293 |
obj[1] = new NUMBER(srid); |
|
294 |
} |
|
295 |
else { |
|
296 |
obj[1] = null; |
|
297 |
} |
|
326 |
return resp; |
|
327 |
} |
|
298 | 328 |
|
299 |
obj[2] = null; |
|
300 |
obj[3] = indices; |
|
301 |
obj[4] = ords; |
|
302 |
resp = new STRUCT(dsc, ((ConnectionJDBC)_conn).getConnection(), obj); |
|
329 |
/** |
|
330 |
* |
|
331 |
* @param cntr |
|
332 |
* @param radius |
|
333 |
* @return |
|
334 |
*/ |
|
335 |
private static Coordinate[] getThreePointsOfCircumference(Point cntr, |
|
336 |
double radius) { |
|
337 |
Coordinate[] resp = new Coordinate[3]; |
|
338 |
double x; |
|
339 |
double y; |
|
340 |
double alpha = 0; |
|
303 | 341 |
|
304 |
return resp; |
|
305 |
} |
|
342 |
for (int i = 0; i < 3; i++) { |
|
343 |
alpha = (i * 120.0 * Math.PI) / 180.0; |
|
344 |
x = cntr.getX() + (radius * Math.cos(alpha)); |
|
345 |
y = cntr.getY() + (radius * Math.sin(alpha)); |
|
346 |
resp[i] = new Coordinate(x, y); |
|
347 |
} |
|
306 | 348 |
|
307 |
private static Coordinate[] getThreePointsOfCircumference(Point2D cntr, |
|
308 |
double radius) { |
|
309 |
Coordinate[] resp = new Coordinate[3]; |
|
310 |
double x; |
|
311 |
double y; |
|
312 |
double alpha = 0; |
|
349 |
return resp; |
|
350 |
} |
|
313 | 351 |
|
314 |
for (int i = 0; i < 3; i++) { |
|
315 |
alpha = (i * 120.0 * Math.PI) / 180.0; |
|
316 |
x = cntr.getX() + (radius * Math.cos(alpha)); |
|
317 |
y = cntr.getY() + (radius * Math.sin(alpha)); |
|
318 |
resp[i] = new Coordinate(x, y); |
|
319 |
} |
|
352 |
/** |
|
353 |
* |
|
354 |
* @param pto |
|
355 |
* @return |
|
356 |
*/ |
|
357 |
private static Coordinate getSingleCoordinate(Point pto) { |
|
358 |
// TODO Auto-generated method stub |
|
359 |
Coordinate resp = new Coordinate(); |
|
360 |
resp.x = pto.getX(); |
|
361 |
resp.y = pto.getY(); |
|
320 | 362 |
|
321 |
return resp; |
|
322 |
} |
|
363 |
if (pto instanceof Point2DZ) { |
|
364 |
resp.z = ((Point2DZ) pto).getCoordinateAt(3); |
|
365 |
} |
|
323 | 366 |
|
324 |
private static Coordinate getSingleCoordinate(Point2D p2d) { |
|
325 |
// TODO Auto-generated method stub |
|
326 |
Coordinate resp = new Coordinate(); |
|
327 |
resp.x = p2d.getX(); |
|
328 |
resp.y = p2d.getY(); |
|
367 |
return resp; |
|
368 |
} |
|
329 | 369 |
|
330 |
if (p2d instanceof Point2DZ) { |
|
331 |
resp.z = ((Point2DZ) p2d).getZs()[0]; |
|
332 |
} |
|
370 |
/** |
|
371 |
* |
|
372 |
* @param cc |
|
373 |
* @return |
|
374 |
*/ |
|
375 |
private static List<Coordinate> ensureSensibleLineString(List cc) { |
|
376 |
if (cc.size() == 2) { |
|
377 |
if (sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(cc |
|
378 |
.size() - 1))) { |
|
379 |
List resp = new ArrayList(); |
|
380 |
resp.add(cc.get(0)); |
|
333 | 381 |
|
334 |
return resp; |
|
335 |
} |
|
382 |
Coordinate newc = new Coordinate((Coordinate) cc.get(0)); |
|
383 |
newc.x = newc.x + IRRELEVANT_DISTANCE; |
|
384 |
resp.add(newc); |
|
336 | 385 |
|
337 |
private static ArrayList ensureSensibleLineString(ArrayList cc) { |
|
338 |
if (cc.size() == 2) { |
|
339 |
if (sameCoordinate((Coordinate) cc.get(0), |
|
340 |
(Coordinate) cc.get(cc.size() - 1))) { |
|
341 |
ArrayList resp = new ArrayList(); |
|
342 |
resp.add(cc.get(0)); |
|
386 |
return resp; |
|
387 |
} |
|
388 |
} |
|
343 | 389 |
|
344 |
Coordinate newc = new Coordinate((Coordinate) cc.get(0)); |
|
345 |
newc.x = newc.x + IRRELEVANT_DISTANCE; |
|
346 |
resp.add(newc); |
|
390 |
return cc; |
|
391 |
} |
|
347 | 392 |
|
348 |
return resp; |
|
349 |
} |
|
350 |
} |
|
393 |
/** |
|
394 |
* |
|
395 |
* @param c1 |
|
396 |
* @param c2 |
|
397 |
* @return |
|
398 |
*/ |
|
399 |
private static boolean sameCoordinate(Coordinate c1, Coordinate c2) { |
|
400 |
if (c1.x != c2.x) { |
|
401 |
return false; |
|
402 |
} |
|
351 | 403 |
|
352 |
return cc; |
|
353 |
} |
|
404 |
if (c1.y != c2.y) { |
|
405 |
return false; |
|
406 |
} |
|
354 | 407 |
|
355 |
private static boolean sameCoordinate(Coordinate c1, Coordinate c2) { |
|
356 |
if (c1.x != c2.x) { |
|
357 |
return false; |
|
358 |
} |
|
408 |
return true; |
|
409 |
} |
|
359 | 410 |
|
360 |
if (c1.y != c2.y) { |
|
361 |
return false; |
|
362 |
} |
|
411 |
/** |
|
412 |
* |
|
413 |
* @param cc |
|
414 |
* @return |
|
415 |
*/ |
|
416 |
private static List getClosedRelevantPolygon(List cc) { |
|
417 |
if (cc.size() == 2) { |
|
418 |
return getMinClosedCoords((Coordinate) cc.get(0)); |
|
419 |
} |
|
363 | 420 |
|
364 |
return true; |
|
365 |
} |
|
421 |
if (cc.size() == 3) { |
|
422 |
if (sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(1))) { |
|
423 |
return getMinClosedCoords((Coordinate) cc.get(0)); |
|
424 |
} |
|
366 | 425 |
|
367 |
private static ArrayList getClosedRelevantPolygon(ArrayList cc) { |
|
368 |
if (cc.size() == 2) { |
|
369 |
return getMinClosedCoords((Coordinate) cc.get(0)); |
|
370 |
} |
|
426 |
if (sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(2))) { |
|
427 |
return getMinClosedCoords((Coordinate) cc.get(0)); |
|
428 |
} |
|
371 | 429 |
|
372 |
if (cc.size() == 3) { |
|
373 |
if (sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(1))) { |
|
374 |
return getMinClosedCoords((Coordinate) cc.get(0)); |
|
375 |
} |
|
430 |
if (sameCoordinate((Coordinate) cc.get(1), (Coordinate) cc.get(2))) { |
|
431 |
return getMinClosedCoords((Coordinate) cc.get(1)); |
|
432 |
} |
|
376 | 433 |
|
377 |
if (sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(2))) { |
|
378 |
return getMinClosedCoords((Coordinate) cc.get(0)); |
|
379 |
} |
|
434 |
cc.add(cc.get(0)); |
|
380 | 435 |
|
381 |
if (sameCoordinate((Coordinate) cc.get(1), (Coordinate) cc.get(2))) { |
|
382 |
return getMinClosedCoords((Coordinate) cc.get(1)); |
|
383 |
} |
|
436 |
return cc; |
|
437 |
} |
|
384 | 438 |
|
385 |
cc.add(cc.get(0)); |
|
439 |
if (!sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(cc |
|
440 |
.size() - 1))) { |
|
441 |
cc.add(cc.get(0)); |
|
442 |
} |
|
386 | 443 |
|
387 |
return cc;
|
|
388 |
}
|
|
444 |
return cc;
|
|
445 |
}
|
|
389 | 446 |
|
390 |
if (!sameCoordinate((Coordinate) cc.get(0), |
|
391 |
(Coordinate) cc.get(cc.size() - 1))) { |
|
392 |
cc.add(cc.get(0)); |
|
393 |
} |
|
447 |
/** |
|
448 |
* |
|
449 |
* @param c |
|
450 |
* @return |
|
451 |
*/ |
|
452 |
private static List getMinClosedCoords(Coordinate c) { |
|
453 |
List resp = new ArrayList(); |
|
454 |
resp.add(c); |
|
394 | 455 |
|
395 |
return cc; |
|
396 |
} |
|
456 |
Coordinate nc = new Coordinate(c); |
|
457 |
nc.x = nc.x + IRRELEVANT_DISTANCE; |
|
458 |
resp.add(nc); |
|
397 | 459 |
|
398 |
private static ArrayList getMinClosedCoords(Coordinate c) {
|
|
399 |
ArrayList resp = new ArrayList();
|
|
400 |
resp.add(c);
|
|
460 |
Coordinate nc2 = new Coordinate(nc);
|
|
461 |
nc2.y = nc2.y + IRRELEVANT_DISTANCE;
|
|
462 |
resp.add(nc2);
|
|
401 | 463 |
|
402 |
Coordinate nc = new Coordinate(c); |
|
403 |
nc.x = nc.x + IRRELEVANT_DISTANCE; |
|
404 |
resp.add(nc); |
|
464 |
resp.add(new Coordinate(c)); |
|
405 | 465 |
|
406 |
Coordinate nc2 = new Coordinate(nc); |
|
407 |
nc2.y = nc2.y + IRRELEVANT_DISTANCE; |
|
408 |
resp.add(nc2); |
|
466 |
return resp; |
|
467 |
} |
|
409 | 468 |
|
410 |
resp.add(new Coordinate(c)); |
|
469 |
/** |
|
470 |
* |
|
471 |
* @param c |
|
472 |
* @return |
|
473 |
*/ |
|
474 |
private static LinearRing getMinLinearRing(Coordinate c) { |
|
475 |
Coordinate[] p = new Coordinate[4]; |
|
476 |
p[0] = c; |
|
411 | 477 |
|
412 |
return resp;
|
|
413 |
}
|
|
478 |
Coordinate nc = new Coordinate(c);
|
|
479 |
nc.x = nc.x + IRRELEVANT_DISTANCE;
|
|
414 | 480 |
|
415 |
private static LinearRing getMinLinearRing(Coordinate c) { |
|
416 |
Coordinate[] p = new Coordinate[4]; |
|
417 |
p[0] = c; |
|
481 |
Coordinate nc2 = new Coordinate(nc); |
|
482 |
nc2.y = nc2.y - IRRELEVANT_DISTANCE; |
|
483 |
p[1] = nc; |
|
484 |
p[2] = nc2; |
|
485 |
p[3] = new Coordinate(c); |
|
418 | 486 |
|
419 |
Coordinate nc = new Coordinate(c);
|
|
420 |
nc.x = nc.x + IRRELEVANT_DISTANCE;
|
|
487 |
CoordinateArraySequence cs = new CoordinateArraySequence(p);
|
|
488 |
LinearRing ls = new LinearRing(cs, geomFactory);
|
|
421 | 489 |
|
422 |
Coordinate nc2 = new Coordinate(nc); |
|
423 |
nc2.y = nc2.y - IRRELEVANT_DISTANCE; |
|
424 |
p[1] = nc; |
|
425 |
p[2] = nc2; |
|
426 |
p[3] = new Coordinate(c); |
|
490 |
return ls; |
|
491 |
} |
|
427 | 492 |
|
428 |
CoordinateArraySequence cs = new CoordinateArraySequence(p); |
|
429 |
LinearRing ls = new LinearRing(cs, geomFactory); |
|
493 |
/** |
|
494 |
* |
|
495 |
* @return |
|
496 |
*/ |
|
497 |
private static double[] getMinLinearRingZ() { |
|
498 |
double[] resp = new double[4]; |
|
430 | 499 |
|
431 |
return ls;
|
|
432 |
}
|
|
500 |
for (int i = 0; i < 4; i++)
|
|
501 |
resp[i] = 0.0;
|
|
433 | 502 |
|
434 |
private static double[] getMinLinearRingZ() {
|
|
435 |
double[] resp = new double[4];
|
|
503 |
return resp;
|
|
504 |
}
|
|
436 | 505 |
|
437 |
for (int i = 0; i < 4; i++) |
|
438 |
resp[i] = 0.0; |
|
506 |
/** |
|
507 |
* |
|
508 |
* @param testPoint |
|
509 |
* @param pointList |
|
510 |
* @return |
|
511 |
*/ |
|
512 |
private static boolean pointInList(Coordinate testPoint, |
|
513 |
Coordinate[] pointList) { |
|
514 |
int t; |
|
515 |
int numpoints; |
|
516 |
Coordinate p; |
|
439 | 517 |
|
440 |
return resp; |
|
441 |
} |
|
518 |
numpoints = Array.getLength(pointList); |
|
442 | 519 |
|
443 |
private static boolean pointInList(Coordinate testPoint, |
|
444 |
Coordinate[] pointList) { |
|
445 |
int t; |
|
446 |
int numpoints; |
|
447 |
Coordinate p; |
|
520 |
for (t = 0; t < numpoints; t++) { |
|
521 |
p = pointList[t]; |
|
448 | 522 |
|
449 |
numpoints = Array.getLength(pointList); |
|
523 |
if ((testPoint.x == p.x) |
|
524 |
&& (testPoint.y == p.y) |
|
525 |
&& ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) // nan |
|
526 |
// test; |
|
527 |
// x!=x |
|
528 |
// iff |
|
529 |
// x |
|
530 |
// is |
|
531 |
// nan |
|
532 |
) { |
|
533 |
return true; |
|
534 |
} |
|
535 |
} |
|
450 | 536 |
|
451 |
for (t = 0; t < numpoints; t++) {
|
|
452 |
p = pointList[t];
|
|
537 |
return false;
|
|
538 |
}
|
|
453 | 539 |
|
454 |
if ((testPoint.x == p.x) && (testPoint.y == p.y) && |
|
455 |
((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) //nan test; x!=x iff x is nan |
|
456 |
) { |
|
457 |
return true; |
|
458 |
} |
|
459 |
} |
|
540 |
/*** |
|
541 |
* |
|
542 |
* @param mpolygon |
|
543 |
* @return |
|
544 |
*/ |
|
545 |
private static List getPolygonsEasily(Geometry mpolygon) { |
|
546 |
boolean threed = false; |
|
460 | 547 |
|
461 |
return false; |
|
462 |
} |
|
548 |
if (mpolygon instanceof Surface2DZ) { |
|
549 |
threed = true; |
|
550 |
} |
|
463 | 551 |
|
464 |
private static ArrayList getPolygonsEasily(FShape mpolygon) { |
|
465 |
boolean threed = false; |
|
552 |
int start_ind = 0; |
|
553 |
int end_ind = 0; |
|
554 |
int ind = 0; |
|
555 |
int new_size; |
|
556 |
List arrayCoords = null; |
|
557 |
List resp = new ArrayList(); |
|
558 |
Coordinate[] points = null; |
|
559 |
int theType = -99; |
|
560 |
double[] theData = new double[6]; |
|
561 |
Coordinate onlyCoord = null; |
|
562 |
int numParts = 0; |
|
466 | 563 |
|
467 |
if (mpolygon instanceof FPolygon3D) { |
|
468 |
threed = true; |
|
469 |
} |
|
564 |
PathIterator theIterator = mpolygon.getPathIterator(null, FLATNESS); |
|
470 | 565 |
|
471 |
int start_ind = 0; |
|
472 |
int end_ind = 0; |
|
473 |
int ind = 0; |
|
474 |
int new_size; |
|
475 |
ArrayList arrayCoords = null; |
|
476 |
ArrayList resp = new ArrayList(); |
|
477 |
Coordinate[] points = null; |
|
478 |
int theType = -99; |
|
479 |
double[] theData = new double[6]; |
|
480 |
Coordinate onlyCoord = null; |
|
481 |
int numParts = 0; |
|
566 |
while (!theIterator.isDone()) { |
|
567 |
// while not done |
|
568 |
theType = theIterator.currentSegment(theData); |
|
482 | 569 |
|
483 |
PathIterator theIterator = mpolygon.getPathIterator(null, FLATNESS); |
|
570 |
if (onlyCoord == null) { |
|
571 |
onlyCoord = new Coordinate(); |
|
572 |
onlyCoord.x = theData[0]; |
|
573 |
onlyCoord.y = theData[1]; |
|
574 |
} |
|
484 | 575 |
|
485 |
while (!theIterator.isDone()) { |
|
486 |
//while not done |
|
487 |
theType = theIterator.currentSegment(theData); |
|
576 |
switch (theType) { |
|
577 |
case PathIterator.SEG_MOVETO: |
|
488 | 578 |
|
489 |
if (onlyCoord == null) { |
|
490 |
onlyCoord = new Coordinate(); |
|
491 |
onlyCoord.x = theData[0]; |
|
492 |
onlyCoord.y = theData[1]; |
|
493 |
} |
|
579 |
if (arrayCoords == null) { |
|
580 |
arrayCoords = new ArrayList(); |
|
581 |
} else { |
|
582 |
end_ind = ind - 1; |
|
494 | 583 |
|
495 |
switch (theType) {
|
|
496 |
case PathIterator.SEG_MOVETO:
|
|
584 |
arrayCoords = getClosedRelevantPolygon(arrayCoords);
|
|
585 |
new_size = arrayCoords.size();
|
|
497 | 586 |
|
498 |
if (arrayCoords == null) { |
|
499 |
arrayCoords = new ArrayList(); |
|
500 |
} |
|
501 |
else { |
|
502 |
end_ind = ind - 1; |
|
587 |
if (arrayCoords != null) { |
|
588 |
points = CoordinateArrays |
|
589 |
.toCoordinateArray(arrayCoords); |
|
503 | 590 |
|
504 |
arrayCoords = getClosedRelevantPolygon(arrayCoords); |
|
505 |
new_size = arrayCoords.size(); |
|
591 |
try { |
|
592 |
LinearRing aux = geomFactory |
|
593 |
.createLinearRing(points); |
|
594 |
double[] z = null; |
|
506 | 595 |
|
507 |
if (arrayCoords != null) { |
|
508 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
|
596 |
if (threed) { |
|
597 |
z = getZ((Surface2DZ) mpolygon, start_ind, |
|
598 |
end_ind, new_size); |
|
599 |
} |
|
509 | 600 |
|
510 |
try { |
|
511 |
LinearRing aux = geomFactory.createLinearRing(points); |
|
512 |
double[] z = null; |
|
601 |
LineString3D ring = new LineString3D(aux, z); |
|
513 | 602 |
|
514 |
if (threed) { |
|
515 |
z = getZ((FPolygon3D) mpolygon, start_ind, |
|
516 |
end_ind, new_size); |
|
517 |
} |
|
603 |
if (CGAlgorithms.isCCW(points)) { |
|
604 |
resp.add(ring); |
|
605 |
} else { |
|
606 |
resp.add(ring.createReverse()); |
|
607 |
} |
|
608 |
} catch (Exception e) { |
|
609 |
logger.error("Topology exception: " |
|
610 |
+ e.getMessage()); |
|
518 | 611 |
|
519 |
LineString3D ring = new LineString3D(aux, z); |
|
612 |
return null; |
|
613 |
} |
|
614 |
} |
|
520 | 615 |
|
521 |
if (CGAlgorithms.isCCW(points)) { |
|
522 |
resp.add(ring); |
|
523 |
} |
|
524 |
else { |
|
525 |
resp.add(ring.createReverse()); |
|
526 |
} |
|
527 |
} |
|
528 |
catch (Exception e) { |
|
529 |
logger.error("Topology exception: " + |
|
530 |
e.getMessage()); |
|
616 |
arrayCoords = new ArrayList(); |
|
531 | 617 |
|
532 |
return null; |
|
533 |
} |
|
534 |
} |
|
618 |
start_ind = ind; |
|
619 |
} |
|
535 | 620 |
|
536 |
arrayCoords = new ArrayList();
|
|
621 |
numParts++;
|
|
537 | 622 |
|
538 |
start_ind = ind;
|
|
539 |
}
|
|
623 |
arrayCoords.add(new Coordinate(theData[0], theData[1]));
|
|
624 |
ind++;
|
|
540 | 625 |
|
541 |
numParts++;
|
|
626 |
break;
|
|
542 | 627 |
|
543 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
544 |
ind++; |
|
628 |
case PathIterator.SEG_LINETO: |
|
629 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
630 |
ind++; |
|
545 | 631 |
|
546 |
break;
|
|
632 |
break;
|
|
547 | 633 |
|
548 |
case PathIterator.SEG_LINETO: |
|
549 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
550 |
ind++; |
|
634 |
case PathIterator.SEG_QUADTO: |
|
635 |
logger.info("SEG_QUADTO Not supported here"); |
|
636 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
637 |
arrayCoords.add(new Coordinate(theData[2], theData[3])); |
|
638 |
ind++; |
|
639 |
ind++; |
|
551 | 640 |
|
552 |
break;
|
|
641 |
break;
|
|
553 | 642 |
|
554 |
case PathIterator.SEG_QUADTO: |
|
555 |
logger.info("SEG_QUADTO Not supported here"); |
|
556 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
557 |
arrayCoords.add(new Coordinate(theData[2], theData[3])); |
|
558 |
ind++; |
|
559 |
ind++; |
|
643 |
case PathIterator.SEG_CUBICTO: |
|
644 |
logger.info("SEG_CUBICTO Not supported here"); |
|
645 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
646 |
arrayCoords.add(new Coordinate(theData[2], theData[3])); |
|
647 |
arrayCoords.add(new Coordinate(theData[4], theData[5])); |
|
648 |
ind++; |
|
649 |
ind++; |
|
650 |
ind++; |
|
560 | 651 |
|
561 |
break;
|
|
652 |
break;
|
|
562 | 653 |
|
563 |
case PathIterator.SEG_CUBICTO: |
|
564 |
logger.info("SEG_CUBICTO Not supported here"); |
|
565 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
566 |
arrayCoords.add(new Coordinate(theData[2], theData[3])); |
|
567 |
arrayCoords.add(new Coordinate(theData[4], theData[5])); |
|
568 |
ind++; |
|
569 |
ind++; |
|
570 |
ind++; |
|
654 |
case PathIterator.SEG_CLOSE: |
|
571 | 655 |
|
572 |
break; |
|
656 |
// Coordinate firstCoord = (Coordinate) arrayCoords.get(0); |
|
657 |
// arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y)); |
|
658 |
break; |
|
659 |
} // end switch |
|
573 | 660 |
|
574 |
case PathIterator.SEG_CLOSE: |
|
661 |
theIterator.next(); |
|
662 |
} // end while loop |
|
575 | 663 |
|
576 |
// Coordinate firstCoord = (Coordinate) arrayCoords.get(0); |
|
577 |
// arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y)); |
|
578 |
break; |
|
579 |
} //end switch |
|
664 |
end_ind = ind - 1; |
|
580 | 665 |
|
581 |
theIterator.next(); |
|
582 |
} //end while loop |
|
666 |
// null shape: |
|
667 |
if (arrayCoords == null) { |
|
668 |
arrayCoords = new ArrayList(); |
|
583 | 669 |
|
584 |
end_ind = ind - 1; |
|
670 |
Coordinate _c = new Coordinate(0, 0, 0); |
|
671 |
arrayCoords.add(new Coordinate(_c)); |
|
672 |
arrayCoords.add(new Coordinate(_c)); |
|
673 |
} |
|
585 | 674 |
|
586 |
// null shape:
|
|
587 |
if (arrayCoords == null) {
|
|
588 |
arrayCoords = new ArrayList();
|
|
675 |
// --------------------------------------------
|
|
676 |
arrayCoords = getClosedRelevantPolygon(arrayCoords);
|
|
677 |
new_size = arrayCoords.size();
|
|
589 | 678 |
|
590 |
Coordinate _c = new Coordinate(0, 0, 0); |
|
591 |
arrayCoords.add(new Coordinate(_c)); |
|
592 |
arrayCoords.add(new Coordinate(_c)); |
|
593 |
} |
|
679 |
if (arrayCoords != null) { |
|
680 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
|
594 | 681 |
|
595 |
// --------------------------------------------
|
|
596 |
arrayCoords = getClosedRelevantPolygon(arrayCoords);
|
|
597 |
new_size = arrayCoords.size();
|
|
682 |
try {
|
|
683 |
LinearRing aux = geomFactory.createLinearRing(points);
|
|
684 |
double[] z = null;
|
|
598 | 685 |
|
599 |
if (arrayCoords != null) { |
|
600 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
|
686 |
if (threed) { |
|
687 |
z = getZ((Surface2DZ) mpolygon, start_ind, end_ind, |
|
688 |
new_size); |
|
689 |
} |
|
601 | 690 |
|
602 |
try { |
|
603 |
LinearRing aux = geomFactory.createLinearRing(points); |
|
604 |
double[] z = null; |
|
691 |
LineString3D ring = new LineString3D(aux, z); |
|
605 | 692 |
|
606 |
if (threed) { |
|
607 |
z = getZ((FPolygon3D) mpolygon, start_ind, end_ind, new_size); |
|
608 |
} |
|
693 |
if (CGAlgorithms.isCCW(points)) { |
|
694 |
resp.add(ring); |
|
695 |
} else { |
|
696 |
resp.add(ring.createReverse()); |
|
697 |
} |
|
698 |
} catch (Exception e) { |
|
699 |
logger.error("Topology exception: " + e.getMessage()); |
|
609 | 700 |
|
610 |
LineString3D ring = new LineString3D(aux, z); |
|
701 |
return null; |
|
702 |
} |
|
703 |
} |
|
611 | 704 |
|
612 |
if (CGAlgorithms.isCCW(points)) { |
|
613 |
resp.add(ring); |
|
614 |
} |
|
615 |
else { |
|
616 |
resp.add(ring.createReverse()); |
|
617 |
} |
|
618 |
} |
|
619 |
catch (Exception e) { |
|
620 |
logger.error("Topology exception: " + e.getMessage()); |
|
705 |
if (resp.size() == 0) { |
|
706 |
resp.add(new LineString3D(getMinLinearRing(onlyCoord), |
|
707 |
getMinLinearRingZ())); |
|
708 |
} |
|
621 | 709 |
|
622 |
return null; |
|
623 |
} |
|
624 |
} |
|
710 |
return resp; |
|
711 |
} |
|
625 | 712 |
|
626 |
if (resp.size() == 0) { |
|
627 |
resp.add(new LineString3D(getMinLinearRing(onlyCoord), |
|
628 |
getMinLinearRingZ())); |
|
629 |
} |
|
713 |
/** |
|
714 |
* Utility method to reverse an array of doubles. |
|
715 |
* |
|
716 |
* @param _z |
|
717 |
* an array of doubles to be reversed. |
|
718 |
* |
|
719 |
* @return the reversed array of doubles |
|
720 |
*/ |
|
721 |
public static double[] reverseArray(double[] _z) { |
|
722 |
int size = _z.length; |
|
723 |
double[] resp = new double[size]; |
|
630 | 724 |
|
631 |
return resp; |
|
632 |
} |
|
725 |
for (int i = 0; i < size; i++) { |
|
726 |
resp[i] = _z[size - 1 - i]; |
|
727 |
} |
|
633 | 728 |
|
634 |
/** |
|
635 |
* Utility method to reverse an array of doubles. |
|
636 |
* |
|
637 |
* @param _z an array of doubles to be reversed. |
|
638 |
* |
|
639 |
* @return the reversed array of doubles |
|
640 |
*/ |
|
641 |
public static double[] reverseArray(double[] _z) { |
|
642 |
int size = _z.length; |
|
643 |
double[] resp = new double[size]; |
|
729 |
return resp; |
|
730 |
} |
|
644 | 731 |
|
645 |
for (int i = 0; i < size; i++) { |
|
646 |
resp[i] = _z[size - 1 - i]; |
|
647 |
} |
|
732 |
/** |
|
733 |
* Utility method to reverse an array of coordinates |
|
734 |
* |
|
735 |
* @param _z |
|
736 |
* an array of coordinaes to be reversed. |
|
737 |
* |
|
738 |
* @return the reversed array of coordinates |
|
739 |
*/ |
|
740 |
public static Coordinate[] reverseCoordinateArray(Coordinate[] _z) { |
|
741 |
int size = _z.length; |
|
742 |
Coordinate[] resp = new Coordinate[size]; |
|
648 | 743 |
|
649 |
return resp; |
|
650 |
} |
|
744 |
for (int i = 0; i < size; i++) { |
|
745 |
resp[i] = _z[size - 1 - i]; |
|
746 |
} |
|
651 | 747 |
|
652 |
/** |
|
653 |
* Utility method to reverse an array of coordinates |
|
654 |
* |
|
655 |
* @param _z an array of coordinaes to be reversed. |
|
656 |
* |
|
657 |
* @return the reversed array of coordinates |
|
658 |
*/ |
|
659 |
public static Coordinate[] reverseCoordinateArray(Coordinate[] _z) { |
|
660 |
int size = _z.length; |
|
661 |
Coordinate[] resp = new Coordinate[size]; |
|
748 |
return resp; |
|
749 |
} |
|
662 | 750 |
|
663 |
for (int i = 0; i < size; i++) { |
|
664 |
resp[i] = _z[size - 1 - i]; |
|
665 |
} |
|
751 |
/** |
|
752 |
* |
|
753 |
* @param geom |
|
754 |
* @param _str |
|
755 |
* @param _end |
|
756 |
* @param size |
|
757 |
* @return |
|
758 |
*/ |
|
759 |
private static double[] getZ(Geometry geom, int _str, int _end, int size) { |
|
760 |
double[] resp = new double[size]; |
|
761 |
|
|
762 |
double[] allz = new double[1]; |
|
763 |
|
|
764 |
if(geom instanceof Point2DZ){ |
|
765 |
allz[0] = ((Point2DZ)geom).getCoordinateAt(3); |
|
766 |
} |
|
767 |
if(geom instanceof MultiPoint2DZ){ |
|
768 |
allz = ((MultiPoint2DZ)geom).getZs(); |
|
769 |
} |
|
770 |
if(geom instanceof Curve2DZ){ |
|
771 |
Curve2DZ curve = (Curve2DZ)geom; |
|
772 |
allz = new double[curve.getNumVertices()]; |
|
773 |
for (int i = 0; i < allz.length; i++) { |
|
774 |
allz[i] = curve.getCoordinateAt(i, 3); |
|
775 |
} |
|
776 |
} |
|
777 |
if(geom instanceof MultiCurve2DZ){ |
|
778 |
allz = ((MultiCurve2DZ)geom).getZs(); |
|
779 |
} |
|
780 |
if(geom instanceof Surface2DZ){ |
|
781 |
allz[0] = ((Surface2DZ)geom).getCoordinateAt(3); |
|
782 |
} |
|
783 |
if(geom instanceof MultiSurface2DZ){ |
|
784 |
allz = ((MultiSurface2DZ)geom).getZs(); |
|
785 |
} |
|
786 |
|
|
787 |
|
|
666 | 788 |
|
667 |
return resp; |
|
668 |
} |
|
789 |
for (int i = _str; ((i <= _end) && ((i - _str) < size)); i++) { |
|
790 |
resp[i - _str] = allz[i]; |
|
791 |
} |
|
669 | 792 |
|
670 |
private static double[] getZ(FShape3D p3d, int _str, int _end, int size) { |
|
671 |
double[] resp = new double[size]; |
|
672 |
double[] allz = p3d.getZs(); |
|
793 |
if ((_end - _str + 1) < size) { |
|
794 |
double repe = allz[_end]; |
|
673 | 795 |
|
674 |
for (int i = _str; ((i <= _end) && ((i - _str) < size)); i++) { |
|
675 |
resp[i - _str] = allz[i]; |
|
676 |
} |
|
796 |
for (int i = (_end - _str + 1); i < size; i++) { |
|
797 |
resp[i] = repe; |
|
798 |
} |
|
799 |
} |
|
677 | 800 |
|
678 |
if ((_end - _str + 1) < size) {
|
|
679 |
double repe = allz[_end];
|
|
801 |
return resp;
|
|
802 |
}
|
|
680 | 803 |
|
681 |
for (int i = (_end - _str + 1); i < size; i++) { |
|
682 |
resp[i] = repe; |
|
683 |
} |
|
684 |
} |
|
804 |
/** |
|
805 |
* |
|
806 |
* @param mlines |
|
807 |
* @return |
|
808 |
*/ |
|
809 |
private static List<LineString3D> getLineStrings(Geometry mlines) { |
|
810 |
boolean threed = false; |
|
685 | 811 |
|
686 |
return resp; |
|
687 |
} |
|
812 |
if (mlines instanceof Curve2DZ) { |
|
813 |
threed = true; |
|
814 |
} |
|
688 | 815 |
|
689 |
private static ArrayList getLineStrings(FShape mlines) { |
|
690 |
boolean threed = false; |
|
816 |
int start_ind = 0; |
|
817 |
int end_ind = 0; |
|
818 |
int ind = 0; |
|
819 |
int new_size = 0; |
|
691 | 820 |
|
692 |
if (mlines instanceof FPolyline3D) { |
|
693 |
threed = true; |
|
694 |
} |
|
821 |
LineString3D lin; |
|
695 | 822 |
|
696 |
int start_ind = 0; |
|
697 |
int end_ind = 0; |
|
698 |
int ind = 0; |
|
699 |
int new_size = 0; |
|
823 |
List<LineString3D> arrayLines = new ArrayList<LineString3D>(); |
|
824 |
PathIterator theIterator = mlines.getPathIterator(null, FLATNESS); |
|
825 |
int theType = -99; |
|
826 |
double[] theData = new double[6]; |
|
827 |
List arrayCoords = null; |
|
828 |
int numParts = 0; |
|
700 | 829 |
|
701 |
LineString3D lin; |
|
830 |
while (!theIterator.isDone()) { |
|
831 |
// while not done |
|
832 |
theType = theIterator.currentSegment(theData); |
|
702 | 833 |
|
703 |
ArrayList arrayLines = new ArrayList(); |
|
704 |
PathIterator theIterator = mlines.getPathIterator(null, FLATNESS); |
|
705 |
int theType = -99; |
|
706 |
double[] theData = new double[6]; |
|
707 |
ArrayList arrayCoords = null; |
|
708 |
int numParts = 0; |
|
834 |
switch (theType) { |
|
835 |
case PathIterator.SEG_MOVETO: |
|
709 | 836 |
|
710 |
while (!theIterator.isDone()) { |
|
711 |
//while not done |
|
712 |
theType = theIterator.currentSegment(theData); |
|
837 |
if (arrayCoords == null) { |
|
838 |
arrayCoords = new ArrayList(); |
|
839 |
} else { |
|
840 |
end_ind = ind - 1; |
|
841 |
arrayCoords = ensureSensibleLineString(arrayCoords); |
|
842 |
new_size = arrayCoords.size(); |
|
713 | 843 |
|
714 |
switch (theType) { |
|
715 |
case PathIterator.SEG_MOVETO: |
|
844 |
LineString aux = geomFactory |
|
845 |
.createLineString(CoordinateArrays |
|
846 |
.toCoordinateArray(arrayCoords)); |
|
847 |
double[] z = null; |
|
716 | 848 |
|
717 |
if (arrayCoords == null) { |
|
718 |
arrayCoords = new ArrayList(); |
|
719 |
} |
|
720 |
else { |
|
721 |
end_ind = ind - 1; |
|
722 |
arrayCoords = ensureSensibleLineString(arrayCoords); |
|
723 |
new_size = arrayCoords.size(); |
|
849 |
if (threed) { |
|
850 |
z = getZ((Curve2DZ) mlines, start_ind, end_ind, |
|
851 |
new_size); |
|
852 |
} |
|
724 | 853 |
|
725 |
LineString aux = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
|
|
726 |
arrayCoords));
|
|
727 |
double[] z = null;
|
|
854 |
lin = new LineString3D(aux, z);
|
|
855 |
arrayLines.add(lin);
|
|
856 |
arrayCoords = new ArrayList();
|
|
728 | 857 |
|
729 |
if (threed) { |
|
730 |
z = getZ((FPolyline3D) mlines, start_ind, end_ind, |
|
731 |
new_size); |
|
732 |
} |
|
858 |
start_ind = ind; |
|
859 |
} |
|
733 | 860 |
|
734 |
lin = new LineString3D(aux, z); |
|
735 |
arrayLines.add(lin); |
|
736 |
arrayCoords = new ArrayList(); |
|
861 |
numParts++; |
|
862 |
arrayCoords.add(new Coordinate(theData[0], theData[1])); |
|
737 | 863 |
|
738 |
start_ind = ind; |
|
739 |
} |
|
864 |
break; |
|
740 | 865 |
|
741 |
numParts++;
|
|
742 |
arrayCoords.add(new Coordinate(theData[0], theData[1]));
|
|
866 |
case PathIterator.SEG_LINETO:
|
|
867 |
arrayCoords.add(new Coordinate(theData[0], theData[1]));
|
|
743 | 868 |
|
744 |
break;
|
|
869 |
break;
|
|
745 | 870 |
|
746 |
case PathIterator.SEG_LINETO:
|
|
747 |
arrayCoords.add(new Coordinate(theData[0], theData[1]));
|
|
871 |
case PathIterator.SEG_QUADTO:
|
|
872 |
logger.info("Not supported here: SEG_QUADTO");
|
|
748 | 873 |
|
749 |
break;
|
|
874 |
break;
|
|
750 | 875 |
|
751 |
case PathIterator.SEG_QUADTO:
|
|
752 |
logger.info("Not supported here: SEG_QUADTO");
|
|
876 |
case PathIterator.SEG_CUBICTO:
|
|
877 |
logger.info("Not supported here: SEG_CUBICTO");
|
|
753 | 878 |
|
754 |
break;
|
|
879 |
break;
|
|
755 | 880 |
|
756 |
case PathIterator.SEG_CUBICTO: |
|
757 |
logger.info("Not supported here: SEG_CUBICTO"); |
|
881 |
case PathIterator.SEG_CLOSE: |
|
758 | 882 |
|
759 |
break; |
|
883 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0); |
|
884 |
arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y)); |
|
760 | 885 |
|
761 |
case PathIterator.SEG_CLOSE: |
|
886 |
break; |
|
887 |
} // end switch |
|
762 | 888 |
|
763 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0); |
|
764 |
arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y)); |
|
889 |
theIterator.next(); |
|
890 |
ind++; |
|
891 |
} // end while loop |
|
765 | 892 |
|
766 |
break;
|
|
767 |
} //end switch
|
|
893 |
arrayCoords = ensureSensibleLineString(arrayCoords);
|
|
894 |
new_size = arrayCoords.size();
|
|
768 | 895 |
|
769 |
theIterator.next();
|
|
770 |
ind++;
|
|
771 |
} //end while loop
|
|
896 |
LineString aux = geomFactory.createLineString(CoordinateArrays
|
|
897 |
.toCoordinateArray(arrayCoords));
|
|
898 |
double[] z = null;
|
|
772 | 899 |
|
773 |
arrayCoords = ensureSensibleLineString(arrayCoords); |
|
774 |
new_size = arrayCoords.size(); |
|
900 |
if (threed) { |
|
901 |
z = getZ((Curve2DZ) mlines, start_ind, end_ind, new_size); |
|
902 |
} |
|
775 | 903 |
|
776 |
LineString aux = geomFactory.createLineString(CoordinateArrays.toCoordinateArray( |
|
777 |
arrayCoords)); |
|
778 |
double[] z = null; |
|
904 |
lin = new LineString3D(aux, z); |
|
905 |
arrayLines.add(lin); |
|
779 | 906 |
|
780 |
if (threed) { |
|
781 |
z = getZ((FPolyline3D) mlines, start_ind, end_ind, new_size); |
|
782 |
} |
|
907 |
return arrayLines; |
|
908 |
} |
|
783 | 909 |
|
784 |
lin = new LineString3D(aux, z); |
|
785 |
arrayLines.add(lin); |
|
910 |
/** |
|
911 |
* |
|
912 |
* @param ls |
|
913 |
* @param threed |
|
914 |
* @return |
|
915 |
*/ |
|
916 |
private static String lineStringToWKT(LineString3D ls, boolean threed) { |
|
917 |
String resp = "("; |
|
918 |
Coordinate[] cc = ls.getLs().getCoordinates(); |
|
919 |
double[] z = ls.getZc(); |
|
920 |
int size = cc.length; |
|
786 | 921 |
|
787 |
return arrayLines; |
|
788 |
} |
|
922 |
if (threed) { |
|
923 |
for (int i = 0; i < size; i++) { |
|
924 |
resp = resp + cc[i].x + " " + cc[i].y + " " + z[i] + ", "; |
|
925 |
} |
|
789 | 926 |
|
790 |
private static String lineStringToWKT(LineString3D ls, boolean threed) { |
|
791 |
String resp = "("; |
|
792 |
Coordinate[] cc = ls.getLs().getCoordinates(); |
|
793 |
double[] z = ls.getZc(); |
|
794 |
int size = cc.length; |
|
927 |
resp = resp.substring(0, resp.length() - 2); |
|
928 |
resp = resp + ")"; |
|
929 |
} else { |
|
930 |
for (int i = 0; i < size; i++) { |
|
931 |
resp = resp + cc[i].x + " " + cc[i].y + ", "; |
|
932 |
} |
|
795 | 933 |
|
796 |
if (threed) { |
|
797 |
for (int i = 0; i < size; i++) { |
|
798 |
resp = resp + cc[i].x + " " + cc[i].y + " " + z[i] + ", "; |
|
799 |
} |
|
934 |
resp = resp.substring(0, resp.length() - 2); |
|
935 |
resp = resp + ")"; |
|
936 |
} |
|
800 | 937 |
|
801 |
resp = resp.substring(0, resp.length() - 2); |
|
802 |
resp = resp + ")"; |
|
803 |
} |
|
804 |
else { |
|
805 |
for (int i = 0; i < size; i++) { |
|
806 |
resp = resp + cc[i].x + " " + cc[i].y + ", "; |
|
807 |
} |
|
938 |
return resp; |
|
939 |
} |
|
808 | 940 |
|
809 |
resp = resp.substring(0, resp.length() - 2); |
|
810 |
resp = resp + ")"; |
|
811 |
} |
|
941 |
/** |
|
942 |
* |
|
943 |
* @param ml |
|
944 |
* @param threed |
|
945 |
* @return |
|
946 |
*/ |
|
947 |
private static String multiLineStringToWKT(List ml, boolean threed) { |
|
948 |
String resp = "MULTILINESTRING("; |
|
812 | 949 |
|
813 |
return resp; |
|
814 |
} |
|
950 |
for (int i = 0; i < ml.size(); i++) { |
|
951 |
LineString3D ls = (LineString3D) ml.get(i); |
|
952 |
resp = resp + lineStringToWKT(ls, threed) + ", "; |
|
953 |
} |
|
815 | 954 |
|
816 |
private static String multiLineStringToWKT(ArrayList ml, boolean threed) { |
|
817 |
String resp = "MULTILINESTRING("; |
|
955 |
resp = resp.substring(0, resp.length() - 2) + ")"; |
|
818 | 956 |
|
819 |
for (int i = 0; i < ml.size(); i++) { |
|
820 |
LineString3D ls = (LineString3D) ml.get(i); |
|
821 |
resp = resp + lineStringToWKT(ls, threed) + ", "; |
|
822 |
} |
|
957 |
return resp; |
|
958 |
} |
|
823 | 959 |
|
824 |
resp = resp.substring(0, resp.length() - 2) + ")"; |
|
960 |
/** |
|
961 |
* |
|
962 |
* @param pols |
|
963 |
* @param threed |
|
964 |
* @return |
|
965 |
*/ |
|
966 |
private static String polygonsToWKT(List pols, boolean threed) { |
|
967 |
String resp = "MULTIPOLYGON("; |
|
968 |
LineString3D ls = null; |
|
825 | 969 |
|
826 |
return resp; |
|
827 |
} |
|
970 |
for (int i = 0; i < pols.size(); i++) { |
|
971 |
ls = (LineString3D) pols.get(i); |
|
972 |
resp = resp + "(" + lineStringToWKT(ls, threed) + "), "; |
|
973 |
} |
|
828 | 974 |
|
829 |
private static String polygonsToWKT(ArrayList pols, boolean threed) { |
|
830 |
String resp = "MULTIPOLYGON("; |
|
831 |
LineString3D ls = null; |
|
975 |
resp = resp.substring(0, resp.length() - 2) + ")"; |
|
832 | 976 |
|
833 |
for (int i = 0; i < pols.size(); i++) { |
|
834 |
ls = (LineString3D) pols.get(i); |
|
835 |
resp = resp + "(" + lineStringToWKT(ls, threed) + "), "; |
|
836 |
} |
|
977 |
return resp; |
|
978 |
} |
|
837 | 979 |
|
838 |
resp = resp.substring(0, resp.length() - 2) + ")"; |
|
980 |
/** |
|
981 |
* |
Also available in: Unified diff