Statistics
| Revision:

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 @ 2109

History | View | Annotate | Download (24.7 KB)

1 159 llmarques
/**
2
 * gvSIG. Desktop Geographic Information System.
3 55 fdiaz
 *
4 159 llmarques
 * 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 4 llmarques
 */
24
package org.gvsig.vectorediting.lib.impl;
25
26 68 fdiaz
import java.util.Iterator;
27 207 llmarques
import java.util.List;
28 241 llmarques
import java.util.Map;
29 2102 fdiaz
import org.gvsig.euclidean.EuclideanLine2D;
30 2109 fdiaz
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33 55 fdiaz
34 844 jjdelcerro
import org.gvsig.fmap.dal.EditingNotification;
35
import org.gvsig.fmap.dal.EditingNotificationManager;
36 55 fdiaz
import org.gvsig.fmap.dal.exception.DataException;
37 2109 fdiaz
import org.gvsig.fmap.dal.expressionevaluator.DALExpressionBuilder;
38 22 llmarques
import org.gvsig.fmap.dal.feature.EditableFeature;
39 4 llmarques
import org.gvsig.fmap.dal.feature.Feature;
40 68 fdiaz
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41 875 dmartinezizquierdo
import org.gvsig.fmap.dal.feature.FeatureSet;
42 4 llmarques
import org.gvsig.fmap.dal.feature.FeatureStore;
43 68 fdiaz
import org.gvsig.fmap.dal.feature.FeatureType;
44 844 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
45 4 llmarques
import org.gvsig.fmap.geom.Geometry;
46 57 llmarques
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
47 55 fdiaz
import org.gvsig.fmap.geom.GeometryLocator;
48
import org.gvsig.fmap.geom.GeometryManager;
49 2109 fdiaz
import org.gvsig.fmap.geom.GeometryUtils;
50 55 fdiaz
import org.gvsig.fmap.geom.exception.CreateGeometryException;
51 178 llmarques
import org.gvsig.fmap.geom.operation.GeometryOperationException;
52
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
53 55 fdiaz
import org.gvsig.fmap.geom.primitive.Arc;
54
import org.gvsig.fmap.geom.primitive.Circle;
55 188 llmarques
import org.gvsig.fmap.geom.primitive.Ellipse;
56 262 llmarques
import org.gvsig.fmap.geom.primitive.Line;
57 55 fdiaz
import org.gvsig.fmap.geom.primitive.Point;
58 207 llmarques
import org.gvsig.fmap.geom.primitive.Spline;
59 55 fdiaz
import org.gvsig.fmap.geom.type.GeometryType;
60 2109 fdiaz
import org.gvsig.fmap.mapcontext.MapContext;
61 22 llmarques
import org.gvsig.fmap.mapcontext.layers.CancelationException;
62 2109 fdiaz
import org.gvsig.fmap.mapcontext.layers.FLayer;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
64 241 llmarques
import org.gvsig.tools.ToolsLocator;
65 178 llmarques
import org.gvsig.tools.exception.BaseException;
66 241 llmarques
import org.gvsig.tools.i18n.I18nManager;
67 4 llmarques
import org.gvsig.tools.service.spi.AbstractProviderServices;
68
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
69 875 dmartinezizquierdo
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71 4 llmarques
72
public class DefaultEditingProviderServices extends AbstractProviderServices
73 2102 fdiaz
        implements EditingProviderServices {
74 4 llmarques
75 2109 fdiaz
    private static final Logger LOGGER = LoggerFactory
76 2102 fdiaz
            .getLogger(DefaultEditingProviderServices.class);
77 78 fdiaz
78 2102 fdiaz
    @Override
79 130 llmarques
    public void insertFeatureIntoFeatureStore(Feature feature,
80 2102 fdiaz
            FeatureStore featureStore) {
81 130 llmarques
        EditableFeature eFeature = null;
82
        try {
83 22 llmarques
84 130 llmarques
            if (feature instanceof EditableFeature) {
85
                eFeature = (EditableFeature) feature;
86
            } else {
87
                eFeature = feature.getEditable();
88
            }
89 4 llmarques
90 2102 fdiaz
            EditingNotificationManager editingNotificationManager
91
                    = DALSwingLocator.getEditingNotificationManager();
92 22 llmarques
93 2102 fdiaz
            EditingNotification notification
94
                    = editingNotificationManager.notifyObservers(this, // source
95
                            EditingNotification.BEFORE_INSERT_FEATURE, // type
96
                            null,// document
97
                            null,// layer
98
                            featureStore,// store
99
                            eFeature// feature
100 130 llmarques
                    );
101 22 llmarques
102 130 llmarques
            if (notification.isCanceled()) {
103 2102 fdiaz
                String msg
104
                        = String
105
                                .format(
106
                                        "Can't insert feature into %1$s, canceled by some observer.",
107
                                        featureStore.getName());
108 130 llmarques
                throw new CancelationException(msg, null);
109
            }
110 22 llmarques
111 253 llmarques
            if (notification.shouldValidateTheFeature()) {
112 130 llmarques
                if (!editingNotificationManager.validateFeature(eFeature)) {
113 2102 fdiaz
                    String msg
114
                            = String.format("%1$s is not valid", eFeature.toString());
115 130 llmarques
                    throw new Exception(msg);
116
                }
117
            }
118 22 llmarques
119 130 llmarques
            featureStore.insert(eFeature);
120 22 llmarques
121 130 llmarques
            editingNotificationManager.notifyObservers(this,
122 2102 fdiaz
                    EditingNotification.AFTER_INSERT_FEATURE, null, null,
123
                    featureStore, eFeature);
124 22 llmarques
125 130 llmarques
        } catch (Exception e) {
126 2102 fdiaz
            String msg
127
                    = String.format("Can't insert %1$s into %2$s",
128
                            eFeature.toString(), featureStore.getName());
129 2109 fdiaz
            LOGGER.info(msg, e);
130 130 llmarques
        }
131
    }
132 63 fdiaz
133 2102 fdiaz
    @Override
134 130 llmarques
    public void insertGeometryIntoFeatureStore(Geometry geometry,
135 2102 fdiaz
            FeatureStore featureStore) {
136 130 llmarques
        EditableFeature eFeature = null;
137 63 fdiaz
138 130 llmarques
        try {
139
            eFeature = featureStore.createNewFeature(true);
140 63 fdiaz
141 130 llmarques
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
142 2102 fdiaz
                    .getDefaultGeometryAttributeName(), geometry);
143 63 fdiaz
144 2102 fdiaz
            EditingNotificationManager editingNotificationManager
145
                    = DALSwingLocator.getEditingNotificationManager();
146 4 llmarques
147 2102 fdiaz
            EditingNotification notification
148
                    = editingNotificationManager.notifyObservers(this, // source
149
                            EditingNotification.BEFORE_INSERT_FEATURE, // type
150
                            null,// document
151
                            null,// layer
152
                            featureStore,// store
153
                            eFeature// feature
154 130 llmarques
                    );
155
156
            if (notification.isCanceled()) {
157 2102 fdiaz
                String msg
158
                        = String
159
                                .format(
160
                                        "Can't insert geometry into %1$s, canceled by some observer.",
161
                                        featureStore.getName());
162 130 llmarques
                throw new CancelationException(msg, null);
163
            }
164
165 238 llmarques
            if (notification.shouldValidateTheFeature()) {
166 130 llmarques
                if (!editingNotificationManager.validateFeature(eFeature)) {
167 2102 fdiaz
                    String msg
168
                            = String.format("%1$s is not valid", eFeature.toString());
169 130 llmarques
                    throw new Exception(msg);
170
                }
171
            }
172
173
            featureStore.insert(eFeature);
174
175
            editingNotificationManager.notifyObservers(this,
176 2102 fdiaz
                    EditingNotification.AFTER_INSERT_FEATURE, null, null,
177
                    featureStore, eFeature);
178 130 llmarques
179
        } catch (Exception e) {
180 2102 fdiaz
            String msg
181
                    = String.format("Can't insert %1$s into %2$s",
182
                            eFeature.toString(), featureStore.getName());
183 2109 fdiaz
            LOGGER.info(msg, e);
184 130 llmarques
        }
185 57 llmarques
    }
