Revision 541

View differences:

org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.main/src/main/java/org/gvsig/derivedgeometries/main/DummyLayerOrderManager.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.main;
24

  
25
import org.gvsig.fmap.mapcontext.layers.FLayer;
26
import org.gvsig.fmap.mapcontext.layers.FLayers;
27
import org.gvsig.fmap.mapcontext.layers.order.LayerOrderManager;
28
import org.gvsig.tools.persistence.PersistentState;
29
import org.gvsig.tools.persistence.exception.PersistenceException;
30

  
31

  
32
public class DummyLayerOrderManager implements LayerOrderManager{
33

  
34
    public void saveToState(PersistentState state) throws PersistenceException {
35
        // TODO Auto-generated method stub
36
        
37
    }
38

  
39
    public void loadFromState(PersistentState state)
40
        throws PersistenceException {
41
        // TODO Auto-generated method stub
42
        
43
    }
44

  
45
    public int getPosition(FLayers target, FLayer newLayer) {
46
        // TODO Auto-generated method stub
47
        return 0;
48
    }
49

  
50
    public String getName() {
51
        // TODO Auto-generated method stub
52
        return null;
53
    }
54

  
55
    public String getDescription() {
56
        // TODO Auto-generated method stub
57
        return null;
58
    }
59

  
60
    public String getCode() {
61
        // TODO Auto-generated method stub
62
        return null;
63
    }
64
    
65
    public Object clone() throws CloneNotSupportedException{
66
        return new DummyLayerOrderManager();
67
    }
68

  
69
}
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.main/src/main/java/org/gvsig/derivedgeometries/main/Main.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.main;
24

  
25
import java.awt.BorderLayout;
26
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.io.File;
29

  
30
import javax.swing.AbstractAction;
31
import javax.swing.JButton;
32
import javax.swing.JComponent;
33
import javax.swing.JFrame;
34
import javax.swing.JMenu;
35
import javax.swing.JMenuBar;
36
import javax.swing.JMenuItem;
37
import javax.swing.JOptionPane;
38
import javax.swing.JToolBar;
39
import javax.swing.WindowConstants;
40

  
41
import org.cresques.cts.IProjection;
42

  
43
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesLocator;
44
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesManager;
45
import org.gvsig.fmap.crs.CRSFactory;
46
import org.gvsig.fmap.dal.DALLocator;
47
import org.gvsig.fmap.dal.DataManager;
48
import org.gvsig.fmap.dal.DataStoreParameters;
49
import org.gvsig.fmap.dal.exception.InitializeException;
50
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
51
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.mapcontext.MapContextLocator;
54
import org.gvsig.fmap.mapcontext.MapContextManager;
55
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
56
import org.gvsig.fmap.mapcontext.layers.FLayer;
57
import org.gvsig.fmap.mapcontrol.MapControl;
58
import org.gvsig.fmap.mapcontrol.MapControlCreationException;
59
import org.gvsig.fmap.mapcontrol.MapControlLocator;
60
import org.gvsig.fmap.mapcontrol.MapControlManager;
61
import org.gvsig.fmap.mapcontrol.tools.PanListenerImpl;
62
import org.gvsig.fmap.mapcontrol.tools.Behavior.MoveBehavior;
63
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
64
import org.gvsig.tools.swing.api.ToolsSwingLocator;
65
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
66
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
67
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
68

  
69
public class Main {
70

  
71
    final String DEFAULT_CRS_CODE = "EPSG:23030";
72

  
73
    private DerivedGeometriesManager derivedGeometriesManager;
74
    private MapControlManager mapControlManager;
75
    private DataManager dataManager;
76
    private MapContextManager mapContextManager;
77
    private WindowManager windowManager;
78

  
79
    private MapControl mapControl;
80

  
81
    private JFrame mainFrame;
82

  
83
    // Actions
84
    private AbstractAction derivedGeometries;
85

  
86
    private AbstractAction openAddLayerDialog;
87

  
88
    private AbstractAction exit;
89

  
90
    public static void main(String[] args) {
91
        new DefaultLibrariesInitializer().fullInitialize();
92
        Main main = new Main();
93
        main.doMain();
94

  
95
    }
96

  
97
    public Main() {
98
        mapControlManager = MapControlLocator.getMapControlManager();
99
        derivedGeometriesManager = DerivedGeometriesLocator.getManager();
100
        dataManager = DALLocator.getDataManager();
101
        mapContextManager = MapContextLocator.getMapContextManager();
102
        windowManager = ToolsSwingLocator.getWindowManager();
103
        mapContextManager.getSymbolManager().getSymbolPreferences()
104
            .setDefaultSymbolFillColorAleatory(true);
105

  
106
        // Register our DummyLayerOrderManger needed to add sample layer
107
        MapContextLocator
108
            .registerDefaultOrderManager(DummyLayerOrderManager.class);
109
    }
110

  
111
    private void doMain() {
112
        try {
113
            mapControl = mapControlManager.createJMapControlPanel(mapContextManager.createMapContext());
114
        } catch (MapControlCreationException e) {
115
            // TODO Auto-generated catch block
116
        }
117

  
118
        mapControl.addBehavior("pan", new MoveBehavior(new PanListenerImpl(
119
            mapControl)));
120

  
121
        mapControl.setTool("pan");
122

  
123
        IProjection defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE);
124
        mapControl.getViewPort().setProjection(defaultProjection);
125

  
126
        // Create JFrame to show data
127
        mainFrame = new JFrame("Editing test app");
128
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
129
        mainFrame.setPreferredSize(new Dimension(800, 680));
130
        mainFrame.add(mapControl, BorderLayout.CENTER);
131

  
132
        // Create actions
133
        createActions();
134

  
135
        // Create menu bar
136
        createMenu();
137

  
138
        // Create tools bar
139
        createToolBar();
140

  
141
        // Display the window.
142
        mainFrame.pack();
143
        mainFrame.setLocation(500, 0);
144
        mainFrame.setVisible(true);
145
    }
