svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / DefaultGeometryManager.java @ 42267
History | View | Annotate | Download (29.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.geom.jts; |
26 |
|
27 |
import java.awt.geom.PathIterator; |
28 |
import java.util.ArrayList; |
29 |
import java.util.HashMap; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import java.util.Map; |
33 |
|
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
39 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
40 |
import org.gvsig.fmap.geom.GeometryException; |
41 |
import org.gvsig.fmap.geom.GeometryLocator; |
42 |
import org.gvsig.fmap.geom.GeometryManager; |
43 |
import org.gvsig.fmap.geom.SpatialIndex; |
44 |
import org.gvsig.fmap.geom.SpatialIndexFactory; |
45 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
46 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
47 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
48 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
49 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
50 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
51 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
52 |
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
53 |
import org.gvsig.fmap.geom.jts.primitive.Envelope2D; |
54 |
import org.gvsig.fmap.geom.jts.primitive.Envelope3D; |
55 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
56 |
import org.gvsig.fmap.geom.operation.GeometryOperation; |
57 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
58 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
59 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
60 |
import org.gvsig.fmap.geom.primitive.Curve; |
61 |
import org.gvsig.fmap.geom.primitive.Envelope; |
62 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
63 |
import org.gvsig.fmap.geom.primitive.IGeneralPathX; |
64 |
import org.gvsig.fmap.geom.primitive.NullGeometry; |
65 |
import org.gvsig.fmap.geom.primitive.Point; |
66 |
import org.gvsig.fmap.geom.primitive.PointGeometryType; |
67 |
import org.gvsig.fmap.geom.primitive.Surface; |
68 |
import org.gvsig.fmap.geom.primitive.Line; |
69 |
import org.gvsig.fmap.geom.primitive.Polygon; |
70 |
import org.gvsig.fmap.geom.type.GeometryType; |
71 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
72 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
73 |
import org.gvsig.tools.dynobject.DynObject; |
74 |
import org.gvsig.tools.service.Service; |
75 |
import org.gvsig.tools.service.ServiceException; |
76 |
import org.gvsig.tools.service.spi.ServiceFactory; |
77 |
|
78 |
/**
|
79 |
* Default implementation for the {@link GeometryManager}. When the
|
80 |
* application starts, this class is registered in the {@link GeometryLocator}
|
81 |
* using the {@link DefaultGeometryLibrary}.
|
82 |
*
|
83 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
84 |
*/
|
85 |
|
86 |
public class DefaultGeometryManager implements GeometryManager { |
87 |
|
88 |
private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class); |
89 |
private double flatness = 0.8; |
90 |
|
91 |
private Map spatialIndexFactories = new HashMap(); |
92 |
|
93 |
/**
|
94 |
* This list holds the unique name of all registered geometry operations.
|
95 |
* The index in which they are stored is also the operation code used to
|
96 |
* invoke each one of them
|
97 |
*/
|
98 |
private List geometryOperations = new ArrayList(); |
99 |
|
100 |
/**
|
101 |
* Common operations are registered here. Type specific operations are
|
102 |
* registered in the corresponding GeometryType instance
|
103 |
*/
|
104 |
// private List commonOperations = new ArrayList();
|
105 |
|
106 |
/**
|
107 |
* This map holds the instances of all registered GeometryType. The key is
|
108 |
* the name of the specific Geometry subclass.
|
109 |
* In other words, the string "org.gvsig.fmap.geom.primitive.Point2D" is the
|
110 |
* hash key to obtain an instance of GeometryType holding the
|
111 |
* operations associated to the class org.gvsig.fmap.geom.primitive.Point2D.
|
112 |
*/
|
113 |
private Map geometryTypeName = new HashMap(); |
114 |
|
115 |
/**
|
116 |
* Matrix of geometry types by type (row) and subtype (column). This matrix
|
117 |
* will contain null values in the cells where a GeometryType hasn't been
|
118 |
* registered.
|
119 |
*/
|
120 |
private GeometryType[][] geometryTypes; |
121 |
|
122 |
// Initially create a matrix of 17 x 6, which are the current default
|
123 |
// types and subtypes. If another type or subtype is registered, the
|
124 |
// matrix will grow as needed
|
125 |
private static final int DEFAULT_TYPES_SIZE = 17; |
126 |
private static final int DEFAULT_SUBTYPES_SIZE = 6; |
127 |
|
128 |
public DefaultGeometryManager() throws GeometryException { |
129 |
this(DEFAULT_TYPES_SIZE, DEFAULT_SUBTYPES_SIZE);
|
130 |
} |
131 |
|
132 |
public DefaultGeometryManager(int initialTypesSize, int initialSubtypesSize) throws GeometryException { |
133 |
geometryTypes = new GeometryType[initialTypesSize][initialSubtypesSize];
|
134 |
this.addServiceFactory(new SpatialIndexFactoryJTS()); |
135 |
this.addServiceFactory(new SpatialIndexFactoryJSI()); |
136 |
} |
137 |
|
138 |
public int registerGeometryOperation(String geomOpName, GeometryOperation geomOp, GeometryType geomType) { |
139 |
if (geomOp == null) { |
140 |
throw new IllegalArgumentException("geomOp cannot be null."); |
141 |
} |
142 |
if (geomType == null) { |
143 |
throw new IllegalArgumentException("geomType cannot be null."); |
144 |
} |
145 |
|
146 |
int index = getGeometryOperationCode(geomOpName);
|
147 |
|
148 |
geomType.setGeometryOperation(index, geomOp); |
149 |
|
150 |
return index;
|
151 |
} |
152 |
|
153 |
public int registerGeometryOperation(String geomOpName, GeometryOperation geomOp) { |
154 |
if (geomOpName == null) { |
155 |
throw new IllegalArgumentException("geomOpName cannot be null."); |
156 |
} |
157 |
if (geomOp == null) { |
158 |
throw new IllegalArgumentException("geomOp cannot be null."); |
159 |
} |
160 |
|
161 |
int index = getGeometryOperationCode(geomOpName);
|
162 |
|
163 |
Iterator it = geometryTypeName.values().iterator();
|
164 |
while (it.hasNext()) {
|
165 |
GeometryType geometryType = (GeometryType) it.next(); |
166 |
registerGeometryOperation(geomOpName, geomOp, geometryType); |
167 |
} |
168 |
|
169 |
return index;
|
170 |
|
171 |
} |
172 |
|
173 |
public int registerGeometryOperation(String geomOpName, GeometryOperation geomOp, int type, int subType) |
174 |
throws GeometryTypeNotSupportedException, GeometryTypeNotValidException {
|
175 |
GeometryType geometryType = getGeometryType(type, subType); |
176 |
return registerGeometryOperation(geomOpName, geomOp, geometryType);
|
177 |
} |
178 |
|
179 |
public int registerGeometryOperation(String geomOpName, GeometryOperation geomOp, int type) { |
180 |
int code = -1; |
181 |
Iterator it = geometryTypeName.values().iterator();
|
182 |
while (it.hasNext()) {
|
183 |
GeometryType geometryType = (GeometryType) it.next(); |
184 |
if ((type == geometryType.getType())) {
|
185 |
code = registerGeometryOperation(geomOpName, geomOp, geometryType); |
186 |
} |
187 |
} |
188 |
return code;
|
189 |
} |
190 |
|
191 |
public int registerGeometryOperationBySubtype(String geomOpName, GeometryOperation geomOp, int subType) { |
192 |
int code = -1; |
193 |
Iterator it = geometryTypeName.values().iterator();
|
194 |
while (it.hasNext()) {
|
195 |
GeometryType geometryType = (GeometryType) it.next(); |
196 |
if ((subType == geometryType.getSubType())) {
|
197 |
code = registerGeometryOperation(geomOpName, geomOp, geometryType); |
198 |
} |
199 |
} |
200 |
return code;
|
201 |
} |
202 |
|
203 |
public int registerGeometryOperationBySuperType(String geomOpName, GeometryOperation geomOp, int superType) { |
204 |
int code = -1; |
205 |
Iterator it = geometryTypeName.values().iterator();
|
206 |
while (it.hasNext()) {
|
207 |
GeometryType geometryType = (GeometryType) it.next(); |
208 |
if (geometryType.isTypeOf(superType)) {
|
209 |
code = registerGeometryOperation(geomOpName, geomOp, geometryType); |
210 |
} |
211 |
} |
212 |
return code;
|
213 |
} |
214 |
|
215 |
public int registerGeometryOperationBySuperSubType(String geomOpName, GeometryOperation geomOp, int superSubType) { |
216 |
int code = -1; |
217 |
Iterator it = geometryTypeName.values().iterator();
|
218 |
while (it.hasNext()) {
|
219 |
GeometryType geometryType = (GeometryType) it.next(); |
220 |
if (geometryType.isSubTypeOf(superSubType)) {
|
221 |
code = registerGeometryOperation(geomOpName, geomOp, geometryType); |
222 |
} |
223 |
} |
224 |
return code;
|
225 |
} |
226 |
|
227 |
public GeometryType registerGeometryType(Class geomClass, String name, int type, int subType) { |
228 |
return registerGeometryType(geomClass, name, type, subType, new int[0], new int[0]); |
229 |
} |
230 |
|
231 |
public GeometryType registerGeometryType(Class geomClass, String name, int type, int subType, int superType, |
232 |
int superSubType) {
|
233 |
return registerGeometryType(geomClass, name, type, subType, new int[] { superType }, new int[] { superSubType }); |
234 |
} |
235 |
|
236 |
public GeometryType registerGeometryType(Class geomClass, String name, int type, int subType, int superType) { |
237 |
return registerGeometryType(geomClass, name, type, subType, new int[] { superType }, new int[0]); |
238 |
} |
239 |
|
240 |
public GeometryType registerGeometryType(Class geomClass, String name, int type, int subType, int[] superTypes) { |
241 |
return registerGeometryType(geomClass, name, type, subType, superTypes, new int[0]); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Registers a Geometry implementation class with a predefined geometry type
|
246 |
* and returns the
|
247 |
* associated GeometryType instance. Available predefined types are defined
|
248 |
* in {@link Geometry.TYPES} If the class is already registered then this
|
249 |
* method throws an IllegalArgumentException.<br>
|
250 |
*
|
251 |
* How to register a geometry class with a predefined type:
|
252 |
*
|
253 |
* <pre>
|
254 |
*
|
255 |
* public class Point2D implements Point {
|
256 |
* private static final GeometryType geomType = GeometryManager.getInstance()
|
257 |
* .registerBasicGeometryType(Point2D.class, "Point2D", Geometry.TYPES.POINT);
|
258 |
*
|
259 |
* ...
|
260 |
* public int getType() {
|
261 |
* return geomType.getType();
|
262 |
* }
|
263 |
* }
|
264 |
* </pre>
|
265 |
*
|
266 |
* @param geomClass
|
267 |
* Geometry subclass. It must not be null and must implement
|
268 |
* Geometry, otherwise an exception
|
269 |
* is raised.
|
270 |
* @param name
|
271 |
* Symbolic name for the geometry type, it can be null. If it is
|
272 |
* null then the symbolic name
|
273 |
* will be the simple class name.
|
274 |
* @param id
|
275 |
* Geometry identifier.
|
276 |
* @param type
|
277 |
* Type of geometry. Must be a value defined in
|
278 |
* {@link Geometry.TYPES}
|
279 |
* @param subType
|
280 |
* SubType of geometry. Must be a value defined in
|
281 |
* {@link Geometry.SUBTYPES}
|
282 |
* @return Instance of GeometryType associated to the Geometry
|
283 |
* implementation class
|
284 |
* geomClass
|
285 |
* @throws IllegalArgumentException
|
286 |
* If geomClass is null or does not implement Geometry
|
287 |
*/
|
288 |
public GeometryType registerGeometryType(Class geomClass, String name, int type, int subType, int[] superTypes, |
289 |
int superSubTypes[]) { |
290 |
if (geomClass == null) { |
291 |
throw new IllegalArgumentException("geomClass cannot be null."); |
292 |
} |
293 |
|
294 |
if (!Geometry.class.isAssignableFrom(geomClass)) {
|
295 |
throw new IllegalArgumentException(geomClass.getName() + " must implement the Geometry interface"); |
296 |
} |
297 |
|
298 |
// Check if it is registered
|
299 |
GeometryType geomType = null;
|
300 |
if (type >= geometryTypes.length || subType >= geometryTypes[0].length |
301 |
|| (geomType = geometryTypes[type][subType]) == null) {
|
302 |
geomType = new DefaultGeometryType(geomClass, name, type, subType, superTypes, superSubTypes);
|
303 |
registerGeometryType(geomType); |
304 |
} |
305 |
|
306 |
logger.debug("Class {} registered with name {}", geomClass, geomType.getName());
|
307 |
|
308 |
return geomType;
|
309 |
} |
310 |
|
311 |
public void registerGeometryType(GeometryType geometryType) { |
312 |
if (geometryType.getType() >= geometryTypes.length || geometryType.getSubType() >= geometryTypes[0].length) { |
313 |
|
314 |
// Recreate the geometry type matrix if the types
|
315 |
// or subtypes don't fit
|
316 |
int newTypesSize =
|
317 |
geometryType.getType() < geometryTypes.length ? geometryTypes.length : geometryType.getType() + 1;
|
318 |
int newSubTypesSize =
|
319 |
geometryType.getSubType() < geometryTypes[0].length ? geometryTypes[0].length : geometryType |
320 |
.getSubType() + 1;
|
321 |
GeometryType[][] newMatrix = new GeometryType[newTypesSize][newSubTypesSize]; |
322 |
|
323 |
for (int i = 0; i < geometryTypes.length; i++) { |
324 |
System.arraycopy(geometryTypes[i], 0, newMatrix[i], 0, geometryTypes[i].length); |
325 |
} |
326 |
geometryTypes = newMatrix; |
327 |
} |
328 |
|
329 |
geometryTypes[geometryType.getType()][geometryType.getSubType()] = geometryType; |
330 |
geometryTypeName.put(geometryType.getName(), geometryType); |
331 |
} |
332 |
|
333 |
public GeometryType registerGeometryType(Class geomClass, int type, int subType) { |
334 |
return registerGeometryType(geomClass, null, type, subType); |
335 |
} |
336 |
|
337 |
public GeometryType getGeometryType(int type, int subType) throws GeometryTypeNotSupportedException, |
338 |
GeometryTypeNotValidException { |
339 |
GeometryType gType = null;
|
340 |
if (type >= geometryTypes.length || subType >= geometryTypes[0].length) { |
341 |
throw new GeometryTypeNotValidException(type, subType); |
342 |
} |
343 |
|
344 |
gType = geometryTypes[type][subType]; |
345 |
|
346 |
if (gType == null) { |
347 |
throw new GeometryTypeNotSupportedException(type, subType); |
348 |
} |
349 |
|
350 |
return gType;
|
351 |
} |
352 |
|
353 |
public Geometry create(GeometryType geomType) throws CreateGeometryException { |
354 |
return geomType.create();
|
355 |
} |
356 |
|
357 |
public Geometry create(String name) throws CreateGeometryException { |
358 |
if (!geometryTypeName.containsKey(name)) {
|
359 |
throw new IllegalArgumentException(name + " has not been registered yet."); |
360 |
} |
361 |
return ((GeometryType) geometryTypeName.get(name)).create();
|
362 |
} |
363 |
|
364 |
public Geometry create(int type, int subType) throws CreateGeometryException { |
365 |
try {
|
366 |
return getGeometryType(type, subType).create();
|
367 |
} catch (GeometryException e) {
|
368 |
throw new CreateGeometryException(type, subType, e); |
369 |
} |
370 |
} |
371 |
|
372 |
public Curve createCurve(GeneralPathX generalPathX, int subType) throws CreateGeometryException { |
373 |
Curve curve = (Curve) create(TYPES.CURVE, subType); |
374 |
curve.setGeneralPath(generalPathX); |
375 |
return curve;
|
376 |
} |
377 |
|
378 |
public NullGeometry createNullGeometry(int subType) throws CreateGeometryException { |
379 |
NullGeometry nullGeom = (NullGeometry) create(TYPES.NULL, subType); |
380 |
return nullGeom;
|
381 |
} |
382 |
|
383 |
public Point createPoint(double x, double y, int subType) throws CreateGeometryException { |
384 |
Point point = (Point) create(TYPES.POINT, subType); |
385 |
point.setX(x); |
386 |
point.setY(y); |
387 |
return point;
|
388 |
} |
389 |
|
390 |
public Surface createSurface(GeneralPathX generalPathX, int subType) throws CreateGeometryException { |
391 |
Surface surface = (Surface) create(TYPES.SURFACE, subType); |
392 |
surface.setGeneralPath(generalPathX); |
393 |
return surface;
|
394 |
} |
395 |
|
396 |
public GeometryOperation getGeometryOperation(int opCode, int type, int subType) |
397 |
throws GeometryTypeNotSupportedException, GeometryOperationNotSupportedException, GeometryTypeNotValidException {
|
398 |
GeometryType geometryType = getGeometryType(type, subType); |
399 |
return geometryType.getGeometryOperation(opCode);
|
400 |
} |
401 |
|
402 |
public GeometryOperation getGeometryOperation(int opCode) throws GeometryOperationNotSupportedException { |
403 |
if (opCode < 0) { |
404 |
throw new GeometryOperationNotSupportedException(opCode); |
405 |
} |
406 |
GeometryType type = (GeometryType) geometryTypeName.get(DefaultNullGeometry.class.getName()); |
407 |
if (type == null) { |
408 |
throw new GeometryOperationNotSupportedException(opCode); |
409 |
} |
410 |
return type.getGeometryOperation(opCode);
|
411 |
} |
412 |
|
413 |
public Object invokeOperation(int opCode, Geometry geom, GeometryOperationContext ctx) |
414 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
415 |
GeometryOperation geomOp = geom.getGeometryType().getGeometryOperation(opCode); |
416 |
|
417 |
if (geomOp != null) { |
418 |
return geomOp.invoke(geom, ctx);
|
419 |
} |
420 |
|
421 |
throw new GeometryOperationNotSupportedException(opCode, geom.getGeometryType()); |
422 |
} |
423 |
|
424 |
public Object invokeOperation(String geomOpName, Geometry geom, GeometryOperationContext ctx) |
425 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
426 |
int index = geometryOperations.indexOf(geomOpName);
|
427 |
if (index == -1) { |
428 |
throw new GeometryOperationNotSupportedException(-1); |
429 |
} |
430 |
return invokeOperation(index, geom, ctx);
|
431 |
} |
432 |
|
433 |
public Object invokeOperation(String geomOpName, GeometryOperationContext ctx) |
434 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
435 |
int index = geometryOperations.indexOf(geomOpName);
|
436 |
GeometryOperation geomOp = getGeometryOperation(index); |
437 |
return geomOp.invoke(null, ctx); |
438 |
} |
439 |
|
440 |
public Envelope createEnvelope(int subType) { |
441 |
// TODO: register the envelopes!!!
|
442 |
switch (subType) {
|
443 |
case SUBTYPES.GEOM3D:
|
444 |
return new Envelope3D(); |
445 |
default:
|
446 |
return new Envelope2D(); |
447 |
} |
448 |
} |
449 |
|
450 |
public Envelope createEnvelope(double minX, double minY, double maxX, double maxY, int subType) |
451 |
throws CreateEnvelopeException {
|
452 |
org.gvsig.fmap.geom.primitive.Point min = null;
|
453 |
org.gvsig.fmap.geom.primitive.Point max = null;
|
454 |
try {
|
455 |
PointGeometryType gType = (PointGeometryType) getGeometryType(TYPES.POINT, subType); |
456 |
min = gType.createPoint(minX, minY); |
457 |
max = gType.createPoint(maxX, maxY); |
458 |
} catch (GeometryTypeNotSupportedException e) {
|
459 |
throw new CreateEnvelopeException(subType, e); |
460 |
} catch (GeometryTypeNotValidException e) {
|
461 |
throw new CreateEnvelopeException(subType, e); |
462 |
} |
463 |
switch (subType) {
|
464 |
case SUBTYPES.GEOM2D:
|
465 |
case SUBTYPES.GEOM2DM:
|
466 |
// Small optimization to directly create an Envelope2D
|
467 |
// return new Envelope2D(minX, minY, maxX, maxY);
|
468 |
return new Envelope2D(min, max); |
469 |
default:
|
470 |
Envelope envelope = createEnvelope(subType); |
471 |
envelope.setLowerCorner(min); |
472 |
envelope.setUpperCorner(max); |
473 |
return envelope;
|
474 |
} |
475 |
} |
476 |
|
477 |
public MultiCurve createMultiCurve(GeneralPathX generalPathX, int subType) throws CreateGeometryException { |
478 |
if (subType != SUBTYPES.GEOM2D) {
|
479 |
// FIXME Exception
|
480 |
throw new UnsupportedOperationException(); |
481 |
} |
482 |
MultiCurve multiCurve = (MultiCurve) create(TYPES.MULTICURVE, subType); |
483 |
PathIterator piter = generalPathX.getPathIterator(null); |
484 |
GeneralPathX tmpPath = null;
|
485 |
Curve tmpCurve = null;
|
486 |
double[] coords = new double[6]; |
487 |
double[] first = new double[6]; |
488 |
int type;
|
489 |
while (!piter.isDone()) {
|
490 |
type = piter.currentSegment(coords); |
491 |
switch (type) {
|
492 |
case PathIterator.SEG_MOVETO: |
493 |
if (tmpPath != null) { |
494 |
tmpCurve = createCurve(tmpPath, subType); |
495 |
multiCurve.addCurve(tmpCurve); |
496 |
} |
497 |
System.arraycopy(coords, 0, first, 0, 2); |
498 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
499 |
tmpPath.moveTo(coords[0], coords[1]); |
500 |
break;
|
501 |
|
502 |
case PathIterator.SEG_LINETO: |
503 |
if (tmpPath == null) { |
504 |
System.arraycopy(coords, 0, first, 0, 2); |
505 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
506 |
} |
507 |
tmpPath.lineTo(coords[0], coords[1]); |
508 |
break;
|
509 |
|
510 |
case PathIterator.SEG_QUADTO: |
511 |
if (tmpPath == null) { |
512 |
System.arraycopy(coords, 0, first, 0, 2); |
513 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
514 |
} |
515 |
tmpPath.quadTo(coords[0], coords[1], coords[2], coords[3]); |
516 |
break;
|
517 |
|
518 |
case PathIterator.SEG_CUBICTO: |
519 |
if (tmpPath == null) { |
520 |
System.arraycopy(coords, 0, first, 0, 2); |
521 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
522 |
} |
523 |
tmpPath.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); |
524 |
break;
|
525 |
|
526 |
case PathIterator.SEG_CLOSE: |
527 |
tmpPath.lineTo(first[0], first[1]); |
528 |
break;
|
529 |
|
530 |
} // end switch
|
531 |
|
532 |
piter.next(); |
533 |
|
534 |
} |
535 |
if (tmpPath != null) { |
536 |
tmpCurve = createCurve(tmpPath, subType); |
537 |
multiCurve.addCurve(tmpCurve); |
538 |
} |
539 |
|
540 |
return multiCurve;
|
541 |
|
542 |
} |
543 |
|
544 |
public MultiSurface createMultiSurface(GeneralPathX generalPathX, int subType) throws CreateGeometryException { |
545 |
if (subType != SUBTYPES.GEOM2D) {
|
546 |
// FIXME Exception
|
547 |
throw new UnsupportedOperationException(); |
548 |
} |
549 |
MultiSurface multiSurface = (MultiSurface) create(TYPES.MULTISURFACE, subType); |
550 |
PathIterator piter = generalPathX.getPathIterator(null); |
551 |
GeneralPathX tmpPath = null;
|
552 |
Surface tmpSurface = null;
|
553 |
double[] coords = new double[6]; |
554 |
double[] first = new double[6]; |
555 |
int type;
|
556 |
while (!piter.isDone()) {
|
557 |
type = piter.currentSegment(coords); |
558 |
switch (type) {
|
559 |
case PathIterator.SEG_MOVETO: |
560 |
if (tmpPath != null) { |
561 |
tmpSurface = createSurface(tmpPath, subType); |
562 |
multiSurface.addSurface(tmpSurface); |
563 |
} |
564 |
System.arraycopy(coords, 0, first, 0, 2); |
565 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
566 |
tmpPath.moveTo(coords[0], coords[1]); |
567 |
|
568 |
case PathIterator.SEG_LINETO: |
569 |
if (tmpPath == null) { |
570 |
System.arraycopy(coords, 0, first, 0, 2); |
571 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
572 |
} |
573 |
tmpPath.lineTo(coords[0], coords[1]); |
574 |
break;
|
575 |
|
576 |
case PathIterator.SEG_QUADTO: |
577 |
if (tmpPath == null) { |
578 |
System.arraycopy(coords, 0, first, 0, 2); |
579 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
580 |
} |
581 |
tmpPath.quadTo(coords[0], coords[1], coords[2], coords[3]); |
582 |
break;
|
583 |
|
584 |
case PathIterator.SEG_CUBICTO: |
585 |
if (tmpPath == null) { |
586 |
System.arraycopy(coords, 0, first, 0, 2); |
587 |
tmpPath = new GeneralPathX(piter.getWindingRule());
|
588 |
} |
589 |
tmpPath.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); |
590 |
break;
|
591 |
|
592 |
case PathIterator.SEG_CLOSE: |
593 |
tmpPath.lineTo(first[0], first[1]); |
594 |
break;
|
595 |
} // end switch
|
596 |
|
597 |
piter.next(); |
598 |
|
599 |
} |
600 |
if (tmpPath != null) { |
601 |
tmpSurface = createSurface(tmpPath, subType); |
602 |
multiSurface.addSurface(tmpSurface); |
603 |
} |
604 |
|
605 |
return multiSurface;
|
606 |
|
607 |
} |
608 |
|
609 |
public int getGeometryOperationCode(String geomOpName) { |
610 |
if (geomOpName == null) { |
611 |
throw new IllegalArgumentException("geomOpName cannot be null."); |
612 |
} |
613 |
|
614 |
int index = geometryOperations.indexOf(geomOpName);
|
615 |
if (index == -1) { |
616 |
geometryOperations.add(geomOpName); |
617 |
index = geometryOperations.indexOf(geomOpName); |
618 |
} |
619 |
return index;
|
620 |
} |
621 |
|
622 |
public List getGeometryOperationNames() { |
623 |
List operations = new ArrayList(); |
624 |
for (int i = 0; i < operations.size(); i++) { |
625 |
operations.add(geometryOperations.get(i)); |
626 |
} |
627 |
return operations;
|
628 |
} |
629 |
|
630 |
public double getFlatness() { |
631 |
return flatness;
|
632 |
} |
633 |
|
634 |
public void setFlatness(double flatness) { |
635 |
this.flatness = flatness;
|
636 |
} |
637 |
|
638 |
public Geometry createFrom(String wkt, String srs) throws GeometryException { |
639 |
GeometryOperationContext context = new GeometryOperationContext();
|
640 |
context.setAttribute("text", wkt);
|
641 |
context.setAttribute("srs", srs);
|
642 |
|
643 |
try {
|
644 |
return (Geometry) this.invokeOperation(OPERATIONS.FROMWKT, context); |
645 |
} catch (GeometryOperationNotSupportedException e) {
|
646 |
throw new GeometryException(e); |
647 |
} catch (GeometryOperationException e) {
|
648 |
throw new GeometryException(e); |
649 |
} |
650 |
} |
651 |
|
652 |
public Geometry createFrom(com.vividsolutions.jts.geom.Geometry geom) throws GeometryException { |
653 |
return JTSUtils.createGeometry(geom);
|
654 |
} |
655 |
|
656 |
public Geometry createFrom(String wkt) throws GeometryException { |
657 |
GeometryOperationContext context = new GeometryOperationContext();
|
658 |
context.setAttribute("text", wkt);
|
659 |
context.setAttribute("srs", null); |
660 |
|
661 |
try {
|
662 |
return (Geometry) this.invokeOperation(OPERATIONS.FROMWKT, context); |
663 |
} catch (GeometryOperationNotSupportedException e) {
|
664 |
throw new GeometryException(e); |
665 |
} catch (GeometryOperationException e) {
|
666 |
throw new GeometryException(e); |
667 |
} |
668 |
} |
669 |
|
670 |
public Geometry createFrom(byte[] wkb) throws GeometryException { |
671 |
GeometryOperationContext context = new GeometryOperationContext();
|
672 |
context.setAttribute("data", wkb);
|
673 |
try {
|
674 |
return (Geometry) this.invokeOperation(OPERATIONS.FROMWKB, context); |
675 |
} catch (GeometryOperationNotSupportedException e) {
|
676 |
throw new GeometryException(e); |
677 |
} catch (GeometryOperationException e) {
|
678 |
throw new GeometryException(e); |
679 |
} |
680 |
} |
681 |
|
682 |
public IGeneralPathX createGeneralPath(int rule, PathIterator pathIterator) { |
683 |
if (pathIterator == null) { |
684 |
return new DefaultGeneralPathX(rule); |
685 |
} |
686 |
return new DefaultGeneralPathX(pathIterator, false, 0); |
687 |
} |
688 |
|
689 |
public MultiPoint createMultiPoint(int subType) throws CreateGeometryException { |
690 |
return (MultiPoint) create(TYPES.MULTIPOINT, subType);
|
691 |
} |
692 |
|
693 |
public Line createLine(int subType) throws CreateGeometryException { |
694 |
return (Line) create(TYPES.CURVE, subType); |
695 |
} |
696 |
|
697 |
public Curve createCurve(int subType) throws CreateGeometryException { |
698 |
return (Curve) create(TYPES.CURVE, subType);
|
699 |
} |
700 |
|
701 |
public MultiCurve createMultiCurve(int subType) throws CreateGeometryException { |
702 |
return (MultiCurve) create(TYPES.MULTICURVE, subType);
|
703 |
} |
704 |
|
705 |
public MultiLine createMultiLine(int subType) throws CreateGeometryException { |
706 |
return (MultiLine) create(TYPES.MULTICURVE, subType);
|
707 |
} |
708 |
|
709 |
public MultiSurface createMultiSurface(int subType) throws CreateGeometryException { |
710 |
return (MultiSurface) create(TYPES.MULTISURFACE, subType);
|
711 |
} |
712 |
|
713 |
public MultiPolygon createMultiPolygon(int subType) throws CreateGeometryException { |
714 |
return (MultiPolygon) create(TYPES.MULTISURFACE, subType);
|
715 |
} |
716 |
|
717 |
public Polygon createPolygon(int subType) throws CreateGeometryException { |
718 |
return (Polygon) create(TYPES.SURFACE, subType); |
719 |
} |
720 |
|
721 |
public Surface createSurface(int subType) throws CreateGeometryException { |
722 |
return (Surface) create(TYPES.SURFACE, subType);
|
723 |
} |
724 |
|
725 |
public SpatialIndex createDefaultMemorySpatialIndex() throws ServiceException { |
726 |
return this.createSpatialIndex(SpatialIndexFactoryJTS.NAME, null); |
727 |
} |
728 |
|
729 |
public SpatialIndex createSpatialIndex(String name, DynObject parameters) throws ServiceException { |
730 |
SpatialIndexFactory factory = this.getSpatialIndexFactory(name);
|
731 |
if (factory == null) { |
732 |
throw new CantExistsService(name); |
733 |
} |
734 |
return (SpatialIndex) factory.create(parameters, this); |
735 |
} |
736 |
|
737 |
public SpatialIndexFactory getSpatialIndexFactory(String name) { |
738 |
return (SpatialIndexFactory) this.spatialIndexFactories.get(name); |
739 |
} |
740 |
|
741 |
public void addServiceFactory(ServiceFactory serviceFactory) { |
742 |
serviceFactory.initialize(); |
743 |
this.spatialIndexFactories.put(serviceFactory.getName(), serviceFactory);
|
744 |
} |
745 |
|
746 |
public Service createService(DynObject serviceParameters) throws ServiceException { |
747 |
throw new UnsupportedOperationException("Not supported yet."); |
748 |
} |
749 |
|
750 |
public DynObject createServiceParameters(String serviceName) throws ServiceException { |
751 |
SpatialIndexFactory factory = this.getSpatialIndexFactory(serviceName);
|
752 |
if (factory == null) { |
753 |
throw new CantExistsService(serviceName); |
754 |
} |
755 |
return factory.createParameters();
|
756 |
} |
757 |
|
758 |
public Service getService(DynObject parameters) throws ServiceException { |
759 |
return this.createSpatialIndex((String) parameters.getDynValue("serviceName"), parameters); |
760 |
} |
761 |
|
762 |
public class CantExistsService extends ServiceException { |
763 |
|
764 |
public CantExistsService(String serviceName) { |
765 |
super("Can't existe service %(service).", "_Cant_existe_service_XserviceX", 100001); |
766 |
setValue("service", serviceName);
|
767 |
} |
768 |
|
769 |
} |
770 |
} |