Statistics
| Revision:

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

History | View | Annotate | Download (6.23 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

    
15
import com.hardcode.driverManager.DriverLoadException;
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.crs.CRSFactory;
23
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory;
24
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC;
25
import com.iver.cit.gvsig.fmap.drivers.DBException;
26
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
27
import com.iver.cit.gvsig.fmap.drivers.IConnection;
28
import com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver;
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
31
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
32
import com.iver.cit.gvsig.project.documents.view.gui.View;
33

    
34
public class LoadLayer extends Extension {
35

    
36
        public void initialize() {
37

    
38
        }
39

    
40
        public void execute(String actionCommand) {
41
                // IN CONFIG.XML, the actionCommand should be the layer name. (table name)
42
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
43
                MapControl mapCtrl = v.getMapControl();
44

    
45
        String dbURL = "jdbc:postgresql://localhost:5432/latin1"; // latin1 is the catalog name
46
        String user = "postgres";
47
        String pwd = "aquilina";
48
        String layerName = actionCommand;
49
        String tableName = actionCommand;
50
        IConnection conn;
51
                try {
52
                        conn = ConnectionFactory.createConnection(dbURL, user, pwd);
53
                ((ConnectionJDBC)conn).getConnection().setAutoCommit(false);
54

    
55
                String fidField = "gid"; // BE CAREFUL => MAY BE NOT!!!
56
                String geomField = "the_geom"; // BE CAREFUL => MAY BE NOT!!! => You should read table GEOMETRY_COLUMNS.
57
                                                                                // See PostGIS help.
58

    
59
                // To obtain the fields, make a connection and get them.
60
                        Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
61
                        ResultSet rs = st.executeQuery("select * from " + tableName + " LIMIT 1");
62
                        ResultSetMetaData rsmd = rs.getMetaData();
63
                        String[] fields = new String[rsmd.getColumnCount()-1]; // We don't want to include the_geom field
64
                        int j = 0;
65
                        for (int i = 0; i < fields.length; i++) {
66
                                if (!rsmd.getColumnName(i+1).equalsIgnoreCase(geomField))
67
                                {
68
                                        fields[j++] = rsmd.getColumnName(i+1);
69
                                }
70
                        }
71
                        rs.close();
72

    
73
                /* String[] fields = new String[1];
74
                fields[0] = "gid"; */
75

    
76
                String whereClause = "";
77

    
78
                IVectorialJDBCDriver driver = (IVectorialJDBCDriver) LayerFactory.getDM()
79
                                        .getDriver("PostGIS JDBC Driver");
80

    
81
                // Here you can set the workingArea
82
                // driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
83

    
84

    
85
                String strEPSG = mapCtrl.getViewPort()
86
                            .getProjection().getAbrev()
87
                            .substring(5);
88
                DBLayerDefinition lyrDef = new DBLayerDefinition();
89
                lyrDef.setName(layerName);
90
                lyrDef.setTableName(tableName);
91
                lyrDef.setWhereClause(whereClause);
92
                lyrDef.setFieldNames(fields);
93
                lyrDef.setFieldGeometry(geomField);
94
                lyrDef.setFieldID(fidField);
95
                // if (dbLayerDefinition.getWorkingArea() != null)
96
                //     lyrDef.setWorkingArea(dbLayerDefinition.getWorkingArea());
97

    
98
                lyrDef.setSRID_EPSG(strEPSG);
99
                if (driver instanceof ICanReproject)
100
                {
101
                    ((ICanReproject)driver).setDestProjection(strEPSG);
102
                }
103
                driver.setData(conn, lyrDef);
104
                IProjection proj = null;
105
                if (driver instanceof ICanReproject)
106
                {
107
                    proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)driver).getSourceProjection());
108
                }
109

    
110
                FLayer lyr = LayerFactory.createDBLayer(driver, layerName, proj);
111

    
112
                        if (lyr != null) {
113
                                lyr.setVisible(true);
114
                                v.getMapControl().getMapContext().beginAtomicEvent();
115
                                // You shoud checkProjection to see if it is necesary to reproject
116
                    checkProjection(lyr, v.getMapControl().getViewPort());
117
                                v.getMapControl().getMapContext().getLayers()
118
                                           .addLayer(lyr);
119
                                v.getMapControl().getMapContext().endAtomicEvent();
120

    
121
                        }
122
                } catch (SQLException e) {
123
                        // TODO Auto-generated catch block
124
                        e.printStackTrace();
125
                } catch (DriverLoadException e) {
126
                        // TODO Auto-generated catch block
127
                        e.printStackTrace();
128
                } catch (DBException e) {
129
                        // TODO Auto-generated catch block
130
                        e.printStackTrace();
131
                }
132

    
133

    
134
        }
135

    
136
        public boolean isEnabled() {
137
                return true;
138
        }
139

    
140
        public boolean isVisible() {
141
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
142

    
143
                if  (v instanceof View)
144
                        return true;
145
                else
146
                        return false;
147
        }
148

    
149
    private void checkProjection(FLayer lyr, ViewPort viewPort)
150
    {
151
        if (lyr instanceof FLyrVect)
152
        {
153
            FLyrVect lyrVect = (FLyrVect) lyr;
154
            IProjection proj = lyr.getProjection();
155
            // Comprobar que la projecci?n es la misma que la vista
156
            if (proj == null)
157
            {
158
                // SUPONEMOS que la capa est? en la proyecci?n que
159
                // estamos pidiendo (que ya es mucho suponer, ya).
160
                lyrVect.setProjection(viewPort.getProjection());
161
                return;
162
            }
163
            if (proj != viewPort.getProjection()) {
164
                int option = JOptionPane.showConfirmDialog(null,
165
                        PluginServices.getText(this, "reproyectar_aviso"),
166
                        PluginServices.getText(this, "reproyectar_pregunta"),
167
                        JOptionPane.YES_NO_OPTION);
168

    
169
                if (option == JOptionPane.NO_OPTION) {
170
                    return;
171
                } else {
172
                    ICoordTrans ct = proj.getCT(viewPort.getProjection());
173
                    lyrVect.setCoordTrans(ct);
174
                    System.err.println("coordTrans = " +
175
                        proj.getAbrev() + " " +
176
                        viewPort.getProjection().getAbrev());
177
                }
178
            }
179
        }
180

    
181
    }
182

    
183

    
184
}