Statistics
| Revision:

root / trunk / examples / exaLoadLayer / src / org / gvsig / example / postgis / LoadLayer.java @ 6885

History | View | Annotate | Download (5.93 KB)

1
package org.gvsig.example.postgis;
2

    
3
import java.sql.Connection;
4
import java.sql.DriverManager;
5
import java.sql.ResultSet;
6
import java.sql.ResultSetMetaData;
7
import java.sql.SQLException;
8
import java.sql.Statement;
9

    
10
import javax.swing.JOptionPane;
11

    
12
import org.cresques.cts.ICoordTrans;
13
import org.cresques.cts.IProjection;
14
import org.cresques.cts.ProjectionPool;
15

    
16
import com.iver.andami.PluginServices;
17
import com.iver.andami.plugins.Extension;
18
import com.iver.andami.ui.mdiManager.IWindow;
19
import com.iver.cit.gvsig.fmap.MapControl;
20
import com.iver.cit.gvsig.fmap.ViewPort;
21
import com.iver.cit.gvsig.fmap.core.ICanReproject;
22
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
23
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
24
import com.iver.cit.gvsig.fmap.layers.FLayer;
25
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
26
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
27

    
28
public class LoadLayer extends Extension {
29

    
30
        public void initialize() {
31
                
32
        }
33

    
34
        public void execute(String actionCommand) {
35
                // IN CONFIG.XML, the actionCommand should be the layer name. (table name)
36
                com.iver.cit.gvsig.gui.View v = (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveWindow();
37
                MapControl mapCtrl = v.getMapControl();
38
                
39
        String dbURL = "jdbc:postgresql://localhost:5432/latin1"; // latin1 is the catalog name 
40
        String user = "postgres";
41
        String pwd = "aquilina";
42
        String layerName = actionCommand;
43
        String tableName = actionCommand;
44
        Connection conn;
45
                try {
46
                        conn = DriverManager.getConnection(dbURL, user, pwd);
47
                conn.setAutoCommit(false);
48
                
49
                String fidField = "gid"; // BE CAREFUL => MAY BE NOT!!!
50
                String geomField = "the_geom"; // BE CAREFUL => MAY BE NOT!!! => You should read table GEOMETRY_COLUMNS.
51
                                                                                // See PostGIS help.
52

    
53
                // To obtain the fields, make a connection and get them. 
54
                        Statement st = conn.createStatement();
55
                        ResultSet rs = st.executeQuery("select * from " + tableName + " LIMIT 1");
56
                        ResultSetMetaData rsmd = rs.getMetaData();
57
                        String[] fields = new String[rsmd.getColumnCount()-1]; // We don't want to include the_geom field
58
                        int j = 0;
59
                        for (int i = 0; i < fields.length; i++) {
60
                                if (!rsmd.getColumnName(i+1).equalsIgnoreCase(geomField))
61
                                {
62
                                        fields[j++] = rsmd.getColumnName(i+1);                                        
63
                                }
64
                        }
65
                        rs.close();        
66
                
67
                /* String[] fields = new String[1];                
68
                fields[0] = "gid"; */ 
69
                
70
                String whereClause = "";
71
        
72
                VectorialJDBCDriver driver = (VectorialJDBCDriver) LayerFactory.getDM()
73
                                        .getDriver("PostGIS JDBC Driver");
74
                
75
                // Here you can set the workingArea
76
                // driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
77
                
78
                
79
                String strEPSG = mapCtrl.getViewPort()
80
                            .getProjection().getAbrev()
81
                            .substring(5);
82
                DBLayerDefinition lyrDef = new DBLayerDefinition();                
83
                lyrDef.setName(layerName);
84
                lyrDef.setTableName(tableName);
85
                lyrDef.setWhereClause(whereClause);
86
                lyrDef.setFieldNames(fields);
87
                lyrDef.setFieldGeometry(geomField);
88
                lyrDef.setFieldID(fidField);
89
                // if (dbLayerDefinition.getWorkingArea() != null)
90
                //     lyrDef.setWorkingArea(dbLayerDefinition.getWorkingArea());
91
                
92
                lyrDef.setSRID_EPSG(strEPSG);
93
                if (driver instanceof ICanReproject)
94
                {                    
95
                    ((ICanReproject)driver).setDestProjection(strEPSG);
96
                }
97
                driver.setData(conn, lyrDef);
98
                IProjection proj = null; 
99
                if (driver instanceof ICanReproject)
100
                {                                        
101
                    proj = ProjectionPool.get("EPSG:" + ((ICanReproject)driver).getSourceProjection()); 
102
                }
103
                
104
                FLayer lyr = LayerFactory.createDBLayer(driver, layerName, proj);
105
        
106
                        if (lyr != null) {
107
                                lyr.setVisible(true);
108
                                v.getMapControl().getMapContext().beginAtomicEvent();
109
                                // You shoud checkProjection to see if it is necesary to reproject
110
                    checkProjection(lyr, v.getMapControl().getViewPort());
111
                                v.getMapControl().getMapContext().getLayers()
112
                                           .addLayer(lyr);
113
                                v.getMapControl().getMapContext().endAtomicEvent();
114
                    
115
                        }
116
                } catch (SQLException e) {
117
                        // TODO Auto-generated catch block
118
                        e.printStackTrace();
119
                }
120
        
121
                
122
        }
123

    
124
        public boolean isEnabled() {
125
                return true;
126
        }
127

    
128
        public boolean isVisible() {
129
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
130

    
131
                if  (v instanceof com.iver.cit.gvsig.gui.View)
132
                        return true;
133
                else
134
                        return false;
135
        }
136
        
137
    private void checkProjection(FLayer lyr, ViewPort viewPort)
138
    {
139
        if (lyr instanceof FLyrVect)
140
        {
141
            FLyrVect lyrVect = (FLyrVect) lyr;
142
            IProjection proj = lyr.getProjection();
143
            // Comprobar que la projecci?n es la misma que la vista
144
            if (proj == null)
145
            {
146
                // SUPONEMOS que la capa est? en la proyecci?n que 
147
                // estamos pidiendo (que ya es mucho suponer, ya).
148
                lyrVect.setProjection(viewPort.getProjection());
149
                return;
150
            }
151
            if (proj != viewPort.getProjection()) {
152
                int option = JOptionPane.showConfirmDialog(null,
153
                        PluginServices.getText(this, "reproyectar_aviso"),
154
                        PluginServices.getText(this, "reproyectar_pregunta"),
155
                        JOptionPane.YES_NO_OPTION);
156

    
157
                if (option == JOptionPane.NO_OPTION) {
158
                    return;
159
                } else {
160
                    ICoordTrans ct = proj.getCT(viewPort.getProjection());
161
                    lyrVect.setCoordTrans(ct);
162
                    System.err.println("coordTrans = " +
163
                        proj.getAbrev() + " " +
164
                        viewPort.getProjection().getAbrev());
165
                }
166
            }
167
        }                    
168

    
169
    }
170

    
171

    
172
}