146

  
147
    private void createToolBar() {
148
        JToolBar toolBar = new JToolBar();
149

  
150
        toolBar.add(new JButton(openAddLayerDialog));
151
        toolBar.add(new JButton(derivedGeometries));
152

  
153
        mainFrame.add(toolBar, BorderLayout.PAGE_START);
154

  
155
    }
156

  
157
    private void createMenu() {
158
        // Create the menu bar.
159
        JMenuBar menuBar = new JMenuBar();
160

  
161
        // Build the menu.
162
        JMenu menuFile = new JMenu("File");
163
        menuFile.add(openAddLayerDialog);
164
        menuFile.add(new JMenuItem(exit));
165

  
166
        mainFrame.setJMenuBar(menuBar);
167

  
168
    }
169

  
170
    @SuppressWarnings("serial")
171
    private void createActions() {
172

  
173
        derivedGeometries = new AbstractAction("DerivedGeometries") {
174

  
175
            public void actionPerformed(ActionEvent e) {
176
                JComponent panel =
177
                    derivedGeometriesManager.getDerivedGeometriesPanel(
178
                        mapControl).asJComponent();
179

  
180
                windowManager.showWindow(panel, "derived_geometries",
181
                    MODE.WINDOW);
182
            }
183
        };
184

  
185
        openAddLayerDialog = new AbstractAction("Add sample layers") {
186

  
187
            public void actionPerformed(ActionEvent e) {
188
                try {
189
                    addLayer("src/main/resources/sample-cartography/puertos_andalucia.shp");
190
                    addLayer("src/main/resources/sample-cartography/hidro_andalucia.shp");
191
                    addLayer("src/main/resources/sample-cartography/Provincias andalucia.shp");
192

  
193
                    ThreadSafeDialogsManager dlgManager =
194
                        ToolsSwingLocator.getThreadSafeDialogsManager();
195

  
196
                    StringBuilder stb = new StringBuilder();
197
                    stb.append("Layer: Provincias_andalucia Type: multisurface\n");
198
                    stb.append("Layer: hidro_andalucia Type: multicurve\n");
199
                    stb.append("Layer: puertos_andalucia Type: point\n");
200

  
201
                    dlgManager.messageDialog(stb.toString(),
202
                        "Sample layers added succesfully",
203
                        JOptionPane.INFORMATION_MESSAGE);
204
                } catch (Exception e1) {
205
                    // TODO Auto-generated catch block
206
                    e1.printStackTrace();
207
                }
208
            }
209
        };
210

  
211
        exit = new AbstractAction("Exit") {
212

  
213
            public void actionPerformed(ActionEvent e) {
214
                System.exit(0);
215
            }
216
        };
217

  
218
    }
219

  
220
    public void addLayer(String shpPath) throws InitializeException,
221
        ProviderNotRegisteredException, ValidateDataParametersException,
222
        LoadLayerException {
223
        DataStoreParameters params;
224
        params = dataManager.createStoreParameters("Shape");
225

  
226
        File shpFile = new File(shpPath);
227

  
228
        params.setDynValue("shpFile", shpFile.getPath());
229
        params.setDynValue("CRS", CRSFactory.getCRS(DEFAULT_CRS_CODE));
230
        params.validate();
231

  
232
        FeatureStore store =
233
            (FeatureStore) dataManager.openStore("Shape", params);
234

  
235
        FLayer layer = mapContextManager.createLayer(store.getName(), store);
236

  
237
        mapControl.getMapContext().getLayers().addLayer(layer);
238
        mapControl.getMapContext().getLayers().setActive(true);
239
        layer.dispose();
240

  
241
    }
242

  
243
}
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.main/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
	<modelVersion>4.0.0</modelVersion>
3
	<parent>
4
		<groupId>org.gvsig</groupId>
5
		<artifactId>org.gvsig.derivedgeometries</artifactId>
6
		<version>1.0.98</version>
7
	</parent>
8
	<artifactId>org.gvsig.derivedgeometries.main</artifactId>
9
	<name>org.gvsig.derivedgeometries.main</name>
10

  
11
	<dependencies>
12
		<dependency>
13
			<groupId>org.gvsig</groupId>
14
			<artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
15
			<scope>compile</scope>
16
		</dependency>
17
		<dependency>
18
			<groupId>org.gvsig</groupId>
19
			<artifactId>org.gvsig.fmap.control</artifactId>
20
			<scope>compile</scope>
21
		</dependency>
22
		<dependency>
23
			<groupId>org.gvsig</groupId>
24
			<artifactId>org.gvsig.fmap.dal.api</artifactId>
25
			<scope>compile</scope>
26
		</dependency>
27
		<dependency>
28
			<groupId>org.gvsig</groupId>
29
			<artifactId>org.gvsig.fmap.geometry.api</artifactId>
30
			<scope>compile</scope>
31
		</dependency>
32
		<dependency>
33
			<groupId>org.gvsig</groupId>
34
			<artifactId>org.gvsig.fmap.mapcontext.api</artifactId>
35
			<scope>compile</scope>
36
		</dependency>
37
		<dependency>
38
	        <groupId>org.gvsig</groupId>
39
	        <artifactId>org.gvsig.tools.swing.api</artifactId>
40
	        <scope>compile</scope>
41
        </dependency>
42

  
43
		<!-- runtime dependencies -->
44
		<dependency>
45
			<groupId>org.gvsig</groupId>
46
			<artifactId>org.gvsig.metadata.lib.basic.impl</artifactId>
