root / tags / v10_RC2c / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / wfsg / parsers / WfsgFeatureParser.java @ 8745
History | View | Annotate | Download (8.59 KB)
1 | 3566 | jorpiell | |
---|---|---|---|
2 | 3214 | jorpiell | /* 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 | 3613 | jorpiell | import java.awt.geom.Point2D; |
48 | 3214 | jorpiell | |
49 | /**
|
||
50 | * This class is used to parse the getFeature request
|
||
51 | *
|
||
52 | 3566 | jorpiell | *
|
53 | 3214 | jorpiell | * @author Jorge Piera Llodra (piera_jor@gva.es)
|
54 | */
|
||
55 | public class WfsgFeatureParser { |
||
56 | 3566 | jorpiell | |
57 | /**
|
||
58 | *
|
||
59 | *
|
||
60 | */
|
||
61 | 3214 | jorpiell | private String geomType = null; |
62 | 3566 | jorpiell | |
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 | 3214 | jorpiell | XMLNode[] nodeFeatures = XMLTree.searchMultipleNode(node,"gml:featureMember"); |
74 | 3311 | jorpiell | |
75 | 3587 | jorpiell | String prefix = thesaurus.getName().split(":")[0]; |
76 | 8604 | jorpiell | String geomField = getGeomField(prefix,thesaurus);
|
77 | |||
78 | 3408 | jorpiell | if ((node.getName().equals("ResultCollection")) || |
79 | 8604 | jorpiell | (node.getName().equals("feature_collection"))){
|
80 | 3566 | jorpiell | return parseWFS(nodeFeatures,thesaurus,"geographicIdentifier",geomField); |
81 | 3311 | jorpiell | }else{
|
82 | 3566 | jorpiell | return parseWFS(nodeFeatures,thesaurus,prefix + ":" + attribute,geomField); |
83 | 3311 | jorpiell | } |
84 | 3566 | jorpiell | } |
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 | 3311 | jorpiell | Feature[] features = new Feature[nodeFeatures.length]; |
98 | 8604 | jorpiell | |
99 | 3214 | jorpiell | for (int i=0 ; i<nodeFeatures.length ; i++){ |
100 | 8604 | jorpiell | String sThesaurusName = thesaurusName.getName();
|
101 | String id = XMLTree.searchNodeAtribute(nodeFeatures[i],sThesaurusName,"fid"); |
||
102 | Point2D point = null; |
||
103 | |||
104 | XMLNode nodeName = XMLTree.searchNode(nodeFeatures[i],sThesaurusName + "->" + attribute);
|
||
105 | if (nodeName == null){ |
||
106 | sThesaurusName = "ms:" + sThesaurusName;
|
||
107 | nodeName = XMLTree.searchNode(nodeFeatures[i],sThesaurusName + "->" + "ms:toponimo"); |
||
108 | geomField = sThesaurusName + "->ms:msGeometry->gml:Point->gml:coordinates";
|
||
109 | point = getCoordinates(XMLTree.searchNodeValue(nodeFeatures[i], |
||
110 | geomField)); |
||
111 | }else{
|
||
112 | point = getCoordinates(XMLTree.searchNodeValue(nodeFeatures[i], |
||
113 | geomField)); |
||
114 | } |
||
115 | 3367 | jorpiell | String name = ""; |
116 | 3408 | jorpiell | |
117 | 3367 | jorpiell | if (nodeName != null){ |
118 | if ((nodeName.getText() != null) && (!(nodeName.getText().equals("")))){ |
||
119 | name = nodeName.getText(); |
||
120 | } else if ((nodeName.getCdata() != null) && (!(nodeName.getCdata().equals("")))){ |
||
121 | name = nodeName.getCdata(); |
||
122 | } |
||
123 | } |
||
124 | 8604 | jorpiell | String description = name;
|
125 | 3214 | jorpiell | |
126 | features[i] = new Feature(id,name,description,point);
|
||
127 | } |
||
128 | |||
129 | return features;
|
||
130 | 3566 | jorpiell | } |
131 | //cv300:entity:layer:level:elevation:color:text:clave:the_geom->gml:MultiLineString->gml:lineStringMember->gml:LineString->gml:coordinates
|
||
132 | |||
133 | /**
|
||
134 | * If returns the Geom route of the XML tree
|
||
135 | *
|
||
136 | *
|
||
137 | * @return
|
||
138 | * @param prefix Typename
|
||
139 | * @param thesaurus Feature selected in the thesaurus list
|
||
140 | */
|
||
141 | private String getGeomField(String prefix, ThesaurusName thesaurus) { |
||
142 | 3587 | jorpiell | if ((thesaurus.getFields() == null) || (thesaurus.getFields().length == 0)){ |
143 | 3311 | jorpiell | geomType = "Point";
|
144 | 3566 | jorpiell | return thesaurus.getName() + "->" + "position->gml:Point->gml:coordinates"; |
145 | 3311 | jorpiell | } |
146 | |||
147 | 3566 | jorpiell | for (int i=0 ; i<thesaurus.getFields().length ; i++){ |
148 | if (thesaurus.getFields()[i].getType().equals("gml:MultiLineStringPropertyType")){ |
||
149 | prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
||
150 | 3214 | jorpiell | geomType = "MultiLine";
|
151 | return prefix +
|
||
152 | "->gml:MultiLineString->gml:lineStringMember->gml:LineString->" +
|
||
153 | "gml:coordinates";
|
||
154 | } |
||
155 | |||
156 | 3566 | jorpiell | if (thesaurus.getFields()[i].getType().equals("gml:MultiPolygonPropertyType")){ |
157 | prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
||
158 | 3214 | jorpiell | geomType = "MultiPolygon";
|
159 | return prefix +
|
||
160 | "->gml:MultiPolygon->gml:polygonMember->gml:Polygon->gml:outerBoundaryIs->" +
|
||
161 | "gml:LinearRing->gml:coordinates";
|
||
162 | } |
||
163 | |||
164 | 3566 | jorpiell | if (thesaurus.getFields()[i].getType().equals("gml:PointPropertyType")){ |
165 | prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
||
166 | 3214 | jorpiell | geomType = "Point";
|
167 | return prefix +
|
||
168 | "->gml:Point->gml:coordinates";
|
||
169 | 8604 | jorpiell | } |
170 | |||
171 | if (thesaurus.getFields()[i].getType().equals("gml:GeometryPropertyType")){ |
||
172 | prefix = thesaurus.getName() + "->" + prefix + ":" + thesaurus.getFields()[i].getName(); |
||
173 | geomType = "Point";
|
||
174 | return prefix +
|
||
175 | "->gml:Point->gml:coordinates";
|
||
176 | } |
||
177 | 3587 | jorpiell | } |
178 | return ""; |
||
179 | 3613 | jorpiell | } |
180 | 3566 | jorpiell | |
181 | /**
|
||
182 | * It returns a pair of coordinates of the Feature
|
||
183 | *
|
||
184 | *
|
||
185 | * @return
|
||
186 | * @param sCoordinates String that contains the coordinates
|
||
187 | */
|
||
188 | private Point2D getCoordinates(String sCoordinates) { |
||
189 | 3214 | jorpiell | if (geomType.equals("MultiLine")){ |
190 | return getCoordinatesMultiline(sCoordinates);
|
||
191 | } |
||
192 | if (geomType.equals("MultiPolygon")){ |
||
193 | return getCoordinatesMultiPolygon(sCoordinates);
|
||
194 | } |
||
195 | if (geomType.equals("Point")){ |
||
196 | return getCoordinatesPoint(sCoordinates);
|
||
197 | } |
||
198 | return null; |
||
199 | 3566 | jorpiell | } |
200 | |||
201 | /**
|
||
202 | * It returns a pair of coordinates from a Multiline string
|
||
203 | *
|
||
204 | *
|
||
205 | * @return
|
||
206 | * @param sCoordinates String
|
||
207 | */
|
||
208 | private java.awt.geom.Point2D.Double getCoordinatesMultiline(String sCoordinates) { |
||
209 | 3214 | jorpiell | return getAverage(sCoordinates.split(" ")); |
210 | 3566 | jorpiell | } |
211 | |||
212 | /**
|
||
213 | * It returns a pair of coordinates from a MultiPolygon string
|
||
214 | *
|
||
215 | *
|
||
216 | * @return
|
||
217 | * @param sCoordinates String
|
||
218 | */
|
||
219 | private java.awt.geom.Point2D.Double getCoordinatesMultiPolygon(String sCoordinates) { |
||
220 | 3214 | jorpiell | return getAverage(sCoordinates.split(" ")); |
221 | 3566 | jorpiell | } |
222 | |||
223 | /**
|
||
224 | * It returns a pair of coordinates from a Point
|
||
225 | *
|
||
226 | *
|
||
227 | * @return
|
||
228 | * @param sCoordinates String
|
||
229 | */
|
||
230 | private java.awt.geom.Point2D.Double getCoordinatesPoint(String sCoordinates) { |
||
231 | 3214 | jorpiell | return getAverage(sCoordinates.split(" ")); |
232 | 3566 | jorpiell | } |
233 | |||
234 | /**
|
||
235 | * It returns the average of a set of coordinates
|
||
236 | *
|
||
237 | *
|
||
238 | * @return Average
|
||
239 | * @param sCoordinates Coordinates: X,Y
|
||
240 | */
|
||
241 | private java.awt.geom.Point2D.Double getAverage(String[] sCoordinates) { |
||
242 | 3613 | jorpiell | double x = 0.0; |
243 | double y = 0.0; |
||
244 | 3214 | jorpiell | for (int i=0 ; i<sCoordinates.length ; i++){ |
245 | x = x + Double.parseDouble(sCoordinates[i].split(",")[0]); |
||
246 | y = y + Double.parseDouble(sCoordinates[i].split(",")[1]); |
||
247 | } |
||
248 | x = x / sCoordinates.length; |
||
249 | y = y / sCoordinates.length; |
||
250 | return new Point2D.Double(x,y); |
||
251 | 3566 | jorpiell | } |
252 | } |