186 22 llmarques
187 2102 fdiaz
    @Override
188 875 dmartinezizquierdo
    public void deleteFeatureFromFeatureSet(Feature feature,
189 2102 fdiaz
            FeatureStore featureStore, FeatureSet set) {
190 875 dmartinezizquierdo
191
        try {
192 2102 fdiaz
            EditingNotificationManager editingNotificationManager
193
                    = DALSwingLocator.getEditingNotificationManager();
194 875 dmartinezizquierdo
195 2102 fdiaz
            EditingNotification notification
196
                    = editingNotificationManager.notifyObservers(this, // source
197
                            EditingNotification.BEFORE_REMOVE_FEATURE, // type
198
                            null,// document
199
                            null,// layer
200
                            featureStore,// store
201
                            feature// feature
202 875 dmartinezizquierdo
                    );
203
204
            if (notification.isCanceled()) {
205 2102 fdiaz
                String msg
206
                        = String
207
                                .format(
208
                                        "Can't delete feature from %1$s, canceled by some observer.",
209
                                        featureStore.getName());
210 875 dmartinezizquierdo
                throw new CancelationException(msg, null);
211
            }
212
213
            set.delete(feature);
214
215
            editingNotificationManager.notifyObservers(this,
216 2102 fdiaz
                    EditingNotification.AFTER_REMOVE_FEATURE, null, null,
217
                    featureStore, feature);
218 875 dmartinezizquierdo
219
        } catch (Exception e) {
220 2109 fdiaz
            LOGGER.warn(e.getMessage(), e);
221 875 dmartinezizquierdo
        }
222
    }
