Statistics
| Revision:

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

History | View | Annotate | Download (9.63 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.HashMap;
55
import java.util.Map;
56

    
57
import javax.swing.JOptionPane;
58

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

    
68
import com.iver.cit.gvsig.fmap.FMap;
69
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
70
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLdriver;
71
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
72
import com.iver.cit.gvsig.fmap.layers.CancelationException;
73
import com.iver.cit.gvsig.fmap.layers.FLayer;
74
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
75
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
76
import com.vividsolutions.jts.geom.LineString;
77
import com.vividsolutions.jts.geom.MultiLineString;
78
import com.vividsolutions.jts.geom.MultiPoint;
79
import com.vividsolutions.jts.geom.Point;
80

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

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

100
       int result = fileChooser.showOpenDialog(theView);
101

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

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

110
            DataStore store;
111
            
112
            // Para shapes
113
            store = new ShapefileDataStore(url);
114
            
115
            loadLayer(store, store.getTypeNames()[0]);
116
           } catch (Throwable t) {
117
               JOptionPane.showMessageDialog(null, "An error occurred while loading the file",
118
                   "Demo GT2", JOptionPane.ERROR_MESSAGE);
119
               t.printStackTrace();
120
           }
121
       } */
122
   }
123
   protected void addLayer_PostGIS()
124
   {
125
       String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
126
       VectorialDatabaseDriver driver = new PostGisDriver();
127
       String dburl = "jdbc:postgresql://localhost/latin1";
128
       String dbuser = "postgres";
129
       String dbpass = "aquilina";
130
       String fields = "ASBINARY(the_geom, 'XDR') as the_geom, ND_4, NOM_PROVIN";
131
       String whereClause = "";
132
       
133
       
134
       Connection conn;
135
    try {
136
        Class.forName("org.postgresql.Driver");
137
        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
138
        conn.setAutoCommit(false);
139
        driver.setData(conn, nomTabla, fields, whereClause);
140
        FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
141
        m_Mapa.getLayers().addLayer(lyr);
142
    } catch (SQLException e) {
143
        // TODO Auto-generated catch block
144
        e.printStackTrace();
145
    } catch (ClassNotFoundException e) {
146
        // TODO Auto-generated catch block
147
        e.printStackTrace();
148
    }
149
        
150
   }
151
   protected void addLayer_mySQL()
152
   {
153
       String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
154
       VectorialDatabaseDriver driver = new MySQLdriver();
155
       String dburl = "jdbc:mysql://localhost/test";
156
       String dbuser = "root";
157
       String dbpass = "aquilina";
158
       String fields = "ASBINARY(ogc_geom) as the_geom, KILOMETERS, RD_11";
159
       String whereClause = "";
160
       
161
       
162
       Connection conn;
163
    try {
164
        Class.forName("com.mysql.jdbc.Driver"); 
165
        // Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");
166

    
167
        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
168
        conn.setAutoCommit(false);
169
        driver.setData(conn, nomTabla, fields, whereClause);
170
        FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
171
        m_Mapa.getLayers().addLayer(lyr);
172
    } catch (SQLException e) {
173
        // TODO Auto-generated catch block
174
        e.printStackTrace();
175
    } catch (ClassNotFoundException e) {
176
        // TODO Auto-generated catch block
177
        e.printStackTrace();
178
    }
179
        
180
   }
181
   
182
   protected void addLayerGT2_PostGIS()
183
   {
184
       String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
185
       
186
            remote = new HashMap();
187
            remote.put("dbtype","postgis");        
188
            remote.put("host","localhost"); //Jos? Miguel
189
            remote.put("port", new Integer(5432));
190
            remote.put("database", "latin1");
191
            remote.put("user", "postgres");
192
            remote.put("passwd", "aquilina");
193
            remote.put("charset", "");
194
            remote.put("namespace", "");
195
            
196
            DataStore store;
197
                try {
198
                        store = postGisFactory.createDataStore(remote);
199
                        loadLayer(store, nomTabla);
200
                        
201
                } catch (IOException e) {
202
                        // TODO Auto-generated catch block
203
                        e.printStackTrace();
204
                } 
205
   }
206

    
207
   
208
   /**
209
    * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
210
    * a default style.
211
    *
212
    * @param url The url of the shapefile to load.
213
    * @param name DOCUMENT ME!
214
    *
215
    * @throws IOException is a I/O error occured.
216
    * @throws DataSourceException if an error occured while reading the data source.
217
    * @throws FileNotFoundException DOCUMENT ME!
218
    */
219
   protected void loadLayer(DataStore store, String layerName)
220
       throws IOException, DataSourceException {
221
       long t1 = System.currentTimeMillis();
222
       final FeatureSource features = store.getFeatureSource(layerName);
223
       long t2 = System.currentTimeMillis();
224
       System.out.println("t2-t1= " + (t2-t1));
225
       // Create the style
226
       final StyleBuilder builder = new StyleBuilder();
227
       final Style style;
228
       Class geometryClass = features.getSchema().getDefaultGeometry().getType();
229

    
230
       if (LineString.class.isAssignableFrom(geometryClass)
231
               || MultiLineString.class.isAssignableFrom(geometryClass)) {
232
           style = builder.createStyle(builder.createLineSymbolizer());
233
       } else if (Point.class.isAssignableFrom(geometryClass)
234
               || MultiPoint.class.isAssignableFrom(geometryClass)) {
235
           style = builder.createStyle(builder.createPointSymbolizer());
236
       } else {
237
           style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1));
238
       }
239

    
240
       final MapLayer layer = new DefaultMapLayer(features, style);
241
       layer.setTitle(layerName);
242
       
243
       FLyrGT2 lyrGT2 = new FLyrGT2(layer);
244
       try {
245
                        m_Mapa.getLayers().addLayer(lyrGT2);
246
                } catch (CancelationException e) {
247
                        // TODO Auto-generated catch block
248
                        e.printStackTrace();
249
                }
250
       
251
   }
252
   
253
   protected void addLayerGT2_ArcSDE()
254
   {
255
            remote = new HashMap();
256
            remote.put("dbtype","arcsde");        
257
            remote.put("server","192.168.0.165"); //Jos? Miguel
258
            remote.put("port", new Integer(5151));
259
            remote.put("instance", "sde");
260
            remote.put("user", "sde");
261
            remote.put("password", "aquilina");
262
            
263
            
264
            DataStore store;
265
                /* try {
266
                        store = arcSdeFactory.createDataStore(remote);
267
                        loadLayer(store, "SDE.VIAS");
268
                        
269
                } catch (IOException e) {
270
                        // TODO Auto-generated catch block
271
                        e.printStackTrace();
272
                } */
273
                
274
           
275
   }
276

    
277
   public void setMapContext(FMap map)
278
   {
279
                   m_Mapa = map;
280
   }
281
        public void actionPerformed(ActionEvent e) {
282

    
283
                if (e.getActionCommand() == "ADD_GT2_POSTGIS_PROPIO") {            
284
                        addLayer_PostGIS();
285
                }
286
                if (e.getActionCommand() == "ADD_GT2_MYSQL_PROPIO") {            
287
                        addLayer_mySQL();
288
                }
289

    
290
                if (e.getActionCommand() == "ADD_GT2_POSTGIS") {            
291
                        addLayerGT2_PostGIS();
292
                }
293
                if (e.getActionCommand() == "ADD_GT2_SHP") {            
294
                    addLayerGT2_Shp();
295
                }
296
                if (e.getActionCommand() == "ADD_GT2_ARCSDE") {            
297
                    addLayerGT2_ArcSDE();
298
                }
299
        }
300
}