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 @ 43377
History | View | Annotate | Download (8.59 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.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.operations.FetchFeatureTypeOperation; |
21 |
import org.gvsig.fmap.geom.Geometry; |
22 |
import org.gvsig.fmap.geom.GeometryLocator; |
23 |
import org.gvsig.fmap.geom.GeometryManager; |
24 |
import org.gvsig.fmap.geom.type.GeometryType; |
25 |
|
26 |
public class H2SpatialFetchFeatureTypeOperation extends FetchFeatureTypeOperation { |
27 |
|
28 |
private static Map<String,GeometryType>h2spatialGeometryTypes = null; |
29 |
private Map<String,GeometryColumnInfo> geometry_column; |
30 |
|
31 |
private static class GeometryColumnInfo { |
32 |
|
33 |
public String columnName; |
34 |
public int geometryType; |
35 |
public String geometryTypeName; |
36 |
public int dimensions; |
37 |
public int srid; |
38 |
|
39 |
public GeometryColumnInfo(String columnName, int geometryType, String geometryTypeName, int dimensions, int srid) { |
40 |
this.columnName = columnName;
|
41 |
this.geometryType = geometryType;
|
42 |
this.geometryTypeName = geometryTypeName;
|
43 |
this.dimensions = dimensions;
|
44 |
this.srid = srid;
|
45 |
} |
46 |
} |
47 |
|
48 |
|
49 |
public H2SpatialFetchFeatureTypeOperation(
|
50 |
JDBCHelper helper |
51 |
) { |
52 |
super(helper);
|
53 |
} |
54 |
|
55 |
private GeometryType getGT(
|
56 |
GeometryManager manager, |
57 |
int type,
|
58 |
int subtype
|
59 |
) { |
60 |
try {
|
61 |
return manager.getGeometryType(type, subtype);
|
62 |
} catch (Exception ex) { |
63 |
return null; |
64 |
} |
65 |
} |
66 |
|
67 |
public H2SpatialFetchFeatureTypeOperation(
|
68 |
JDBCHelper helper, |
69 |
EditableFeatureType featureType, |
70 |
String dbname,
|
71 |
String schema,
|
72 |
String table,
|
73 |
List<String> primaryKeys, |
74 |
String defaultGeometryColumn,
|
75 |
IProjection crs |
76 |
) { |
77 |
super(helper, featureType, dbname, schema, table, primaryKeys, defaultGeometryColumn, crs);
|
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void fetch(EditableFeatureType featureType, Connection conn, String dbname, String schema, String 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( !StringUtils.isEmpty(dbname) ) {
|
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(dbname.toUpperCase()); |
96 |
where.append("'");
|
97 |
} |
98 |
if( !StringUtils.isEmpty(schema) ) {
|
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(schema.toUpperCase()); |
106 |
where.append("'");
|
107 |
} |
108 |
if( !StringUtils.isEmpty(table) ) {
|
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.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, dbname, schema, table, pks, defaultGeometryColumn, crs);
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
protected void fetchGeometryTypeAndSRS( |
141 |
EditableFeatureAttributeDescriptor attr, |
142 |
ResultSetMetaData rsMetadata,
|
143 |
int colIndex
|
144 |
) { |
145 |
if( attr.getType()==DataTypes.GEOMETRY ) {
|
146 |
GeometryColumnInfo column_info = this.geometry_column.get(attr.getName());
|
147 |
String type = "GEOMETRY"; |
148 |
if( column_info!=null ) { |
149 |
// H2GIS solo soporte 2D y 3D no soporta Ms
|
150 |
if( column_info.dimensions==3 ) { |
151 |
type = column_info.geometryTypeName+"Z";
|
152 |
} else {
|
153 |
type = column_info.geometryTypeName; |
154 |
} |
155 |
attr.setSRS( |
156 |
this.helper.getProjectionFromDatabaseCode(
|
157 |
String.valueOf(column_info.srid)
|
158 |
) |
159 |
); |
160 |
} |
161 |
GeometryType gt = getGeometryTypeFromH2SpatialType(type); |
162 |
if( gt != null ) { |
163 |
attr.setGeometryType(gt); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
private GeometryType getGeometryTypeFromH2SpatialType(String typeName) { |
169 |
if( h2spatialGeometryTypes==null ) { |
170 |
//
|
171 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
|
172 |
//
|
173 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
174 |
h2spatialGeometryTypes = new HashMap<>(); |
175 |
h2spatialGeometryTypes.put("POINT", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2D));
|
176 |
h2spatialGeometryTypes.put("POINTZ", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3D));
|
177 |
|
178 |
h2spatialGeometryTypes.put("LINESTRING", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2D));
|
179 |
h2spatialGeometryTypes.put("LINESTRINGZ", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3D));
|
180 |
|
181 |
h2spatialGeometryTypes.put("POLYGON", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2D));
|
182 |
h2spatialGeometryTypes.put("POLYGONZ", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3D));
|
183 |
|
184 |
h2spatialGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
|
185 |
h2spatialGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
|
186 |
|
187 |
h2spatialGeometryTypes.put("MULTILINESTRING", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2D));
|
188 |
h2spatialGeometryTypes.put("MULTILINESTRINGZ", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3D));
|
189 |
|
190 |
h2spatialGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
|
191 |
h2spatialGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
|
192 |
|
193 |
h2spatialGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
194 |
h2spatialGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
195 |
|
196 |
h2spatialGeometryTypes.put("GEOMCOLLECTION", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
197 |
h2spatialGeometryTypes.put("GEOMCOLLECTIONZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
198 |
} |
199 |
return h2spatialGeometryTypes.get(typeName);
|
200 |
} |
201 |
|
202 |
} |