223
224 2102 fdiaz
    @Override
225
    public void deleteFeatureFromFeatureStore(Feature feature,
226
            FeatureStore featureStore) {
227 4 llmarques
228 253 llmarques
        try {
229 2102 fdiaz
            EditingNotificationManager editingNotificationManager
230
                    = DALSwingLocator.getEditingNotificationManager();
231 253 llmarques
232 2102 fdiaz
            EditingNotification notification
233
                    = editingNotificationManager.notifyObservers(this, // source
234
                            EditingNotification.BEFORE_REMOVE_FEATURE, // type
235
                            null,// document
236
                            null,// layer
237
                            featureStore,// store
238
                            feature// feature
239 253 llmarques
                    );
240
241
            if (notification.isCanceled()) {
242 2102 fdiaz
                String msg
243
                        = String
244
                                .format(
245
                                        "Can't delete feature from %1$s, canceled by some observer.",
246
                                        featureStore.getName());
247 253 llmarques
                throw new CancelationException(msg, null);
248
            }
249
250
            featureStore.delete(feature);
251
252
            editingNotificationManager.notifyObservers(this,
253 2102 fdiaz
                    EditingNotification.AFTER_REMOVE_FEATURE, null, null,
254
                    featureStore, feature);
255 253 llmarques
256
        } catch (Exception e) {
257 2109 fdiaz
            LOGGER.warn(e.getMessage(), e);
258 253 llmarques
        }
259 130 llmarques
    }
260 22 llmarques
261 2102 fdiaz
    @Override
262 130 llmarques
    public void updateFeatureInFeatureStore(Feature feature,
263 2102 fdiaz
            FeatureStore featureStore) {
264 130 llmarques
        EditableFeature eFeature = null;
265 4 llmarques
266 130 llmarques
        try {
267 4 llmarques
268 130 llmarques
            if (feature instanceof EditableFeature) {
269
                eFeature = (EditableFeature) feature;
270
            } else {
271
                eFeature = feature.getEditable();
272
            }
273 4 llmarques
274 2102 fdiaz
            EditingNotificationManager editingNotificationManager
275
                    = DALSwingLocator.getEditingNotificationManager();
276 22 llmarques
277 2102 fdiaz
            EditingNotification notification
278
                    = editingNotificationManager.notifyObservers(this, // source
279
                            EditingNotification.BEFORE_UPDATE_FEATURE, // type
280
                            null,// document
281
                            null,// layer
282
                            featureStore,// store
283
                            eFeature// feature
284 130 llmarques
                    );
285 55 fdiaz
286 130 llmarques
            if (notification.isCanceled()) {
287 2102 fdiaz
                String msg
288
                        = String
289
                                .format(
290
                                        "Can't update feature in %1$s, canceled by some observer.",
291
                                        featureStore.getName());
292 130 llmarques
                throw new CancelationException(msg, null);
293
            }
294 55 fdiaz
295 238 llmarques
            if (notification.shouldValidateTheFeature()) {
296 130 llmarques
                if (!editingNotificationManager.validateFeature(eFeature)) {
297 2102 fdiaz
                    String msg
298
                            = String.format("%1$s is not valid", eFeature.toString());
299 130 llmarques
                    throw new Exception(msg);
300
                }
301
            }
302 55 fdiaz
303 130 llmarques
            featureStore.update(eFeature);
304
            editingNotificationManager.notifyObservers(this,
305 2102 fdiaz
                    EditingNotification.AFTER_UPDATE_FEATURE, null, null,
306
                    featureStore, eFeature);
307 55 fdiaz
308 130 llmarques
        } catch (Exception e) {
309 2102 fdiaz
            String msg
310
                    = String.format("Can't update %1$s in %2$s", eFeature.toString(),
311
                            featureStore.getName());
312 2109 fdiaz
            LOGGER.info(msg, e);
313 130 llmarques
        }
314
    }
