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