Revision 178 org.gvsig.vectorediting/trunk/org.gvsig.vectorediting/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.impl/src/main/java/org/gvsig/vectorediting/lib/impl/DefaultEditingProviderServices.java
DefaultEditingProviderServices.java | ||
---|---|---|
43 | 43 |
import org.gvsig.fmap.geom.GeometryLocator; |
44 | 44 |
import org.gvsig.fmap.geom.GeometryManager; |
45 | 45 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
46 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
|
47 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
|
46 | 48 |
import org.gvsig.fmap.geom.primitive.Arc; |
47 | 49 |
import org.gvsig.fmap.geom.primitive.Circle; |
48 | 50 |
import org.gvsig.fmap.geom.primitive.Curve; |
... | ... | |
50 | 52 |
import org.gvsig.fmap.geom.type.GeometryType; |
51 | 53 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
52 | 54 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
55 |
import org.gvsig.tools.exception.BaseException; |
|
53 | 56 |
import org.gvsig.tools.service.spi.AbstractProviderServices; |
54 | 57 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
55 | 58 |
|
... | ... | |
247 | 250 |
|
248 | 251 |
} |
249 | 252 |
|
250 |
public Circle createCircle(Point center, double radius, |
|
251 |
FeatureStore featureStore) throws CreateGeometryException, |
|
252 |
DataException { |
|
253 |
public Circle createCircle(Point center, double radius, int subtype) |
|
254 |
throws CreateGeometryException, DataException { |
|
253 | 255 |
Circle circle = null; |
254 |
GeometryType featStoreGeomType = getGeomType(featureStore); |
|
255 |
circle = |
|
256 |
(Circle) geomManager.create(TYPES.CIRCLE, |
|
257 |
featStoreGeomType.getSubType()); |
|
256 |
circle = (Circle) geomManager.create(TYPES.CIRCLE, subtype); |
|
258 | 257 |
circle.setPoints(center, radius); |
259 | 258 |
|
260 | 259 |
return circle; |
261 | 260 |
} |
262 | 261 |
|
263 | 262 |
public Arc createArc(Point center, double radius, double startAngle, |
264 |
double angleExt, FeatureStore featureStore)
|
|
265 |
throws CreateGeometryException, DataException {
|
|
263 |
double angleExt, int subtype) throws CreateGeometryException,
|
|
264 |
DataException { |
|
266 | 265 |
Arc arc = null; |
267 |
GeometryType featStoreGeomType = getGeomType(featureStore); |
|
268 |
arc = |
|
269 |
(Arc) geomManager.create(TYPES.ARC, featStoreGeomType.getSubType()); |
|
266 |
arc = (Arc) geomManager.create(TYPES.ARC, subtype); |
|
270 | 267 |
arc.setPoints(center, radius, startAngle, angleExt); |
271 | 268 |
return arc; |
272 | 269 |
} |
273 | 270 |
|
274 |
public Point createPoint(double x, double y, FeatureStore featureStore) |
|
271 |
public Arc createArc(Point start, Point middle, Point end, int subtype) |
|
272 |
throws BaseException { |
|
273 |
|
|
274 |
Point center = getCenter(start, middle, end, subtype); |
|
275 |
double radius = center.distance(start); |
|
276 |
double startAngle = getAngle(center, start); |
|
277 |
double endAngle = getAngle(center, end); |
|
278 |
double middleAngle = getAngle(center, middle); |
|
279 |
double extAngle = 0; |
|
280 |
|
|
281 |
if (startAngle < endAngle) { |
|
282 |
if (startAngle < middleAngle && middleAngle < endAngle) { |
|
283 |
extAngle = angleDistance(endAngle, startAngle); |
|
284 |
return createArc(center, radius, startAngle, extAngle, subtype); |
|
285 |
} else { |
|
286 |
extAngle = angleDistance(startAngle, endAngle); |
|
287 |
return createArc(center, radius, endAngle, extAngle, subtype); |
|
288 |
} |
|
289 |
} else { |
|
290 |
if (startAngle > middleAngle && middleAngle > endAngle) { |
|
291 |
extAngle = angleDistance(startAngle, endAngle); |
|
292 |
return createArc(center, radius, endAngle, extAngle, subtype); |
|
293 |
} else { |
|
294 |
extAngle = angleDistance(endAngle, startAngle); |
|
295 |
return createArc(center, radius, startAngle, extAngle, subtype); |
|
296 |
} |
|
297 |
} |
|
298 |
} |
|
299 |
|
|
300 |
public Point createPoint(double x, double y, int subtype) |
|
275 | 301 |
throws CreateGeometryException, DataException { |
276 | 302 |
Point point = null; |
277 |
point = |
|
278 |
(Point) geomManager.create(TYPES.POINT, getSubType(featureStore)); |
|
303 |
point = (Point) geomManager.create(TYPES.POINT, subtype); |
|
279 | 304 |
point.setX(x); |
280 | 305 |
point.setY(y); |
281 | 306 |
return point; |
282 | 307 |
} |
283 | 308 |
|
284 | 309 |
public Curve createLine(double x1, double y1, double x2, double y2, |
285 |
FeatureStore featureStore) throws CreateGeometryException, |
|
286 |
DataException { |
|
310 |
int subtype) throws CreateGeometryException, DataException { |
|
287 | 311 |
Curve line = null; |
288 |
line = |
|
289 |
(Curve) geomManager.create(TYPES.CURVE, getSubType(featureStore)); |
|
312 |
line = (Curve) geomManager.create(TYPES.CURVE, subtype); |
|
290 | 313 |
line.addVertex(x1, y1); |
291 | 314 |
line.addVertex(x2, y2); |
292 | 315 |
return line; |
... | ... | |
333 | 356 |
return editableFeature; |
334 | 357 |
} |
335 | 358 |
|
359 |
public Point getCenter(Point a, Point b, Point c, int subtype) |
|
360 |
throws CreateGeometryException, DataException { |
|
361 |
|
|
362 |
Point midPointAC = getMidPoint(a, c, subtype); |
|
363 |
Double[] lineParamsAC = getLineParams(a, c); |
|
364 |
Point[] bisectorAC = |
|
365 |
getPerpendicular(lineParamsAC[0], lineParamsAC[1], midPointAC, |
|
366 |
subtype); |
|
367 |
|
|
368 |
Point midPointBC = getMidPoint(b, c, subtype); |
|
369 |
Double[] lineParamsBC = getLineParams(c, b); |
|
370 |
Point[] bisectorBC = |
|
371 |
getPerpendicular(lineParamsBC[0], lineParamsBC[1], midPointBC, |
|
372 |
subtype); |
|
373 |
|
|
374 |
return getIntersection(bisectorAC, bisectorBC, subtype); |
|
375 |
} |
|
376 |
|
|
377 |
public Point getMidPoint(Point a, Point b, int subtype) |
|
378 |
throws CreateGeometryException, DataException { |
|
379 |
double x = (a.getX() + b.getX()) / 2; |
|
380 |
double y = (a.getY() + b.getY()) / 2; |
|
381 |
return createPoint(x, y, subtype); |
|
382 |
} |
|
383 |
|
|
384 |
public Double[] getLineParams(Point point, Point nextPoint) { |
|
385 |
Double[] lineParams = new Double[2]; |
|
386 |
double denom = nextPoint.getX() - point.getX(); |
|
387 |
if (denom != 0) { |
|
388 |
lineParams[0] = (nextPoint.getY() - point.getY()) / denom; |
|
389 |
lineParams[1] = point.getY() - (lineParams[0] * point.getX()); |
|
390 |
} else { |
|
391 |
if (nextPoint.getY() >= point.getY()) { |
|
392 |
lineParams[0] = Double.POSITIVE_INFINITY; |
|
393 |
lineParams[1] = Double.NEGATIVE_INFINITY; |
|
394 |
if (point.getX() == 0) { |
|
395 |
lineParams[1] = 0.0; |
|
396 |
} |
|
397 |
} else { |
|
398 |
lineParams[0] = Double.NEGATIVE_INFINITY; |
|
399 |
lineParams[1] = Double.POSITIVE_INFINITY; |
|
400 |
if (point.getX() == 0) { |
|
401 |
lineParams[1] = 0.0; |
|
402 |
} |
|
403 |
} |
|
404 |
} |
|
405 |
return lineParams; |
|
406 |
} |
|
407 |
|
|
408 |
public Point[] getPerpendicular(Double m, Double b, Point perp, int subtype) |
|
409 |
throws CreateGeometryException, DataException { |
|
410 |
if (m == Double.POSITIVE_INFINITY) { |
|
411 |
Point[] res = new Point[2]; |
|
412 |
res[0] = createPoint(0, perp.getY(), subtype); |
|
413 |
res[1] = createPoint(1, perp.getY(), subtype); |
|
414 |
return res; |
|
415 |
} else if (m == Double.NEGATIVE_INFINITY) { |
|
416 |
Point[] res = new Point[2]; |
|
417 |
res[0] = createPoint(1, perp.getY(), subtype); |
|
418 |
res[1] = createPoint(0, perp.getY(), subtype); |
|
419 |
return res; |
|
420 |
} else { |
|
421 |
// Pendiente de la recta perpendicular |
|
422 |
Double m1 = -1 / m; |
|
423 |
|
|
424 |
// b de la funcion de la recta perpendicular |
|
425 |
Double b1 = perp.getY() - (m1 * perp.getX()); |
|
426 |
|
|
427 |
// Obtenemos un par de puntos |
|
428 |
Point[] res = new Point[2]; |
|
429 |
|
|
430 |
if (Double.isInfinite(m1)) { |
|
431 |
res[0] = createPoint(perp.getX(), 0.0, subtype); |
|
432 |
res[1] = createPoint(perp.getX(), perp.getY(), subtype); |
|
433 |
} else { |
|
434 |
res[0] = createPoint(0, 0.0 + b1, subtype); |
|
435 |
res[1] = |
|
436 |
createPoint(perp.getX(), (m1 * perp.getX()) + b1, subtype); |
|
437 |
} |
|
438 |
|
|
439 |
return res; |
|
440 |
} |
|
441 |
} |
|
442 |
|
|
443 |
public Point getIntersection(Point[] lineA, Point[] lineB, int subtype) |
|
444 |
throws CreateGeometryException, DataException { |
|
445 |
Point p1 = lineA[0]; |
|
446 |
Point p2 = lineA[1]; |
|
447 |
Point p3 = lineB[0]; |
|
448 |
Point p4 = lineB[1]; |
|
449 |
|
|
450 |
double m1 = Double.POSITIVE_INFINITY; |
|
451 |
|
|
452 |
if ((p2.getX() - p1.getX()) != 0) { |
|
453 |
m1 = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); |
|
454 |
} |
|
455 |
|
|
456 |
double m2 = Double.POSITIVE_INFINITY; |
|
457 |
|
|
458 |
if ((p4.getX() - p3.getX()) != 0) { |
|
459 |
m2 = (p4.getY() - p3.getY()) / (p4.getX() - p3.getX()); |
|
460 |
} |
|
461 |
|
|
462 |
if ((m1 == Double.POSITIVE_INFINITY) |
|
463 |
&& (m2 == Double.POSITIVE_INFINITY)) { |
|
464 |
return null; |
|
465 |
} |
|
466 |
|
|
467 |
double b1 = p2.getY() - (m1 * p2.getX()); |
|
468 |
|
|
469 |
double b2 = p4.getY() - (m2 * p4.getX()); |
|
470 |
|
|
471 |
if ((m1 != Double.POSITIVE_INFINITY) |
|
472 |
&& (m2 != Double.POSITIVE_INFINITY)) { |
|
473 |
if (m1 == m2) { |
|
474 |
return null; |
|
475 |
} |
|
476 |
|
|
477 |
double x = (b2 - b1) / (m1 - m2); |
|
478 |
|
|
479 |
return createPoint(x, (m1 * x) + b1, subtype); |
|
480 |
} else if (m1 == Double.POSITIVE_INFINITY) { |
|
481 |
double x = p1.getX(); |
|
482 |
|
|
483 |
return createPoint(x, (m2 * x) + b2, subtype); |
|
484 |
} else if (m2 == Double.POSITIVE_INFINITY) { |
|
485 |
double x = p3.getX(); |
|
486 |
|
|
487 |
return createPoint(x, (m1 * x) + b1, subtype); |
|
488 |
} |
|
489 |
|
|
490 |
return null; |
|
491 |
} |
|
492 |
|
|
493 |
public double getAngle(Point start, Point end) |
|
494 |
throws GeometryOperationNotSupportedException, |
|
495 |
GeometryOperationException { |
|
496 |
double angle = |
|
497 |
Math.acos((end.getX() - start.getX()) / start.distance(end)); |
|
498 |
|
|
499 |
if (start.getY() > end.getY()) { |
|
500 |
angle = -angle; |
|
501 |
} |
|
502 |
|
|
503 |
if (angle < 0) { |
|
504 |
angle += (2 * Math.PI); |
|
505 |
} |
|
506 |
|
|
507 |
return angle; |
|
508 |
} |
|
509 |
|
|
510 |
public double angleDistance(double angle1, double angle2) { |
|
511 |
if (angle1 < angle2) { |
|
512 |
return angle2 - angle1; |
|
513 |
} else { |
|
514 |
return ((Math.PI * 2) - angle1) + angle2; |
|
515 |
} |
|
516 |
} |
|
336 | 517 |
} |
Also available in: Unified diff