315 55 fdiaz
316 2102 fdiaz
    @Override
317 178 llmarques
    public Circle createCircle(Point center, double radius, int subtype)
318 2102 fdiaz
            throws CreateGeometryException {
319 2109 fdiaz
        return GeometryUtils.createCircle(center, radius, subtype);
320 130 llmarques
    }
321 55 fdiaz
322 2102 fdiaz
    @Override
323 247 llmarques
    public Circle createCircle(Point firstPoint, Point secondPoint,
324 2102 fdiaz
            Point thridPoint, int subtype) throws CreateGeometryException {
325 2109 fdiaz
        return GeometryUtils.createCircle(firstPoint, secondPoint, thridPoint, subtype);
326 247 llmarques
    }
327
328 2102 fdiaz
    @Override
329 130 llmarques
    public Arc createArc(Point center, double radius, double startAngle,
330 2102 fdiaz
            double angleExt, int subtype) throws CreateGeometryException {
331 2109 fdiaz
        return GeometryUtils.createArc(center, radius, startAngle, angleExt, subtype);
332 130 llmarques
    }
333 55 fdiaz
334 2102 fdiaz
    @Override
335 188 llmarques
    public Ellipse createFilledEllipse(Point firstPointAxisA,
336 2102 fdiaz
            Point secondPointAxisA, double halfLengthAxisB, int subtype)
337
            throws CreateGeometryException {
338 2109 fdiaz
        return GeometryUtils.createFilledEllipse(firstPointAxisA, secondPointAxisA, halfLengthAxisB, subtype);
339 188 llmarques
    }
340
341 2102 fdiaz
    @Override
342 178 llmarques
    public Arc createArc(Point start, Point middle, Point end, int subtype)
343 2102 fdiaz
            throws BaseException {
344 2109 fdiaz
        return GeometryUtils.createArc(start, middle, end, subtype);
345 178 llmarques
    }
346
347 2102 fdiaz
    @Override
348 178 llmarques
    public Point createPoint(double x, double y, int subtype)
349 2102 fdiaz
            throws CreateGeometryException {
350
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
351 2109 fdiaz
        return geomManager.createPoint(x, y, subtype);
352 130 llmarques
    }
353 63 fdiaz
354 2102 fdiaz
    @Override
355 262 llmarques
    public Line createLine(double x1, double y1, double x2, double y2,
356 2102 fdiaz
            int subtype) throws CreateGeometryException {
357 2109 fdiaz
        return GeometryUtils.createLine(x1, y1, x2, y2, subtype);
358 130 llmarques
    }
359 68 fdiaz
360 2102 fdiaz
    @Override
361 130 llmarques
    public int getSubType(FeatureStore featureStore) throws DataException {
362 68 fdiaz
363 130 llmarques
        GeometryType geomType = getGeomType(featureStore);
364
        if (geomType != null) {
365
            return geomType.getSubType();
366
        }
367 169 llmarques
        return SUBTYPES.UNKNOWN;
368 130 llmarques
    }
369 68 fdiaz
370 2102 fdiaz
    @Override
371 130 llmarques
    public GeometryType getGeomType(FeatureStore featureStore)
372 2102 fdiaz
            throws DataException {
373 130 llmarques
        return featureStore.getDefaultFeatureType()
374 2102 fdiaz
                .getDefaultGeometryAttribute().getGeomType();
375 130 llmarques
    }
376 68 fdiaz
377 2102 fdiaz
    @Override
