Statistics
| Revision:

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

History | View | Annotate | Download (8.34 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.postgis.PostGisDriver;
71
import com.iver.cit.gvsig.fmap.layers.CancelationException;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
74
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
75
import com.vividsolutions.jts.geom.LineString;
76
import com.vividsolutions.jts.geom.MultiLineString;
77
import com.vividsolutions.jts.geom.MultiPoint;
78
import com.vividsolutions.jts.geom.Point;
79

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

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

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

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

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

109
            DataStore store;
110
            
111
            // Para shapes
112
            store = new ShapefileDataStore(url);
113
            
114
            loadLayer(store, store.getTypeNames()[0]);
115
           } catch (Throwable t) {
116
               JOptionPane.showMessageDialog(null, "An error occurred while loading the file",
117
                   "Demo GT2", JOptionPane.ERROR_MESSAGE);
118
               t.printStackTrace();
119
           }
120
       } */
121
   }
122
   protected void addLayer_PostGIS()
123
   {
124
       String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
125
       VectorialDatabaseDriver driver = new PostGisDriver();
126
       String dburl = "jdbc:postgresql://localhost/latin1";
127
       String dbuser = "postgres";
128
       String dbpass = "aquilina";
129
       String fields = "ASTEXT(force_2d(the_geom)) as geom, area, nd_4, nom_provin";
130
       String whereClause = "";
131
       
132
       
133
       Connection conn;
134
    try {
135
        Class.forName("org.postgresql.Driver");
136
        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
137
        conn.setAutoCommit(false);
138
        driver.setData(conn, nomTabla, fields, whereClause);
139
        FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
140
        m_Mapa.getLayers().addLayer(lyr);
141
    } catch (SQLException e) {
142
        // TODO Auto-generated catch block
143
        e.printStackTrace();
144
    } catch (ClassNotFoundException e) {
145
        // TODO Auto-generated catch block
146
        e.printStackTrace();
147
    }
148
        
149
   }
150
   protected void addLayerGT2_PostGIS()
151
   {
152
       String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
153
       
154
            remote = new HashMap();
155
            remote.put("dbtype","postgis");        
156
            remote.put("host","localhost"); //Jos? Miguel
157
            remote.put("port", new Integer(5432));
158
            remote.put("database", "latin1");
159
            remote.put("user", "postgres");
160
            remote.put("passwd", "aquilina");
161
            remote.put("charset", "");
162
            remote.put("namespace", "");
163
            
164
            DataStore store;
165
                try {
166
                        store = postGisFactory.createDataStore(remote);
167
                        loadLayer(store, nomTabla);
168
                        
169
                } catch (IOException e) {
170
                        // TODO Auto-generated catch block
171
                        e.printStackTrace();
172
                } 
173
   }
174

    
175
   
176
   /**
177
    * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
178
    * a default style.
179
    *
180
    * @param url The url of the shapefile to load.
181
    * @param name DOCUMENT ME!
182
    *
183
    * @throws IOException is a I/O error occured.
184
    * @throws DataSourceException if an error occured while reading the data source.
185
    * @throws FileNotFoundException DOCUMENT ME!
186
    */
187
   protected void loadLayer(DataStore store, String layerName)
188
       throws IOException, DataSourceException {
189
       long t1 = System.currentTimeMillis();
190
       final FeatureSource features = store.getFeatureSource(layerName);
191
       long t2 = System.currentTimeMillis();
192
       System.out.println("t2-t1= " + (t2-t1));
193
       // Create the style
194
       final StyleBuilder builder = new StyleBuilder();
195
       final Style style;
196
       Class geometryClass = features.getSchema().getDefaultGeometry().getType();
197

    
198
       if (LineString.class.isAssignableFrom(geometryClass)
199
               || MultiLineString.class.isAssignableFrom(geometryClass)) {
200
           style = builder.createStyle(builder.createLineSymbolizer());
201
       } else if (Point.class.isAssignableFrom(geometryClass)
202
               || MultiPoint.class.isAssignableFrom(geometryClass)) {
203
           style = builder.createStyle(builder.createPointSymbolizer());
204
       } else {
205
           style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1));
206
       }
207

    
208
       final MapLayer layer = new DefaultMapLayer(features, style);
209
       layer.setTitle(layerName);
210
       
211
       FLyrGT2 lyrGT2 = new FLyrGT2(layer);
212
       try {
213
                        m_Mapa.getLayers().addLayer(lyrGT2);
214
                } catch (CancelationException e) {
215
                        // TODO Auto-generated catch block
216
                        e.printStackTrace();
217
                }
218
       
219
   }
220
   
221
   protected void addLayerGT2_ArcSDE()
222
   {
223
            remote = new HashMap();
224
            remote.put("dbtype","arcsde");        
225
            remote.put("server","192.168.0.165"); //Jos? Miguel
226
            remote.put("port", new Integer(5151));
227
            remote.put("instance", "sde");
228
            remote.put("user", "sde");
229
            remote.put("password", "aquilina");
230
            
231
            
232
            DataStore store;
233
                /* try {
234
                        store = arcSdeFactory.createDataStore(remote);
235
                        loadLayer(store, "SDE.VIAS");
236
                        
237
                } catch (IOException e) {
238
                        // TODO Auto-generated catch block
239
                        e.printStackTrace();
240
                } */
241
                
242
           
243
   }
244

    
245
   public void setMapContext(FMap map)
246
   {
247
                   m_Mapa = map;
248
   }
249
        public void actionPerformed(ActionEvent e) {
250

    
251
                if (e.getActionCommand() == "ADD_GT2_POSTGIS_PROPIO") {            
252
                        addLayer_PostGIS();
253
                }
254

    
255
                if (e.getActionCommand() == "ADD_GT2_POSTGIS") {            
256
                        addLayerGT2_PostGIS();
257
                }
258
                if (e.getActionCommand() == "ADD_GT2_SHP") {            
259
                    addLayerGT2_Shp();
260
                }
261
                if (e.getActionCommand() == "ADD_GT2_ARCSDE") {            
262
                    addLayerGT2_ArcSDE();
263
                }
264
        }
265
}