Revision 22887

View differences:

trunk/extensions/extTopology/src-test/org/gvsig/topology/PolyToLinesGeoprocessTest.java
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.HashMap;
54

  
55
import junit.framework.TestCase;
56

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

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

  
76
public class PolyToLinesGeoprocessTest extends TestCase {
77
	
78
	private File baseDataPath;
79
	private File baseDriversPath;
80
	private IProjection PROJECTION_DEFAULT;
81
	private FLyrVect poly1;
82

  
83
	public void setUp() throws Exception {
84
		super.setUp();
85
		URL url = PolyToLinesGeoprocessTest.class.getResource("testdata");
86
		if (url == null)
87
			throw new Exception(
88
					"No se encuentra el directorio con datos de prueba");
89

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

  
95
		baseDriversPath = new File(
96
				"../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers");
97
		if (!baseDriversPath.exists())
98
			throw new Exception("Can't find drivers path ");
99
		com.iver.cit.gvsig.fmap.layers.LayerFactory
100
				.setDriversPath(baseDriversPath.getAbsolutePath());
101
		LayerFactory.setWritersPath(baseDriversPath.getAbsolutePath());
102
		if (LayerFactory.getDM().getDriverNames().length < 1)
103
			throw new Exception("Can't find drivers in path: "
104
					+ baseDriversPath);
105
		PROJECTION_DEFAULT = CRSFactory.getCRS("EPSG:23030");
106
		
107
		poly1 = (FLyrVect) newLayer("poligonos.shp",
108
				"gvSIG shp driver");
109
		poly1.createSpatialIndex();
110

  
111
	}
112

  
113
	public FLayer newLayer(String fileName, String driverName)
114
			throws LoadLayerException {
115
		FLayer solution = null;
116
		File file = new File(baseDataPath, fileName);
117
		solution = com.iver.cit.gvsig.fmap.layers.LayerFactory.createLayer(
118
				fileName, driverName, file, PROJECTION_DEFAULT);
119
		solution.setAvailable(true);
120
		return solution;
121

  
122
	}
123
	
124
	
125
	
126
	public void test1(){
127
		
128
		PolyToLinesGeoprocess geoprocess = new PolyToLinesGeoprocess(poly1);
129
		String temp = System.getProperty("java.io.tmpdir") + poly1.getName();
130
		File newFile = new File(temp);
131
		SHPLayerDefinition definition = (SHPLayerDefinition) geoprocess
132
				.createLayerDefinition();
133
		definition.setFile(newFile);
134
		ShpSchemaManager schemaManager = new ShpSchemaManager(newFile
135
				.getAbsolutePath());
136
		IWriter writer = null;
137
		try {
138
			schemaManager.createSchema(definition);
139
			int shapeType = definition.getShapeType();
140
			if (shapeType != XTypes.MULTI) {
141
				writer = new ShpWriter();
142
				((ShpWriter) writer).setFile(definition.getFile());
143
			} else {
144
				writer = new MultiShpWriter();
145
				((MultiShpWriter) writer).setFile(definition.getFile());
146
			}
147
			writer.initialize(definition);
148
		} catch (Exception e1) {
149
			String error = PluginServices.getText(this,
150
					"Error_escritura_resultados");
151
			String errorDescription = PluginServices.getText(this,
152
					"Error_preparar_escritura_resultados");
153
			GUIUtil.getInstance().messageBox(error, errorDescription);
154
		}
155

  
156
		geoprocess.setResultLayerProperties(writer, schemaManager);
157
		
158
		HashMap params = new HashMap();
159
		Boolean selected = new Boolean(false);
160
		params.put("layer_selection", selected);
161

  
162
		try {
163
			geoprocess.setParameters(params);
164
			geoprocess.checkPreconditions();
165
			IMonitorableTask task = geoprocess.createTask();
166

  
167
			task.run();
168

  
169
			FLyrVect result = (FLyrVect) geoprocess.getResult();
170

  
171
			System.out.println("numero de geometrias de la capa resultado :"
172
					+ result.getSource().getShapeCount());
173
			
174
		} catch (GeoprocessException e1) {
175
			// TODO Auto-generated catch block
176
			e1.printStackTrace();
177
		} catch (Exception e) {
178
			// TODO Auto-generated catch block
179
			e.printStackTrace();
180
		}
181
	}
182
}

Also available in: Unified diff