378 130 llmarques
    public EditableFeature getFeatureCopyWithoutPK(FeatureStore featureStore,
379 2102 fdiaz
            Feature feature) throws DataException {
380
        EditableFeature editableFeature
381
                = featureStore.createNewFeature(feature.getType(), true);
382 68 fdiaz
383 130 llmarques
        FeatureType type_src = feature.getType();
384
        @SuppressWarnings("rawtypes")
385
        Iterator iterator = type_src.iterator();
386
        FeatureType type_dest = editableFeature.getType();
387
388
        while (iterator.hasNext()) {
389 2102 fdiaz
            FeatureAttributeDescriptor attribute_src
390
                    = (FeatureAttributeDescriptor) iterator.next();
391 130 llmarques
392 2102 fdiaz
            FeatureAttributeDescriptor attribute_dest
393
                    = type_dest.getAttributeDescriptor(attribute_src.getName());
394 130 llmarques
            if (attribute_dest != null) {
395
                if (!attribute_dest.isPrimaryKey()) {
396
                    editableFeature.set(attribute_dest.getIndex(),
397 2102 fdiaz
                            feature.get(attribute_src.getIndex()));
398 130 llmarques
                }
399
            }
400
        }
401
        return editableFeature;
402
    }
403
404 2102 fdiaz
    @Override
405 178 llmarques
    public Point getCenter(Point a, Point b, Point c, int subtype)
406 2102 fdiaz
            throws CreateGeometryException {
407 2109 fdiaz
        return GeometryUtils.getCenter(a, b, c, subtype);
408 178 llmarques
    }
409
410 2102 fdiaz
    @Override
411 178 llmarques
    public Point getMidPoint(Point a, Point b, int subtype)
412 2102 fdiaz
            throws CreateGeometryException {
413 2109 fdiaz
        return GeometryUtils.getMidPoint(a, b, subtype);
414 178 llmarques
    }
415
416 2102 fdiaz
    @Override
417 178 llmarques
    public Double[] getLineParams(Point point, Point nextPoint) {
418
        Double[] lineParams = new Double[2];
419
        double denom = nextPoint.getX() - point.getX();
420
        if (denom != 0) {
421
            lineParams[0] = (nextPoint.getY() - point.getY()) / denom;
422
            lineParams[1] = point.getY() - (lineParams[0] * point.getX());
423
        } else {
424
            if (nextPoint.getY() >= point.getY()) {
425
                lineParams[0] = Double.POSITIVE_INFINITY;
426
                lineParams[1] = Double.NEGATIVE_INFINITY;
427
                if (point.getX() == 0) {
428
                    lineParams[1] = 0.0;
429
                }
430
            } else {
431
                lineParams[0] = Double.NEGATIVE_INFINITY;
432
                lineParams[1] = Double.POSITIVE_INFINITY;
433
                if (point.getX() == 0) {
434
                    lineParams[1] = 0.0;
435
                }
436
            }
437
        }
438
        return lineParams;
439
    }
440
441 2102 fdiaz
    @Override
442
    public Point[] getPerpendicular(Double m, Double b, Point point, int subtype)
443
            throws CreateGeometryException {
444
        // Pendiente de la recta perpendicular
445
        Double m1 = -1 / m;
446 178 llmarques
447 2102 fdiaz
        // b de la funcion de la recta perpendicular
448
        Double b1 = point.getY() - (m1 * point.getX());
449 178 llmarques
450 2102 fdiaz
        // Obtenemos un par de puntos
451
        Point[] res = new Point[2];
452 178 llmarques
453 2102 fdiaz
        if (Double.isInfinite(m1)) {
454
            if (m1 > 0) {
455
                //return the director vector of the line
456
                res[0] = createPoint(point.getX(), 0.0, subtype);
457
                res[1] = createPoint(point.getX(), 1.0, subtype);
458 178 llmarques
            } else {
459 2102 fdiaz
                res[0] = createPoint(point.getX(), 0.0, subtype);
460
                res[1] = createPoint(point.getX(), -1.0, subtype);
461 178 llmarques
            }
462 2102 fdiaz
        } else {
463
            //return the director vector of the line
464
            res[0] = createPoint(0.0, b1, subtype);
465
            Double mod = Math.sqrt(1+Math.pow(m1,2));
466
            Double x = 1/mod;
467
            Double y = m1*x + b1;
468
            res[1] = createPoint(x, y, subtype);
469
        }
470 178 llmarques
471 2102 fdiaz
        return res;
472 178 llmarques
    }
473
474 2102 fdiaz
    @Override
475 178 llmarques
    public Point getIntersection(Point[] lineA, Point[] lineB, int subtype)
