Revision 2910
Added by harris over 18 years ago
SpatialQueryProcessor.java | ||
---|---|---|
59 | 59 |
|
60 | 60 |
public class SpatialQueryProcessor { |
61 | 61 |
|
62 |
private String METACATURL = "http://nebulous.msi.ucsb.edu:9999/knp/metacat"; |
|
62 |
private String METACATURL = |
|
63 |
"http://nebulous.msi.ucsb.edu:9999/knp/metacat"; |
|
63 | 64 |
private File QUERYFILE = null; |
64 |
private String MCUSER = "uid=harris,o=NCEAS,dc=ecoinformatics,dc=org";
|
|
65 |
private String MCUSER = "uid=harris,o=NCEAS,dc=ecoinformatics,dc=org";
|
|
65 | 66 |
private String MCPASS = "yeahRight"; |
66 | 67 |
|
67 | 68 |
|
68 | 69 |
|
69 | 70 |
|
70 |
private DBConnection dbconn;
|
|
71 |
private DBConnection dbconn; |
|
71 | 72 |
|
72 |
private static Logger log = Logger.getLogger(SpatialQueryProcessor.class.getName()); |
|
73 |
private static Logger log = |
|
74 |
Logger.getLogger(SpatialQueryProcessor.class.getName()); |
|
73 | 75 |
|
74 |
/** constructor that uses the defaults **/
|
|
76 |
/** constructor that uses the defaults **/
|
|
75 | 77 |
public SpatialQueryProcessor() { |
76 | 78 |
try { |
77 | 79 |
dbconn = new DBConnection(); |
78 |
} catch ( java.sql.SQLException se) {
|
|
80 |
} catch(java.sql.SQLException se) {
|
|
79 | 81 |
log.fatal("Error connecting to the database ..."); |
80 |
se.getMessage();
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
82 |
se.getMessage(); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
84 | 86 |
/** call this after the processor is done **/ |
85 | 87 |
protected void close() { |
86 | 88 |
try { |
87 | 89 |
dbconn.close(); |
88 |
} catch ( java.sql.SQLException se) {
|
|
90 |
} catch(java.sql.SQLException se) {
|
|
89 | 91 |
log.fatal("Error closing connecting to the database ..."); |
90 |
se.getMessage();
|
|
91 |
}
|
|
92 |
se.getMessage(); |
|
93 |
} |
|
92 | 94 |
} |
93 |
|
|
94 |
public SpatialQueryProcessor(String mcUser, String mcPass, String metacatUrl, |
|
95 |
File queryFile) { |
|
96 |
log.debug("constructor called"); |
|
97 |
this.METACATURL = metacatUrl; |
|
98 |
this.MCPASS = mcPass; |
|
99 |
this.MCUSER = mcUser; |
|
100 |
this.QUERYFILE = queryFile; |
|
101 |
} |
|
102 | 95 |
|
103 |
public String execute() throws IOException { |
|
104 |
FileReader queryFileReader = new FileReader(QUERYFILE); |
|
105 |
String res = runQuery((Reader)queryFileReader); |
|
106 |
return res; |
|
107 |
} |
|
96 |
public SpatialQueryProcessor(String mcUser, String mcPass, |
|
97 |
String metacatUrl, File queryFile) { |
|
98 |
log.debug("constructor called"); |
|
99 |
this.METACATURL = metacatUrl; |
|
100 |
this.MCPASS = mcPass; |
|
101 |
this.MCUSER = mcUser; |
|
102 |
this.QUERYFILE = queryFile; |
|
103 |
} |
|
108 | 104 |
|
109 |
protected MetacatSpatialExtents queryExtentsForDataset() { |
|
105 |
public String execute() throws IOException { |
|
106 |
FileReader queryFileReader = new FileReader(QUERYFILE); |
|
107 |
String res = runQuery((Reader) queryFileReader); |
|
108 |
return res; |
|
109 |
} protected MetacatSpatialDataset queryDatasetByCartesianBounds(float |
|
110 |
_xmin, |
|
111 |
float |
|
112 |
_ymin, |
|
113 |
float |
|
114 |
_xmax, |
|
115 |
float |
|
116 |
_ymax) { |
|
110 | 117 |
|
118 |
MetacatSpatialDataset _data = new MetacatSpatialDataset(); |
|
111 | 119 |
|
112 |
String query = "<?xml version=\"1.0\" ?> <pathquery version=\"1.2\"> " |
|
113 |
+"<querytitle>Untitled-Search-2</querytitle> " |
|
114 |
+"<returndoctype>-//ecoinformatics.org//eml-dataset-2.0.0beta6//EN</returndoctype> " |
|
115 |
+"<returndoctype>-//NCEAS//eml-dataset-2.0//EN</returndoctype> " |
|
116 |
+"<returndoctype>eml://ecoinformatics.org/eml-2.0.0</returndoctype> " |
|
117 |
+"<returnfield>dataset/title</returnfield> " |
|
118 |
+"<returnfield>individualName/surName</returnfield> " |
|
119 |
+"<returnfield>keyword</returnfield><returnfield>westBoundingCoordinate</returnfield>" |
|
120 |
+"<returnfield>eastBoundingCoordinate</returnfield><returnfield>northBoundingCoordinate" |
|
121 |
+"</returnfield><returnfield>southBoundingCoordinate</returnfield>" |
|
122 |
+"<returnfield>westbc</returnfield><returnfield>eastbc</returnfield>" |
|
123 |
+"<returnfield>northbc</returnfield><returnfield>southbc</returnfield>" |
|
124 |
+"<querygroup operator=\"INTERSECT\"><querygroup operator=\"UNION\">" |
|
125 |
+"<querygroup operator=\"UNION\"><queryterm searchmode=\"contains\" casesensitive=\"false\">" |
|
126 |
+"<value>%</value></queryterm></querygroup></querygroup></querygroup></pathquery>"; |
|
127 |
|
|
128 |
StringReader _sr = new StringReader(query); |
|
129 |
String _result = runQuery(_sr); |
|
120 |
// get all docid's |
|
121 |
Vector _docs = queryAllDocids(); |
|
130 | 122 |
|
131 |
log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$: result data: " + _result); |
|
123 |
// for each, see if they fall w/in bounds |
|
124 |
for (int i = 0; i < _docs.size(); i++) { |
|
125 |
float _docMaxY = |
|
126 |
queryNorthBoundingCoordinate((String) _docs.elementAt(i)); |
|
127 |
float _docMinY = |
|
128 |
querySouthBoundingCoordinate((String) _docs.elementAt(i)); |
|
129 |
float _docMaxX = |
|
130 |
queryEastBoundingCoordinate((String) _docs.elementAt(i)); |
|
131 |
float _docMinX = |
|
132 |
queryWestBoundingCoordinate((String) _docs.elementAt(i)); |
|
132 | 133 |
|
133 |
|
|
134 |
return null; |
|
135 |
|
|
136 |
} |
|
137 |
|
|
134 |
// check if inside the bounding box |
|
135 |
if (_docMaxX <= _xmax && _docMaxX >= _xmin) { |
|
136 |
MetacatSpatialDocument _doc = |
|
137 |
new MetacatSpatialDocument((String) _docs.elementAt(i)); |
|
138 |
_doc.setDocid((String) _docs.elementAt(i)); |
|
139 |
// set the extents |
|
140 |
_data.add(_doc); |
|
141 |
} |
|
142 |
} |
|
143 |
return _data; |
|
144 |
} |
|
145 |
|
|
138 | 146 |
/** |
139 |
* returns the north bounding coordinate -- this is a single point
|
|
147 |
* Returns a Vector of all the docids in the xml_nodes tables
|
|
140 | 148 |
*/ |
141 |
private float queryNorthBoundingCoordinate(String docid) { |
|
142 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
143 |
PreparedStatement pstmt = null; |
|
144 |
ResultSet rs = null; |
|
145 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
146 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
147 |
+docid.trim()+"' and nodename like 'north%')+1;"; |
|
148 |
|
|
149 |
try { |
|
150 |
pstmt = dbconn.prepareStatement(query); |
|
151 |
pstmt.execute(); |
|
152 |
rs = pstmt.getResultSet(); |
|
153 |
if ( rs.next() ) |
|
154 |
_coord = rs.getFloat(2); |
|
155 |
rs.close(); |
|
156 |
pstmt.close(); |
|
157 |
} catch (Exception e){ |
|
158 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
159 |
e.printStackTrace(); |
|
149 |
protected Vector queryAllDocids() { |
|
150 |
Vector _docs = new Vector(); |
|
151 |
PreparedStatement pstmt = null; |
|
152 |
ResultSet rs = null; |
|
153 |
String query = "select distinct docid from xml_nodes"; |
|
154 |
|
|
155 |
try { |
|
156 |
pstmt = dbconn.prepareStatement(query); |
|
157 |
pstmt.execute(); |
|
158 |
rs = pstmt.getResultSet(); |
|
159 |
while (rs.next()) { |
|
160 |
String docid = rs.getString(1); |
|
161 |
log.fatal("adding docid: " + docid); |
|
162 |
_docs.add(docid); |
|
160 | 163 |
} |
161 |
return _coord; |
|
164 |
rs.close(); |
|
165 |
pstmt.close(); |
|
162 | 166 |
} |
167 |
catch(Exception e) { |
|
168 |
log.error("Error getting docids from queryAllDocids"); |
|
169 |
e.printStackTrace(); |
|
170 |
} |
|
171 |
return _docs; |
|
172 |
} |
|
163 | 173 |
|
164 | 174 |
|
165 | 175 |
/** |
176 |
* returns the north bounding coordinate -- this is a single point |
|
177 |
*/ |
|
178 |
private float queryNorthBoundingCoordinate(String docid) { |
|
179 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
180 |
PreparedStatement pstmt = null; |
|
181 |
ResultSet rs = null; |
|
182 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
183 |
+ "nodeid =(select nodeid from xml_nodes where docid like '" |
|
184 |
+ docid.trim() + "' and nodename like 'north%')+1;"; |
|
185 |
|
|
186 |
try { |
|
187 |
pstmt = dbconn.prepareStatement(query); |
|
188 |
pstmt.execute(); |
|
189 |
rs = pstmt.getResultSet(); |
|
190 |
if (rs.next()) |
|
191 |
_coord = rs.getFloat(2); |
|
192 |
rs.close(); |
|
193 |
pstmt.close(); |
|
194 |
} |
|
195 |
catch(Exception e) { |
|
196 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
197 |
e.printStackTrace(); |
|
198 |
} |
|
199 |
return _coord; |
|
200 |
} |
|
201 |
|
|
202 |
|
|
203 |
/** |
|
166 | 204 |
* returns the east bounding coordinate -- this is a single point |
167 | 205 |
*/ |
168 |
private float queryEastBoundingCoordinate(String docid) { |
|
169 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
170 |
PreparedStatement pstmt = null; |
|
171 |
ResultSet rs = null; |
|
172 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
173 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
174 |
+docid.trim()+"' and nodename like 'east%')+1;"; |
|
175 |
|
|
176 |
try { |
|
177 |
pstmt = dbconn.prepareStatement(query); |
|
178 |
pstmt.execute(); |
|
179 |
rs = pstmt.getResultSet(); |
|
180 |
if ( rs.next() ) |
|
181 |
_coord = rs.getFloat(2); |
|
182 |
rs.close(); |
|
183 |
pstmt.close(); |
|
184 |
} catch (Exception e){ |
|
185 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
186 |
e.printStackTrace(); |
|
187 |
} |
|
188 |
return _coord; |
|
206 |
private float queryEastBoundingCoordinate(String docid) { |
|
207 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
208 |
PreparedStatement pstmt = null; |
|
209 |
ResultSet rs = null; |
|
210 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
211 |
+ "nodeid =(select nodeid from xml_nodes where docid like '" |
|
212 |
+ docid.trim() + "' and nodename like 'east%')+1;"; |
|
213 |
|
|
214 |
try { |
|
215 |
pstmt = dbconn.prepareStatement(query); |
|
216 |
pstmt.execute(); |
|
217 |
rs = pstmt.getResultSet(); |
|
218 |
if (rs.next()) |
|
219 |
_coord = rs.getFloat(2); |
|
220 |
rs.close(); |
|
221 |
pstmt.close(); |
|
189 | 222 |
} |
223 |
catch(Exception e) { |
|
224 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
225 |
e.printStackTrace(); |
|
226 |
} |
|
227 |
return _coord; |
|
228 |
} |
|
190 | 229 |
|
191 | 230 |
|
192 | 231 |
/** |
193 | 232 |
* returns the south bounding coordinate -- this is a single point |
194 | 233 |
*/ |
195 |
private float querySouthBoundingCoordinate(String docid) { |
|
196 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
197 |
PreparedStatement pstmt = null; |
|
198 |
ResultSet rs = null; |
|
199 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
200 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
201 |
+docid.trim()+"' and nodename like 'south%')+1;"; |
|
202 |
|
|
203 |
try { |
|
204 |
pstmt = dbconn.prepareStatement(query); |
|
205 |
pstmt.execute(); |
|
206 |
rs = pstmt.getResultSet(); |
|
207 |
if ( rs.next() ) |
|
208 |
_coord = rs.getFloat(2); |
|
209 |
rs.close(); |
|
210 |
pstmt.close(); |
|
211 |
} catch (Exception e){ |
|
212 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
213 |
e.printStackTrace(); |
|
214 |
} |
|
215 |
return _coord; |
|
234 |
private float querySouthBoundingCoordinate(String docid) { |
|
235 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
236 |
PreparedStatement pstmt = null; |
|
237 |
ResultSet rs = null; |
|
238 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
239 |
+ "nodeid =(select nodeid from xml_nodes where docid like '" |
|
240 |
+ docid.trim() + "' and nodename like 'south%')+1;"; |
|
241 |
|
|
242 |
try { |
|
243 |
pstmt = dbconn.prepareStatement(query); |
|
244 |
pstmt.execute(); |
|
245 |
rs = pstmt.getResultSet(); |
|
246 |
if (rs.next()) |
|
247 |
_coord = rs.getFloat(2); |
|
248 |
rs.close(); |
|
249 |
pstmt.close(); |
|
216 | 250 |
} |
251 |
catch(Exception e) { |
|
252 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
253 |
e.printStackTrace(); |
|
254 |
} |
|
255 |
return _coord; |
|
256 |
} |
|
217 | 257 |
|
218 | 258 |
|
219 | 259 |
|
220 | 260 |
/** |
221 | 261 |
* returns the west bounding coordinate -- this is a single point |
222 | 262 |
*/ |
223 |
private float queryWestBoundingCoordinate(String docid) { |
|
224 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
225 |
PreparedStatement pstmt = null; |
|
226 |
ResultSet rs = null; |
|
227 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
228 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
229 |
+docid.trim()+"' and nodename like 'west%')+1;"; |
|
230 |
|
|
231 |
try { |
|
232 |
pstmt = dbconn.prepareStatement(query); |
|
233 |
pstmt.execute(); |
|
234 |
rs = pstmt.getResultSet(); |
|
235 |
if ( rs.next() ) |
|
236 |
_coord = rs.getFloat(2); |
|
237 |
rs.close(); |
|
238 |
pstmt.close(); |
|
239 |
} catch (Exception e){ |
|
240 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
241 |
e.printStackTrace(); |
|
242 |
} |
|
243 |
return _coord; |
|
263 |
private float queryWestBoundingCoordinate(String docid) { |
|
264 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
265 |
PreparedStatement pstmt = null; |
|
266 |
ResultSet rs = null; |
|
267 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
268 |
+ "nodeid =(select nodeid from xml_nodes where docid like '" |
|
269 |
+ docid.trim() + "' and nodename like 'west%')+1;"; |
|
270 |
|
|
271 |
try { |
|
272 |
pstmt = dbconn.prepareStatement(query); |
|
273 |
pstmt.execute(); |
|
274 |
rs = pstmt.getResultSet(); |
|
275 |
if (rs.next()) |
|
276 |
_coord = rs.getFloat(2); |
|
277 |
rs.close(); |
|
278 |
pstmt.close(); |
|
244 | 279 |
} |
280 |
catch(Exception e) { |
|
281 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
282 |
e.printStackTrace(); |
|
283 |
} |
|
284 |
return _coord; |
|
285 |
} |
|
245 | 286 |
|
246 | 287 |
|
247 | 288 |
|
... | ... | |
252 | 293 |
* @param docid -- the docid for which the extents are to be queried |
253 | 294 |
* @returns spatialextents -- the MetcataSpatialExtents object for this query |
254 | 295 |
*/ |
255 |
protected MetacatSpatialExtents queryExtentsByDocid(String docid) { |
|
256 |
MetacatSpatialExtents _extents = new MetacatSpatialExtents(); |
|
257 |
|
|
258 |
float _northbc = queryNorthBoundingCoordinate(docid); |
|
259 |
float _eastbc = queryEastBoundingCoordinate(docid); |
|
260 |
float _southbc = querySouthBoundingCoordinate(docid); |
|
261 |
float _westbc = queryWestBoundingCoordinate(docid); |
|
296 |
protected MetacatSpatialExtents queryExtentsByDocid(String docid) { |
|
297 |
MetacatSpatialExtents _extents = new MetacatSpatialExtents(); |
|
262 | 298 |
|
263 |
if (( _northbc == _southbc) && (_eastbc == _westbc) ) { |
|
264 |
_extents.setIsPoint(true); } else { _extents.setIsPoint(false); } |
|
299 |
float _northbc = queryNorthBoundingCoordinate(docid); |
|
300 |
float _eastbc = queryEastBoundingCoordinate(docid); |
|
301 |
float _southbc = querySouthBoundingCoordinate(docid); |
|
302 |
float _westbc = queryWestBoundingCoordinate(docid); |
|
265 | 303 |
|
266 |
_extents.setYMin( (_southbc < _northbc? _southbc : _northbc) ); |
|
267 |
_extents.setYMax( (_southbc > _northbc? _southbc : _northbc) ); |
|
268 |
_extents.setXMin( (_westbc < _eastbc? _westbc : _eastbc) ); |
|
269 |
_extents.setXMax( (_westbc > _eastbc? _westbc : _eastbc) ); |
|
270 |
|
|
271 |
_extents.setDocid(docid); |
|
272 |
|
|
273 |
log.warn("**************************** > " + _extents.toString() ); |
|
274 |
|
|
275 |
|
|
276 |
return _extents; |
|
304 |
if ((_northbc == _southbc) && (_eastbc == _westbc)) { |
|
305 |
_extents.setIsPoint(true); |
|
306 |
} else { |
|
307 |
_extents.setIsPoint(false); |
|
277 | 308 |
} |
278 | 309 |
|
279 |
|
|
280 |
/** |
|
310 |
_extents.setYMin((_southbc < _northbc ? _southbc : _northbc)); |
|
311 |
_extents.setYMax((_southbc > _northbc ? _southbc : _northbc)); |
|
312 |
_extents.setXMin((_westbc < _eastbc ? _westbc : _eastbc)); |
|
313 |
_extents.setXMax((_westbc > _eastbc ? _westbc : _eastbc)); |
|
314 |
|
|
315 |
_extents.setDocid(docid); |
|
316 |
|
|
317 |
return _extents; |
|
318 |
} |
|
319 |
|
|
320 |
|
|
321 |
/** |
|
281 | 322 |
* Query the Metacat Server to get a list of documents, |
282 | 323 |
* if it doesn't exist there, from the backend storage server. |
283 | 324 |
* |
... | ... | |
286 | 327 |
* @throws QueryFailedException when any thing happens to prevent a query |
287 | 328 |
* returns |
288 | 329 |
*/ |
289 |
private String runQuery(Reader query) |
|
290 |
{ |
|
330 |
private String runQuery(Reader query) { |
|
291 | 331 |
String document = null; |
292 | 332 |
|
293 | 333 |
Reader xdoc = null; |
... | ... | |
299 | 339 |
log.debug("metacat instance: " + m); |
300 | 340 |
xdoc = m.query(query); |
301 | 341 |
document = IOUtil.getAsString(xdoc, true); |
302 |
m.logout();
|
|
342 |
m.logout();
|
|
303 | 343 |
if (document == null) { |
304 | 344 |
throw new QueryFailedException("Document String Was null"); |
305 | 345 |
} |
Also available in: Unified diff
Implementation of the rubber-bad query that PMARK has implemented through the UI.