svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.mdb / src / main / java / org / gvsig / fmap / dal / store / mdb / operations / MdbFetchFeatureTypeOperation.java @ 43788
History | View | Annotate | Download (8.83 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.store.mdb.operations; |
3 |
|
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.ResultSetMetaData; |
7 |
import java.sql.SQLException; |
8 |
import java.sql.Statement; |
9 |
import java.util.HashMap; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.cresques.cts.IProjection; |
14 |
import org.gvsig.fmap.dal.DataTypes; |
15 |
import org.gvsig.fmap.dal.exception.DataException; |
16 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
20 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver; |
21 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
22 |
import org.gvsig.fmap.geom.Geometry; |
23 |
import org.gvsig.fmap.geom.GeometryLocator; |
24 |
import org.gvsig.fmap.geom.GeometryManager; |
25 |
import org.gvsig.fmap.geom.type.GeometryType; |
26 |
|
27 |
public class MdbFetchFeatureTypeOperation extends FetchFeatureTypeOperation { |
28 |
|
29 |
private static Map<String,GeometryType>h2spatialGeometryTypes = null; |
30 |
private Map<String,GeometryColumnInfo> geometry_column; |
31 |
|
32 |
private static class GeometryColumnInfo { |
33 |
|
34 |
public String columnName; |
35 |
public int geometryType; |
36 |
public String geometryTypeName; |
37 |
public int dimensions; |
38 |
public int srid; |
39 |
|
40 |
public GeometryColumnInfo(String columnName, int geometryType, String geometryTypeName, int dimensions, int srid) { |
41 |
this.columnName = columnName;
|
42 |
this.geometryType = geometryType;
|
43 |
this.geometryTypeName = geometryTypeName;
|
44 |
this.dimensions = dimensions;
|
45 |
this.srid = srid;
|
46 |
} |
47 |
} |
48 |
|
49 |
|
50 |
public MdbFetchFeatureTypeOperation(
|
51 |
JDBCHelper helper |
52 |
) { |
53 |
super(helper);
|
54 |
} |
55 |
|
56 |
private GeometryType getGT(
|
57 |
GeometryManager manager, |
58 |
int type,
|
59 |
int subtype
|
60 |
) { |
61 |
try {
|
62 |
return manager.getGeometryType(type, subtype);
|
63 |
} catch (Exception ex) { |
64 |
return null; |
65 |
} |
66 |
} |
67 |
|
68 |
public MdbFetchFeatureTypeOperation(
|
69 |
JDBCHelper helper, |
70 |
EditableFeatureType featureType, |
71 |
String dbname,
|
72 |
String schema,
|
73 |
String table,
|
74 |
List<String> primaryKeys, |
75 |
String defaultGeometryColumn,
|
76 |
IProjection crs |
77 |
) { |
78 |
super(helper, featureType, dbname, schema, table, primaryKeys, defaultGeometryColumn, crs);
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void fetch(EditableFeatureType featureType, Connection conn, String dbname, String schema, String table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException { |
83 |
geometry_column = new HashMap<>(); |
84 |
try {
|
85 |
//
|
86 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
|
87 |
//
|
88 |
StringBuilder where = null; |
89 |
if( !StringUtils.isEmpty(dbname) ) {
|
90 |
if( where == null ) { |
91 |
where = new StringBuilder(); |
92 |
} else {
|
93 |
where.append(" AND ");
|
94 |
} |
95 |
where.append("UPPER(F_TABLE_CATALOG) = '");
|
96 |
where.append(dbname.toUpperCase()); |
97 |
where.append("'");
|
98 |
} |
99 |
if( !StringUtils.isEmpty(schema) ) {
|
100 |
if( where == null ) { |
101 |
where = new StringBuilder(); |
102 |
} else {
|
103 |
where.append(" AND ");
|
104 |
} |
105 |
where.append("UPPER(F_TABLE_SCHEMA) = '");
|
106 |
where.append(schema.toUpperCase()); |
107 |
where.append("'");
|
108 |
} |
109 |
if( !StringUtils.isEmpty(table) ) {
|
110 |
if( where == null ) { |
111 |
where = new StringBuilder(); |
112 |
} else {
|
113 |
where.append(" AND ");
|
114 |
} |
115 |
where.append("UPPER(F_TABLE_NAME) = '");
|
116 |
where.append(table.toUpperCase()); |
117 |
where.append("'");
|
118 |
} |
119 |
String sql = "SELECT F_GEOMETRY_COLUMN, GEOMETRY_TYPE, COORD_DIMENSION, SRID, TYPE FROM GEOMETRY_COLUMNS WHERE " + where; |
120 |
Statement st = this.getConnection().createStatement(); |
121 |
ResultSet rs = JDBCUtils.executeQuery(st,sql);
|
122 |
while( rs.next() ) {
|
123 |
geometry_column.put( |
124 |
rs.getString("F_GEOMETRY_COLUMN"),
|
125 |
new GeometryColumnInfo(
|
126 |
rs.getString("F_GEOMETRY_COLUMN"),
|
127 |
rs.getInt("GEOMETRY_TYPE"),
|
128 |
rs.getString("TYPE"),
|
129 |
rs.getInt("COORD_DIMENSION"),
|
130 |
rs.getInt("SRID")
|
131 |
) |
132 |
); |
133 |
} |
134 |
} catch (SQLException ex) { |
135 |
logger.warn("Can't read metadata from table '"+table+"'.",ex); |
136 |
} |
137 |
super.fetch(featureType, conn, dbname, schema, table, pks, defaultGeometryColumn, crs);
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
protected void fetchGeometryTypeAndSRS( |
142 |
EditableFeatureAttributeDescriptor attr, |
143 |
ResultSetMetaData rsMetadata,
|
144 |
int colIndex
|
145 |
) { |
146 |
try {
|
147 |
if( attr.getType()==DataTypes.GEOMETRY ) {
|
148 |
GeometryColumnInfo column_info = this.geometry_column.get(attr.getName());
|
149 |
String type = "GEOMETRY"; |
150 |
if( column_info!=null ) { |
151 |
// H2GIS solo soporte 2D y 3D no soporta Ms
|
152 |
if( column_info.dimensions==3 ) { |
153 |
type = column_info.geometryTypeName+"Z";
|
154 |
} else {
|
155 |
type = column_info.geometryTypeName; |
156 |
} |
157 |
SRSSolver solver = this.helper.getSRSSolver();
|
158 |
attr.setSRS( |
159 |
solver.getProjection(getConnection(), column_info.srid) |
160 |
); |
161 |
} |
162 |
GeometryType gt = getGeometryTypeFromMdbType(type); |
163 |
if( gt != null ) { |
164 |
attr.setGeometryType(gt); |
165 |
} |
166 |
} |
167 |
} catch(Exception ex) { |
168 |
throw new RuntimeException("Can't fetch geometry type and SRS.", ex); |
169 |
} |
170 |
} |
171 |
|
172 |
private GeometryType getGeometryTypeFromMdbType(String typeName) { |
173 |
if( h2spatialGeometryTypes==null ) { |
174 |
//
|
175 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
|
176 |
//
|
177 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
178 |
h2spatialGeometryTypes = new HashMap<>(); |
179 |
h2spatialGeometryTypes.put("POINT", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2D));
|
180 |
h2spatialGeometryTypes.put("POINTZ", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3D));
|
181 |
|
182 |
h2spatialGeometryTypes.put("LINESTRING", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2D));
|
183 |
h2spatialGeometryTypes.put("LINESTRINGZ", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3D));
|
184 |
|
185 |
h2spatialGeometryTypes.put("POLYGON", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2D));
|
186 |
h2spatialGeometryTypes.put("POLYGONZ", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3D));
|
187 |
|
188 |
h2spatialGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
|
189 |
h2spatialGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
|
190 |
|
191 |
h2spatialGeometryTypes.put("MULTILINESTRING", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2D));
|
192 |
h2spatialGeometryTypes.put("MULTILINESTRINGZ", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3D));
|
193 |
|
194 |
h2spatialGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
|
195 |
h2spatialGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
|
196 |
|
197 |
h2spatialGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
198 |
h2spatialGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
199 |
|
200 |
h2spatialGeometryTypes.put("GEOMCOLLECTION", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
201 |
h2spatialGeometryTypes.put("GEOMCOLLECTIONZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
202 |
} |
203 |
return h2spatialGeometryTypes.get(typeName);
|
204 |
} |
205 |
|
206 |
} |