root / trunk / extensions / extCatalogYNomenclator / src / es / gva / cit / gvsig / catalogClient / loaders / PostgisLoader.java @ 2886
History | View | Annotate | Download (4.75 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package es.gva.cit.gvsig.catalogClient.loaders; |
42 |
|
43 |
import java.sql.Connection; |
44 |
import java.sql.DriverManager; |
45 |
import java.sql.SQLException; |
46 |
import java.util.StringTokenizer; |
47 |
import java.util.TreeMap; |
48 |
|
49 |
import com.hardcode.driverManager.DriverLoadException; |
50 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
51 |
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver; |
52 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
53 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
54 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
55 |
|
56 |
|
57 |
|
58 |
/**
|
59 |
* This class is used to load a POSTGIS resource in gvSIG
|
60 |
*
|
61 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
62 |
*/
|
63 |
|
64 |
public class PostgisLoader extends AbstractLoader{ |
65 |
/**
|
66 |
* This function adds a Postgis Layer to gvSIG
|
67 |
* @param jdbcUrl
|
68 |
* JDBC url connection
|
69 |
* @param table
|
70 |
* Table to load
|
71 |
*/
|
72 |
|
73 |
public boolean load(String jdbcUrl, String table) { |
74 |
FLayer flayer; |
75 |
try {
|
76 |
flayer = createPostgisLayer(jdbcUrl, table); |
77 |
if (flayer == null){ |
78 |
return false; |
79 |
}else{
|
80 |
addLayerToView(flayer); |
81 |
return true; |
82 |
} |
83 |
} catch (SQLException e) { |
84 |
// TODO Auto-generated catch block
|
85 |
e.printStackTrace(); |
86 |
} catch (DriverLoadException e) {
|
87 |
// TODO Auto-generated catch block
|
88 |
e.printStackTrace(); |
89 |
} catch (ClassNotFoundException e) { |
90 |
// TODO Auto-generated catch block
|
91 |
e.printStackTrace(); |
92 |
} |
93 |
return false; |
94 |
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* It returns a Postgis Layer
|
99 |
* @param jdbcUrl
|
100 |
* JDBC url connection
|
101 |
* @param table
|
102 |
* Table to load
|
103 |
* @return
|
104 |
* @throws SQLException
|
105 |
* @throws DriverLoadException
|
106 |
* @throws ClassNotFoundException
|
107 |
*/
|
108 |
private FLayer createPostgisLayer(String jdbcUrl, String table) throws SQLException, DriverLoadException, ClassNotFoundException { |
109 |
//jdbc:postgresql://sercartlin/carto_300k?user=gis&password=gis
|
110 |
//NAME=Comunicaciones&TABLENAME=comunic_lin_300k&ID=6&FIELDS=entity,layer,codigo,tipo,gid&GEOMFIELD=the_geom
|
111 |
StringTokenizer sti = new StringTokenizer(jdbcUrl,"?"); |
112 |
String dbURL = sti.nextToken();
|
113 |
|
114 |
String p = sti.nextToken();
|
115 |
|
116 |
TreeMap credentials = separateParams(p);
|
117 |
String user = (String) credentials.get((String) "USER"); |
118 |
String pwd = (String) credentials.get((String) "PASSWORD"); |
119 |
|
120 |
TreeMap params = separateParams(table);
|
121 |
String layerName = (String) params.get((String) "NAME"); |
122 |
int fidField = Integer.valueOf((String) params.get((String) "ID")).intValue(); |
123 |
String fields = getFieldsClause((String) params.get((String) "FIELDS"), |
124 |
(String) params.get((String) "GEOMFIELD")); |
125 |
String tableName = (String) params.get((String) "TABLENAME"); |
126 |
String whereClause = ""; |
127 |
|
128 |
|
129 |
Connection conn = DriverManager.getConnection(dbURL, user, pwd); |
130 |
conn.setAutoCommit(false);
|
131 |
|
132 |
PostGisDriver pgd = new PostGisDriver();
|
133 |
pgd.setData(conn, tableName, fields, whereClause, fidField); |
134 |
|
135 |
return LayerFactory.createDBLayer(pgd, layerName, null); |
136 |
} |
137 |
|
138 |
private String getFieldsClause(String fields,String geomField){ |
139 |
return "ASBINARY(" + geomField + ", 'XDR')," + fields; |
140 |
} |
141 |
|
142 |
private TreeMap separateParams(String pairValues){ |
143 |
TreeMap map = new TreeMap(); |
144 |
String[] params = pairValues.split("&"); |
145 |
for (int i = 0; i < params.length; i++) { |
146 |
String[] nameValue = params[i].split("="); |
147 |
map.put(nameValue[0].toUpperCase(), nameValue[1]); |
148 |
} |
149 |
return map;
|
150 |
} |
151 |
} |