svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / wfsg / parsers / WfsgFeatureParser.java @ 3613
History | View | Annotate | Download (7.75 KB)
1 |
|
---|---|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* Generalitat Valenciana
|
23 |
* Conselleria d'Infraestructures i Transport
|
24 |
* Av. Blasco Ib??ez, 50
|
25 |
* 46010 VALENCIA
|
26 |
* SPAIN
|
27 |
*
|
28 |
* +34 963862235
|
29 |
* gvsig@gva.es
|
30 |
* www.gvsig.gva.es
|
31 |
*
|
32 |
* or
|
33 |
*
|
34 |
* IVER T.I. S.A
|
35 |
* Salamanca 50
|
36 |
* 46005 Valencia
|
37 |
* Spain
|
38 |
*
|
39 |
* +34 963163400
|
40 |
* dac@iver.es
|
41 |
*/
|
42 |
package es.gva.cit.gazetteer.wfsg.parsers; |
43 |
import es.gva.cit.catalogClient.metadataXML.XMLNode; |
44 |
import es.gva.cit.catalogClient.metadataXML.XMLTree; |
45 |
import es.gva.cit.gazetteer.querys.Feature; |
46 |
import es.gva.cit.gazetteer.querys.ThesaurusName; |
47 |
import java.awt.geom.Point2D; |
48 |
|
49 |
/**
|
50 |
* This class is used to parse the getFeature request
|
51 |
*
|
52 |
*
|
53 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
54 |
*/
|
55 |
public class WfsgFeatureParser { |
56 |
|
57 |
/**
|
58 |
*
|
59 |
*
|
60 |
*/
|
61 |
private String geomType = null; |
62 |
|
63 |
/**
|
64 |
* It parses the answer
|
65 |
*
|
66 |
*
|
67 |
* @return Array of features
|
68 |
* @param node XML tree that contains the getFeature Answer
|
69 |
* @param thesaurus FEature selected in the thesaurus list
|
70 |
* @param attribute Attribute to do the search
|
71 |
*/
|
72 |
public Feature[] parse(XMLNode node, ThesaurusName thesaurus, String attribute) { |
73 |
XMLNode[] nodeFeatures = XMLTree.searchMultipleNode(node,"gml:featureMember"); |
74 |
|
75 |
String prefix = thesaurus.getName().split(":")[0]; |
76 |
String geomField = getGeomField(prefix,thesaurus);
|
77 |
|
78 |
if ((node.getName().equals("ResultCollection")) || |
79 |
(node.getName().equals("feature_collection"))){
|
80 |
return parseWFS(nodeFeatures,thesaurus,"geographicIdentifier",geomField); |
81 |
}else{
|
82 |
return parseWFS(nodeFeatures,thesaurus,prefix + ":" + attribute,geomField); |
83 |
} |
84 |
} |
85 |
|
86 |
/**
|
87 |
* It parses the a WFS answer
|
88 |
*
|
89 |
*
|
90 |
* @return Array of features
|
91 |
* @param nodeFeatures XML tree that contains the Features
|
92 |
* @param thesaurusName FEature selected in the thesaurus list
|
93 |
* @param attribute Attribute to do the search
|
94 |
* @param geomField Field that contains the geometry
|
95 |
*/
|
96 |
public Feature[] parseWFS(XMLNode[] nodeFeatures, ThesaurusName thesaurusName, String attribute, String geomField) { |
97 |
Feature[] features = new Feature[nodeFeatures.length]; |
98 |
|
99 |
for (int i=0 ; i<nodeFeatures.length ; i++){ |
100 |
String id = XMLTree.searchNodeAtribute(nodeFeatures[i],thesaurusName.getName(),"fid"); |
101 |
|
102 |
XMLNode nodeName = XMLTree.searchNode(nodeFeatures[i],thesaurusName.getName() + "->" + attribute);
|
103 |
String name = ""; |
104 |
|
105 |
if (nodeName != null){ |
106 |
if ((nodeName.getText() != null) && (!(nodeName.getText().equals("")))){ |
107 |
name = nodeName.getText(); |
108 |
} else if ((nodeName.getCdata() != null) && (!(nodeName.getCdata().equals("")))){ |
109 |
name = nodeName.getCdata(); |
110 |
} |
111 |
} |
112 |
String description = name;
|
113 |
Point2D point = getCoordinates(XMLTree.searchNodeValue(nodeFeatures[i],
|
114 |
geomField)); |
115 |
|
116 |
features[i] = new Feature(id,name,description,point);
|
117 |
} |
118 |
|
119 |
return features;
|
120 |
} |
121 |
//cv300:entity:layer:level:elevation:color:text:clave:the_geom->gml:MultiLineString->gml:lineStringMember->gml:LineString->gml:coordinates
|
122 |
|
123 |
/**
|
124 |
* If returns the Geom route of the XML tree
|
125 |
*
|
126 |
*
|
127 |
* @return
|
128 |
* @param prefix Typename
|
129 |
* @param thesaurus Feature selected in the thesaurus list
|
130 |
*/
|
131 |
private String getGeomField(String prefix, ThesaurusName thesaurus) { |
132 |
if ((thesaurus.getFields() == null) || (thesaurus.getFields().length == 0)){ |
133 |
geomType = "Point";
|
134 |
return thesaurus.getName() + "->" + "position->gml:Point->gml:coordinates"; |
135 |
} |
136 |
|
137 |
for (int i=0 ; i<thesaurus.getFields().length ; i++){ |
138 |
if (thesaurus.getFields()[i].getType().equals("gml:MultiLineStringPropertyType")){ |
139 |
prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
140 |
geomType = "MultiLine";
|
141 |
return prefix +
|
142 |
"->gml:MultiLineString->gml:lineStringMember->gml:LineString->" +
|
143 |
"gml:coordinates";
|
144 |
} |
145 |
|
146 |
if (thesaurus.getFields()[i].getType().equals("gml:MultiPolygonPropertyType")){ |
147 |
prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
148 |
geomType = "MultiPolygon";
|
149 |
return prefix +
|
150 |
"->gml:MultiPolygon->gml:polygonMember->gml:Polygon->gml:outerBoundaryIs->" +
|
151 |
"gml:LinearRing->gml:coordinates";
|
152 |
} |
153 |
|
154 |
if (thesaurus.getFields()[i].getType().equals("gml:PointPropertyType")){ |
155 |
prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
156 |
geomType = "Point";
|
157 |
return prefix +
|
158 |
"->gml:Point->gml:coordinates";
|
159 |
} |
160 |
} |
161 |
return ""; |
162 |
} |
163 |
|
164 |
/**
|
165 |
* It returns a pair of coordinates of the Feature
|
166 |
*
|
167 |
*
|
168 |
* @return
|
169 |
* @param sCoordinates String that contains the coordinates
|
170 |
*/
|
171 |
private Point2D getCoordinates(String sCoordinates) { |
172 |
if (geomType.equals("MultiLine")){ |
173 |
return getCoordinatesMultiline(sCoordinates);
|
174 |
} |
175 |
if (geomType.equals("MultiPolygon")){ |
176 |
return getCoordinatesMultiPolygon(sCoordinates);
|
177 |
} |
178 |
if (geomType.equals("Point")){ |
179 |
return getCoordinatesPoint(sCoordinates);
|
180 |
} |
181 |
return null; |
182 |
} |
183 |
|
184 |
/**
|
185 |
* It returns a pair of coordinates from a Multiline string
|
186 |
*
|
187 |
*
|
188 |
* @return
|
189 |
* @param sCoordinates String
|
190 |
*/
|
191 |
private java.awt.geom.Point2D.Double getCoordinatesMultiline(String sCoordinates) { |
192 |
return getAverage(sCoordinates.split(" ")); |
193 |
} |
194 |
|
195 |
/**
|
196 |
* It returns a pair of coordinates from a MultiPolygon string
|
197 |
*
|
198 |
*
|
199 |
* @return
|
200 |
* @param sCoordinates String
|
201 |
*/
|
202 |
private java.awt.geom.Point2D.Double getCoordinatesMultiPolygon(String sCoordinates) { |
203 |
return getAverage(sCoordinates.split(" ")); |
204 |
} |
205 |
|
206 |
/**
|
207 |
* It returns a pair of coordinates from a Point
|
208 |
*
|
209 |
*
|
210 |
* @return
|
211 |
* @param sCoordinates String
|
212 |
*/
|
213 |
private java.awt.geom.Point2D.Double getCoordinatesPoint(String sCoordinates) { |
214 |
return getAverage(sCoordinates.split(" ")); |
215 |
} |
216 |
|
217 |
/**
|
218 |
* It returns the average of a set of coordinates
|
219 |
*
|
220 |
*
|
221 |
* @return Average
|
222 |
* @param sCoordinates Coordinates: X,Y
|
223 |
*/
|
224 |
private java.awt.geom.Point2D.Double getAverage(String[] sCoordinates) { |
225 |
double x = 0.0; |
226 |
double y = 0.0; |
227 |
for (int i=0 ; i<sCoordinates.length ; i++){ |
228 |
x = x + Double.parseDouble(sCoordinates[i].split(",")[0]); |
229 |
y = y + Double.parseDouble(sCoordinates[i].split(",")[1]); |
230 |
} |
231 |
x = x / sCoordinates.length; |
232 |
y = y / sCoordinates.length; |
233 |
return new Point2D.Double(x,y); |
234 |
} |
235 |
} |