Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / PruebasGT2.java @ 2183

History | View | Annotate | Download (10.5 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.sql.Connection;
52
import java.sql.DriverManager;
53
import java.sql.SQLException;
54
import java.util.Map;
55

    
56
import javax.swing.JOptionPane;
57

    
58
import org.geotools.data.DataSourceException;
59
import org.geotools.data.DataStore;
60
import org.geotools.data.FeatureSource;
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.drivers.arcsde.ArcSdeDriver;
68
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLDriver;
69
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
70
import com.iver.cit.gvsig.fmap.layers.CancelationException;
71
import com.iver.cit.gvsig.fmap.layers.FLayer;
72
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
73
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
74
import com.vividsolutions.jts.geom.LineString;
75
import com.vividsolutions.jts.geom.MultiLineString;
76
import com.vividsolutions.jts.geom.MultiPoint;
77
import com.vividsolutions.jts.geom.Point;
78

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

    
88
    Map remote;
89
    private FMap m_Mapa;
90
    
91
    /**
92
    *
93
    */
94
   protected void addLayerGT2_Shp() {
95
       /* JFileChooser fileChooser = new JFileChooser(); // lastFolder);
96
       fileChooser.addChoosableFileFilter(new SimpleFileFilter("shp", "Shapefile (*.shp)"));
97

98
       int result = fileChooser.showOpenDialog(theView);
99

100
       if (result == JFileChooser.APPROVE_OPTION) {
101
           File file = fileChooser.getSelectedFile();
102
           // lastFolder = file.getParentFile();
103

104
           try {
105
            // Load the file
106
                   URL url = file.toURL();
107

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

    
187
        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
188
        conn.setAutoCommit(false);
189
        driver.setData(conn, nomTabla, fields, whereClause, 2);
190
        FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
191
        m_Mapa.getLayers().addLayer(lyr);
192
    } catch (SQLException e) {
193
        // TODO Auto-generated catch block
194
        e.printStackTrace();
195
    } catch (ClassNotFoundException e) {
196
        // TODO Auto-generated catch block
197
        e.printStackTrace();
198
    }
199
        
200
   }
201
   
202
   protected void addLayerGT2_PostGIS()
203
   {
204
       /* String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
205
       
206
            remote = new HashMap();
207
            remote.put("dbtype","postgis");        
208
            remote.put("host","localhost"); //Jos? Miguel
209
            remote.put("port", new Integer(5432));
210
            remote.put("database", "latin1");
211
            remote.put("user", "postgres");
212
            remote.put("passwd", "aquilina");
213
            remote.put("charset", "");
214
            remote.put("namespace", "");
215
            
216
            DataStore store;
217
                try {
218
                        store = postGisFactory.createDataStore(remote);
219
                        loadLayer(store, nomTabla);
220
                        
221
                } catch (IOException e) {
222
                        // TODO Auto-generated catch block
223
                        e.printStackTrace();
224
                } */
225
   }
226

    
227
   
228
   /**
229
    * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
230
    * a default style.
231
    *
232
    * @param url The url of the shapefile to load.
233
    * @param name DOCUMENT ME!
234
    *
235
    * @throws IOException is a I/O error occured.
236
    * @throws DataSourceException if an error occured while reading the data source.
237
    * @throws FileNotFoundException DOCUMENT ME!
238
    */
239
   protected void loadLayer(DataStore store, String layerName)
240
       throws IOException, DataSourceException {
241
       long t1 = System.currentTimeMillis();
242
       final FeatureSource features = store.getFeatureSource(layerName);
243
       long t2 = System.currentTimeMillis();
244
       System.out.println("t2-t1= " + (t2-t1));
245
       // Create the style
246
       final StyleBuilder builder = new StyleBuilder();
247
       final Style style;
248
       Class geometryClass = features.getSchema().getDefaultGeometry().getType();
249

    
250
       if (LineString.class.isAssignableFrom(geometryClass)
251
               || MultiLineString.class.isAssignableFrom(geometryClass)) {
252
           style = builder.createStyle(builder.createLineSymbolizer());
253
       } else if (Point.class.isAssignableFrom(geometryClass)
254
               || MultiPoint.class.isAssignableFrom(geometryClass)) {
255
           style = builder.createStyle(builder.createPointSymbolizer());
256
       } else {
257
           style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1));
258
       }
259

    
260
       final MapLayer layer = new DefaultMapLayer(features, style);
261
       layer.setTitle(layerName);
262
       
263
       FLyrGT2 lyrGT2 = new FLyrGT2(layer);
264
       try {
265
                        m_Mapa.getLayers().addLayer(lyrGT2);
266
                } catch (CancelationException e) {
267
                        // TODO Auto-generated catch block
268
                        e.printStackTrace();
269
                }
270
       
271
   }
272
   
273
   protected void addLayerGT2_ArcSDE()
274
   {
275
            /* remote = new HashMap();
276
            remote.put("dbtype","arcsde");        
277
            remote.put("server","Alvaro"); //Jos? Miguel
278
            remote.put("port", new Integer(5151));
279
            remote.put("instance", "sigespa");
280
            remote.put("user", "sde");
281
            remote.put("password", "sde");
282
            
283
            
284
            DataStore store;
285
                try {
286
                        store = arcSdeFactory.createDataStore(remote);
287
                        loadLayer(store, "SDE.VIAS");
288
                        
289
                } catch (IOException e) {
290
                        // TODO Auto-generated catch block
291
                        e.printStackTrace();
292
                } */
293
                
294
           
295
   }
296

    
297
   public void setMapContext(FMap map)
298
   {
299
                   m_Mapa = map;
300
   }
301
        public void actionPerformed(ActionEvent e) {
302

    
303
                if (e.getActionCommand() == "ADD_GT2_POSTGIS_PROPIO") {            
304
                        addLayer_PostGIS();
305
                }
306
                if (e.getActionCommand() == "ADD_GT2_MYSQL_PROPIO") {            
307
                        addLayer_mySQL();
308
                }
309
        if (e.getActionCommand() == "ADD_GT2_ARCSDE_PROPIO") {            
310
            addLayer_ArcSDE_propio();
311
        }
312

    
313
                if (e.getActionCommand() == "ADD_GT2_POSTGIS") {            
314
                        addLayerGT2_PostGIS();
315
                }
316
                if (e.getActionCommand() == "ADD_GT2_SHP") {            
317
                    addLayerGT2_Shp();
318
                }
319
                if (e.getActionCommand() == "ADD_GT2_ARCSDE") {            
320
                    addLayerGT2_ArcSDE();
321
                }
322
        }
323
}