Statistics
| Revision:

root / trunk / extensions / extTopology / src-test / org / gvsig / topology / SnapAndCrackGeoprocessTest.java @ 19330

History | View | Annotate | Download (6.33 KB)

1
/*
2
 * Created on 10-abr-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: 
47
* $Log: 
48
*/
49
package org.gvsig.topology;
50

    
51
import java.io.File;
52
import java.net.URL;
53
import java.util.ArrayList;
54
import java.util.HashMap;
55

    
56
import junit.framework.TestCase;
57

    
58
import org.cresques.cts.IProjection;
59
import org.gvsig.topology.ui.util.GUIUtil;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
63
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
64
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
65
import com.iver.cit.gvsig.fmap.edition.IWriter;
66
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
67
import com.iver.cit.gvsig.fmap.edition.writers.shp.MultiShpWriter;
68
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
71
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
72
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
73
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
74
import com.iver.cit.gvsig.geoprocess.impl.snapandcrack.fmap.SnapAndCrackGeoprocess;
75
import com.iver.utiles.swing.threads.IMonitorableTask;
76

    
77
public class SnapAndCrackGeoprocessTest extends TestCase {
78

    
79
        
80
        private File baseDataPath;
81
        private File baseDriversPath;
82
        private IProjection PROJECTION_DEFAULT;
83
        
84
        public void setUp() throws Exception{
85
                super.setUp();
86
                URL url = SnapAndCrackGeoprocessTest.class.getResource("testdata");
87
                if (url == null)
88
                        throw new Exception("No se encuentra el directorio con datos de prueba");
89

    
90
                baseDataPath = new File(url.getFile());
91
                if (!baseDataPath.exists())
92
                        throw new Exception("No se encuentra el directorio con datos de prueba");
93

    
94
                baseDriversPath = new File("../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers");
95
                if (!baseDriversPath.exists())
96
                        throw new Exception("Can't find drivers path " );
97
                com.iver.cit.gvsig.fmap.layers.LayerFactory.setDriversPath(baseDriversPath.getAbsolutePath());
98
                LayerFactory.setWritersPath(baseDriversPath.getAbsolutePath());
99
                if (LayerFactory.getDM().getDriverNames().length < 1)
100
                        throw new Exception("Can't find drivers in path: " + baseDriversPath);
101
                PROJECTION_DEFAULT = CRSFactory.getCRS("EPSG:23030");
102
        }
103
        
104
        public  FLayer newLayer(String fileName,
105
                           String driverName)
106
                throws LoadLayerException {
107
                FLayer solution = null;        
108
                File file = new File(baseDataPath, fileName);
109
                solution = com.iver.cit.gvsig.fmap.layers.LayerFactory.createLayer(fileName,
110
                                                                driverName,
111
                                                                file, PROJECTION_DEFAULT);
112
                solution.setAvailable(true);
113
                return solution;
114
                        
115
}
116
        public void testSnapAndCrackGeoprocess() throws LoadLayerException{
117
                
118
                FLyrVect vc1_1500 = (FLyrVect) newLayer("vc1-1500.shp", "gvSIG shp driver");
119
                
120
                FLyrVect lyr1 = (FLyrVect)newLayer("layer1.shp", "gvSIG shp driver");
121
                FLyrVect lyr2 = (FLyrVect)newLayer("layer2.shp", "gvSIG shp driver");
122
                FLyrVect lyr3 = (FLyrVect)newLayer("layer3.shp", "gvSIG shp driver");
123
                
124
                ArrayList<FLayer> crackingLyrs = new ArrayList<FLayer>();
125
                crackingLyrs.add(lyr1);
126
                crackingLyrs.add(lyr2);
127
                crackingLyrs.add(lyr3);
128
                
129
                double clusterTolerance = 0.1;
130
                
131
                SnapAndCrackGeoprocess geoprocess = new SnapAndCrackGeoprocess(vc1_1500, 
132
                                                                                                                                        crackingLyrs);
133
                String temp = System.getProperty("java.io.tmpdir")
134
                                                                                + vc1_1500.getName()
135
                                                                                + System.currentTimeMillis()
136
                                                                                + ".shp";
137
                File newFile = new File(temp);
138
                SHPLayerDefinition definition = (SHPLayerDefinition) geoprocess
139
                                .createLayerDefinition();
140
                definition.setFile(newFile);
141
                ShpSchemaManager schemaManager = new ShpSchemaManager(
142
                                newFile.getAbsolutePath());
143
                IWriter writer = null;
144
                try {
145
                        schemaManager.createSchema(definition);
146
                        int shapeType = definition.getShapeType();
147
                        if (shapeType != XTypes.MULTI) {
148
                                writer = new ShpWriter();
149
                                ((ShpWriter) writer).setFile(definition.getFile());
150
                        } else {
151
                                writer = new MultiShpWriter();
152
                                ((MultiShpWriter) writer).setFile(definition.getFile());
153
                        }
154
                        writer.initialize(definition);
155
                } catch (Exception e1) {
156
                        String error = PluginServices.getText(
157
                                        this,
158
                                        "Error_escritura_resultados");
159
                        String errorDescription = PluginServices
160
                                        .getText(this,
161
                                                        "Error_preparar_escritura_resultados");
162
                        GUIUtil.getInstance().messageBox(error,
163
                                        errorDescription);
164
                }
165

    
166
                geoprocess.setResultLayerProperties(writer,schemaManager);
167
                HashMap params = new HashMap();
168
                Double snapTolerance = new Double(clusterTolerance);
169
                params.put("snap_tolerance", snapTolerance);
170

    
171
                try {
172
                        geoprocess.setParameters(params);
173
                        geoprocess.checkPreconditions();
174
                        IMonitorableTask task = geoprocess
175
                                        .createTask();
176
                        
177
                        task.run();
178

    
179
                        FLyrVect result = (FLyrVect) geoprocess.getResult();
180
                        
181
                        assertTrue( result.getSource().getShapeCount() 
182
                                                        != 
183
                                                vc1_1500.getSource().getShapeCount());
184
                        
185
                } catch (GeoprocessException e1) {
186
                        // TODO Auto-generated catch block
187
                        e1.printStackTrace();
188
                } catch (Exception e) {
189
                        // TODO Auto-generated catch block
190
                        e.printStackTrace();
191
                }
192
        }
193
        
194
        
195
}