root / branches / v10 / extensions / extPublish / src-test / com / iver / cit / gvsig / publish / util / LocalResourcesTestUtilities.java @ 13570
History | View | Annotate | Download (14 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Iba?ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
/**
|
42 |
* @author Jos? Vicente Hig?n (josevicente.higon@iver.es)
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.publish.util; |
45 |
|
46 |
|
47 |
import java.io.BufferedReader; |
48 |
import java.io.File; |
49 |
import java.io.FileNotFoundException; |
50 |
import java.io.FileReader; |
51 |
import java.io.IOException; |
52 |
import java.io.InputStream; |
53 |
import java.io.InputStreamReader; |
54 |
import java.net.URL; |
55 |
import java.sql.Connection; |
56 |
import java.sql.DriverManager; |
57 |
import java.sql.SQLException; |
58 |
import java.sql.Statement; |
59 |
import java.util.ArrayList; |
60 |
|
61 |
import org.cresques.cts.IProjection; |
62 |
|
63 |
import com.hardcode.driverManager.Driver; |
64 |
import com.hardcode.gdbms.engine.values.Value; |
65 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
66 |
import com.iver.cit.gvsig.fmap.DriverException; |
67 |
import com.iver.cit.gvsig.fmap.MapContext; |
68 |
import com.iver.cit.gvsig.fmap.ViewPort; |
69 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
70 |
import com.iver.cit.gvsig.fmap.core.FShape; |
71 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
72 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
73 |
import com.iver.cit.gvsig.fmap.drivers.ConcreteMemoryDriver; |
74 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory; |
75 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
76 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
77 |
import com.iver.cit.gvsig.fmap.drivers.IConnection; |
78 |
import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
79 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
80 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
81 |
import com.iver.cit.gvsig.fmap.drivers.raster.CmsRasterDriver; |
82 |
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver; |
83 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
84 |
import com.iver.cit.gvsig.fmap.layers.FLayerFileVectorial; |
85 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
86 |
import com.iver.cit.gvsig.fmap.layers.FLyrRaster; |
87 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
88 |
import com.iver.cit.gvsig.project.Project; |
89 |
import com.iver.cit.gvsig.project.ProjectFactory; |
90 |
import com.iver.cit.gvsig.project.documents.view.ProjectView; |
91 |
|
92 |
/**
|
93 |
* This class represents a factory which can make gvSIG objects like a project,
|
94 |
* a view, a layer, ...
|
95 |
* This class is useful to do tests
|
96 |
*
|
97 |
* @author Jos? Vicente Hig?n (josevicente.higon@iver.es)
|
98 |
*
|
99 |
*/
|
100 |
public class LocalResourcesTestUtilities { |
101 |
|
102 |
private String basePath = null; |
103 |
|
104 |
/**
|
105 |
* Constructor: gets absolute path to resources directory
|
106 |
* @throws Exception
|
107 |
*/
|
108 |
|
109 |
public LocalResourcesTestUtilities(String basePath){ |
110 |
this.basePath = basePath;
|
111 |
//Load the drivers
|
112 |
LayerFactory.setDriversPath("../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers");
|
113 |
//LayerFactory.setDriversPath("/home/jvhigon/eclipse/workspace-v10/_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers/");
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Creates a project
|
118 |
*/
|
119 |
public Project getProject(){
|
120 |
Project gvProj = ProjectFactory.createProject(); |
121 |
gvProj.setOwner("test_project_owner");
|
122 |
gvProj.setName("test_project_name");
|
123 |
return gvProj;
|
124 |
} |
125 |
/**
|
126 |
* Creates a basic view with one layer
|
127 |
*/
|
128 |
public ProjectView getBasicView(){
|
129 |
ProjectView gvView = new ProjectView();
|
130 |
ViewPort viewPort = new ViewPort(CRSFactory.getCRS("EPSG:23031")); |
131 |
MapContext mapContext = new MapContext(viewPort);
|
132 |
mapContext.getLayers().addLayer(getLayerBasic()); |
133 |
gvView.setMapContext(mapContext); |
134 |
gvView.setComment("test_view_comment");
|
135 |
gvView.setCreationDate("07/07/07 13:30");
|
136 |
gvView.setName("test_view_name");
|
137 |
gvView.setOwner("test_view_owner");
|
138 |
return gvView;
|
139 |
} |
140 |
/**
|
141 |
* Creates a basic view with two layers
|
142 |
*/
|
143 |
public ProjectView getBasic2layersView(){
|
144 |
ProjectView gvView = new ProjectView();
|
145 |
ViewPort viewPort = new ViewPort(CRSFactory.getCRS("EPSG:23031")); |
146 |
MapContext mapContext = new MapContext(viewPort);
|
147 |
mapContext.getLayers().addLayer(getLayerBasic()); |
148 |
mapContext.getLayers().addLayer(getShapeLine()); |
149 |
gvView.setMapContext(mapContext); |
150 |
gvView.setComment("test_view_comment");
|
151 |
gvView.setCreationDate("07/07/07 13:30");
|
152 |
gvView.setName("test_view_name");
|
153 |
gvView.setOwner("test_view_owner");
|
154 |
return gvView;
|
155 |
} |
156 |
/**
|
157 |
* Creates a View with a simple layer and a nested layers
|
158 |
* @return
|
159 |
*/
|
160 |
public ProjectView getGroupView(){
|
161 |
ProjectView view = this.getBasicView();
|
162 |
MapContext map = view.getMapContext(); |
163 |
map.getLayers().addLayer(this.getGroupLayer(map));
|
164 |
return view;
|
165 |
} |
166 |
/**
|
167 |
* Creates a grouped layer
|
168 |
*/
|
169 |
public FLayers getGroupLayer(MapContext mapCtxt){
|
170 |
FLayers layers = null;
|
171 |
layers = new FLayers(mapCtxt, null); |
172 |
layers.setName("test_grouped_layer");
|
173 |
layers.addLayer(this.getShapeLine());
|
174 |
layers.addLayer(this.getShapePoint());
|
175 |
return layers;
|
176 |
} |
177 |
/**
|
178 |
* Creates a vectorial layer with two geometries
|
179 |
*/
|
180 |
private FLayer getLayerBasic() {
|
181 |
// DRIVER DEFINITION (SHAPE TYPE AND FIELDS)
|
182 |
ConcreteMemoryDriver driver = new ConcreteMemoryDriver();
|
183 |
driver.setShapeType(FShape.POLYGON); |
184 |
|
185 |
ArrayList arrayFields = new ArrayList(); |
186 |
arrayFields.add("ID");
|
187 |
arrayFields.add("NOMBRE");
|
188 |
arrayFields.add("POBLACION");
|
189 |
Value[] auxRow = new Value[3]; |
190 |
|
191 |
driver.getTableModel().setColumnIdentifiers(arrayFields.toArray()); |
192 |
|
193 |
// ATRIBUTES
|
194 |
auxRow[0] = ValueFactory.createValue(0); |
195 |
auxRow[1] = ValueFactory.createValue("Valencia"); |
196 |
auxRow[2] = ValueFactory.createValue(200000); |
197 |
|
198 |
// GEOMETRY 1 DEFINITION
|
199 |
GeneralPathX gp1 = new GeneralPathX();
|
200 |
gp1.moveTo(20,80); |
201 |
gp1.lineTo(80,60); |
202 |
gp1.lineTo(100, 140); |
203 |
|
204 |
FShape pol1 = new FPolygon2D(gp1);
|
205 |
driver.addShape(pol1, auxRow); |
206 |
|
207 |
auxRow[0] = ValueFactory.createValue(1); |
208 |
auxRow[1] = ValueFactory.createValue("Madrid"); |
209 |
auxRow[2] = ValueFactory.createValue(300000); |
210 |
|
211 |
// GEOMETRY 2 DEFINITION
|
212 |
GeneralPathX gp2 = new GeneralPathX();
|
213 |
gp2.moveTo(100,100); |
214 |
gp2.lineTo(110,100); |
215 |
gp2.lineTo(110,110); |
216 |
gp2.lineTo(100,110); |
217 |
gp2.lineTo(100,100); |
218 |
|
219 |
FShape pol2 = new FPolygon2D(gp2);
|
220 |
driver.addShape(pol2, auxRow); |
221 |
|
222 |
// CREATE AND ADD LAYER
|
223 |
FLayer lyr; |
224 |
String layerName = "basic_layer"; |
225 |
|
226 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
227 |
|
228 |
lyr = LayerFactory.createLayer(layerName,driver, proj); |
229 |
return lyr;
|
230 |
} |
231 |
/**
|
232 |
* creates a shapefile
|
233 |
* @return
|
234 |
*/
|
235 |
public FLayer getShapePoint(){
|
236 |
String name = "shp_point"; |
237 |
String fileName = this.basePath + "/shp_point.shp"; |
238 |
String driverName = "gvSIG shp driver"; |
239 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
240 |
FLayer lyr = null;
|
241 |
try {
|
242 |
lyr = LayerFactory.createLayer(name, driverName, new File(fileName), proj); |
243 |
} catch (DriverException e) {
|
244 |
// TODO Auto-generated catch block
|
245 |
e.printStackTrace(); |
246 |
} |
247 |
return (FLayer)lyr;
|
248 |
} |
249 |
|
250 |
public FLayer getShapeLine(){
|
251 |
String name = "shp_line"; |
252 |
String fileName = this.basePath + "/shp_line.shp"; |
253 |
String driverName = "gvSIG shp driver"; |
254 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
255 |
FLayer lyr = null;
|
256 |
try {
|
257 |
lyr = LayerFactory.createLayer(name, driverName, new File(fileName), proj); |
258 |
} catch (DriverException e) {
|
259 |
// TODO Auto-generated catch block
|
260 |
e.printStackTrace(); |
261 |
} |
262 |
return (FLayer)lyr;
|
263 |
} |
264 |
/**
|
265 |
* creates a shape of polygons
|
266 |
* @throws Exception
|
267 |
*/
|
268 |
public FLayer getShapePolygon(){
|
269 |
String name = "shp_polygon"; |
270 |
String fileName = this.basePath + "/shp_polygon.shp"; |
271 |
String driverName = "gvSIG shp driver"; |
272 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
273 |
FLayer lyr = null;
|
274 |
try {
|
275 |
lyr = LayerFactory.createLayer(name, driverName, new File(fileName), proj); |
276 |
|
277 |
} catch (DriverException e) {
|
278 |
// TODO Auto-generated catch block
|
279 |
e.printStackTrace(); |
280 |
} |
281 |
return (FLayer)lyr;
|
282 |
} |
283 |
/**
|
284 |
* creates a test layer postGIS
|
285 |
*/
|
286 |
public FLayer getLayerPostGIS(){
|
287 |
try {
|
288 |
Class.forName("org.postgresql.Driver"); |
289 |
} catch (ClassNotFoundException e1) { |
290 |
// TODO Auto-generated catch block
|
291 |
e1.printStackTrace(); |
292 |
} |
293 |
String dbURL = "jdbc:postgresql://localhost:5432/publish"; |
294 |
String user = "postgres"; |
295 |
String pwd = "xxx"; |
296 |
String layerName = "sql_polygon"; |
297 |
String fidField = "gid"; |
298 |
//String sFields = (String) args.get((String) "FIELDS");
|
299 |
//String[] fields = sFields.split(",");
|
300 |
String[] fields = {"gid", "area", "perimeter", "nucleos_", "nucleos_id", "cod_ent", "cod_ine", "nombre", "municipio", "cod_mun"}; |
301 |
String geomField = "the_geom"; |
302 |
String tableName = "sql_polygon"; |
303 |
String whereClause = ""; |
304 |
|
305 |
IConnection conn; |
306 |
try {
|
307 |
conn = ConnectionFactory.createConnection(dbURL, user, pwd); |
308 |
} catch (DBException e) {
|
309 |
e.printStackTrace(); |
310 |
return null; |
311 |
} |
312 |
|
313 |
DBLayerDefinition lyrDef = new DBLayerDefinition();
|
314 |
lyrDef.setName(layerName); |
315 |
lyrDef.setTableName(tableName); |
316 |
lyrDef.setWhereClause(whereClause); |
317 |
lyrDef.setFieldNames(fields); |
318 |
lyrDef.setFieldGeometry(geomField); |
319 |
lyrDef.setFieldID(fidField); |
320 |
|
321 |
PostGisDriver pgd = new PostGisDriver();
|
322 |
try {
|
323 |
pgd.setData(conn, lyrDef); |
324 |
} catch (DBException e) {
|
325 |
// TODO Auto-generated catch block
|
326 |
e.printStackTrace(); |
327 |
} |
328 |
|
329 |
return LayerFactory.createDBLayer(pgd, layerName, CRSFactory.getCRS("EPSG:23030")); |
330 |
|
331 |
} |
332 |
/**
|
333 |
* Creates a Raster Layer from a ECW
|
334 |
*/
|
335 |
public FLayer getLayerECW(){
|
336 |
//RasterDriver driv = new CmsRasterDriver();
|
337 |
String name = "orto_ecw"; |
338 |
String fileName = this.basePath + "/orto.ecw"; |
339 |
String driverName = "gvSIG Image Driver"; |
340 |
FLayer lyr=null;
|
341 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
342 |
//Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
|
343 |
try {
|
344 |
//lyr = LayerFactory.createLayer(name, (RasterDriver)driver, new File(fileName), proj);
|
345 |
lyr = LayerFactory.createLayer(name, driverName, new File(fileName), proj); |
346 |
} catch (DriverException e) {
|
347 |
// TODO Auto-generated catch block
|
348 |
e.printStackTrace(); |
349 |
} |
350 |
return lyr;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* Create DB PostGIS (publish)
|
355 |
* Preconditions:
|
356 |
* - exists commands createdb, psql
|
357 |
* - you don't need password to connect with postgres
|
358 |
*/
|
359 |
public void createDBPostGIS(){ |
360 |
try {
|
361 |
BufferedReader br = null; |
362 |
String line = null; |
363 |
String[] commands = null; |
364 |
Process child = null; |
365 |
// Creates the database
|
366 |
commands = new String[]{"createdb", "-U", "postgres", "publish"}; |
367 |
child = Runtime.getRuntime().exec(commands);
|
368 |
//Show output
|
369 |
br = new BufferedReader(new InputStreamReader(child.getErrorStream())); |
370 |
line = br.readLine(); |
371 |
while (line != null){ |
372 |
System.out.println(line);
|
373 |
line = br.readLine(); |
374 |
} |
375 |
// adds plpgsql support
|
376 |
commands = new String[]{"createlang", "-U", "postgres", "plpgsql", "publish"}; |
377 |
child = Runtime.getRuntime().exec(commands);
|
378 |
//Show output
|
379 |
br = new BufferedReader(new InputStreamReader(child.getErrorStream())); |
380 |
line = br.readLine(); |
381 |
while (line != null){ |
382 |
System.out.println(line);
|
383 |
line = br.readLine(); |
384 |
} |
385 |
|
386 |
//load lwpostgis.sql
|
387 |
commands = new String[]{"psql", "-U", "postgres", "-d", "publish", "-f", this.basePath + "/lwpostgis.sql"}; |
388 |
child = Runtime.getRuntime().exec(commands);
|
389 |
//Show output
|
390 |
br = new BufferedReader(new InputStreamReader(child.getErrorStream())); |
391 |
line = br.readLine(); |
392 |
while (line != null){ |
393 |
System.out.println(line);
|
394 |
line = br.readLine(); |
395 |
} |
396 |
//load spatial_ref_sys.sql
|
397 |
System.out.println("Loading spatial_ref_sys.sql"); |
398 |
commands = new String[]{"psql", "-U", "postgres", "-d", "publish", "-f", this.basePath + "/spatial_ref_sys.sql"}; |
399 |
child = Runtime.getRuntime().exec(commands);
|
400 |
//Show output
|
401 |
br = new BufferedReader(new InputStreamReader(child.getErrorStream())); |
402 |
line = br.readLine(); |
403 |
while (line != null){ |
404 |
System.out.println(line);
|
405 |
line = br.readLine(); |
406 |
} |
407 |
|
408 |
} catch (IOException e) { |
409 |
e.printStackTrace(); |
410 |
} |
411 |
//Load a table
|
412 |
this.loadTablePostGIS();
|
413 |
} |
414 |
/**
|
415 |
* Drop DB PostGIS (publish)
|
416 |
*/
|
417 |
public void dropDBPostGIS(){ |
418 |
try {
|
419 |
BufferedReader br = null; |
420 |
String line = null; |
421 |
|
422 |
// Execute a command with an argument that contains a space
|
423 |
String[] commands = new String[]{"dropdb", "-U", "postgres", "publish"}; |
424 |
Process child = Runtime.getRuntime().exec(commands); |
425 |
//Show output
|
426 |
br = new BufferedReader(new InputStreamReader(child.getErrorStream())); |
427 |
line = br.readLine(); |
428 |
while (line != null){ |
429 |
System.out.println(line);
|
430 |
line = br.readLine(); |
431 |
} |
432 |
} catch (IOException e) { |
433 |
e.printStackTrace(); |
434 |
} |
435 |
|
436 |
} |
437 |
/**
|
438 |
* Load a table (resources/sql_polygon.sql) into the database
|
439 |
*/
|
440 |
private void loadTablePostGIS(){ |
441 |
try {
|
442 |
BufferedReader br = null; |
443 |
String line = null; |
444 |
String[] commands = new String[]{"psql", "-U", "postgres", "-d", "publish", "-f", this.basePath + "/sql_polygon.sql"}; |
445 |
Process child = Runtime.getRuntime().exec(commands); |
446 |
//Show output
|
447 |
br = new BufferedReader(new InputStreamReader(child.getErrorStream())); |
448 |
line = br.readLine(); |
449 |
while (line != null){ |
450 |
System.out.println(line);
|
451 |
line = br.readLine(); |
452 |
} |
453 |
} catch (IOException e) { |
454 |
e.printStackTrace(); |
455 |
} |
456 |
|
457 |
} |
458 |
} |