Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / PruebasGT2.java @ 3718

History | View | Annotate | Download (11.3 KB)

1
/*
2
 * Created on 22-dic-2004
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
package com.iver.cit.gvsig.DEMO;
45

    
46
import java.awt.Color;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.io.FileNotFoundException;
50
import java.io.IOException;
51
import java.util.HashMap;
52
import java.util.Map;
53

    
54
import javax.swing.JOptionPane;
55

    
56
import org.geotools.data.DataSourceException;
57
import org.geotools.data.DataStore;
58
import org.geotools.data.FeatureSource;
59
import org.geotools.data.postgis.PostgisDataStore;
60
import org.geotools.data.postgis.PostgisDataStoreFactory;
61
import org.geotools.map.DefaultMapLayer;
62
import org.geotools.map.MapLayer;
63
import org.geotools.styling.Style;
64
import org.geotools.styling.StyleBuilder;
65

    
66
import com.iver.cit.gvsig.fmap.FMap;
67
import com.iver.cit.gvsig.fmap.layers.CancelationException;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
70
import com.iver.cit.gvsig.fmap.layers.FLyrGT2_old;
71
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
72
import com.vividsolutions.jts.geom.LineString;
73
import com.vividsolutions.jts.geom.MultiLineString;
74
import com.vividsolutions.jts.geom.MultiPoint;
75
import com.vividsolutions.jts.geom.Point;
76

    
77
/**
78
 * Pruebas de capas GT2 (ArcSDE, etc), hechas por Fran, sacadas del
79
 * CommandListener
80
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
81
 */
