svn-gvsig-desktop / trunk / examples / exaLoadLayer / src / org / gvsig / example / postgis / LoadLayer.java @ 10626
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 |
|
15 |
import com.iver.andami.PluginServices; |
16 |
import com.iver.andami.plugins.Extension; |
17 |
import com.iver.andami.ui.mdiManager.IWindow; |
18 |
import com.iver.cit.gvsig.fmap.MapControl; |
19 |
import com.iver.cit.gvsig.fmap.ViewPort; |
20 |
import com.iver.cit.gvsig.fmap.core.ICanReproject; |
21 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
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 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
28 |
|
29 |
public class LoadLayer extends Extension { |
30 |
|
31 |
public void initialize() { |
32 |
|
33 |
} |
34 |
|
35 |
public void execute(String actionCommand) { |
36 |
// IN CONFIG.XML, the actionCommand should be the layer name. (table name)
|
37 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
38 |
MapControl mapCtrl = v.getMapControl(); |
39 |
|
40 |
String dbURL = "jdbc:postgresql://localhost:5432/latin1"; // latin1 is the catalog name |
41 |
String user = "postgres"; |
42 |
String pwd = "aquilina"; |
43 |
String layerName = actionCommand;
|
44 |
String tableName = actionCommand;
|
45 |
Connection conn;
|
46 |
try {
|
47 |
conn = DriverManager.getConnection(dbURL, user, pwd);
|
48 |
conn.setAutoCommit(false);
|
49 |
|
50 |
String fidField = "gid"; // BE CAREFUL => MAY BE NOT!!! |
51 |
String geomField = "the_geom"; // BE CAREFUL => MAY BE NOT!!! => You should read table GEOMETRY_COLUMNS. |
52 |
// See PostGIS help.
|
53 |
|
54 |
// To obtain the fields, make a connection and get them.
|
55 |
Statement st = conn.createStatement();
|
56 |
ResultSet rs = st.executeQuery("select * from " + tableName + " LIMIT 1"); |
57 |
ResultSetMetaData rsmd = rs.getMetaData();
|
58 |
String[] fields = new String[rsmd.getColumnCount()-1]; // We don't want to include the_geom field |
59 |
int j = 0; |
60 |
for (int i = 0; i < fields.length; i++) { |
61 |
if (!rsmd.getColumnName(i+1).equalsIgnoreCase(geomField)) |
62 |
{ |
63 |
fields[j++] = rsmd.getColumnName(i+1);
|
64 |
} |
65 |
} |
66 |
rs.close(); |
67 |
|
68 |
/* String[] fields = new String[1];
|
69 |
fields[0] = "gid"; */
|
70 |
|
71 |
String whereClause = ""; |
72 |
|
73 |
VectorialJDBCDriver driver = (VectorialJDBCDriver) LayerFactory.getDM() |
74 |
.getDriver("PostGIS JDBC Driver");
|
75 |
|
76 |
// Here you can set the workingArea
|
77 |
// driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
|
78 |
|
79 |
|
80 |
String strEPSG = mapCtrl.getViewPort()
|
81 |
.getProjection().getAbrev() |
82 |
.substring(5);
|
83 |
DBLayerDefinition lyrDef = new DBLayerDefinition();
|
84 |
lyrDef.setName(layerName); |
85 |
lyrDef.setTableName(tableName); |
86 |
lyrDef.setWhereClause(whereClause); |
87 |
lyrDef.setFieldNames(fields); |
88 |
lyrDef.setFieldGeometry(geomField); |
89 |
lyrDef.setFieldID(fidField); |
90 |
// if (dbLayerDefinition.getWorkingArea() != null)
|
91 |
// lyrDef.setWorkingArea(dbLayerDefinition.getWorkingArea());
|
92 |
|
93 |
lyrDef.setSRID_EPSG(strEPSG); |
94 |
if (driver instanceof ICanReproject) |
95 |
{ |
96 |
((ICanReproject)driver).setDestProjection(strEPSG); |
97 |
} |
98 |
driver.setData(conn, lyrDef); |
99 |
IProjection proj = null;
|
100 |
if (driver instanceof ICanReproject) |
101 |
{ |
102 |
proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)driver).getSourceProjection());
|
103 |
} |
104 |
|
105 |
FLayer lyr = LayerFactory.createDBLayer(driver, layerName, proj); |
106 |
|
107 |
if (lyr != null) { |
108 |
lyr.setVisible(true);
|
109 |
v.getMapControl().getMapContext().beginAtomicEvent(); |
110 |
// You shoud checkProjection to see if it is necesary to reproject
|
111 |
checkProjection(lyr, v.getMapControl().getViewPort()); |
112 |
v.getMapControl().getMapContext().getLayers() |
113 |
.addLayer(lyr); |
114 |
v.getMapControl().getMapContext().endAtomicEvent(); |
115 |
|
116 |
} |
117 |
} catch (SQLException e) { |
118 |
// TODO Auto-generated catch block
|
119 |
e.printStackTrace(); |
120 |
} |
121 |
|
122 |
|
123 |
} |
124 |
|
125 |
public boolean isEnabled() { |
126 |
return true; |
127 |
} |
128 |
|
129 |
public boolean isVisible() { |
130 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
131 |
|
132 |
if (v instanceof View) |
133 |
return true; |
134 |
else
|
135 |
return false; |
136 |
} |
137 |
|
138 |
private void checkProjection(FLayer lyr, ViewPort viewPort) |
139 |
{ |
140 |
if (lyr instanceof FLyrVect) |
141 |
{ |
142 |
FLyrVect lyrVect = (FLyrVect) lyr; |
143 |
IProjection proj = lyr.getProjection(); |
144 |
// Comprobar que la projecci?n es la misma que la vista
|
145 |
if (proj == null) |
146 |
{ |
147 |
// SUPONEMOS que la capa est? en la proyecci?n que
|
148 |
// estamos pidiendo (que ya es mucho suponer, ya).
|
149 |
lyrVect.setProjection(viewPort.getProjection()); |
150 |
return;
|
151 |
} |
152 |
if (proj != viewPort.getProjection()) {
|
153 |
int option = JOptionPane.showConfirmDialog(null, |
154 |
PluginServices.getText(this, "reproyectar_aviso"), |
155 |
PluginServices.getText(this, "reproyectar_pregunta"), |
156 |
JOptionPane.YES_NO_OPTION);
|
157 |
|
158 |
if (option == JOptionPane.NO_OPTION) { |
159 |
return;
|
160 |
} else {
|
161 |
ICoordTrans ct = proj.getCT(viewPort.getProjection()); |
162 |
lyrVect.setCoordTrans(ct); |
163 |
System.err.println("coordTrans = " + |
164 |
proj.getAbrev() + " " +
|
165 |
viewPort.getProjection().getAbrev()); |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
} |
171 |
|
172 |
|
173 |
} |