476 2102 fdiaz
            throws CreateGeometryException {
477 178 llmarques
        Point p1 = lineA[0];
478
        Point p2 = lineA[1];
479
        Point p3 = lineB[0];
480
        Point p4 = lineB[1];
481
482
        double m1 = Double.POSITIVE_INFINITY;
483
484
        if ((p2.getX() - p1.getX()) != 0) {
485
            m1 = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
486
        }
487
488
        double m2 = Double.POSITIVE_INFINITY;
489
490
        if ((p4.getX() - p3.getX()) != 0) {
491
            m2 = (p4.getY() - p3.getY()) / (p4.getX() - p3.getX());
492
        }
493
494
        if ((m1 == Double.POSITIVE_INFINITY)
495 2102 fdiaz
                && (m2 == Double.POSITIVE_INFINITY)) {
496 178 llmarques
            return null;
497
        }
498
499
        double b1 = p2.getY() - (m1 * p2.getX());
500
501
        double b2 = p4.getY() - (m2 * p4.getX());
502
503
        if ((m1 != Double.POSITIVE_INFINITY)
504 2102 fdiaz
                && (m2 != Double.POSITIVE_INFINITY)) {
505 178 llmarques
            if (m1 == m2) {
506
                return null;
507
            }
508
509
            double x = (b2 - b1) / (m1 - m2);
510
511
            return createPoint(x, (m1 * x) + b1, subtype);
512
        } else if (m1 == Double.POSITIVE_INFINITY) {
513
            double x = p1.getX();
514
515
            return createPoint(x, (m2 * x) + b2, subtype);
516
        } else if (m2 == Double.POSITIVE_INFINITY) {
517
            double x = p3.getX();
518
519
            return createPoint(x, (m1 * x) + b1, subtype);
520
        }
521
522
        return null;
523
    }
524
525 2102 fdiaz
    @Override
526 178 llmarques
    public double getAngle(Point start, Point end)
527 2102 fdiaz
            throws GeometryOperationNotSupportedException,
528
            GeometryOperationException {
529
        double angle
530
                = Math.acos((end.getX() - start.getX()) / start.distance(end));
531 178 llmarques
532
        if (start.getY() > end.getY()) {
533
            angle = -angle;
534
        }
535
536
        if (angle < 0) {
537
            angle += (2 * Math.PI);
538
        }
539
540
        return angle;
541
    }
542
543 2102 fdiaz
    @Override
544 178 llmarques
    public double angleDistance(double angle1, double angle2) {
545 2102 fdiaz
        double result = angle2 - angle1;
546
        if (result < 0) {
547
            result = (Math.PI * 2) + result;
548 178 llmarques
        }
549 575 fdiaz
        return result;
550 178 llmarques
    }
551 202 llmarques
552 2102 fdiaz
    @Override
553 262 llmarques
    public Line createLine(Point p1, Point p2, int subtype)
554 2102 fdiaz
            throws CreateGeometryException {
555 2109 fdiaz
        return GeometryUtils.createLine(p1, p2, subtype);
556 202 llmarques
    }
557 207 llmarques
558 2102 fdiaz
    @Override
559 207 llmarques
    public Spline createSpline(List<Point> points, int subtype)
560 2102 fdiaz
            throws CreateGeometryException {
561 2109 fdiaz
        return GeometryUtils.createSpline(points, subtype);
562 207 llmarques
    }
563 241 llmarques
564 2102 fdiaz
    @Override
565 241 llmarques
    public String makeConsoleMessage(String preText, Map<String, String> options) {
566
567
        I18nManager i18nManager = ToolsLocator.getI18nManager();
568
569
        StringBuilder stb = new StringBuilder();
570
571
        if (preText != null) {
572
            stb.append(i18nManager.getTranslation(preText));
573
            stb.append(" ");
574
        }
575
576
        for (String option : options.keySet()) {
577
            stb.append("[");
578
            stb.append(option);
579
            stb.append("]");
580
            stb.append(i18nManager.getTranslation(options.get(option)));
581
            stb.append(" ");
582
        }
583
584
        return stb.toString();
585
    }
586 272 fdiaz
587 2102 fdiaz
    @Override
588 272 fdiaz
    public Arc createEllipse(Point firstPointAxisA, Point secondPointAxisA,
589 2102 fdiaz
            double halfLengthAxisB, int subtype) throws CreateGeometryException {
590 2109 fdiaz
        return GeometryUtils.createEllipse(firstPointAxisA, secondPointAxisA, halfLengthAxisB, subtype);
591 272 fdiaz
    }
