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 | 43377 | jjdelcerro | |
---|---|---|---|
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 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
20 | 43687 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver; |
21 | 43377 | jjdelcerro | 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 | 44058 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
28 | 43377 | jjdelcerro | 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 | 44058 | jjdelcerro | TableReference table, |
73 | 43377 | jjdelcerro | List<String> primaryKeys, |
74 | String defaultGeometryColumn,
|
||
75 | IProjection crs |
||
76 | ) { |
||
77 | 44058 | jjdelcerro | super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs);
|
78 | 43377 | jjdelcerro | } |
79 | |||
80 | @Override
|
||
81 | 44058 | jjdelcerro | public void fetch(EditableFeatureType featureType, Connection conn, TableReference table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException { |
82 | 43377 | jjdelcerro | 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 | 44058 | jjdelcerro | if( table.hasDatabase() ) {
|
89 | 43377 | jjdelcerro | if( where == null ) { |
90 | where = new StringBuilder(); |
||
91 | } else {
|
||
92 | where.append(" AND ");
|
||
93 | } |
||
94 | where.append("UPPER(F_TABLE_CATALOG) = '");
|
||
95 | 44058 | jjdelcerro | where.append(table.getDatabase().toUpperCase()); |
96 | 43377 | jjdelcerro | where.append("'");
|
97 | } |
||
98 | 44058 | jjdelcerro | if( table.hasSchema()) {
|
99 | 43377 | jjdelcerro | if( where == null ) { |
100 | where = new StringBuilder(); |
||
101 | } else {
|
||
102 | where.append(" AND ");
|
||
103 | } |
||
104 | where.append("UPPER(F_TABLE_SCHEMA) = '");
|
||
105 | 44058 | jjdelcerro | where.append(table.getSchema().toUpperCase()); |
106 | 43377 | jjdelcerro | where.append("'");
|
107 | } |
||
108 | 44058 | jjdelcerro | if( table.hasTable()) {
|
109 | 43377 | jjdelcerro | if( where == null ) { |
110 | where = new StringBuilder(); |
||
111 | } else {
|
||
112 | where.append(" AND ");
|
||
113 | } |
||
114 | where.append("UPPER(F_TABLE_NAME) = '");
|
||
115 | 44058 | jjdelcerro | where.append(table.getTable().toUpperCase()); |
116 | 43377 | jjdelcerro | 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 | 44058 | jjdelcerro | LOGGER.warn("Can't read metadata from table '"+table+"'.",ex); |
135 | 43377 | jjdelcerro | } |
136 | 44058 | jjdelcerro | super.fetch(featureType, conn, table, pks, defaultGeometryColumn, crs);
|
137 | 43377 | jjdelcerro | } |
138 | |||
139 | @Override
|
||
140 | protected void fetchGeometryTypeAndSRS( |
||
141 | EditableFeatureAttributeDescriptor attr, |
||
142 | ResultSetMetaData rsMetadata,
|
||
143 | int colIndex
|
||
144 | ) { |
||
145 | 43687 | jjdelcerro | 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 | 43377 | jjdelcerro | } |
161 | 43687 | jjdelcerro | GeometryType gt = getGeometryTypeFromH2SpatialType(type); |
162 | if( gt != null ) { |
||
163 | attr.setGeometryType(gt); |
||
164 | } |
||
165 | 43377 | jjdelcerro | } |
166 | 43687 | jjdelcerro | } catch(Exception ex) { |
167 | throw new RuntimeException("Can't fetch geometry type and SRS.", ex); |
||
168 | 43377 | jjdelcerro | } |
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 | } |