comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/datasources/ArcSDEStatement.java @ 263:031ef9649cd1

Added SFS-Support to ArcSDE-Query-Part geo-backend/trunk@210 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 13 Oct 2009 08:57:19 +0000
parents 122fdc9da5f0
children 1530890b28c9
comparison
equal deleted inserted replaced
144:fcfa9d2de572 263:031ef9649cd1
1 /** 1 /**
2 * 2 *
3 */ 3 */
4 package de.intevation.gnv.geobackend.sde.datasources; 4 package de.intevation.gnv.geobackend.sde.datasources;
5 5
6 import java.sql.Connection; 6 import java.sql.Connection;
7 import java.sql.ResultSet; 7 import java.sql.ResultSet;
8 import java.sql.SQLException; 8 import java.sql.SQLException;
9 import java.sql.SQLWarning; 9 import java.sql.SQLWarning;
10 import java.sql.Statement; 10 import java.sql.Statement;
11 11
12 import org.apache.log4j.Logger; 12 import org.apache.log4j.Logger;
13 13
14 import com.esri.sde.sdk.client.SDEPoint;
14 import com.esri.sde.sdk.client.SeColumnDefinition; 15 import com.esri.sde.sdk.client.SeColumnDefinition;
16 import com.esri.sde.sdk.client.SeConnection;
17 import com.esri.sde.sdk.client.SeCoordinateReference;
15 import com.esri.sde.sdk.client.SeException; 18 import com.esri.sde.sdk.client.SeException;
19 import com.esri.sde.sdk.client.SeFilter;
20 import com.esri.sde.sdk.client.SeLayer;
16 import com.esri.sde.sdk.client.SeQuery; 21 import com.esri.sde.sdk.client.SeQuery;
17 import com.esri.sde.sdk.client.SeRow; 22 import com.esri.sde.sdk.client.SeRow;
23 import com.esri.sde.sdk.client.SeShape;
24 import com.esri.sde.sdk.client.SeShapeFilter;
25 import com.esri.sde.sdk.client.SeSqlConstruct;
26 import com.vividsolutions.jts.geom.Geometry;
27 import com.vividsolutions.jts.geom.GeometryFactory;
28 import com.vividsolutions.jts.io.WKTReader;
29
18 30
19 /** 31 /**
20 * @author Tim Englich <tim.englich@intevation.de> 32 * @author Tim Englich <tim.englich@intevation.de>
21 * 33 *
22 */ 34 */
109 /** 121 /**
110 * @see java.sql.Statement#executeQuery(java.lang.String) 122 * @see java.sql.Statement#executeQuery(java.lang.String)
111 */ 123 */
112 public ResultSet executeQuery(String statement) throws SQLException { 124 public ResultSet executeQuery(String statement) throws SQLException {
113 try { 125 try {
114 SeQuery query = new SeQuery(this.connection.getSeConnection()); 126 SeQuery query = null;
115 query.prepareSql(statement); 127 if (statement.toLowerCase().contains("st_astext")){
116 query.execute(); 128
117 return this.handleResultSet(query); 129 String[] values = statement.toLowerCase().split("where", 2);
118 } catch (SeException e) { 130 String where = values.length > 1 ? values[1].trim() : "";
131 values = values[0].split("from", 2);
132 String layerName = values[1].toUpperCase().trim();
133 String[] returnFields = values[0].replaceAll("select", "").trim().split(",");
134 String geometryColumnName = "N/N";
135 for (int i = 0; i < returnFields.length; i++){
136 returnFields[i] = returnFields[i].trim();
137 if (returnFields[i].startsWith("st_astext(")){
138 returnFields[i] = returnFields[i].replaceAll("st_astext", "");
139 returnFields[i] = returnFields[i].substring(1, returnFields[i].length()-1);
140 geometryColumnName = returnFields[i];
141 }
142 }
143 Geometry g = null;
144 int pos = where.indexOf("intersects");
145 if (pos > 0 ){
146 String substr = where.substring(pos);
147 where = where.substring(0,where.lastIndexOf("and")); // TODO auch or unterstützen
148 String intersectsStmt = substr.substring(0, substr.lastIndexOf(")")); // Annahme räumliches Stmt steht am Ende
149 String wkt = intersectsStmt.substring(intersectsStmt.indexOf("\""), intersectsStmt.lastIndexOf("\"")).replace("\"", "").trim();
150 g = new WKTReader().read(wkt);
151
152 }
153 return this.executeQuery(this.connection.getSeConnection(), layerName, geometryColumnName, where, g, returnFields);
154 }else{
155 query = new SeQuery(this.connection.getSeConnection());
156 query.prepareSql(statement);
157 query.execute();
158 return this.handleResultSet(query);
159 }
160
161 } catch (Exception e) {
119 log.error(e,e); 162 log.error(e,e);
120 throw new SQLException(e.getMessage()); 163 throw new SQLException(e.getMessage());
121 } 164 }
122 165
123 166
124 } 167 }
168
169 private ResultSet executeQuery(SeConnection con, String pLayername,
170 String pSpatialColumnName, String pWhere,
171 Geometry g, String[] pReturnFields)
172 throws SQLException {
173 log.debug("executeQuery()");
174 try {
175 // get the layer for querying
176 SeLayer lLayer = new SeLayer(con, pLayername, pSpatialColumnName);
177 SeCoordinateReference cref = lLayer.getCoordRef();
178
179 SeShape shape = new SeShape();
180 shape.setCoordRef(lLayer.getCoordRef());
181 SDEPoint[] lPoints = new ArcSDEUtils().createPoints(g);
182
183 shape.generatePolygon(lPoints.length, 1, null, lPoints);
184 SeShapeFilter filter = new SeShapeFilter(pLayername,
185 pSpatialColumnName, shape, SeFilter.METHOD_AI);
186 SeShapeFilter[] filters = new SeShapeFilter[1];
187 filters[0] = filter;
188
189 SeQuery spatialQuery = null;
190 SeSqlConstruct sqlCons = new SeSqlConstruct(pLayername, pWhere);
191 spatialQuery = new SeQuery(con, pReturnFields, sqlCons);
192 spatialQuery.prepareQuery();
193 /*
194 * Set spatial constraints
195 */
196 spatialQuery.setSpatialConstraints(SeQuery.SE_OPTIMIZE, false,
197 filters);
198 spatialQuery.execute();
199
200 return this.handleResultSet(spatialQuery);
201
202 } catch (Exception e) {
203 log.error(e.getMessage(), e);
204 throw new SQLException(e.getMessage());
205 }
206 }
125 207
126 /** 208 /**
127 * @see java.sql.Statement#executeUpdate(java.lang.String) 209 * @see java.sql.Statement#executeUpdate(java.lang.String)
128 */ 210 */
129 public int executeUpdate(String arg0) throws SQLException { 211 public int executeUpdate(String arg0) throws SQLException {

http://dive4elements.wald.intevation.org