Statistics
| Revision:

gvsig-derived-geometries / org.gvsig.derivedgeometries / trunk / org.gvsig.derivedgeometries / org.gvsig.derivedgeometries.swing / org.gvsig.derivedgeometries.swing.impl / src / main / java / org / gvsig / derivedgeometries / swing / impl / processes / PointToPolygonDerivedGeometriesProcess.java @ 52

History | View | Annotate | Download (6.73 KB)

1 3 llmarques
/*
2
 * Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
3
 *
4
 * Project  : DiSiD org.gvsig.derivedgeometries.swing.impl
5
 * SVN Id   : $Id$
6
 */
7
package org.gvsig.derivedgeometries.swing.impl.processes;
8
9
import java.util.List;
10
11
import javax.swing.JOptionPane;
12
13
import org.cresques.cts.IProjection;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
17
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters;
18
import org.gvsig.derivedgeometries.swing.impl.AbstractDerivedGeometriesProcess;
19
import org.gvsig.fmap.dal.feature.FeatureReference;
20
import org.gvsig.fmap.dal.feature.FeatureStore;
21
import org.gvsig.fmap.dal.feature.FeatureType;
22
import org.gvsig.fmap.geom.Geometry;
23
import org.gvsig.fmap.geom.GeometryLocator;
24
import org.gvsig.fmap.geom.GeometryManager;
25
import org.gvsig.fmap.geom.primitive.Point;
26
import org.gvsig.fmap.geom.primitive.Polygon;
27
import org.gvsig.fmap.geom.type.GeometryType;
28
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
29
import org.gvsig.tools.swing.api.ToolsSwingLocator;
30
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
31
import org.gvsig.tools.task.SimpleTaskStatus;
32
33
public class PointToPolygonDerivedGeometriesProcess extends
34 24 llmarques
    AbstractDerivedGeometriesProcess {
35
36 3 llmarques
    private static final Logger logger = LoggerFactory
37
        .getLogger(PointToPolygonDerivedGeometriesProcess.class);
38
39 24 llmarques
    public PointToPolygonDerivedGeometriesProcess(
40
        DerivedGeometriesParameters parameters) {
41 3 llmarques
        super("Point to polygon process", parameters);
42
    }
43
44
    public void run() {
45
        SimpleTaskStatus status = null;
46
47
        try {
48
            status = (SimpleTaskStatus) this.getTaskStatus();
49
50
            if (status.isCancellationRequested()) {
51
52
                status.cancel();
53
                return;
54
            }
55
56
            // Get data process
57
            DerivedGeometriesParameters parameters = super.getParameters();
58
            String outputLayerName = parameters.getOutPutLayerName();
59
            String outputLayerPath = parameters.getOutPutLayerPath();
60
            int outputLayerType = parameters.getOutPutLayerType();
61 30 llmarques
            boolean addLayer = parameters.getAddLayer();
62
            boolean createNewFeatureStore =
63
                parameters.getCreateNewFeatureStore();
64 3 llmarques
            FLyrVect sourceLayer = parameters.getSourceLayer();
65
            IProjection projection = sourceLayer.getProjection();
66
            FeatureType sourceFeatureType =
67
                sourceLayer.getFeatureStore().getDefaultFeatureType();
68
            List<FeatureReference> selectedFeatures =
69
                parameters.getSelectedFeatures();
70
71
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
72
            GeometryType sourceGeomType =
73
                sourceLayer.getFeatureStore().getDefaultFeatureType()
74
                    .getDefaultGeometryAttribute().getGeomType();
75
            Polygon polygon =
76
                geomManager.createPolygon(sourceGeomType.getSubType());
77 24 llmarques
            Point vertexAnt = null;
78 3 llmarques
79
            status.setRangeOfValues(1, selectedFeatures.size());
80
81
            // Iterate over features getting geometries to create polygon
82 8 llmarques
            for (int i = 0; i < selectedFeatures.size(); i++) {
83 3 llmarques
84
                // Si ha sido solicitada la cancelacion de la tarea
85
                // la marcamos como cancelada y salimos del proceso.
86
                if (status.isCancellationRequested()) {
87
88
                    status.cancel();
89
                    return;
90
                }
91
92
                // Informamos del progreso de la tarea
93
                status.setCurValue(i);
94
95
                FeatureReference featureReference = selectedFeatures.get(i);
96 24 llmarques
                Geometry point =
97 3 llmarques
                    featureReference.getFeature().getDefaultGeometry();
98
99 24 llmarques
                if (vertexAnt == null || !vertexAnt.equals(point)) {
100
                    polygon.addVertex((Point) point);
101
                    vertexAnt = (Point) point;
102
103
                }
104
105 3 llmarques
            }
106 24 llmarques
107
            // Close polygon if it is necessary
108
            closeSurfaceIfNecessary(polygon);
109
110 14 llmarques
            if (status.isCancellationRequested()) {
111
                status.cancel();
112 3 llmarques
113 14 llmarques
                return;
114
            }
115
116 30 llmarques
            if (createNewFeatureStore) {
117
                // Creating new feature store
118
                createNewFeatureStore(sourceFeatureType, outputLayerType,
119
                    outputLayerPath, projection);
120 3 llmarques
121 30 llmarques
                // Set createNewFeatureStore false to avoid create new feature
122
                // if process is started again
123
                parameters.setCreateNewFeatureStore(false);
124
            }
125
126 3 llmarques
            // Open the new feature store
127 30 llmarques
            FeatureStore featureStore = parameters.getFeatureStore();
128
            if (featureStore == null) {
129
                featureStore = getFeatureStore(outputLayerPath, projection);
130
            }
131 3 llmarques
132
            // Set feature store in edit mode
133 30 llmarques
            setEditingMode(featureStore);
134 3 llmarques
135
            // Add line
136 30 llmarques
            insertGeometryIntoFeauteStore(featureStore, polygon);
137 3 llmarques
138
            // Save changes and finish editing
139 30 llmarques
            endEditingMode(featureStore);
140 24 llmarques
141 14 llmarques
            if (status.isCancellationRequested()) {
142
                status.cancel();
143 3 llmarques
144 14 llmarques
                return;
145
            }
146
147 30 llmarques
            if (addLayer) {
148 52 llmarques
                //Dispose feature store to liberate resources
149
                featureStore.dispose();
150
151 30 llmarques
                // Add layer to MapControl
152
                featureStore =
153
                    addLayerToMapContex(outputLayerName, outputLayerPath,
154
                        projection);
155
                parameters.setAddLayer(false);
156
                parameters.setFeatureStore(featureStore);
157
            }
158 3 llmarques
159
            this.postProcess();
160
161
        } catch (Exception e) {
162
            ThreadSafeDialogsManager dlgManager =
163
                ToolsSwingLocator.getThreadSafeDialogsManager();
164 11 llmarques
165
            dlgManager.messageDialog("_process_error", "_error",
166 3 llmarques
                JOptionPane.ERROR_MESSAGE);
167
168 10 llmarques
            logger.info("Error in point to polygon process", e);
169 3 llmarques
            if (status != null) {
170
                status.abort();
171
            }
172
173
        } finally {
174
            if (status != null) {
175
                // Mark process as terminate if it is running.
176
                if (status.isRunning()) {
177
                    status.terminate();
178
                }
179
            }
180
        }
181
    }
182
183
    private void postProcess() {
184 40 llmarques
185 3 llmarques
        ThreadSafeDialogsManager dlgManager =
186
            ToolsSwingLocator.getThreadSafeDialogsManager();
187
188
        String message = "_process_finished_successfully";
189
        String title = "_information";
190
        dlgManager.messageDialog(message, title,
191
            JOptionPane.INFORMATION_MESSAGE);
192 40 llmarques
193
        // Repaint mapContext to view new derived geometries added.
194
        getParameters().getMapControl().getMapContext().invalidate();
195 3 llmarques
    }
196
}