82
public class PruebasGT2 implements ActionListener {
83
    static PostgisDataStoreFactory postGisFactory = new PostgisDataStoreFactory();
84
    // static ArcSDEDataStoreFactory arcSdeFactory = new ArcSDEDataStoreFactory();
85

    
86
    Map remote;
87
    private FMap m_Mapa;
88
    
89
    
90
    /**
91
     * Returns the parameters
92
     */
93
    public HashMap getParams() {
94
        HashMap params = new HashMap();
95
        // Param[] dbParams = postGisFactory.getParametersInfo();
96
        params.put("dbtype", "postgis"); //$NON-NLS-1$
97
        params.put("host", "localhost");
98
        params.put("port", new Integer(5432));
99

    
100
        params.put("database", "latin1");
101

    
102
        params.put("user", "postgres");
103
        params.put("passwd", "aquilina");
104

    
105
        params.put("wkb enabled", Boolean.TRUE);
106
        params.put("loose bbox", Boolean.TRUE);
107

    
108
        params.put("namespace", ""); //$NON-NLS-1$
109
        
110
        return params;
111
    }
112
    
113
    /**
114
    *
115
    */
116
   protected void addLayerGT2_Shp() {
117
       /* JFileChooser fileChooser = new JFileChooser(); // lastFolder);
118
       fileChooser.addChoosableFileFilter(new SimpleFileFilter("shp", "Shapefile (*.shp)"));
119

120
       int result = fileChooser.showOpenDialog(theView);
121

122
       if (result == JFileChooser.APPROVE_OPTION) {
123
           File file = fileChooser.getSelectedFile();
124
           // lastFolder = file.getParentFile();
125

126
           try {
127
            // Load the file
128
                   URL url = file.toURL();
129

130
            DataStore store;
131
            
132
            // Para shapes
133
            store = new ShapefileDataStore(url);
134
            
135
            loadLayer(store, store.getTypeNames()[0]);
136
           } catch (Throwable t) {
137
               JOptionPane.showMessageDialog(null, "An error occurred while loading the file",
138
                   "Demo GT2", JOptionPane.ERROR_MESSAGE);
139
               t.printStackTrace();
140
           }
141
       } */
142
   }
143
   protected void addLayer_PostGIS()
144
   {
145
       /* String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
146
       PostGisDriver driver = new PostGisDriver();
147
       String dburl = "jdbc:postgresql://localhost/latin1";
148
       String dbuser = "postgres";
149
       String dbpass = "aquilina";
150
       String fields = "ASBINARY(the_geom, 'XDR') as the_geom, ND_4, NOM_PROVIN";
151
       String whereClause = "";
152
       
153
       
154
       Connection conn;
155
    try {
156
        Class.forName("org.postgresql.Driver");
157
        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
158
        conn.setAutoCommit(false);
159
        driver.setData(conn, nomTabla, fields, whereClause, -1);
160
        FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
161
        m_Mapa.getLayers().addLayer(lyr);
162
    } catch (SQLException e) {
163
        // TODO Auto-generated catch block
164
        e.printStackTrace();
165
    } catch (ClassNotFoundException e) {
166
        // TODO Auto-generated catch block
167
        e.printStackTrace();
168
    }
169
        */ 
170
   }
171
   protected void addLayer_ArcSDE_propio()
172
   {       
173
     /*  String nomTabla = "vias"; //JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "vias");
174
       ArcSdeDriver driver = new ArcSdeDriver();
175
       String dbHost = "Alvaro";
176
       String dbuser = "sde";
177
       String dbpass = "sde";
178
       // String[] fields = {"Shape", "NOM_PROVIN"};
179
       String[] fields = null; //{"Shape", "RD_5", "RD_11"};
180
       int instance      = 5151;
181
       String database   = "sigespa";
182
       
183
       String whereClause = "";
184
       
185
       
186
        driver.setData(dbHost, instance, database, dbuser, dbpass, nomTabla, fields, whereClause);
187
        FLayer lyr = LayerFactory.createArcSDELayer(nomTabla, driver, null);
188
        m_Mapa.getLayers().addLayer(lyr);
189
       */ 
190
   }
191
   
192
   protected void addLayer_mySQL()
193
   {
194
       /* String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
195
       MySQLDriver driver = new MySQLDriver();
196
       String dburl = "jdbc:mysql://localhost/test";
197
       String dbuser = "root";
198
       String dbpass = "aquilina";
199
       String fields = "ASBINARY(ogc_geom) as the_geom, ID, NOM_PROVIN, ND_4, ND_5, ND_14, AREA";
200
       // String fields = "ASBINARY(ogc_geom) as the_geom, ID, RD_5, RD_11";
201
       String whereClause = "";
202
       
203
       
204
       Connection conn;
205
    try {
206
        Class.forName("com.mysql.jdbc.Driver"); 
207
        // Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");
208

209
        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
210
        conn.setAutoCommit(false);
211
        driver.setData(conn, nomTabla, fields, whereClause, 2);
212
        FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
213
        m_Mapa.getLayers().addLayer(lyr);
214
    } catch (SQLException e) {
215
        // TODO Auto-generated catch block
216
        e.printStackTrace();
217
    } catch (ClassNotFoundException e) {
218
        // TODO Auto-generated catch block
219
        e.printStackTrace();
220
    }
221
        */
222
   }
223
   
224
   protected void addLayerGT2_PostGIS()
225
   {
226
       String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
227
       
228
            remote = getParams();
229
            /* remote.put("dbtype","postgis");        
230
            remote.put("host","localhost"); //Jos? Miguel
231
            remote.put("port", new Integer(5432));
232
            remote.put("database", "latin1");
233
            remote.put("user", "postgres");
234
            remote.put("passwd", "aquilina");
235
            remote.put("charset", "");
236
            remote.put("namespace", ""); */
237
            
238
            PostgisDataStore store;
239
                try {
240
                        store = (PostgisDataStore) postGisFactory.createDataStore(remote);
241
            FLyrGT2 lyrGT2 = new FLyrGT2();
242
            lyrGT2.setDataStore(store);
243
            lyrGT2.setTableName(nomTabla);
244
            m_Mapa.getLayers().addLayer(lyrGT2);
245
            // loadLayer(store, nomTabla);
246
                                    
247
                } catch (IOException e) {
248
                        // TODO Auto-generated catch block
249
                        e.printStackTrace();
250
                } 
251
   }
252

    
253
   
254
   /**
255
    * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
256
    * a default style.
257
    *
258
    * @param url The url of the shapefile to load.
259
    * @param name DOCUMENT ME!
260
    *
261
    * @throws IOException is a I/O error occured.
262
    * @throws DataSourceException if an error occured while reading the data source.
263
    * @throws FileNotFoundException DOCUMENT ME!
264
    */
265
   protected void loadLayer(DataStore store, String layerName)
266
       throws IOException, DataSourceException {
267
       long t1 = System.currentTimeMillis();
268
       final FeatureSource features = store.getFeatureSource(layerName);
269
       long t2 = System.currentTimeMillis();
270
       System.out.println("t2-t1= " + (t2-t1));
271
       // Create the style
272
       final StyleBuilder builder = new StyleBuilder();
273
       final Style style;
274
       Class geometryClass = features.getSchema().getDefaultGeometry().getType();
275

    
276
       if (LineString.class.isAssignableFrom(geometryClass)
277
               || MultiLineString.class.isAssignableFrom(geometryClass)) {
278
           style = builder.createStyle(builder.createLineSymbolizer());
279
       } else if (Point.class.isAssignableFrom(geometryClass)
280
               || MultiPoint.class.isAssignableFrom(geometryClass)) {
281
           style = builder.createStyle(builder.createPointSymbolizer());
282
       } else {
283
           style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1));
284
       }
