gvsig-vectorediting / 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 @ 202
History | View | Annotate | Download (19.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 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 2
|
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.vectorediting.lib.impl; |
26 |
|
27 |
import java.util.Iterator; |
28 |
|
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
import org.gvsig.editing.EditingNotification; |
33 |
import org.gvsig.editing.EditingNotificationManager; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.dal.feature.FeatureType; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
42 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
43 |
import org.gvsig.fmap.geom.GeometryLocator; |
44 |
import org.gvsig.fmap.geom.GeometryManager; |
45 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
46 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
47 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
48 |
import org.gvsig.fmap.geom.primitive.Arc; |
49 |
import org.gvsig.fmap.geom.primitive.Circle; |
50 |
import org.gvsig.fmap.geom.primitive.Curve; |
51 |
import org.gvsig.fmap.geom.primitive.Ellipse; |
52 |
import org.gvsig.fmap.geom.primitive.Point; |
53 |
import org.gvsig.fmap.geom.type.GeometryType; |
54 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
55 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
56 |
import org.gvsig.tools.exception.BaseException; |
57 |
import org.gvsig.tools.service.spi.AbstractProviderServices; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
59 |
|
60 |
public class DefaultEditingProviderServices extends AbstractProviderServices |
61 |
implements EditingProviderServices {
|
62 |
|
63 |
private static final Logger logger = LoggerFactory |
64 |
.getLogger(DefaultEditingProviderServices.class); |
65 |
|
66 |
protected GeometryManager geomManager = GeometryLocator
|
67 |
.getGeometryManager(); |
68 |
|
69 |
public void insertFeatureIntoFeatureStore(Feature feature, |
70 |
FeatureStore featureStore) { |
71 |
EditableFeature eFeature = null;
|
72 |
try {
|
73 |
|
74 |
if (feature instanceof EditableFeature) { |
75 |
eFeature = (EditableFeature) feature; |
76 |
} else {
|
77 |
eFeature = feature.getEditable(); |
78 |
} |
79 |
|
80 |
EditingNotificationManager editingNotificationManager = |
81 |
MapControlLocator.getEditingNotificationManager(); |
82 |
|
83 |
EditingNotification notification = |
84 |
editingNotificationManager.notifyObservers(this, // source |
85 |
EditingNotification.BEFORE_INSERT_FEATURE, // type
|
86 |
null,// document |
87 |
null,// layer |
88 |
featureStore,// store
|
89 |
eFeature// feature
|
90 |
); |
91 |
|
92 |
if (notification.isCanceled()) {
|
93 |
String msg =
|
94 |
String
|
95 |
.format( |
96 |
"Can't insert feature into %1$s, canceled by some observer.",
|
97 |
featureStore.getName()); |
98 |
throw new CancelationException(msg, null); |
99 |
} |
100 |
|
101 |
// FIXME: Este m?todo est? hecho al rev?s. Cuando est? corregido en
|
102 |
// EditingNotification, deber?a quitarse el !
|
103 |
if (!notification.shouldValidateTheFeature()) {
|
104 |
if (!editingNotificationManager.validateFeature(eFeature)) {
|
105 |
String msg =
|
106 |
String.format("%1$s is not valid", eFeature.toString()); |
107 |
throw new Exception(msg); |
108 |
} |
109 |
} |
110 |
|
111 |
featureStore.insert(eFeature); |
112 |
|
113 |
editingNotificationManager.notifyObservers(this,
|
114 |
EditingNotification.AFTER_INSERT_FEATURE, null, null, |
115 |
featureStore, eFeature); |
116 |
|
117 |
} catch (Exception e) { |
118 |
String msg =
|
119 |
String.format("Can't insert %1$s into %2$s", |
120 |
eFeature.toString(), featureStore.getName()); |
121 |
logger.info(msg, e); |
122 |
} |
123 |
} |
124 |
|
125 |
public void insertGeometryIntoFeatureStore(Geometry geometry, |
126 |
FeatureStore featureStore) { |
127 |
EditableFeature eFeature = null;
|
128 |
|
129 |
try {
|
130 |
eFeature = featureStore.createNewFeature(true);
|
131 |
|
132 |
eFeature.setGeometry(featureStore.getDefaultFeatureType() |
133 |
.getDefaultGeometryAttributeName(), geometry); |
134 |
|
135 |
EditingNotificationManager editingNotificationManager = |
136 |
MapControlLocator.getEditingNotificationManager(); |
137 |
|
138 |
EditingNotification notification = |
139 |
editingNotificationManager.notifyObservers(this, // source |
140 |
EditingNotification.BEFORE_INSERT_FEATURE, // type
|
141 |
null,// document |
142 |
null,// layer |
143 |
featureStore,// store
|
144 |
eFeature// feature
|
145 |
); |
146 |
|
147 |
if (notification.isCanceled()) {
|
148 |
String msg =
|
149 |
String
|
150 |
.format( |
151 |
"Can't insert geometry into %1$s, canceled by some observer.",
|
152 |
featureStore.getName()); |
153 |
throw new CancelationException(msg, null); |
154 |
} |
155 |
|
156 |
// FIXME: Este m?todo est? hecho al rev?s. Cuando est? corregido en
|
157 |
// EditingNotification, deber?a quitarse el !
|
158 |
if (!notification.shouldValidateTheFeature()) {
|
159 |
if (!editingNotificationManager.validateFeature(eFeature)) {
|
160 |
String msg =
|
161 |
String.format("%1$s is not valid", eFeature.toString()); |
162 |
throw new Exception(msg); |
163 |
} |
164 |
} |
165 |
|
166 |
featureStore.insert(eFeature); |
167 |
|
168 |
editingNotificationManager.notifyObservers(this,
|
169 |
EditingNotification.AFTER_INSERT_FEATURE, null, null, |
170 |
featureStore, eFeature); |
171 |
|
172 |
} catch (Exception e) { |
173 |
String msg =
|
174 |
String.format("Can't insert %1$s into %2$s", |
175 |
eFeature.toString(), featureStore.getName()); |
176 |
logger.info(msg, e); |
177 |
} |
178 |
} |
179 |
|
180 |
public void deleteFeatureFromFeatureStore(Feature feature, |
181 |
FeatureStore featureStore) { |
182 |
// TODO Auto-generated method stub
|
183 |
|
184 |
} |
185 |
|
186 |
public void deleteGeometryFromFeatureStore(Geometry geometry, |
187 |
FeatureStore featureStore) { |
188 |
// TODO Auto-generated method stub
|
189 |
|
190 |
} |
191 |
|
192 |
public void updateFeatureInFeatureStore(Feature feature, |
193 |
FeatureStore featureStore) { |
194 |
EditableFeature eFeature = null;
|
195 |
|
196 |
try {
|
197 |
|
198 |
if (feature instanceof EditableFeature) { |
199 |
eFeature = (EditableFeature) feature; |
200 |
} else {
|
201 |
eFeature = feature.getEditable(); |
202 |
} |
203 |
|
204 |
EditingNotificationManager editingNotificationManager = |
205 |
MapControlLocator.getEditingNotificationManager(); |
206 |
|
207 |
EditingNotification notification = |
208 |
editingNotificationManager.notifyObservers(this, // source |
209 |
EditingNotification.BEFORE_UPDATE_FEATURE, // type
|
210 |
null,// document |
211 |
null,// layer |
212 |
featureStore,// store
|
213 |
eFeature// feature
|
214 |
); |
215 |
|
216 |
if (notification.isCanceled()) {
|
217 |
String msg =
|
218 |
String
|
219 |
.format( |
220 |
"Can't update feature in %1$s, canceled by some observer.",
|
221 |
featureStore.getName()); |
222 |
throw new CancelationException(msg, null); |
223 |
} |
224 |
|
225 |
// FIXME: Este m?todo est? hecho al rev?s. Cuando est? corregido en
|
226 |
// EditingNotification, deber?a quitarse el !
|
227 |
if (!notification.shouldValidateTheFeature()) {
|
228 |
if (!editingNotificationManager.validateFeature(eFeature)) {
|
229 |
String msg =
|
230 |
String.format("%1$s is not valid", eFeature.toString()); |
231 |
throw new Exception(msg); |
232 |
} |
233 |
} |
234 |
|
235 |
featureStore.update(eFeature); |
236 |
editingNotificationManager.notifyObservers(this,
|
237 |
EditingNotification.AFTER_UPDATE_FEATURE, null, null, |
238 |
featureStore, eFeature); |
239 |
|
240 |
} catch (Exception e) { |
241 |
String msg =
|
242 |
String.format("Can't update %1$s in %2$s", eFeature.toString(), |
243 |
featureStore.getName()); |
244 |
logger.info(msg, e); |
245 |
} |
246 |
} |
247 |
|
248 |
public void updateGeometryInFeatureStore(Geometry geometry, |
249 |
FeatureStore featureStore) { |
250 |
// TODO Auto-generated method stub
|
251 |
|
252 |
} |
253 |
|
254 |
public Circle createCircle(Point center, double radius, int subtype) |
255 |
throws CreateGeometryException, DataException {
|
256 |
Circle circle = null;
|
257 |
circle = (Circle) geomManager.create(TYPES.CIRCLE, subtype); |
258 |
circle.setPoints(center, radius); |
259 |
|
260 |
return circle;
|
261 |
} |
262 |
|
263 |
public Arc createArc(Point center, double radius, double startAngle, |
264 |
double angleExt, int subtype) throws CreateGeometryException, |
265 |
DataException { |
266 |
Arc arc = null;
|
267 |
arc = (Arc) geomManager.create(TYPES.ARC, subtype); |
268 |
arc.setPoints(center, radius, startAngle, angleExt); |
269 |
return arc;
|
270 |
} |
271 |
|
272 |
public Ellipse createFilledEllipse(Point firstPointAxisA, |
273 |
Point secondPointAxisA, double halfLengthAxisB, int subtype) |
274 |
throws CreateGeometryException {
|
275 |
Ellipse ellipse = (Ellipse) geomManager.create(TYPES.ELLIPSE, subtype); |
276 |
ellipse.setPoints(firstPointAxisA, secondPointAxisA, halfLengthAxisB); |
277 |
return ellipse;
|
278 |
} |
279 |
|
280 |
public Arc createArc(Point start, Point middle, Point end, int subtype) |
281 |
throws BaseException {
|
282 |
|
283 |
Point center = getCenter(start, middle, end, subtype);
|
284 |
double radius = center.distance(start);
|
285 |
double startAngle = getAngle(center, start);
|
286 |
double endAngle = getAngle(center, end);
|
287 |
double middleAngle = getAngle(center, middle);
|
288 |
double extAngle = 0; |
289 |
|
290 |
if (startAngle < endAngle) {
|
291 |
if (startAngle < middleAngle && middleAngle < endAngle) {
|
292 |
extAngle = angleDistance(endAngle, startAngle); |
293 |
return createArc(center, radius, startAngle, extAngle, subtype);
|
294 |
} else {
|
295 |
extAngle = angleDistance(startAngle, endAngle); |
296 |
return createArc(center, radius, endAngle, extAngle, subtype);
|
297 |
} |
298 |
} else {
|
299 |
if (startAngle > middleAngle && middleAngle > endAngle) {
|
300 |
extAngle = angleDistance(startAngle, endAngle); |
301 |
return createArc(center, radius, endAngle, extAngle, subtype);
|
302 |
} else {
|
303 |
extAngle = angleDistance(endAngle, startAngle); |
304 |
return createArc(center, radius, startAngle, extAngle, subtype);
|
305 |
} |
306 |
} |
307 |
} |
308 |
|
309 |
public Point createPoint(double x, double y, int subtype) |
310 |
throws CreateGeometryException, DataException {
|
311 |
Point point = null; |
312 |
point = (Point) geomManager.create(TYPES.POINT, subtype);
|
313 |
point.setX(x); |
314 |
point.setY(y); |
315 |
return point;
|
316 |
} |
317 |
|
318 |
public Curve createLine(double x1, double y1, double x2, double y2, |
319 |
int subtype) throws CreateGeometryException, DataException { |
320 |
Curve line = null;
|
321 |
line = (Curve) geomManager.create(TYPES.CURVE, subtype); |
322 |
line.addVertex(x1, y1); |
323 |
line.addVertex(x2, y2); |
324 |
return line;
|
325 |
} |
326 |
|
327 |
public int getSubType(FeatureStore featureStore) throws DataException { |
328 |
|
329 |
GeometryType geomType = getGeomType(featureStore); |
330 |
if (geomType != null) { |
331 |
return geomType.getSubType();
|
332 |
} |
333 |
return SUBTYPES.UNKNOWN;
|
334 |
} |
335 |
|
336 |
public GeometryType getGeomType(FeatureStore featureStore)
|
337 |
throws DataException {
|
338 |
return featureStore.getDefaultFeatureType()
|
339 |
.getDefaultGeometryAttribute().getGeomType(); |
340 |
} |
341 |
|
342 |
public EditableFeature getFeatureCopyWithoutPK(FeatureStore featureStore,
|
343 |
Feature feature) throws DataException {
|
344 |
EditableFeature editableFeature = |
345 |
featureStore.createNewFeature(feature.getType(), true);
|
346 |
|
347 |
FeatureType type_src = feature.getType(); |
348 |
@SuppressWarnings("rawtypes") |
349 |
Iterator iterator = type_src.iterator();
|
350 |
FeatureType type_dest = editableFeature.getType(); |
351 |
|
352 |
while (iterator.hasNext()) {
|
353 |
FeatureAttributeDescriptor attribute_src = |
354 |
(FeatureAttributeDescriptor) iterator.next(); |
355 |
|
356 |
FeatureAttributeDescriptor attribute_dest = |
357 |
type_dest.getAttributeDescriptor(attribute_src.getName()); |
358 |
if (attribute_dest != null) { |
359 |
if (!attribute_dest.isPrimaryKey()) {
|
360 |
editableFeature.set(attribute_dest.getIndex(), |
361 |
feature.get(attribute_src.getIndex())); |
362 |
} |
363 |
} |
364 |
} |
365 |
return editableFeature;
|
366 |
} |
367 |
|
368 |
public Point getCenter(Point a, Point b, Point c, int subtype) |
369 |
throws CreateGeometryException, DataException {
|
370 |
|
371 |
Point midPointAC = getMidPoint(a, c, subtype);
|
372 |
Double[] lineParamsAC = getLineParams(a, c); |
373 |
Point[] bisectorAC = |
374 |
getPerpendicular(lineParamsAC[0], lineParamsAC[1], midPointAC, |
375 |
subtype); |
376 |
|
377 |
Point midPointBC = getMidPoint(b, c, subtype);
|
378 |
Double[] lineParamsBC = getLineParams(c, b); |
379 |
Point[] bisectorBC = |
380 |
getPerpendicular(lineParamsBC[0], lineParamsBC[1], midPointBC, |
381 |
subtype); |
382 |
|
383 |
return getIntersection(bisectorAC, bisectorBC, subtype);
|
384 |
} |
385 |
|
386 |
public Point getMidPoint(Point a, Point b, int subtype) |
387 |
throws CreateGeometryException, DataException {
|
388 |
double x = (a.getX() + b.getX()) / 2; |
389 |
double y = (a.getY() + b.getY()) / 2; |
390 |
return createPoint(x, y, subtype);
|
391 |
} |
392 |
|
393 |
public Double[] getLineParams(Point point, Point nextPoint) { |
394 |
Double[] lineParams = new Double[2]; |
395 |
double denom = nextPoint.getX() - point.getX();
|
396 |
if (denom != 0) { |
397 |
lineParams[0] = (nextPoint.getY() - point.getY()) / denom;
|
398 |
lineParams[1] = point.getY() - (lineParams[0] * point.getX()); |
399 |
} else {
|
400 |
if (nextPoint.getY() >= point.getY()) {
|
401 |
lineParams[0] = Double.POSITIVE_INFINITY; |
402 |
lineParams[1] = Double.NEGATIVE_INFINITY; |
403 |
if (point.getX() == 0) { |
404 |
lineParams[1] = 0.0; |
405 |
} |
406 |
} else {
|
407 |
lineParams[0] = Double.NEGATIVE_INFINITY; |
408 |
lineParams[1] = Double.POSITIVE_INFINITY; |
409 |
if (point.getX() == 0) { |
410 |
lineParams[1] = 0.0; |
411 |
} |
412 |
} |
413 |
} |
414 |
return lineParams;
|
415 |
} |
416 |
|
417 |
public Point[] getPerpendicular(Double m, Double b, Point perp, int subtype) |
418 |
throws CreateGeometryException, DataException {
|
419 |
if (m == Double.POSITIVE_INFINITY) { |
420 |
Point[] res = new Point[2]; |
421 |
res[0] = createPoint(0, perp.getY(), subtype); |
422 |
res[1] = createPoint(1, perp.getY(), subtype); |
423 |
return res;
|
424 |
} else if (m == Double.NEGATIVE_INFINITY) { |
425 |
Point[] res = new Point[2]; |
426 |
res[0] = createPoint(1, perp.getY(), subtype); |
427 |
res[1] = createPoint(0, perp.getY(), subtype); |
428 |
return res;
|
429 |
} else {
|
430 |
// Pendiente de la recta perpendicular
|
431 |
Double m1 = -1 / m; |
432 |
|
433 |
// b de la funcion de la recta perpendicular
|
434 |
Double b1 = perp.getY() - (m1 * perp.getX());
|
435 |
|
436 |
// Obtenemos un par de puntos
|
437 |
Point[] res = new Point[2]; |
438 |
|
439 |
if (Double.isInfinite(m1)) { |
440 |
res[0] = createPoint(perp.getX(), 0.0, subtype); |
441 |
res[1] = createPoint(perp.getX(), perp.getY(), subtype);
|
442 |
} else {
|
443 |
res[0] = createPoint(0, 0.0 + b1, subtype); |
444 |
res[1] =
|
445 |
createPoint(perp.getX(), (m1 * perp.getX()) + b1, subtype); |
446 |
} |
447 |
|
448 |
return res;
|
449 |
} |
450 |
} |
451 |
|
452 |
public Point getIntersection(Point[] lineA, Point[] lineB, int subtype) |
453 |
throws CreateGeometryException, DataException {
|
454 |
Point p1 = lineA[0]; |
455 |
Point p2 = lineA[1]; |
456 |
Point p3 = lineB[0]; |
457 |
Point p4 = lineB[1]; |
458 |
|
459 |
double m1 = Double.POSITIVE_INFINITY; |
460 |
|
461 |
if ((p2.getX() - p1.getX()) != 0) { |
462 |
m1 = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); |
463 |
} |
464 |
|
465 |
double m2 = Double.POSITIVE_INFINITY; |
466 |
|
467 |
if ((p4.getX() - p3.getX()) != 0) { |
468 |
m2 = (p4.getY() - p3.getY()) / (p4.getX() - p3.getX()); |
469 |
} |
470 |
|
471 |
if ((m1 == Double.POSITIVE_INFINITY) |
472 |
&& (m2 == Double.POSITIVE_INFINITY)) {
|
473 |
return null; |
474 |
} |
475 |
|
476 |
double b1 = p2.getY() - (m1 * p2.getX());
|
477 |
|
478 |
double b2 = p4.getY() - (m2 * p4.getX());
|
479 |
|
480 |
if ((m1 != Double.POSITIVE_INFINITY) |
481 |
&& (m2 != Double.POSITIVE_INFINITY)) {
|
482 |
if (m1 == m2) {
|
483 |
return null; |
484 |
} |
485 |
|
486 |
double x = (b2 - b1) / (m1 - m2);
|
487 |
|
488 |
return createPoint(x, (m1 * x) + b1, subtype);
|
489 |
} else if (m1 == Double.POSITIVE_INFINITY) { |
490 |
double x = p1.getX();
|
491 |
|
492 |
return createPoint(x, (m2 * x) + b2, subtype);
|
493 |
} else if (m2 == Double.POSITIVE_INFINITY) { |
494 |
double x = p3.getX();
|
495 |
|
496 |
return createPoint(x, (m1 * x) + b1, subtype);
|
497 |
} |
498 |
|
499 |
return null; |
500 |
} |
501 |
|
502 |
public double getAngle(Point start, Point end) |
503 |
throws GeometryOperationNotSupportedException,
|
504 |
GeometryOperationException { |
505 |
double angle =
|
506 |
Math.acos((end.getX() - start.getX()) / start.distance(end));
|
507 |
|
508 |
if (start.getY() > end.getY()) {
|
509 |
angle = -angle; |
510 |
} |
511 |
|
512 |
if (angle < 0) { |
513 |
angle += (2 * Math.PI); |
514 |
} |
515 |
|
516 |
return angle;
|
517 |
} |
518 |
|
519 |
public double angleDistance(double angle1, double angle2) { |
520 |
if (angle1 < angle2) {
|
521 |
return angle2 - angle1;
|
522 |
} else {
|
523 |
return ((Math.PI * 2) - angle1) + angle2; |
524 |
} |
525 |
} |
526 |
|
527 |
/*
|
528 |
* (non-Javadoc)
|
529 |
*
|
530 |
* @see
|
531 |
* org.gvsig.vectorediting.lib.spi.EditingProviderServices#createLine(org
|
532 |
* .gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point,
|
533 |
* int)
|
534 |
*/
|
535 |
public Curve createLine(Point p1, Point p2, int subtype) |
536 |
throws CreateGeometryException, DataException {
|
537 |
return this.createLine(p1.getX(), p1.getY(), p2.getX(), p2.getY(), |
538 |
subtype); |
539 |
} |
540 |
} |