Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.split / src / main / java / org / gvsig / vectorediting / lib / prov / split / SplitEditingProvider.java @ 493

History | View | Annotate | Download (13.4 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.prov.split;
26

    
27
import java.util.ArrayList;
28
import java.util.HashMap;
29
import java.util.List;
30
import java.util.Map;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.EditableFeature;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureSelection;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.fmap.geom.Geometry.TYPES;
39
import org.gvsig.fmap.geom.aggregate.MultiCurve;
40
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
41
import org.gvsig.fmap.geom.exception.CreateGeometryException;
42
import org.gvsig.fmap.geom.operation.GeometryOperationException;
43
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
44
import org.gvsig.fmap.geom.primitive.Curve;
45
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
46
import org.gvsig.fmap.geom.primitive.Point;
47
import org.gvsig.fmap.geom.primitive.Primitive;
48
import org.gvsig.fmap.geom.type.GeometryType;
49
import org.gvsig.tools.dispose.DisposableIterator;
50
import org.gvsig.tools.dynobject.DynObject;
51
import org.gvsig.tools.exception.BaseException;
52
import org.gvsig.tools.service.spi.ProviderServices;
53
import org.gvsig.tools.visitor.VisitCanceledException;
54
import org.gvsig.tools.visitor.Visitor;
55
import org.gvsig.vectorediting.lib.api.DrawingStatus;
56
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
57
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
58
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
59
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
60
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
61
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
62
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
63
import org.gvsig.vectorediting.lib.prov.split.operation.SplitOperation;
64
import org.gvsig.vectorediting.lib.prov.split.operation.SplitOperationUtils;
65
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
66
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
67
import org.gvsig.vectorediting.lib.spi.EditingProvider;
68
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
69
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
70

    
71
/**
72
 * @author llmarques
73
 *
74
 */
75
public class SplitEditingProvider extends AbstractEditingProvider implements
76
    EditingProvider {
77

    
78
    private EditingServiceParameter selection;
79

    
80
    private EditingServiceParameter splitGeometry;
81

    
82
    private FeatureStore featureStore;
83

    
84
    private Map<EditingServiceParameter, Object> values;
85

    
86
    /**
87
     * Default constructor.
88
     *
89
     * @param providerServices
90
     *            available services for this provider
91
     * @param parameters
92
     *            of this provider
93
     */
94
    public SplitEditingProvider(ProviderServices services, DynObject parameters) {
95
        super(services);
96

    
97
        this.featureStore =
98
            (FeatureStore) parameters
99
                .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
100

    
101
        this.selection =
102
            new DefaultEditingServiceParameter("selection", "selection",
103
                EditingServiceParameter.TYPE.SELECTION);
104

    
105
        this.splitGeometry =
106
            new DefaultEditingServiceParameter("draw_geometry_to_split",
107
                "draw_geometry_to_split", Geometry.TYPES.CURVE,
108
                EditingServiceParameter.TYPE.GEOMETRY);
109
    }
110

    
111
    public EditingServiceParameter next() {
112
        if (values.get(selection) == null) {
113
            return selection;
114
        } else if (values.get(splitGeometry) == null) {
115
            return splitGeometry;
116
        }
117
        return null;
118
    }
119

    
120
    public DrawingStatus getDrawingStatus(Point mousePosition)
121
        throws DrawServiceException {
122
        return null;
123
    }
124

    
125
    public void stop() throws StopServiceException {
126
        if (values != null) {
127
            values.clear();
128
        }
129
    }
130

    
131
    public List<EditingServiceParameter> getParameters() {
132
        List<EditingServiceParameter> parameters =
133
            new ArrayList<EditingServiceParameter>();
134
        parameters.add(selection);
135
        parameters.add(splitGeometry);
136
        return parameters;
137
    }
138

    
139
    public void setValue(Object value) throws InvalidEntryException {
140
        EditingServiceParameter parameter = next();
141
        validateAndInsertValue(parameter, value);
142
    }
143

    
144
    private void validateAndInsertValue(EditingServiceParameter parameter,
145
        Object value) throws InvalidEntryException {
146

    
147
        if (values != null && value != null) {
148

    
149
            if (value instanceof FeatureSelection) {
150

    
151
                FeatureSelection featureSelection = (FeatureSelection) value;
152

    
153
                try {
154
                    featureSelection.accept(new Visitor() {
155

    
156
                        public void visit(Object obj)
157
                            throws VisitCanceledException, BaseException {
158
                            Feature feature = (Feature) obj;
159
                            Geometry geometry = feature.getDefaultGeometry();
160
                            GeometryType geoType = geometry.getGeometryType();
161

    
162
                            if (geoType.isTypeOf(POINT)
163
                                || geoType.isTypeOf(MULTIPOINT)) {
164
                                throw new InvalidEntryException(null);
165
                            }
166
                        }
167
                    });
168

    
169
                    values.put(selection, value);
170
                    return;
171
                } catch (BaseException e) {
172
                    throw new InvalidEntryException(e);
173
                }
174

    
175
            } else if (value instanceof Geometry) {
176

    
177
                Geometry geometry = (Geometry) value;
178

    
179
                if (geometry instanceof MultiCurve) {
180
                    MultiCurve multiCurve = (MultiCurve) geometry;
181
                    for (int i = 0; i < multiCurve.getPrimitivesNumber(); i++) {
182
                        if (isValid((Curve) multiCurve.getPrimitiveAt(i))) {
183
                            values.put(splitGeometry, geometry);
184
                            return;
185
                        }
186
                    }
187

    
188
                } else if (geometry instanceof Curve) {
189
                    if (isValid((Curve) geometry)) {
190
                        values.put(splitGeometry, geometry);
191
                        return;
192
                    }
193
                }
194
            }
195
        }
196
        throw new InvalidEntryException(null);
197
    }
198

    
199
    private boolean isValid(final Curve curve) {
200
        FeatureSelection featureSelection =
201
            (FeatureSelection) values.get(selection);
202

    
203
        try {
204
            featureSelection.accept(new Visitor() {
205

    
206
                public void visit(Object obj) throws VisitCanceledException,
207
                    BaseException {
208
                    Feature feature = (Feature) obj;
209
                    Geometry geometry = feature.getDefaultGeometry();
210

    
211
                    Geometry intersection = geometry.intersection(curve);
212

    
213
                    if (intersection == null
214
                        || (!intersection.getGeometryType().isTypeOf(MULTIPOINT)
215
                            && !intersection.getGeometryType().isTypeOf(CURVE)
216
                            && !intersection.getGeometryType().isTypeOf(MULTICURVE))) {
217
                        throw new VectorEditingException();
218
                    }
219
                }
220
            });
221
            return true;
222
        } catch (BaseException e) {
223
            return false;
224
        }
225
    }
226

    
227
    private boolean isClosed(Geometry geometry) {
228

    
229
        if (geometry instanceof OrientablePrimitive) {
230

    
231
            OrientablePrimitive orientablePrimitive =
232
                (OrientablePrimitive) geometry;
233
            Point firstPoint = orientablePrimitive.getVertex(0);
234
            Point lastPoint =
235
                orientablePrimitive.getVertex(orientablePrimitive
236
                    .getNumVertices() - 1);
237

    
238
            if (firstPoint.equals(lastPoint)) {
239
                return true;
240
            }
241

    
242
        } else if (geometry instanceof MultiPrimitive) {
243

    
244
            MultiPrimitive multiPrimitive = (MultiPrimitive) geometry;
245

    
246
            boolean bool = true;
247
            for (int i = 0; i < multiPrimitive.getPrimitivesNumber(); i++) {
248
                if (bool) {
249
                    bool = bool && isClosed(multiPrimitive.getPrimitiveAt(i));
250
                } else {
251
                    break;
252
                }
253
            }
254
            return bool;
255
        }
256
        return false;
257
    }
258

    
259
    public Geometry finish() throws FinishServiceException {
260
        return null;
261
    }
262

    
263
    public void finishAndStore() throws FinishServiceException {
264

    
265
        if (values != null) {
266

    
267
            EditingProviderServices editingProviderServices =
268
                (EditingProviderServices) getProviderServices();
269

    
270
            FeatureSelection featureSelection =
271
                (FeatureSelection) values.get(selection);
272
            Geometry splitter = (Geometry) values.get(splitGeometry);
273

    
274
            DisposableIterator it = null;
275
            try {
276
                it = featureSelection.fastIterator();
277

    
278
                while (it.hasNext()) {
279
                    Feature feature = (Feature) it.next();
280
                    Geometry geometryToBeSplitted =
281
                        feature.getDefaultGeometry();
282
                    GeometryType geoType =
283
                        geometryToBeSplitted.getGeometryType();
284

    
285
                    if (geoType.isTypeOf(TYPES.SURFACE)
286
                        || geoType.isTypeOf(TYPES.CURVE)) {
287

    
288
                        MultiPrimitive splittedGeometries =
289
                            (MultiPrimitive) split(
290
                                (Primitive) geometryToBeSplitted, splitter);
291

    
292
                        copyAlfanumericDataAndInsert(splittedGeometries,
293
                            feature);
294

    
295
                    } else if (geoType.isTypeOf(TYPES.MULTISURFACE)
296
                        || geoType.isTypeOf(TYPES.MULTICURVE)) {
297

    
298
                        MultiPrimitive multiPrimitive =
299
                            (MultiPrimitive) geometryToBeSplitted;
300

    
301
                        for (int i = 0; i < multiPrimitive
302
                            .getPrimitivesNumber(); i++) {
303

    
304
                            MultiPrimitive splittedGeometries =
305
                                (MultiPrimitive) split(
306
                                    multiPrimitive.getPrimitiveAt(i), splitter);
307

    
308
                            copyAlfanumericDataAndInsert(splittedGeometries,
309
                                feature);
310
                        }
311
                    }
312
                    editingProviderServices.deleteFeatureFromFeatureStore(
313
                        feature, featureStore);
314
                }
315

    
316
                featureSelection.deselectAll();
317
                featureStore.getFeatureSelection().deselectAll();
318

    
319
            } catch (BaseException e) {
320
                throw new FinishServiceException(e);
321
            } finally {
322
                it.dispose();
323
            }
324
        }
325
    }
326

    
327
    private void copyAlfanumericDataAndInsert(MultiPrimitive multiPrimitive,
328
        Feature feature) throws DataException {
329
        EditingProviderServices editingProviderServices =
330
            (EditingProviderServices) getProviderServices();
331

    
332
        for (int j = 0; j < multiPrimitive.getPrimitivesNumber(); j++) {
333

    
334
            EditableFeature eFeature =
335
                editingProviderServices.getFeatureCopyWithoutPK(featureStore,
336
                    feature);
337
            eFeature.setDefaultGeometry(multiPrimitive.getPrimitiveAt(j));
338
            editingProviderServices.insertFeatureIntoFeatureStore(eFeature,
339
                featureStore);
340
        }
341
    }
342

    
343
    private Geometry split(Primitive geometry, Geometry splitter)
344
        throws CreateGeometryException, GeometryOperationNotSupportedException,
345
        GeometryOperationException {
346
        SplitOperation operation = SplitOperationUtils.getOperation(geometry);
347

    
348
        if (operation != null) {
349
            return operation.split(geometry, splitter);
350
        }
351
        return null;
352
    }
353

    
354
    public void start() throws StartServiceException, InvalidEntryException {
355
        values = new HashMap<EditingServiceParameter, Object>();
356
        FeatureSelection selected = null;
357
        if (featureStore != null) {
358
            try {
359
                selected = featureStore.getFeatureSelection();
360
            } catch (DataException e) {
361
                throw new StartServiceException(e);
362
            }
363
            if (selected.getSelectedCount() > 0) {
364
                setValue(selected);
365
            }
366
        }
367
    }
368

    
369
    public String getName() {
370
        return SplitEditingProviderFactory.PROVIDER_NAME;
371
    }
372

    
373
}