Statistics
| Revision:

svn-gvsig-desktop / trunk / examples / exaLoadLayer / src / org / gvsig / example / postgis / LoadLayer.java @ 10661

History | View | Annotate | Download (5.85 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.DBLayerDefinition;
24
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
25
import com.iver.cit.gvsig.fmap.layers.FLayer;
26
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
27
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
28
import com.iver.cit.gvsig.project.documents.view.gui.View;
29

    
30
public class LoadLayer extends Extension {
31

    
32
        public void initialize() {
33

    
34
        }
35

    
36
        public void execute(String actionCommand) {
37
                // IN CONFIG.XML, the actionCommand should be the layer name. (table name)
38
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
39
                MapControl mapCtrl = v.getMapControl();
40

    
41
        String dbURL = "jdbc:postgresql://localhost:5432/latin1"; // latin1 is the catalog name
42
        String user = "postgres";
43
        String pwd = "aquilina";
44
        String layerName = actionCommand;
45
        String tableName = actionCommand;
46
        Connection conn;
47
                try {
48
                        conn = DriverManager.getConnection(dbURL, user, pwd);
49
                conn.setAutoCommit(false);
50

    
51
                String fidField = "gid"; // BE CAREFUL => MAY BE NOT!!!
52
                String geomField = "the_geom"; // BE CAREFUL => MAY BE NOT!!! => You should read table GEOMETRY_COLUMNS.
53
                                                                                // See PostGIS help.
54

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

    
69
                /* String[] fields = new String[1];
70
                fields[0] = "gid"; */
71

    
72
                String whereClause = "";
73

    
74
                VectorialJDBCDriver driver = (VectorialJDBCDriver) LayerFactory.getDM()
75
                                        .getDriver("PostGIS JDBC Driver");
76

    
77
                // Here you can set the workingArea
78
                // driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
79

    
80

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

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

    
106
                FLayer lyr = LayerFactory.createDBLayer(driver, layerName, proj);
107

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

    
117
                        }
118
                } catch (SQLException e) {
119
                        // TODO Auto-generated catch block
120
                        e.printStackTrace();
121
                } catch (DriverLoadException e) {
122
                        // TODO Auto-generated catch block
123
                        e.printStackTrace();
124
                }
125

    
126

    
127
        }
128

    
129
        public boolean isEnabled() {
130
                return true;
131
        }
132

    
133
        public boolean isVisible() {
134
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
135

    
136
                if  (v instanceof View)
137
                        return true;
138
                else
139
                        return false;
140
        }
141

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

    
162
                if (option == JOptionPane.NO_OPTION) {
163
                    return;
164
                } else {
165
                    ICoordTrans ct = proj.getCT(viewPort.getProjection());
166
                    lyrVect.setCoordTrans(ct);
167
                    System.err.println("coordTrans = " +
168
                        proj.getAbrev() + " " +
169
                        viewPort.getProjection().getAbrev());
170
                }
171
            }
172
        }
173

    
174
    }
175

    
176

    
177
}