285

    
286
       final MapLayer layer = new DefaultMapLayer(features, style);
287
       layer.setTitle(layerName);
288
       
289
       FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
290
       try {
291
                        m_Mapa.getLayers().addLayer(lyrGT2);
292
                } catch (CancelationException e) {
293
                        // TODO Auto-generated catch block
294
                        e.printStackTrace();
295
                }
296
       
297
   }
298
   
299
   protected void addLayerGT2_ArcSDE()
300
   {
301
            /* remote = new HashMap();
302
            remote.put("dbtype","arcsde");        
303
            remote.put("server","Alvaro"); //Jos? Miguel
304
            remote.put("port", new Integer(5151));
305
            remote.put("instance", "sigespa");
306
            remote.put("user", "sde");
307
            remote.put("password", "sde");
308
            
309
            
310
            DataStore store;
311
                try {
312
                        store = arcSdeFactory.createDataStore(remote);
313
                        loadLayer(store, "SDE.VIAS");
314
                        
315
                } catch (IOException e) {
316
                        // TODO Auto-generated catch block
317
                        e.printStackTrace();
318
                } */
319
                
320
           
321
   }
322

    
323
   public void setMapContext(FMap map)
324
   {
325
                   m_Mapa = map;
326
   }
327
        public void actionPerformed(ActionEvent e) {
328

    
329
                if (e.getActionCommand() == "ADD_GT2_POSTGIS_PROPIO") {            
330
                        addLayer_PostGIS();
331
                }
332
                if (e.getActionCommand() == "ADD_GT2_MYSQL_PROPIO") {            
333
                        addLayer_mySQL();
334
                }
335
        if (e.getActionCommand() == "ADD_GT2_ARCSDE_PROPIO") {            
336
            addLayer_ArcSDE_propio();
337
        }
338

    
339
                if (e.getActionCommand() == "ADD_GT2_POSTGIS") {            
340
                        addLayerGT2_PostGIS();
341
                }
342
                if (e.getActionCommand() == "ADD_GT2_SHP") {            
343
                    addLayerGT2_Shp();
344
                }
345
                if (e.getActionCommand() == "ADD_GT2_ARCSDE") {            
346
                    addLayerGT2_ArcSDE();
347
                }
348
        }
349
}