47
			<scope>runtime</scope>
48
		</dependency>
49
                <!--
50
		<dependency>
51
			<groupId>org.gvsig</groupId>
52
			<artifactId>org.gvsig.fmap.geometry.generalpath</artifactId>
53
			<scope>runtime</scope>
54
		</dependency>
55
		<dependency>
56
			<groupId>org.gvsig</groupId>
57
			<artifactId>org.gvsig.fmap.geometry.operation</artifactId>
58
			<scope>runtime</scope>
59
		</dependency>
60
                -->
61
		<dependency>
62
			<groupId>org.gvsig</groupId>
63
			<artifactId>org.gvsig.fmap.mapcontext.impl</artifactId>
64
			<scope>runtime</scope>
65
		</dependency>
66
		<dependency>
67
			<groupId>org.gvsig</groupId>
68
			<artifactId>org.gvsig.symbology.lib.impl</artifactId>
69
			<scope>runtime</scope>
70
		</dependency>
71
		<dependency>
72
			<groupId>org.gvsig</groupId>
73
			<artifactId>org.gvsig.fmap.dal.impl</artifactId>
74
			<scope>runtime</scope>
75
		</dependency>
76
		<dependency>
77
			<groupId>org.gvsig</groupId>
78
			<artifactId>org.gvsig.fmap.dal.spi</artifactId>
79
			<scope>runtime</scope>
80
		</dependency>
81
		<dependency>
82
			<groupId>org.gvsig</groupId>
83
			<artifactId>org.gvsig.fmap.dal.file.shp</artifactId>
84
			<scope>runtime</scope>
85
		</dependency>
86
		<dependency>
87
			<groupId>org.gvsig</groupId>
88
			<artifactId>org.gvsig.fmap.dal.file.dbf</artifactId>
89
			<scope>runtime</scope>
90
		</dependency>
91
		<dependency>
92
			<groupId>org.gvsig</groupId>
93
			<artifactId>org.gvsig.fmap.dal.file.lib</artifactId>
94
			<scope>runtime</scope>
95
		</dependency>
96
		<dependency>
97
			<groupId>org.gvsig</groupId>
98
			<artifactId>org.gvsig.timesupport.lib.api</artifactId>
99
			<scope>runtime</scope>
100
		</dependency>
101
		<dependency>
102
			<groupId>org.gvsig</groupId>
103
			<artifactId>org.gvsig.timesupport.lib.impl</artifactId>
104
			<scope>runtime</scope>
105
		</dependency>
106
		<dependency>
107
			<groupId>org.gvsig</groupId>
108
			<artifactId>org.gvsig.tools.evaluator.sqljep</artifactId>
109
			<scope>runtime</scope>
110
		</dependency>
111
		<dependency>
112
			<groupId>org.gvsig</groupId>
113
			<artifactId>org.gvsig.proj.lib.proj4j</artifactId>
114
			<scope>runtime</scope>
115
		</dependency>
116
		<dependency>
117
			<groupId>org.gvsig</groupId>
118
			<artifactId>
119
				org.gvsig.derivedgeometries.swing.api
120
			</artifactId>
121
		</dependency>
122
		<dependency>
123
			<groupId>org.gvsig</groupId>
124
			<artifactId>
125
				org.gvsig.derivedgeometries.swing.impl
126
			</artifactId>
127
		</dependency>
128
		<dependency>
129
	        <groupId>org.gvsig</groupId>
130
	        <artifactId>org.gvsig.tools.swing.impl</artifactId>
131
        <scope>runtime</scope>
132
    </dependency>
133
	</dependencies>
