root / branches / v2_0_0_prep / libraries / org.gvsig.arcims / src / org / gvsig / remoteclient / arcims / ArcImsProtFeatureHandler.java @ 32538
History | View | Annotate | Download (30.5 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2010 Prodevelop S.L. main development
|
26 |
* http://www.prodevelop.es
|
27 |
*/
|
28 |
package org.gvsig.remoteclient.arcims; |
29 |
|
30 |
import java.awt.geom.Rectangle2D; |
31 |
import java.io.BufferedReader; |
32 |
import java.io.File; |
33 |
import java.io.FileInputStream; |
34 |
import java.io.FileNotFoundException; |
35 |
import java.io.FileReader; |
36 |
import java.io.IOException; |
37 |
import java.io.InputStream; |
38 |
import java.io.InputStreamReader; |
39 |
import java.io.Reader; |
40 |
import java.io.UnsupportedEncodingException; |
41 |
import java.net.MalformedURLException; |
42 |
import java.net.URL; |
43 |
import java.util.ArrayList; |
44 |
import java.util.Date; |
45 |
import java.util.Iterator; |
46 |
import java.util.List; |
47 |
import java.util.StringTokenizer; |
48 |
import java.util.Vector; |
49 |
|
50 |
import org.exolab.castor.xml.dtd.parser.ParseException; |
51 |
import org.gvsig.fmap.geom.Geometry; |
52 |
import org.gvsig.fmap.geom.GeometryLocator; |
53 |
import org.gvsig.fmap.geom.GeometryManager; |
54 |
import org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D; |
55 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
56 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
57 |
import org.gvsig.fmap.geom.primitive.Point; |
58 |
import org.gvsig.remoteclient.arcims.exceptions.ArcImsException; |
59 |
import org.gvsig.remoteclient.arcims.utils.ArcImsDownloadUtils; |
60 |
import org.gvsig.remoteclient.arcims.utils.ArcImsFeature; |
61 |
import org.gvsig.remoteclient.arcims.utils.FieldInformation; |
62 |
import org.gvsig.remoteclient.arcims.utils.GetFeaturesTags; |
63 |
import org.gvsig.remoteclient.arcims.utils.ServiceInfoTags; |
64 |
import org.gvsig.remoteclient.arcims.utils.ServiceInformation; |
65 |
import org.gvsig.remoteclient.arcims.utils.ServiceInformationLayerFeatures; |
66 |
import org.gvsig.remoteclient.utils.BoundaryBox; |
67 |
import org.kxml2.io.KXmlParser; |
68 |
import org.slf4j.Logger; |
69 |
import org.slf4j.LoggerFactory; |
70 |
import org.xmlpull.v1.XmlPullParserException; |
71 |
|
72 |
/**
|
73 |
* @author jsanz
|
74 |
*
|
75 |
*/
|
76 |
public class ArcImsProtFeatureHandler extends ArcImsProtocolHandler { |
77 |
private static boolean hasMore; |
78 |
private static int featCount; |
79 |
private static Logger logger = LoggerFactory |
80 |
.getLogger(ArcImsProtFeatureHandler.class.getName()); |
81 |
|
82 |
/**
|
83 |
* Method to retrieve an ArrayList of features from an ArcIMS FeatureService
|
84 |
*
|
85 |
* @param status
|
86 |
* @return ArrayList of IFeatures with geometries and Id's to attribute
|
87 |
* table
|
88 |
* @throws ArcImsException
|
89 |
*/
|
90 |
public List<List<ArcImsFeature>> getMap(ArcImsVectStatus status) |
91 |
throws ArcImsException {
|
92 |
logger.info("Start getMap");
|
93 |
|
94 |
List<List<ArcImsFeature>> array = new ArrayList<List<ArcImsFeature>>(); |
95 |
|
96 |
try {
|
97 |
/*
|
98 |
* Build the request
|
99 |
*/
|
100 |
URL url = new URL(buildCapabilitiesRequest(status)); |
101 |
|
102 |
hasMore = true;
|
103 |
featCount = 1;
|
104 |
|
105 |
while (hasMore) {
|
106 |
/*
|
107 |
* Build the proper ArcXML
|
108 |
*/
|
109 |
String request = ArcXMLFeatures.getFeatureLayerRequest(status,
|
110 |
featCount); |
111 |
|
112 |
if (status.verbose) {
|
113 |
System.err.println(request);
|
114 |
} |
115 |
|
116 |
/*
|
117 |
* Request the reader
|
118 |
*/
|
119 |
logger.info("Start features downloading");
|
120 |
|
121 |
File f = ArcImsDownloadUtils.doRequestPost(url, request,
|
122 |
"getFeatures.xml");
|
123 |
logger.info("End features downloading");
|
124 |
|
125 |
/*
|
126 |
* Parse response and add geometries to the general Array
|
127 |
*/
|
128 |
String layerId = (String) status.getLayersIdsSelected().get(0); |
129 |
|
130 |
// Deal with UTF-8
|
131 |
Reader reader = null; |
132 |
FileInputStream fis = new FileInputStream(f); |
133 |
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); |
134 |
BufferedReader br = new BufferedReader(isr); |
135 |
reader = br; |
136 |
|
137 |
logger.debug("Start features with attributes parsing");
|
138 |
List<ArcImsFeature> feats = this.getFeatures(reader, status |
139 |
.getServiceInfo(), layerId, status.getSubfields(), 1);
|
140 |
array.add(feats); |
141 |
logger.debug("End features with attributes parsing");
|
142 |
} |
143 |
|
144 |
hasMore = true;
|
145 |
featCount = 1;
|
146 |
} catch (MalformedURLException e) { |
147 |
logger.error(e.getMessage(), e); |
148 |
throw new ArcImsException("arcims_server_error"); |
149 |
} catch (FileNotFoundException e) { |
150 |
logger.error(e.getMessage(), e); |
151 |
throw new ArcImsException("arcims_server_error"); |
152 |
} catch (UnsupportedEncodingException e) { |
153 |
logger.error(e.getMessage(), e); |
154 |
} |
155 |
|
156 |
logger.debug("End getMap");
|
157 |
|
158 |
return array;
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* Parse the XML data retrieved from ArcIMS GET_FEATURES request to get a
|
163 |
* set of Features if withGeometries is true or an ArrayList of Value[] if
|
164 |
* this boolean is false
|
165 |
*
|
166 |
* @param Reader
|
167 |
* Stream to parse
|
168 |
* @param si
|
169 |
* ServiceInformation with metadata
|
170 |
* @param layerId
|
171 |
* Layer retrieved
|
172 |
* @param subfields
|
173 |
* Subfields retrieved
|
174 |
* @param withGeometries
|
175 |
* Will geometries be parsed?: 0=no 1=yes 2=evelopes
|
176 |
* @return ArrayList of IFeatures or Value[]
|
177 |
* @throws ArcImsException
|
178 |
*/
|
179 |
private List<ArcImsFeature> getFeatures(Reader lector, |
180 |
ServiceInformation si, String layerId, String[] subfields, |
181 |
int withGeometries) throws ArcImsException { |
182 |
|
183 |
// fill features map
|
184 |
FieldInformation fi = null;
|
185 |
ServiceInformationLayerFeatures sil = (ServiceInformationLayerFeatures) si |
186 |
.getLayerById(layerId); |
187 |
|
188 |
/*
|
189 |
* Prepare a Value array to store every FEATURE tag information
|
190 |
*/
|
191 |
Vector<FieldInformation> fieldsInfo = sil.getFieldsInfo();
|
192 |
|
193 |
// If flag == true, throw an Exception (no layerId found)
|
194 |
if (fieldsInfo == null) { |
195 |
throw new ArcImsException("arcims_no_features"); |
196 |
} |
197 |
|
198 |
// We will store names and types in a hashmap
|
199 |
List<FieldInformation> fieldsInfoA = new ArrayList<FieldInformation>(); |
200 |
|
201 |
/*
|
202 |
* If subfields[0] is equal to #ALL we add the entire vector into the
|
203 |
* Hasmap
|
204 |
*/
|
205 |
if (subfields[0].equals("#ALL#")) { |
206 |
for (int i = 0; i < fieldsInfo.size(); i++) { |
207 |
fi = fieldsInfo.get(i); |
208 |
fieldsInfoA.add(fi); |
209 |
} |
210 |
} else {
|
211 |
for (int i = 0; i < subfields.length; i++) { |
212 |
fi = null;
|
213 |
fi = sil.getFieldInformation(subfields[i]); |
214 |
|
215 |
if (fi != null) { |
216 |
fieldsInfoA.add(fi); |
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
/*
|
222 |
* Get the separators
|
223 |
*/
|
224 |
String delCoords = si.getSeparators().getCs();
|
225 |
String delTuple = si.getSeparators().getTs();
|
226 |
char delDec = si.getSeparators().getDs();
|
227 |
|
228 |
/*
|
229 |
* ArrayList with features
|
230 |
*/
|
231 |
List<ArcImsFeature> features = new ArrayList<ArcImsFeature>(); |
232 |
|
233 |
/*
|
234 |
* Start parsing
|
235 |
*/
|
236 |
int tag;
|
237 |
KXmlParser kxmlParser = new KXmlParser();
|
238 |
|
239 |
Geometry geom = null;
|
240 |
Object[] values = new Object[fieldsInfoA.size()]; |
241 |
// Value[] values = new Value[fieldsInfoA.size()];
|
242 |
int[] position = new int[fieldsInfoA.size()]; |
243 |
|
244 |
// Initialize position array
|
245 |
for (int i = 0; i < position.length; i++) |
246 |
position[i] = -1;
|
247 |
|
248 |
// long timeFeat = 0;
|
249 |
// long timeGeom = 0;
|
250 |
// long timeTemp = 0;
|
251 |
try {
|
252 |
kxmlParser.setInput(lector); |
253 |
kxmlParser.nextTag(); |
254 |
|
255 |
if (kxmlParser.getEventType() != KXmlParser.END_DOCUMENT) {
|
256 |
kxmlParser.require(KXmlParser.START_TAG, null,
|
257 |
ServiceInfoTags.tARCXML); |
258 |
tag = kxmlParser.nextTag(); |
259 |
|
260 |
while (tag != KXmlParser.END_DOCUMENT) {
|
261 |
|
262 |
ArcImsFeature afeat = new ArcImsFeature();
|
263 |
|
264 |
switch (tag) {
|
265 |
case KXmlParser.START_TAG:
|
266 |
|
267 |
if (kxmlParser.getName().compareTo(
|
268 |
GetFeaturesTags.FEATURE) == 0) {
|
269 |
/*
|
270 |
* parse FEATURES' tag
|
271 |
*/
|
272 |
|
273 |
// FIRST ENVELOPE TAG (We hope that ENVELOPE will
|
274 |
// ALLWAYS be returned at first...)
|
275 |
if (withGeometries == 2) { |
276 |
// timeTemp = System.currentTimeMillis();
|
277 |
geom = parseEnvelopeFromFeatureTag(kxmlParser, |
278 |
delDec); |
279 |
afeat.putGeometry(geom); |
280 |
} |
281 |
|
282 |
// SECOND FIELDS TAG
|
283 |
// timeTemp = System.currentTimeMillis();
|
284 |
afeat = parseValuesFromFeatureTag(kxmlParser, // the
|
285 |
// kxml
|
286 |
// parser
|
287 |
fieldsInfoA, // Hashmap with names and
|
288 |
// FieldInformation objects)
|
289 |
delDec, // the separators
|
290 |
position, // the relative positions of
|
291 |
// returned values
|
292 |
afeat |
293 |
); |
294 |
|
295 |
// timeFeat += System.currentTimeMillis() -
|
296 |
// timeTemp;
|
297 |
|
298 |
// If geometries are neede we add to the ArrayList
|
299 |
// the Ifeature
|
300 |
if (withGeometries == 1) { |
301 |
// THIRD GEOMETRIES
|
302 |
// timeTemp = System.currentTimeMillis();
|
303 |
geom = parseGeomFromFeatureTag(kxmlParser, // the
|
304 |
// kxml
|
305 |
// parser
|
306 |
delTuple, delCoords, delDec // the
|
307 |
// separators
|
308 |
|
309 |
); |
310 |
afeat.putGeometry(geom); |
311 |
|
312 |
// timeGeom += System.currentTimeMillis() -
|
313 |
// timeTemp;
|
314 |
} |
315 |
|
316 |
// // If some geometry is created, we add a IFeature
|
317 |
// // into features ArrayList
|
318 |
// if (withGeometries > 0) {
|
319 |
// features.add(new ArcImsFeature(geom, values));
|
320 |
// }
|
321 |
// // Else we only need the Value[] array
|
322 |
// else {
|
323 |
// features.add(new ArcImsFeature(values));
|
324 |
// }
|
325 |
} else if (kxmlParser.getName().compareTo( |
326 |
GetFeaturesTags.FEATURECOUNT) == 0) {
|
327 |
String value = new String(); |
328 |
|
329 |
// Get the feature count
|
330 |
value = kxmlParser.getAttributeValue("",
|
331 |
GetFeaturesTags.COUNT); |
332 |
|
333 |
Integer intFeatCount = new Integer(value); |
334 |
featCount += intFeatCount.intValue(); |
335 |
|
336 |
// Get the hasmore boolean
|
337 |
value = kxmlParser.getAttributeValue("",
|
338 |
GetFeaturesTags.HASMORE); |
339 |
|
340 |
Boolean boHasMore = new Boolean(value); |
341 |
hasMore = boHasMore.booleanValue(); |
342 |
} else if (kxmlParser.getName().compareTo( |
343 |
ServiceInfoTags.tERROR) == 0) {
|
344 |
logger.error("Error parsing GET_FEATURES:\r\n"
|
345 |
+ kxmlParser.nextText()); |
346 |
throw new ArcImsException("arcims_server_error"); |
347 |
} |
348 |
|
349 |
break;
|
350 |
|
351 |
case KXmlParser.END_TAG:
|
352 |
break;
|
353 |
|
354 |
case KXmlParser.TEXT:
|
355 |
break;
|
356 |
} |
357 |
if(afeat != null){ |
358 |
features.add(afeat); |
359 |
} |
360 |
tag = kxmlParser.next(); |
361 |
} |
362 |
|
363 |
kxmlParser.require(KXmlParser.END_DOCUMENT, null, null); |
364 |
} |
365 |
} |
366 |
|
367 |
catch (XmlPullParserException parser_ex) {
|
368 |
logger.error(parser_ex.getMessage(), parser_ex); |
369 |
throw new ArcImsException("arcims_no_features"); |
370 |
} catch (IOException ioe) { |
371 |
logger.error(ioe.getMessage(), ioe); |
372 |
throw new ArcImsException("arcims_no_features"); |
373 |
} catch (CreateGeometryException e) {
|
374 |
logger.error(e.getMessage(), e); |
375 |
throw new ArcImsException("arcims_no_features"); |
376 |
} |
377 |
|
378 |
return features;
|
379 |
|
380 |
} |
381 |
|
382 |
/**
|
383 |
* Returns an IGeometry with a line between corners of an ENVELOPE
|
384 |
*
|
385 |
* @param parser
|
386 |
* @param delDec
|
387 |
* @return
|
388 |
* @throws XmlPullParserException
|
389 |
* @throws IOException
|
390 |
*/
|
391 |
private Geometry parseEnvelopeFromFeatureTag(KXmlParser parser, char delDec) |
392 |
throws XmlPullParserException, IOException { |
393 |
int currentTag;
|
394 |
boolean end = false; |
395 |
|
396 |
BoundaryBox bb = null;
|
397 |
|
398 |
// parser.require(KXmlParser.START_TAG,null,GetFeaturesTags.FIELDS);
|
399 |
currentTag = parser.next(); |
400 |
|
401 |
while (!end) {
|
402 |
switch (currentTag) {
|
403 |
case KXmlParser.START_TAG:
|
404 |
|
405 |
if (parser.getName().equals(ServiceInfoTags.tENVELOPE)) {
|
406 |
bb = super.parseEnvelope(parser, delDec);
|
407 |
end = true;
|
408 |
} |
409 |
|
410 |
break;
|
411 |
|
412 |
case KXmlParser.END_TAG:
|
413 |
|
414 |
if (parser.getName().equals(GetFeaturesTags.FIELDS)) {
|
415 |
end = true;
|
416 |
} |
417 |
|
418 |
break;
|
419 |
|
420 |
case KXmlParser.TEXT:
|
421 |
break;
|
422 |
} |
423 |
|
424 |
if (!end) {
|
425 |
currentTag = parser.next(); |
426 |
} |
427 |
} |
428 |
|
429 |
// If BoundaryBox object has been created, we can convert it into a
|
430 |
// IGeometry (polyline).
|
431 |
if (bb != null) { |
432 |
GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD);
|
433 |
|
434 |
polyline.moveTo(bb.getXmin(), bb.getYmin()); |
435 |
polyline.lineTo(bb.getXmax(), bb.getYmax()); |
436 |
|
437 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
438 |
Geometry curve = null;
|
439 |
try {
|
440 |
curve = geomManager.createCurve(polyline, |
441 |
Geometry.SUBTYPES.GEOM2D); |
442 |
} catch (CreateGeometryException e) {
|
443 |
logger.error("Error creating curve", e);
|
444 |
return null; |
445 |
} |
446 |
return curve;
|
447 |
} else {
|
448 |
return null; |
449 |
} |
450 |
|
451 |
} |
452 |
|
453 |
/**
|
454 |
* @param kxmlParser
|
455 |
* @param fieldsInfoH
|
456 |
* @param delDec
|
457 |
* @param position
|
458 |
* @return
|
459 |
* @throws IOException
|
460 |
* @throws XmlPullParserException
|
461 |
* @throws IOException
|
462 |
* @throws XmlPullParserException
|
463 |
* @throws ArcImsException
|
464 |
* @throws ArcImsException
|
465 |
* @throws ParseException
|
466 |
*/
|
467 |
private ArcImsFeature parseValuesFromFeatureTag(KXmlParser parser,
|
468 |
List<FieldInformation> fieldsInfoA, char delDec, int[] position, ArcImsFeature afeat ) |
469 |
throws XmlPullParserException, IOException, ArcImsException { |
470 |
|
471 |
int currentTag;
|
472 |
boolean end = false; |
473 |
int f = 0; |
474 |
String att;
|
475 |
String text;
|
476 |
int type;
|
477 |
|
478 |
//Object[] values = new Object[fieldsInfoA.size()];
|
479 |
|
480 |
currentTag = parser.next(); |
481 |
|
482 |
while (!end) {
|
483 |
switch (currentTag) {
|
484 |
case KXmlParser.START_TAG:
|
485 |
|
486 |
if (parser.getName().equals(GetFeaturesTags.FIELD)) {
|
487 |
att = parser.getAttributeValue("", GetFeaturesTags.NAME);
|
488 |
text = parser.getAttributeValue("", GetFeaturesTags.VALUE);
|
489 |
|
490 |
/*
|
491 |
* As NAME is known, we need to get the position of that
|
492 |
* attribute into the ArrayList of fieldInformation objects
|
493 |
*/
|
494 |
if (position[f] == -1) { |
495 |
position[f] = getPosition(fieldsInfoA.iterator(), att); |
496 |
} |
497 |
|
498 |
if (position[f] == -1) { |
499 |
logger.error("Attribute not found at Metadata");
|
500 |
throw new ArcImsException( |
501 |
"Attribute not found at Metadata");
|
502 |
} |
503 |
|
504 |
// This way we can create a NullValue
|
505 |
if (text.equals("")) { |
506 |
text = null;
|
507 |
} |
508 |
|
509 |
/*
|
510 |
* At this point we know wat FieldInfo of the ArrayList we
|
511 |
* have to retrieve
|
512 |
*/
|
513 |
type = ((FieldInformation) fieldsInfoA.get(position[f])) |
514 |
.getType(); |
515 |
|
516 |
// Add the Value into the correct position
|
517 |
if (text == null) { |
518 |
String fieldName = fieldsInfoA.get(position[f]).getName();
|
519 |
afeat.put(fieldName,null);
|
520 |
} else {
|
521 |
String fieldName = fieldsInfoA.get(position[f]).getName();
|
522 |
switch (type) {
|
523 |
case FieldInformation.BOOLEAN:
|
524 |
afeat.put(fieldName,new Boolean(text)); |
525 |
f++; |
526 |
break;
|
527 |
case FieldInformation.SHAPE:
|
528 |
case FieldInformation.STRING:
|
529 |
afeat.put(fieldName, new String(text)); |
530 |
f++; |
531 |
break;
|
532 |
case FieldInformation.DATE:
|
533 |
afeat.put(fieldName, new Date(Long.parseLong(text))); |
534 |
f++; |
535 |
break;
|
536 |
case FieldInformation.FLOAT:
|
537 |
String textfloat = text.replace(delDec, '.'); |
538 |
afeat.put(fieldName, new Float(textfloat)); |
539 |
f++; |
540 |
break;
|
541 |
case FieldInformation.DOUBLE:
|
542 |
String textdouble = text.replace(delDec, '.'); |
543 |
afeat.put(fieldName, new Double(textdouble)); |
544 |
f++; |
545 |
break;
|
546 |
case FieldInformation.SMALLINT:
|
547 |
afeat.put(fieldName, new Short(text)); |
548 |
f++; |
549 |
break;
|
550 |
case FieldInformation.BIGINT:
|
551 |
afeat.put(fieldName, new Long(text)); |
552 |
f++; |
553 |
break;
|
554 |
case FieldInformation.ID:
|
555 |
case FieldInformation.INTEGER:
|
556 |
afeat.put(fieldName, new Integer(text)); |
557 |
f++; |
558 |
break;
|
559 |
default:
|
560 |
afeat.put(fieldName, new String(text)); |
561 |
f++; |
562 |
} |
563 |
} |
564 |
} |
565 |
|
566 |
break;
|
567 |
|
568 |
case KXmlParser.END_TAG:
|
569 |
|
570 |
if (parser.getName().equals(GetFeaturesTags.FIELDS)) {
|
571 |
end = true;
|
572 |
} |
573 |
|
574 |
break;
|
575 |
|
576 |
case KXmlParser.TEXT:
|
577 |
break;
|
578 |
} |
579 |
|
580 |
if (!end) {
|
581 |
currentTag = parser.next(); |
582 |
} |
583 |
} |
584 |
|
585 |
return afeat;
|
586 |
|
587 |
} |
588 |
|
589 |
/**
|
590 |
* Method to get the position of an attribute into the subfields array
|
591 |
*
|
592 |
* @param fieldsInfoA
|
593 |
* @param att
|
594 |
* @return
|
595 |
*/
|
596 |
private int getPosition(Iterator it, String att) { |
597 |
int res = 0; |
598 |
FieldInformation fi; |
599 |
|
600 |
while (it.hasNext()) {
|
601 |
fi = (FieldInformation) it.next(); |
602 |
|
603 |
if (fi.getName().equals(att)) {
|
604 |
return res;
|
605 |
} |
606 |
|
607 |
res++; |
608 |
} |
609 |
|
610 |
// If no integer has returned at this point we return a -1
|
611 |
return -1; |
612 |
} |
613 |
|
614 |
/**
|
615 |
* Parses the Feature Tag to get a IFeature
|
616 |
*
|
617 |
* @param KxmlParser
|
618 |
* with the FEATURE tag
|
619 |
* @param Value
|
620 |
* array with the correct subtypes to store FIELDS data
|
621 |
* @param String
|
622 |
* array with the correct fields names to retrieve
|
623 |
* @param String
|
624 |
* with the tuple separator
|
625 |
* @param String
|
626 |
* with the coordinates separator
|
627 |
* @param Char
|
628 |
* with the decimal separator
|
629 |
* @return @see com.hardcode.gdbms.engine.values.Value.IFeature
|
630 |
* @throws IOException
|
631 |
* @throws XmlPullParserException
|
632 |
* @throws IOException
|
633 |
* @throws XmlPullParserException
|
634 |
* @throws CreateGeometryException
|
635 |
* @throws ArcImsException
|
636 |
* @throws CreateGeometryException
|
637 |
* @throws ArcImsException
|
638 |
*/
|
639 |
private Geometry parseGeomFromFeatureTag(KXmlParser parser,
|
640 |
String delTuple, String delCoords, char delDec) |
641 |
throws XmlPullParserException, IOException, |
642 |
CreateGeometryException, ArcImsException { |
643 |
|
644 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
645 |
int currentTag;
|
646 |
boolean end = false; |
647 |
Geometry geom = null;
|
648 |
currentTag = parser.next(); |
649 |
|
650 |
while (!end) {
|
651 |
String featType = parser.getName();
|
652 |
|
653 |
switch (currentTag) {
|
654 |
case KXmlParser.START_TAG:
|
655 |
// //////////////////////////
|
656 |
// geometry type MULTIPOINT
|
657 |
// //////////////////////////
|
658 |
if (featType.equals(GetFeaturesTags.MULTIPOINT)) {
|
659 |
// Go to COORDS tag
|
660 |
parser.nextTag(); |
661 |
|
662 |
if (parser.getName().equals(GetFeaturesTags.COORDS)) {
|
663 |
// Create a tokenizer with the tuples
|
664 |
String strPoints = parser.nextText();
|
665 |
|
666 |
// Get the points from COORDS string
|
667 |
List<Point> points = parseCoords(strPoints, delTuple, |
668 |
delCoords, delDec); |
669 |
|
670 |
geom = geomManager.create(Geometry.TYPES.MULTIPOINT, |
671 |
Geometry.SUBTYPES.GEOM2D); |
672 |
MultiPoint2D mpoint = (MultiPoint2D) geom; |
673 |
for (Point point : points) { |
674 |
mpoint.addPoint(point); |
675 |
} |
676 |
} else {
|
677 |
logger.error("Error parsing MULTIPOINT tag");
|
678 |
throw new ArcImsException("arcims_features_error"); |
679 |
} |
680 |
} |
681 |
// ////////////////////////////////////////////
|
682 |
// POLYLINE or POLYGON
|
683 |
// ////////////////////////////////////////////
|
684 |
else if (featType.equals(GetFeaturesTags.POLYLINE) |
685 |
|| featType.equals(GetFeaturesTags.POLYGON)) { |
686 |
// Parse while parser doesn't found </FEATURES> tag
|
687 |
// The GeneralPath to store the different paths
|
688 |
GeneralPathX polyline = new GeneralPathX(
|
689 |
GeneralPathX.WIND_EVEN_ODD); |
690 |
boolean endpol = false; |
691 |
|
692 |
while (!endpol) {
|
693 |
switch (currentTag) {
|
694 |
case KXmlParser.START_TAG:
|
695 |
|
696 |
if (parser.getName().equals(GetFeaturesTags.COORDS)) {
|
697 |
// Create a tokenizer with the tuples
|
698 |
String strPoints = parser.nextText();
|
699 |
|
700 |
// Get the points (Point2D) from COORDS string
|
701 |
List<Point> points = parseCoords(strPoints, |
702 |
delTuple, delCoords, delDec); |
703 |
|
704 |
Iterator<Point> it = points.iterator(); |
705 |
Point point;
|
706 |
|
707 |
// First we MOVE to the first point of the path
|
708 |
point = (Point) it.next();
|
709 |
polyline.moveTo(point.getX(), point.getY()); |
710 |
|
711 |
// And now we can LINE to the rest of the points
|
712 |
// of the path
|
713 |
while (it.hasNext()) {
|
714 |
point = (Point) it.next();
|
715 |
polyline.lineTo(point.getX(), point.getY()); |
716 |
} |
717 |
} |
718 |
|
719 |
break;
|
720 |
|
721 |
case KXmlParser.END_TAG:
|
722 |
|
723 |
if (parser.getName().equals(featType)) {
|
724 |
endpol = true;
|
725 |
} |
726 |
|
727 |
break;
|
728 |
|
729 |
case KXmlParser.TEXT:
|
730 |
break;
|
731 |
} |
732 |
|
733 |
if (!endpol) {
|
734 |
currentTag = parser.next(); |
735 |
} |
736 |
} |
737 |
|
738 |
if (featType.equals(GetFeaturesTags.POLYLINE)) {
|
739 |
geom = geomManager.createMultiCurve(polyline, |
740 |
Geometry.SUBTYPES.GEOM2D); |
741 |
} else if (featType.equals(GetFeaturesTags.POLYGON)) { |
742 |
geom = geomManager.createMultiSurface(polyline, |
743 |
Geometry.SUBTYPES.GEOM2D); |
744 |
} |
745 |
} |
746 |
|
747 |
break;
|
748 |
|
749 |
case KXmlParser.END_TAG:
|
750 |
|
751 |
if (parser.getName().compareTo(GetFeaturesTags.FEATURE) == 0) { |
752 |
end = true;
|
753 |
} |
754 |
|
755 |
break;
|
756 |
|
757 |
case KXmlParser.TEXT:
|
758 |
break;
|
759 |
} |
760 |
|
761 |
if (!end) {
|
762 |
currentTag = parser.next(); |
763 |
} |
764 |
} |
765 |
|
766 |
return geom;
|
767 |
} |
768 |
|
769 |
/**
|
770 |
* Private method that parses a COORDS tag to return an ArrayList of
|
771 |
* Point2D.Double objects
|
772 |
*
|
773 |
* @param strCoords
|
774 |
* @param delTuple
|
775 |
* @param delCoords
|
776 |
* @param delDec
|
777 |
* @return
|
778 |
* @throws CreateGeometryException
|
779 |
*/
|
780 |
private List<Point> parseCoords(String strCoords, String delTuple, |
781 |
String delCoords, char delDec) throws CreateGeometryException { |
782 |
|
783 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
784 |
List<Point> points = new ArrayList<Point>(); |
785 |
String tupla = new String(); |
786 |
String[] tuplaPart; |
787 |
Double x;
|
788 |
Double y;
|
789 |
|
790 |
/*
|
791 |
* Replaces Decimal Separator to convert coords to suitable doubles.
|
792 |
*
|
793 |
* Maybe it will have a better performance if we insert this replace
|
794 |
* into the while
|
795 |
*/
|
796 |
if (delDec != '.') { |
797 |
strCoords = strCoords.replace(delDec, '.');
|
798 |
} |
799 |
|
800 |
// Creates a tokenizer to run over the string
|
801 |
StringTokenizer tuplas = new StringTokenizer(strCoords, delTuple, false); |
802 |
|
803 |
// Get ArrayLists collections with X's and Y's
|
804 |
while (tuplas.hasMoreTokens()) {
|
805 |
// tupla = tuplas.nextToken().replace(delDec,'.');
|
806 |
tupla = tuplas.nextToken(); |
807 |
tuplaPart = tupla.split(delCoords); |
808 |
x = new Double(tuplaPart[0]); |
809 |
y = new Double(tuplaPart[1]); |
810 |
|
811 |
Point pto = geomManager.createPoint(x.doubleValue(), y
|
812 |
.doubleValue(), Geometry.SUBTYPES.GEOM2D); |
813 |
points.add(pto); |
814 |
} |
815 |
|
816 |
// Return an array of Point objects
|
817 |
return points;
|
818 |
} |
819 |
|
820 |
/**
|
821 |
* @param status
|
822 |
* @return
|
823 |
* @throws ArcImsException
|
824 |
*/
|
825 |
public Rectangle2D getLayerExtent(ArcImsVectStatus status) |
826 |
throws ArcImsException {
|
827 |
Rectangle2D envelope = null; |
828 |
String layerId = (String) status.getLayersIdsSelected().get(0); |
829 |
|
830 |
// logger.info("Getting Vectorial Layer Extent (" + status.getl)
|
831 |
ServiceInformation si = status.getServiceInfo(); |
832 |
String ini_srs = ServiceInfoTags.vINI_SRS;
|
833 |
|
834 |
String srsView = status.getSrs().substring(ini_srs.length()).trim();
|
835 |
String srsServ = si.getFeaturecoordsys();
|
836 |
boolean srsAssumed = si.isSrsAssumed();
|
837 |
|
838 |
if (srsAssumed || (srsView.equals(srsServ))) {
|
839 |
BoundaryBox bb = si.getLayerById(layerId).getEnvelope(); |
840 |
|
841 |
/*
|
842 |
* At the end, we convert the BoundaryBox to a Rectangle2D
|
843 |
*/
|
844 |
envelope = new Rectangle2D.Double(); |
845 |
|
846 |
envelope.setFrameFromDiagonal(bb.getXmin(), bb.getYmin(), bb |
847 |
.getXmax(), bb.getYmax()); |
848 |
|
849 |
return envelope;
|
850 |
} |
851 |
|
852 |
/*
|
853 |
* If SRS of View and Service are different, we do a custom request to
|
854 |
* retrieve the global envelope of the layer
|
855 |
*/
|
856 |
try {
|
857 |
/*
|
858 |
* Build the request
|
859 |
*/
|
860 |
URL url;
|
861 |
url = new URL(buildCapabilitiesRequest(status)); |
862 |
|
863 |
/*
|
864 |
* Build the proper ArcXML
|
865 |
*/
|
866 |
String request = ArcXMLFeatures.getLayerExtentRequest(status);
|
867 |
|
868 |
if (status.verbose) {
|
869 |
System.err.println(request);
|
870 |
} |
871 |
|
872 |
/*
|
873 |
* Request the envelope
|
874 |
*/
|
875 |
File response;
|
876 |
response = ArcImsDownloadUtils.doRequestPost(url, request, |
877 |
"getLayerExtent.xml");
|
878 |
|
879 |
/*
|
880 |
* Parse response and return a Rectangle2D
|
881 |
*/
|
882 |
char ds = si.getSeparators().getDs();
|
883 |
envelope = parseEnvelopeTag(new FileReader(response), ds); |
884 |
} catch (MalformedURLException e) { |
885 |
logger.error(e.getMessage(), e); |
886 |
throw new ArcImsException("arcims_server_error"); |
887 |
} catch (FileNotFoundException e) { |
888 |
logger.error(e.getMessage(), e); |
889 |
throw new ArcImsException("arcims_server_error"); |
890 |
} |
891 |
|
892 |
return envelope;
|
893 |
} |
894 |
|
895 |
/**
|
896 |
* Retrieves an ArrayList of Value arrays. The query can be filtered by a
|
897 |
* WHERE clause or an envelope. Only subfields passed will be retrieved
|
898 |
*
|
899 |
* @param status
|
900 |
* @param subfields
|
901 |
* @param where
|
902 |
* @param envelope
|
903 |
* @return
|
904 |
* @throws ArcImsException
|
905 |
*/
|
906 |
public List<List<ArcImsFeature>> getAttributes(ArcImsVectStatus status, |
907 |
String[] subfields, String where, Rectangle2D envelope) |
908 |
throws ArcImsException {
|
909 |
|
910 |
logger.info("Start getAttributes");
|
911 |
|
912 |
List<List<ArcImsFeature>> features = new ArrayList<List<ArcImsFeature>>(); |
913 |
|
914 |
// Clone the status and set temporal subfields and where clause
|
915 |
ArcImsVectStatus statusCloned = status; |
916 |
|
917 |
String[] subTemp = status.getSubfields(); |
918 |
String whereTemp = status.getWhere();
|
919 |
Rectangle2D rectTemp = status.getEnvelopeRect();
|
920 |
|
921 |
statusCloned.setSubfields(subfields); |
922 |
statusCloned.setWhere(where); |
923 |
statusCloned.setEnvelope(envelope); |
924 |
|
925 |
try {
|
926 |
/*
|
927 |
* Build the request
|
928 |
*/
|
929 |
URL url;
|
930 |
url = new URL(buildCapabilitiesRequest(statusCloned)); |
931 |
|
932 |
hasMore = true;
|
933 |
featCount = 0;
|
934 |
|
935 |
while (hasMore) {
|
936 |
/*
|
937 |
* Build the proper ArcXML
|
938 |
*/
|
939 |
String request = ArcXMLFeatures.getAttributesRequest(
|
940 |
statusCloned, featCount); |
941 |
|
942 |
if (status.verbose) {
|
943 |
System.err.println(request);
|
944 |
} |
945 |
|
946 |
// The attributes come from ArcIMS in ASCII/cp1252 encoding
|
947 |
boolean withUTF = false; |
948 |
|
949 |
// Don't download the file, pass the data directly from the
|
950 |
// connection
|
951 |
boolean withFile = false; |
952 |
|
953 |
/*
|
954 |
* Get an InputStream from File or directly from the connection
|
955 |
*/
|
956 |
InputStream is = null; |
957 |
|
958 |
if (withFile) {
|
959 |
File f = ArcImsDownloadUtils.doRequestPost(url, request,
|
960 |
"getAttributes.xml");
|
961 |
|
962 |
try {
|
963 |
is = new FileInputStream(f); |
964 |
} catch (FileNotFoundException e) { |
965 |
logger.error(e.getMessage(), e); |
966 |
} |
967 |
} else {
|
968 |
is = ArcImsDownloadUtils.getRemoteIS(url, request); |
969 |
} |
970 |
|
971 |
InputStreamReader isr = null; |
972 |
|
973 |
if (withUTF) {
|
974 |
// Deal with UTF-8
|
975 |
try {
|
976 |
isr = new InputStreamReader(is, "UTF-8"); |
977 |
} catch (UnsupportedEncodingException e) { |
978 |
logger.error(e.getMessage(), e); |
979 |
} |
980 |
} else {
|
981 |
// No deal with UTF-8
|
982 |
isr = new InputStreamReader(is); |
983 |
} |
984 |
|
985 |
/*
|
986 |
* Finally, we can create a BufferedReader from the
|
987 |
* InputStreamReader
|
988 |
*/
|
989 |
BufferedReader br = new BufferedReader(isr); |
990 |
|
991 |
/*
|
992 |
* Parse response and return a Rectangle2D
|
993 |
*/
|
994 |
String layerId = (String) statusCloned.getLayersIdsSelected().get(0); |
995 |
logger.debug("Start attributes downloading and parsing ("
|
996 |
+ featCount + ") ids retrieved");
|
997 |
features.add(getFeatures(br, statusCloned.getServiceInfo(), |
998 |
layerId, statusCloned.getSubfields(), 0));
|
999 |
logger.debug("End attributes downloading and parsing");
|
1000 |
} |
1001 |
|
1002 |
hasMore = true;
|
1003 |
featCount = 0;
|
1004 |
} catch (MalformedURLException e) { |
1005 |
logger.error(e.getMessage(), e); |
1006 |
throw new ArcImsException("arcims_server_error"); |
1007 |
} catch (ArcImsException e) {
|
1008 |
logger.error(e.getMessage(), e); |
1009 |
throw new ArcImsException("arcims_server_error"); |
1010 |
} finally {
|
1011 |
status.setSubfields(subTemp); |
1012 |
status.setWhere(whereTemp); |
1013 |
status.setEnvelope(rectTemp); |
1014 |
} |
1015 |
|
1016 |
logger.debug("End attributes retrieving");
|
1017 |
|
1018 |
return features;
|
1019 |
} |
1020 |
|
1021 |
/**
|
1022 |
* Retrieves an ArrayList of I by some IDs. The geometry returned is the
|
1023 |
* envelope of the feature requested. The query can be filtered by a WHERE
|
1024 |
* clause or an envelope. Only subfields passed will be retrieved
|
1025 |
*
|
1026 |
* @param status
|
1027 |
* @param subfields
|
1028 |
* @param where
|
1029 |
* @param envelope
|
1030 |
* @return
|
1031 |
* @throws ArcImsException
|
1032 |
*/
|
1033 |
public List<List<ArcImsFeature>> getAttributesWithEnvelope( |
1034 |
ArcImsVectStatus status, String[] subfields, String where, |
1035 |
Rectangle2D envelope) throws ArcImsException { |
1036 |
|
1037 |
logger.info("Start getAttributesWihtEnvelope");
|
1038 |
|
1039 |
List<List<ArcImsFeature>> features = new ArrayList<List<ArcImsFeature>>(); |
1040 |
|
1041 |
// Clone the status and set temporal subfields and where clause
|
1042 |
ArcImsVectStatus statusCloned = status; |
1043 |
|
1044 |
String[] subTemp = status.getSubfields(); |
1045 |
String whereTemp = status.getWhere();
|
1046 |
Rectangle2D rectTemp = status.getEnvelopeRect();
|
1047 |
|
1048 |
statusCloned.setSubfields(subfields); |
1049 |
statusCloned.setWhere(where); |
1050 |
statusCloned.setEnvelope(envelope); |
1051 |
|
1052 |
try {
|
1053 |
/*
|
1054 |
* Build the request
|
1055 |
*/
|
1056 |
URL url;
|
1057 |
url = new URL(buildCapabilitiesRequest(statusCloned)); |
1058 |
|
1059 |
hasMore = true;
|
1060 |
featCount = 0;
|
1061 |
|
1062 |
while (hasMore) {
|
1063 |
/*
|
1064 |
* Build the proper ArcXML
|
1065 |
*/
|
1066 |
String request = ArcXMLFeatures.getAttributesRequest(
|
1067 |
statusCloned, featCount); |
1068 |
|
1069 |
if (status.verbose) {
|
1070 |
System.err.println(request);
|
1071 |
} |
1072 |
|
1073 |
// The attributes come from ArcIMS in ASCII/cp1252 encoding
|
1074 |
boolean withUTF = false; |
1075 |
|
1076 |
// Don't download the file, pass the data directly from the
|
1077 |
// connection
|
1078 |
boolean withFile = false; |
1079 |
|
1080 |
/*
|
1081 |
* Get an InputStream from File or directly from the connection
|
1082 |
*/
|
1083 |
InputStream is = null; |
1084 |
|
1085 |
if (withFile) {
|
1086 |
File f = ArcImsDownloadUtils.doRequestPost(url, request,
|
1087 |
"getAttributes.xml");
|
1088 |
|
1089 |
try {
|
1090 |
is = new FileInputStream(f); |
1091 |
} catch (FileNotFoundException e) { |
1092 |
logger.error(e.getMessage(), e); |
1093 |
} |
1094 |
} else {
|
1095 |
is = ArcImsDownloadUtils.getRemoteIS(url, request); |
1096 |
} |
1097 |
|
1098 |
InputStreamReader isr = null; |
1099 |
|
1100 |
if (withUTF) {
|
1101 |
// Deal with UTF-8
|
1102 |
try {
|
1103 |
isr = new InputStreamReader(is, "UTF-8"); |
1104 |
} catch (UnsupportedEncodingException e) { |
1105 |
logger.error(e.getMessage(), e); |
1106 |
} |
1107 |
} else {
|
1108 |
// No deal with UTF-8
|
1109 |
isr = new InputStreamReader(is); |
1110 |
} |
1111 |
|
1112 |
/*
|
1113 |
* Finally, we can create a BufferedReader from the
|
1114 |
* InputStreamReader
|
1115 |
*/
|
1116 |
BufferedReader br = new BufferedReader(isr); |
1117 |
|
1118 |
/*
|
1119 |
* Parse response and return a Rectangle2D
|
1120 |
*/
|
1121 |
String layerId = (String) statusCloned.getLayersIdsSelected().get(0); |
1122 |
logger.debug("Start attributes downloading and parsing ("
|
1123 |
+ featCount + ") ids retrieved");
|
1124 |
features.add(getFeatures(br, statusCloned.getServiceInfo(), |
1125 |
layerId, statusCloned.getSubfields(), 2)); // Here we |
1126 |
// request
|
1127 |
// envelopes instead
|
1128 |
// of geometries
|
1129 |
logger.debug("End attributes downloading and parsing");
|
1130 |
} |
1131 |
|
1132 |
hasMore = true;
|
1133 |
featCount = 0;
|
1134 |
} catch (MalformedURLException e) { |
1135 |
logger.error(e.getMessage(), e); |
1136 |
throw new ArcImsException("arcims_server_error"); |
1137 |
} catch (ArcImsException e) {
|
1138 |
logger.error(e.getMessage(), e); |
1139 |
throw new ArcImsException("arcims_server_error"); |
1140 |
} finally {
|
1141 |
status.setSubfields(subTemp); |
1142 |
status.setWhere(whereTemp); |
1143 |
status.setEnvelope(rectTemp); |
1144 |
} |
1145 |
|
1146 |
logger.debug("End attributes retrieving");
|
1147 |
|
1148 |
return features;
|
1149 |
} |
1150 |
|
1151 |
/*
|
1152 |
* (non-Javadoc)
|
1153 |
*
|
1154 |
* @see
|
1155 |
* org.gvsig.remoteClient.arcims.ArcImsProtocolHandler#getElementInfo(org
|
1156 |
* .gvsig.remoteClient.arcims.ArcImsStatus, int, int, int)
|
1157 |
*/
|
1158 |
public String getElementInfo(ArcImsStatus status, int x, int y, |
1159 |
int featureCount) throws ArcImsException { |
1160 |
throw new ClassCastException( |
1161 |
"Invalid request, this method is only valid for Image Services");
|
1162 |
} |
1163 |
} |