Statistics
| Revision:

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

History | View | Annotate | Download (5.85 KB)

1 6146 fjp
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 10661 caballero
import com.hardcode.driverManager.DriverLoadException;
16 6146 fjp
import com.iver.andami.PluginServices;
17
import com.iver.andami.plugins.Extension;
18 6885 fjp
import com.iver.andami.ui.mdiManager.IWindow;
19 6146 fjp
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 8164 fjp
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
23 6146 fjp
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 7531 fjp
import com.iver.cit.gvsig.project.documents.view.gui.View;
29 6146 fjp
30
public class LoadLayer extends Extension {
31
32
        public void initialize() {
33 10661 caballero
34 6146 fjp
        }
35
36
        public void execute(String actionCommand) {
37
                // IN CONFIG.XML, the actionCommand should be the layer name. (table name)
38 7531 fjp
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
39 6146 fjp
                MapControl mapCtrl = v.getMapControl();
40 10661 caballero
41
        String dbURL = "jdbc:postgresql://localhost:5432/latin1"; // latin1 is the catalog name
42 6146 fjp
        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 10661 caballero
51 6146 fjp
                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 10661 caballero
                // To obtain the fields, make a connection and get them.
56 6146 fjp
                        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 10661 caballero
                                        fields[j++] = rsmd.getColumnName(i+1);
65 6146 fjp
                                }
66
                        }
67 10661 caballero
                        rs.close();
68
69
                /* String[] fields = new String[1];
70
                fields[0] = "gid"; */
71
72 6146 fjp
                String whereClause = "";
73 10661 caballero
74 6146 fjp
                VectorialJDBCDriver driver = (VectorialJDBCDriver) LayerFactory.getDM()
75
                                        .getDriver("PostGIS JDBC Driver");
76 10661 caballero
77 6146 fjp
                // Here you can set the workingArea
78
                // driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
79 10661 caballero
80
81 6146 fjp
                String strEPSG = mapCtrl.getViewPort()
82
                            .getProjection().getAbrev()
83
                            .substring(5);
84 10661 caballero
                DBLayerDefinition lyrDef = new DBLayerDefinition();
85 6146 fjp
                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 10661 caballero
94 6146 fjp
                lyrDef.setSRID_EPSG(strEPSG);
95
                if (driver instanceof ICanReproject)
96 10661 caballero
                {
97 6146 fjp
                    ((ICanReproject)driver).setDestProjection(strEPSG);
98
                }
99
                driver.setData(conn, lyrDef);
100 10661 caballero
                IProjection proj = null;
101 6146 fjp
                if (driver instanceof ICanReproject)
102 10661 caballero
                {
103
                    proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)driver).getSourceProjection());
104 6146 fjp
                }
105 10661 caballero
106 6146 fjp
                FLayer lyr = LayerFactory.createDBLayer(driver, layerName, proj);
107 10661 caballero
108 6146 fjp
                        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 10661 caballero
117 6146 fjp
                        }
118
                } catch (SQLException e) {
119
                        // TODO Auto-generated catch block
120
                        e.printStackTrace();
121 10661 caballero
                } catch (DriverLoadException e) {
122
                        // TODO Auto-generated catch block
123
                        e.printStackTrace();
124 6146 fjp
                }
125 10661 caballero
126
127 6146 fjp
        }
128
129
        public boolean isEnabled() {
130
                return true;
131
        }
132
133
        public boolean isVisible() {
134 6885 fjp
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
135 6146 fjp
136 7531 fjp
                if  (v instanceof View)
137 6146 fjp
                        return true;
138
                else
139
                        return false;
140
        }
141 10661 caballero
142 6146 fjp
    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 10661 caballero
                // SUPONEMOS que la capa est? en la proyecci?n que
152 6146 fjp
                // 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 6213 fjp
                    ICoordTrans ct = proj.getCT(viewPort.getProjection());
166 6146 fjp
                    lyrVect.setCoordTrans(ct);
167
                    System.err.println("coordTrans = " +
168
                        proj.getAbrev() + " " +
169
                        viewPort.getProjection().getAbrev());
170
                }
171
            }
172 10661 caballero
        }
173 6146 fjp
174
    }
175
176
177
}