592 2102 fdiaz
593
    @Override
594
    public Circle createCircle(Point firstPoint, Point secondPoint, Point thirdPoint, Point fourthPoint, Point fifthPoint, int subtype) throws CreateGeometryException {
595 2109 fdiaz
        return GeometryUtils.createCircle(firstPoint, secondPoint, thirdPoint, fourthPoint, fifthPoint, subtype);
596 2102 fdiaz
    }
597
598
    @Override
599
    public Circle createCircle(EuclideanLine2D line1, EuclideanLine2D line2, Point point, int subtype) throws CreateGeometryException {
600 2109 fdiaz
        return GeometryUtils.createCircle(line1, line2, point, subtype);
601
    }
602 2102 fdiaz
603 2109 fdiaz
    @Override
604
    public Circle createCircle(Geometry geometry1, Geometry geometry2, double radius, Point firstPoint, Point secondPoint, int subtype) throws CreateGeometryException {
605
        return GeometryUtils.createCircle(geometry1, geometry2, radius, firstPoint, secondPoint, subtype);
606
    }
607
608
    @Override
609
    public Geometry getGeometry(Point point, FeatureStore store, MapContext mapContext) {
610
        Geometry geometry = null;
611 2102 fdiaz
        try {
612 2109 fdiaz
            double tolerance = mapContext.getViewPort().toMapDistance(mapContext.getLayers().getDefaultTolerance());
613
            Geometry buffer = point.buffer(tolerance);
614
615
            DataManager dataManager = DALLocator.getDataManager();
616
            DALExpressionBuilder dalBuilder = dataManager.createDALExpressionBuilder();
617
            GeometryExpressionBuilder builder = dalBuilder.expression();
618
            String filter = builder.ST_Intersects(builder.geometry(buffer, mapContext.getProjection()), dalBuilder.geometry()).toString();
619
            String sortBy = builder.ST_Distance(builder.geometry(buffer), dalBuilder.geometry()).toString();
620 2102 fdiaz
621 2109 fdiaz
            Feature f;
622
                f = store.findFirst(filter, sortBy, true);
623
            if(f!=null){
624
                geometry = f.getDefaultGeometry();
625
            }
626
627
        } catch (Exception ex) {
628
            LOGGER.warn("Can't get geometry on point ("+point.getX()+","+point.getY(), ex);
629
        }
630
631
        return geometry;
632
    }
633 2102 fdiaz
634 2109 fdiaz
    @Override
635
    public Geometry getGeometryOfVisibleLayers(Point point, FeatureStore store, MapContext mapContext) {
636
637
        // Search in the store itself
638
        Geometry geometry = this.getGeometry(point, store, mapContext);
639
        // Search in the store for the layers that are in edit
640
        if(geometry == null){
641
            for( Iterator<FLayer> iterator = mapContext.getLayers().deepiterator(); iterator.hasNext(); ) {
642
                FLayer layer = iterator.next();
643
                if(layer instanceof FLyrVect){
644
                    FLyrVect vectLayer = (FLyrVect) layer;
645
                    if(vectLayer.getFeatureStore()!=store && vectLayer.isEditing()){
646
                        geometry  = this.getGeometry(point, vectLayer.getFeatureStore(), mapContext);
647
                        if(geometry != null){
648
                            break;
649
                        }
650
                    }
651 2102 fdiaz
                }
652
            }
653 2109 fdiaz
        }
654
        // Search in the store for the active layers
655
        if(geometry == null){
656
            FLayer[] activeLayers = mapContext.getLayers().getActives();
657
            for (FLayer activeLayer : activeLayers) {
658
                if(activeLayer instanceof FLyrVect){
659
                    FLyrVect activeVectLayer = (FLyrVect) activeLayer;
660
                    if(activeVectLayer.getFeatureStore()!=store && !activeVectLayer.isEditing()){
661
                        geometry  = this.getGeometry(point, activeVectLayer.getFeatureStore(), mapContext);
662
                        if(geometry != null){
663
                            break;
664
                        }
665
                    }
666 2102 fdiaz
                }
667
            }
668
        }
669 2109 fdiaz
        return geometry;
670 2102 fdiaz
    }
671 2109 fdiaz
672
673 4 llmarques
}