134
</project>
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.swing/org.gvsig.derivedgeometries.swing.impl/src/main/java/org/gvsig/derivedgeometries/swing/impl/DefaultDerivedGeometriesManager.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.swing.impl;
24

  
25
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesManager;
26
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters;
27
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesProcess;
28
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesProcess.TYPE;
29
import org.gvsig.derivedgeometries.swing.api.panels.DerivedGeometriesPanel;
30
import org.gvsig.derivedgeometries.swing.api.panels.FeaturesControlPanel;
31
import org.gvsig.derivedgeometries.swing.api.panels.LayerAndProcessSelectionPanel;
32
import org.gvsig.derivedgeometries.swing.impl.panels.DefaultDerivedGeometriesPanel;
33
import org.gvsig.derivedgeometries.swing.impl.panels.DefaultFeaturesControlPanel;
34
import org.gvsig.derivedgeometries.swing.impl.panels.DefaultLayerAndProcessSelectionPanel;
35
import org.gvsig.derivedgeometries.swing.impl.processes.LineToClosedPolylineDerivedGeometriesProcess;
36
import org.gvsig.derivedgeometries.swing.impl.processes.PointToLineDerivedGeometriesProcess;
37
import org.gvsig.derivedgeometries.swing.impl.processes.PointToPolygonDerivedGeometriesProcess;
38
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
39
import org.gvsig.fmap.mapcontrol.MapControl;
40

  
41
public class DefaultDerivedGeometriesManager implements
42
    DerivedGeometriesManager {
43

  
44
    public DerivedGeometriesParameters createDerivedGeometriesParameters(
45
        MapControl mapControl, FLyrVect sourceLayer, String outputLayerName,
46
        String outputLayerPath, int outputLayerType, TYPE processType) {
47

  
48
        return new DefaultDerivedGeometriesParameters(mapControl, sourceLayer,
49
            outputLayerName, outputLayerPath, outputLayerType, processType);
50
    }
51

  
52
    public DerivedGeometriesParameters createDerivedGeometriesParameters() {
53
        return new DefaultDerivedGeometriesParameters();
54
    }
55

  
56
    public LayerAndProcessSelectionPanel getLayerAndProcessSelectionPanel(
57
        MapControl mapControl) {
58
        return new DefaultLayerAndProcessSelectionPanel(mapControl);
59
    }
60

  
61
    public FeaturesControlPanel getFeaturesControlPanel() {
62
        return new DefaultFeaturesControlPanel();
63
    }
64

  
65
    public DerivedGeometriesPanel getDerivedGeometriesPanel(
66
        MapControl mapControl) {
67
        return new DefaultDerivedGeometriesPanel(mapControl);
68
    }
69

  
70
    public void startDerivedGeometriesProcess(
71
        DerivedGeometriesParameters parameters) {
72
        TYPE processType = parameters.getProcessType();
73
        DerivedGeometriesProcess newProcess = null;
74

  
75
        switch (processType) {
76
        case POINTS_TO_LINE:
77
            newProcess = new PointToLineDerivedGeometriesProcess(parameters);
78
            break;
79
        case POINTS_TO_POLYGON:
80
            newProcess = new PointToPolygonDerivedGeometriesProcess(parameters);
81
            break;
82
        case LINES_TO_CLOSED_POLYLINE:
83
            newProcess =
84
                new LineToClosedPolylineDerivedGeometriesProcess(parameters);
85
            break;
86
        default:
87
            break;
88
        }
89

  
90
        newProcess.start();
91

  
92
    }
93
}
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.swing/org.gvsig.derivedgeometries.swing.impl/src/main/java/org/gvsig/derivedgeometries/swing/impl/processes/LineToClosedPolylineDerivedGeometriesProcess.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.swing.impl.processes;
24

  
25
import java.util.List;
26

  
27
import javax.swing.JOptionPane;
28

  
29
import org.cresques.cts.IProjection;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

  
33
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters;
34
import org.gvsig.derivedgeometries.swing.impl.AbstractDerivedGeometriesProcess;
35
import org.gvsig.fmap.dal.feature.FeatureReference;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.primitive.Curve;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.primitive.Polygon;
44
import org.gvsig.fmap.geom.type.GeometryType;
45
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
46
import org.gvsig.tools.swing.api.ToolsSwingLocator;
47
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
48
import org.gvsig.tools.task.SimpleTaskStatus;
49

  
50
public class LineToClosedPolylineDerivedGeometriesProcess extends
51
    AbstractDerivedGeometriesProcess {
52

  
53
    private static final Logger logger = LoggerFactory
54
        .getLogger(LineToClosedPolylineDerivedGeometriesProcess.class);
55

  
56
    public LineToClosedPolylineDerivedGeometriesProcess(
57
        DerivedGeometriesParameters parameters) {
58
        super("Line to polyline process", parameters);
59
    }
60

  
61
    public void run() {
62
        SimpleTaskStatus status = null;
63

  
64
        try {
65
            status = (SimpleTaskStatus) this.getTaskStatus();
66

  
67
            if (status.isCancellationRequested()) {
68

  
69
                status.cancel();
70
                return;
71
            }
72

  
73
            // Get data process
74
            DerivedGeometriesParameters parameters = super.getParameters();
75
            String outputLayerName = parameters.getOutPutLayerName();
76
            String outputLayerPath = parameters.getOutPutLayerPath();
77
            int outputLayerType = parameters.getOutPutLayerType();
78
            boolean addLayer = parameters.getAddLayer();
79
            boolean createNewFeatureStore =
80
                parameters.getCreateNewFeatureStore();
81
            FLyrVect sourceLayer = parameters.getSourceLayer();
82
            IProjection projection = sourceLayer.getProjection();
83
            FeatureType sourceFeatureType =
84
                sourceLayer.getFeatureStore().getDefaultFeatureType();
85
            List<FeatureReference> selectedFeatures =
86
                parameters.getSelectedFeatures();
87

  
88
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
89
            GeometryType sourceGeomType =
90
                sourceLayer.getFeatureStore().getDefaultFeatureType()
91
                    .getDefaultGeometryAttribute().getGeomType();
92
            Polygon polygon =
93
                geomManager.createPolygon(sourceGeomType.getSubType());
94
            Point vertexAnt = null;
95

  
96
            status.setRangeOfValues(1, selectedFeatures.size());
97

  
98
            // Iterate over features getting geometries to create polygon
99
            for (int i = 0; i < selectedFeatures.size(); i++) {
100

  
101
                // Si ha sido solicitada la cancelacion de la tarea
102
                // la marcamos como cancelada y salimos del proceso.
103
                if (status.isCancellationRequested()) {
104

  
105
                    status.cancel();
106
                    return;
107
                }
108

  
109
                // Informamos del progreso de la tarea
110
                status.setCurValue(i);
111

  
112
                FeatureReference featureReference = selectedFeatures.get(i);
113
                Geometry geom =
114
                    featureReference.getFeature().getDefaultGeometry();
115
                for (int j = 0; j < ((Curve) geom).getNumVertices(); j++) {
116
                    Point point = ((Curve) geom).getVertex(j);
117

  
118
                    if (vertexAnt == null || !vertexAnt.equals(point)) {
119
                        polygon.addVertex(point);
120
                        vertexAnt = point;
121
                    }
122

  
123
                }
124

  
125
            }
126

  
127
            // Close polygon if it is necessary
128
            closeSurfaceIfNecessary(polygon);
129

  
130
            if (status.isCancellationRequested()) {
131
                status.cancel();
132

  
133
                return;
134
            }
135

  
136
            if (createNewFeatureStore) {
137
                // Creating new feature store
138
                createNewFeatureStore(sourceFeatureType, outputLayerType,
139
                    outputLayerPath, projection);
140

  
141
                // Set createNewFeatureStore false to avoid create new feature
142
                // if process is started again
143
                parameters.setCreateNewFeatureStore(false);
144
            }
145

  
146
            // Open the new feature store
147
            FeatureStore featureStore = parameters.getFeatureStore();
148
            if (featureStore == null) {
149
                featureStore = getFeatureStore(outputLayerPath, projection);
150
            }
151

  
152
            // Set feature store in edit mode
153
            setEditingMode(featureStore);
154

  
155
            // Add line
156
            insertGeometryIntoFeauteStore(featureStore, polygon);
157

  
158
            // Save changes and finish editing
159
            endEditingMode(featureStore);
160

  
161
            if (status.isCancellationRequested()) {
162
                status.cancel();
163

  
164
                return;
165
            }
166

  
167
            if (addLayer) {
168
                // Dispose feature store to liberate resources
169
                featureStore.dispose();
170

  
171
                // Add layer to MapControl
172
                featureStore =
173
                    addLayerToMapContex(outputLayerName, outputLayerPath,
174
                        projection);
175
                parameters.setAddLayer(false);
176
                parameters.setFeatureStore(featureStore);
177
            }
178

  
179
            this.postProcess();
180

  
181
        } catch (Exception e) {
182
            ThreadSafeDialogsManager dlgManager =
183
                ToolsSwingLocator.getThreadSafeDialogsManager();
184

  
185
            dlgManager.messageDialog("_process_error", "_error",
186
                JOptionPane.ERROR_MESSAGE);
187

  
188
            logger.info("Error in line to closed process", e);
189
            if (status != null) {
190
                status.abort();
191
            }
192

  
193
        } finally {
194
            if (status != null) {
195
                // Mark process as terminate if it is running.
196
                if (status.isRunning()) {
197
                    status.terminate();
198
                }
199
            }
200
        }
201
    }
202

  
203
    private void postProcess() {
204

  
205
        ThreadSafeDialogsManager dlgManager =
206
            ToolsSwingLocator.getThreadSafeDialogsManager();
207

  
208
        String message = "_process_finished_successfully";
209
        String title = "_information";
210
        dlgManager.messageDialog(message, title,
211
            JOptionPane.INFORMATION_MESSAGE);
212

  
213
        // Repaint mapContext to view new derived geometries added.
214
        getParameters().getMapControl().getMapContext().invalidate();
215
    }
216

  
217
}
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.swing/org.gvsig.derivedgeometries.swing.impl/src/main/java/org/gvsig/derivedgeometries/swing/impl/processes/PointToLineDerivedGeometriesProcess.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.swing.impl.processes;
24

  
25
import java.util.List;
26

  
27
import javax.swing.JOptionPane;
28

  
29
import org.cresques.cts.IProjection;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

  
33
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters;
34
import org.gvsig.derivedgeometries.swing.impl.AbstractDerivedGeometriesProcess;
35
import org.gvsig.fmap.dal.feature.FeatureReference;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.primitive.Line;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.type.GeometryType;
44
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
45
import org.gvsig.tools.swing.api.ToolsSwingLocator;
46
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
47
import org.gvsig.tools.task.SimpleTaskStatus;
48

  
49
public class PointToLineDerivedGeometriesProcess extends
50
    AbstractDerivedGeometriesProcess {
51

  
52
    private static final Logger logger = LoggerFactory
53
        .getLogger(PointToLineDerivedGeometriesProcess.class);
54

  
55
    public PointToLineDerivedGeometriesProcess(
56
        DerivedGeometriesParameters parameters) {
57
        super("Point to line process", parameters);
58
    }
59

  
60
    public void run() {
61
        SimpleTaskStatus status = null;
62

  
63
        try {
64
            status = (SimpleTaskStatus) this.getTaskStatus();
65

  
66
            if (status.isCancellationRequested()) {
67

  
68
                status.cancel();
69
                return;
70
            }
71

  
72
            // Get data process
73
            DerivedGeometriesParameters parameters = super.getParameters();
74
            String outputLayerName = parameters.getOutPutLayerName();
75
            String outputLayerPath = parameters.getOutPutLayerPath();
76
            int outputLayerType = parameters.getOutPutLayerType();
77
            boolean addLayer = parameters.getAddLayer();
78
            boolean createNewFeatureStore =
79
                parameters.getCreateNewFeatureStore();
80
            FLyrVect sourceLayer = parameters.getSourceLayer();
81
            IProjection projection = sourceLayer.getProjection();
82
            FeatureType sourceFeatureType =
83
                sourceLayer.getFeatureStore().getDefaultFeatureType();
84
            List<FeatureReference> selectedFeatures =
85
                parameters.getSelectedFeatures();
86

  
87
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
88
            GeometryType sourceGeomType =
89
                sourceLayer.getFeatureStore().getDefaultFeatureType()
90
                    .getDefaultGeometryAttribute().getGeomType();
91
            Line line = geomManager.createLine(sourceGeomType.getSubType());
92

  
93
            status.setRangeOfValues(1, selectedFeatures.size());
94

  
95
            // Iterate over features getting geometries to create polygon
96
            for (int i = 0; i < selectedFeatures.size(); i++) {
97

  
98
                // Si ha sido solicitada la cancelacion de la tarea
99
                // la marcamos como cancelada y salimos del proceso.
100
                if (status.isCancellationRequested()) {
101
                    status.cancel();
102
                    return;
103
                }
104

  
105
                // Informamos del progreso de la tarea
106
                status.setCurValue(i);
107

  
108
                FeatureReference featureReference = selectedFeatures.get(i);
109
                Geometry geom =
110
                    featureReference.getFeature().getDefaultGeometry();
111
                line.addVertex((Point) geom);
112

  
113
            }
114

  
115
            if (status.isCancellationRequested()) {
116
                status.cancel();
117

  
118
                return;
119
            }
120

  
121
            if (createNewFeatureStore) {
122
                // Creating new feature store
123
                createNewFeatureStore(sourceFeatureType, outputLayerType,
124
                    outputLayerPath, projection);
125

  
126
                // Set createNewFeatureStore false to avoid create new feature
127
                // if process is started again
128
                parameters.setCreateNewFeatureStore(false);
129
            }
130

  
131
            // Open the new feature store
132
            FeatureStore featureStore = parameters.getFeatureStore();
133
            if (featureStore == null) {
134
                featureStore = getFeatureStore(outputLayerPath, projection);
135
            }
136

  
137
            // Set feature store in edit mode
138
            setEditingMode(featureStore);
139

  
140
            // Add line
141
            insertGeometryIntoFeauteStore(featureStore, line);
142

  
143
            // Save changes and finish editing
144
            endEditingMode(featureStore);
145

  
146
            if (status.isCancellationRequested()) {
147
                status.cancel();
148

  
149
                return;
150
            }
151

  
152
            if (addLayer) {
153
                // Dispose feature store to liberate resources
154
                featureStore.dispose();
155

  
156
                // Add layer to MapControl
157
                featureStore =
158
                    addLayerToMapContex(outputLayerName, outputLayerPath,
159
                        projection);
160
                parameters.setAddLayer(false);
161
                parameters.setFeatureStore(featureStore);
162
            }
163

  
164
            this.postProcess();
165

  
166
        } catch (Exception e) {
167
            ThreadSafeDialogsManager dlgManager =
168
                ToolsSwingLocator.getThreadSafeDialogsManager();
169

  
170
            dlgManager.messageDialog("_process_error", "_error",
171
                JOptionPane.ERROR_MESSAGE);
172

  
173
            logger.info("Error in point to line process", e);
174
            if (status != null) {
175
                status.abort();
176
            }
177

  
178
        } finally {
179
            if (status != null) {
180
                // Mark process as terminate if it is running.
181
                if (status.isRunning()) {
182
                    status.terminate();
183
                }
184
            }
185
        }
186
    }
187

  
188
    private void postProcess() {
189

  
190
        ThreadSafeDialogsManager dlgManager =
191
            ToolsSwingLocator.getThreadSafeDialogsManager();
192

  
193
        String message = "_process_finished_successfully";
194
        String title = "_information";
195
        dlgManager.messageDialog(message, title,
196
            JOptionPane.INFORMATION_MESSAGE);
197

  
198
        // Repaint mapContext to view new derived geometries added.
199
        getParameters().getMapControl().getMapContext().invalidate();
200
    }
201

  
202
}
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.swing/org.gvsig.derivedgeometries.swing.impl/src/main/java/org/gvsig/derivedgeometries/swing/impl/processes/PointToPolygonDerivedGeometriesProcess.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.swing.impl.processes;
24

  
25
import java.util.List;
26

  
27
import javax.swing.JOptionPane;
28

  
29
import org.cresques.cts.IProjection;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

  
33
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters;
34
import org.gvsig.derivedgeometries.swing.impl.AbstractDerivedGeometriesProcess;
35
import org.gvsig.fmap.dal.feature.FeatureReference;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.primitive.Point;
42
import org.gvsig.fmap.geom.primitive.Polygon;
43
import org.gvsig.fmap.geom.type.GeometryType;
44
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
45
import org.gvsig.tools.swing.api.ToolsSwingLocator;
46
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
47
import org.gvsig.tools.task.SimpleTaskStatus;
48

  
49
public class PointToPolygonDerivedGeometriesProcess extends
50
    AbstractDerivedGeometriesProcess {
51

  
52
    private static final Logger logger = LoggerFactory
53
        .getLogger(PointToPolygonDerivedGeometriesProcess.class);
54

  
55
    public PointToPolygonDerivedGeometriesProcess(
56
        DerivedGeometriesParameters parameters) {
57
        super("Point to polygon process", parameters);
58
    }
59

  
60
    public void run() {
61
        SimpleTaskStatus status = null;
62

  
63
        try {
64
            status = (SimpleTaskStatus) this.getTaskStatus();
65

  
66
            if (status.isCancellationRequested()) {
67

  
68
                status.cancel();
69
                return;
70
            }
71

  
72
            // Get data process
73
            DerivedGeometriesParameters parameters = super.getParameters();
74
            String outputLayerName = parameters.getOutPutLayerName();
75
            String outputLayerPath = parameters.getOutPutLayerPath();
76
            int outputLayerType = parameters.getOutPutLayerType();
77
            boolean addLayer = parameters.getAddLayer();
78
            boolean createNewFeatureStore =
79
                parameters.getCreateNewFeatureStore();
80
            FLyrVect sourceLayer = parameters.getSourceLayer();
81
            IProjection projection = sourceLayer.getProjection();
82
            FeatureType sourceFeatureType =
83
                sourceLayer.getFeatureStore().getDefaultFeatureType();
84
            List<FeatureReference> selectedFeatures =
85
                parameters.getSelectedFeatures();
86

  
87
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
88
            GeometryType sourceGeomType =
89
                sourceLayer.getFeatureStore().getDefaultFeatureType()
90
                    .getDefaultGeometryAttribute().getGeomType();
91
            Polygon polygon =
92
                geomManager.createPolygon(sourceGeomType.getSubType());
93
            Point vertexAnt = null;
94

  
95
            status.setRangeOfValues(1, selectedFeatures.size());
96

  
97
            // Iterate over features getting geometries to create polygon
98
            for (int i = 0; i < selectedFeatures.size(); i++) {
99

  
100
                // Si ha sido solicitada la cancelacion de la tarea
101
                // la marcamos como cancelada y salimos del proceso.
102
                if (status.isCancellationRequested()) {
103

  
104
                    status.cancel();
105
                    return;
106
                }
107

  
108
                // Informamos del progreso de la tarea
109
                status.setCurValue(i);
110

  
111
                FeatureReference featureReference = selectedFeatures.get(i);
112
                Geometry point =
113
                    featureReference.getFeature().getDefaultGeometry();
114

  
115
                if (vertexAnt == null || !vertexAnt.equals(point)) {
116
                    polygon.addVertex((Point) point);
117
                    vertexAnt = (Point) point;
118

  
119
                }
120

  
121
            }
122

  
123
            // Close polygon if it is necessary
124
            closeSurfaceIfNecessary(polygon);
125

  
126
            if (status.isCancellationRequested()) {
127
                status.cancel();
128

  
129
                return;
130
            }
131

  
132
            if (createNewFeatureStore) {
133
                // Creating new feature store
134
                createNewFeatureStore(sourceFeatureType, outputLayerType,
135
                    outputLayerPath, projection);
136

  
137
                // Set createNewFeatureStore false to avoid create new feature
138
                // if process is started again
139
                parameters.setCreateNewFeatureStore(false);
140
            }
141

  
142
            // Open the new feature store
143
            FeatureStore featureStore = parameters.getFeatureStore();
144
            if (featureStore == null) {
145
                featureStore = getFeatureStore(outputLayerPath, projection);
146
            }
147

  
148
            // Set feature store in edit mode
149
            setEditingMode(featureStore);
150

  
151
            // Add line
152
            insertGeometryIntoFeauteStore(featureStore, polygon);
153

  
154
            // Save changes and finish editing
155
            endEditingMode(featureStore);
156

  
157
            if (status.isCancellationRequested()) {
158
                status.cancel();
159

  
160
                return;
161
            }
162

  
163
            if (addLayer) {
164
                // Dispose feature store to liberate resources
165
                featureStore.dispose();
166

  
167
                // Add layer to MapControl
168
                featureStore =
169
                    addLayerToMapContex(outputLayerName, outputLayerPath,
170
                        projection);
171
                parameters.setAddLayer(false);
172
                parameters.setFeatureStore(featureStore);
173
            }
174

  
175
            this.postProcess();
176

  
177
        } catch (Exception e) {
178
            ThreadSafeDialogsManager dlgManager =
179
                ToolsSwingLocator.getThreadSafeDialogsManager();
180

  
181
            dlgManager.messageDialog("_process_error", "_error",
182
                JOptionPane.ERROR_MESSAGE);
183

  
184
            logger.info("Error in point to polygon process", e);
185
            if (status != null) {
186
                status.abort();
187
            }
188

  
189
        } finally {
190
            if (status != null) {
191
                // Mark process as terminate if it is running.
192
                if (status.isRunning()) {
193
                    status.terminate();
194
                }
195
            }
196
        }
197
    }
198

  
199
    private void postProcess() {
200

  
201
        ThreadSafeDialogsManager dlgManager =
202
            ToolsSwingLocator.getThreadSafeDialogsManager();
203

  
204
        String message = "_process_finished_successfully";
205
        String title = "_information";
206
        dlgManager.messageDialog(message, title,
207
            JOptionPane.INFORMATION_MESSAGE);
208

  
209
        // Repaint mapContext to view new derived geometries added.
210
        getParameters().getMapControl().getMapContext().invalidate();
211
    }
212
}
org.gvsig.derivedgeometries/tags/org.gvsig.derivedgeometries-1.0.98/org.gvsig.derivedgeometries.swing/org.gvsig.derivedgeometries.swing.impl/src/main/java/org/gvsig/derivedgeometries/swing/impl/AbstractDerivedGeometriesProcess.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.swing.impl;
24

  
25
import java.util.ArrayList;
26
import java.util.List;
27

  
28
import org.cresques.cts.IProjection;
29

  
30
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters;
31
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesProcess;
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.DataManager;
34
import org.gvsig.fmap.dal.DataServerExplorer;
35
import org.gvsig.fmap.dal.DataServerExplorerParameters;
36
import org.gvsig.fmap.dal.DataStoreParameters;
37
import org.gvsig.fmap.dal.EditingNotification;
38
import org.gvsig.fmap.dal.EditingNotificationManager;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.EditableFeatureType;
42
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
43
import org.gvsig.fmap.dal.feature.FeatureReference;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45
import org.gvsig.fmap.dal.feature.FeatureType;
46
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
47
import org.gvsig.fmap.dal.swing.DALSwingLocator;
48
import org.gvsig.fmap.geom.DataTypes;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.GeometryLocator;
51
import org.gvsig.fmap.geom.GeometryManager;
52
import org.gvsig.fmap.geom.primitive.Point;
53
import org.gvsig.fmap.geom.primitive.Surface;
54
import org.gvsig.fmap.geom.type.GeometryType;
55
import org.gvsig.fmap.mapcontext.MapContextLocator;
56
import org.gvsig.fmap.mapcontext.MapContextManager;
57
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
58
import org.gvsig.fmap.mapcontrol.MapControlLocator;
59
import org.gvsig.tools.task.AbstractMonitorableTask;
60

  
61
public abstract class AbstractDerivedGeometriesProcess extends
62
    AbstractMonitorableTask implements DerivedGeometriesProcess {
63

  
64
    private DerivedGeometriesParameters parameters;
65

  
66
    public AbstractDerivedGeometriesProcess(String name,
67
        DerivedGeometriesParameters parameters) {
68
        super(name);
69
        this.parameters = parameters;
70
    }
71

  
72
    protected FeatureStore addLayerToMapContex(String name, String path,
73
        IProjection projection) throws Exception {
74
        DataManager dataManager = DALLocator.getDataManager();
75
        MapContextManager mapContextManager =
76
            MapContextLocator.getMapContextManager();
77
        DataStoreParameters dataStoreParams =
78
            dataManager.createStoreParameters("Shape");
79
        dataStoreParams.setDynValue("shpfile", path);
80
        dataStoreParams.setDynValue("CRS", projection);
81
        dataStoreParams.setDynValue("useNullGeometry", false);
82
        dataStoreParams.validate();
83

  
84
        FLyrVect newLayer =
85
            (FLyrVect) mapContextManager.createLayer(name, dataStoreParams);
86
        getParameters().getMapControl().getMapContext().getLayers()
87
            .addLayer(newLayer);
88

  
89
        return newLayer.getFeatureStore();
90
    }
91

  
92
    protected Surface closeSurfaceIfNecessary(Surface surface) {
93
        if (!isClosed(surface) && surface != null) {
94
            Point firstp = surface.getVertex(0);
95
            firstp = (Point) firstp.cloneGeometry();
96
            surface.addVertex(firstp);
97
        }
98
        return surface;
99
    }
100

  
101
    protected void createNewFeatureStore(FeatureType sourceFeatureType,
102
        int outputLayerType, String outputLayerPath, IProjection projection)
103
        throws Exception {
104

  
105
        DataManager dataManager = DALLocator.getDataManager();
106
        DataServerExplorerParameters eparams =
107
            dataManager.createServerExplorerParameters("FilesystemExplorer");
108
        eparams.setDynValue("initialpath", "/data");
109
        DataServerExplorer serverExplorer =
110
            dataManager.openServerExplorer(eparams.getExplorerName(), eparams);
111

  
112
        NewFeatureStoreParameters sparams =
113
            (NewFeatureStoreParameters) serverExplorer
114
                .getAddParameters("Shape");
115

  
116
        EditableFeatureType newEditableFeatureType =
117
            (EditableFeatureType) sparams.getDefaultFeatureType();
118

  
119
        // Iterate over feature type. Don't add geometry fields.
120
        for (int i = 0; i < sourceFeatureType.size(); i++) {
121
            String fieldName =
122
                sourceFeatureType.getAttributeDescriptor(i).getName();
123
            int fieldType =
124
                sourceFeatureType.getAttributeDescriptor(i).getType();
125
            if (fieldType != DataTypes.GEOMETRY) {
126
                newEditableFeatureType.add(fieldName, fieldType);
127
            }
128
        }
129

  
130
        // Add new geometry field with new geometry type
131
        FeatureAttributeDescriptor geometryAttribute =
132
            (FeatureAttributeDescriptor) sourceFeatureType
133
                .get(sourceFeatureType.getDefaultGeometryAttributeName());
134
        EditableFeatureAttributeDescriptor newGeometryAttribute =
135
            newEditableFeatureType.add(geometryAttribute.getName(),
136
                geometryAttribute.getType(), geometryAttribute.getSize());
137

  
138
        GeometryManager geoManager = GeometryLocator.getGeometryManager();
139
        GeometryType sourceLayerGeomType =
140
            sourceFeatureType.getDefaultGeometryAttribute().getGeomType();
141
        GeometryType outputLayerGeomType =
142
            geoManager.getGeometryType(outputLayerType,
143
                sourceLayerGeomType.getSubType());
144

  
145
        newGeometryAttribute.setGeometryType(outputLayerGeomType);
146
        newGeometryAttribute.setPrecision(geometryAttribute.getPrecision());
147
        newGeometryAttribute.setDefaultValue(geometryAttribute
148
            .getDefaultValue());
149

  
150
        newEditableFeatureType
151
            .setDefaultGeometryAttributeName(geometryAttribute.getName());
152

  
153
        sparams.setDynValue("geometryType", null);
154
        sparams.setDynValue("shpfile", outputLayerPath);
155
        sparams.setDynValue("CRS", projection);
156
        sparams.setDynValue("useNullGeometry", false);
157
        sparams.setDefaultFeatureType(newEditableFeatureType);
158
        sparams.validate();
159

  
160
        serverExplorer.add("Shape", sparams, true);
161
    }
162

  
163
    protected void endEditingMode(FeatureStore featureStore) throws Exception {
164

  
165
        EditingNotificationManager editingNotificationManager =
166
            DALSwingLocator.getEditingNotificationManager();
167

  
168
        EditingNotification notification =
169
            editingNotificationManager.notifyObservers(this,
170
                EditingNotification.BEFORE_EXIT_EDITING_STORE, null,
171
                featureStore);
172

  
173
        if (notification.isCanceled()) {
174
            String message =
175
                String.format(
176
                    "FinishEditing of %1 has been canceled by some observer",
177
                    featureStore.getName());
178
            throw new InterruptedException(message);
179
        }
180

  
181
        getParameters().getMapControl().getCanceldraw().setCanceled(true);
182

  
183
        featureStore.finishEditing();
184
        featureStore.deleteObserver(parameters.getMapControl());
185

  
186
        editingNotificationManager.notifyObservers(this,
187
            EditingNotification.AFTER_EXIT_EDITING_STORE, null, featureStore);
188
    }
189

  
190
    protected FeatureStore getFeatureStore(String outputLayerPath,
191
        IProjection projection) throws Exception {
192

  
193
        DataManager dataManager = DALLocator.getDataManager();
194
        DataStoreParameters dataStoreParams =
195
            dataManager.createStoreParameters("Shape");
196
        dataStoreParams.setDynValue("shpfile", outputLayerPath);
197
        dataStoreParams.setDynValue("CRS", projection);
198
        dataStoreParams.setDynValue("useNullGeometry", false);
199
        dataStoreParams.validate();
200

  
201
        return (FeatureStore) dataManager.openStore("Shape", dataStoreParams);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff