Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_900 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / wfsg / parsers / WfsgFeatureParser.java @ 10562

History | View | Annotate | Download (8.95 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 8765 jjdelcerro
    private String namespace = null;
63 3566 jorpiell
64
/**
65
 * It parses the answer
66
 *
67
 *
68
 * @return Array of features
69
 * @param node XML tree that contains the getFeature Answer
70
 * @param thesaurus FEature selected in the thesaurus list
71
 * @param attribute Attribute to do the search
72
 */
73
    public Feature[] parse(XMLNode node, ThesaurusName thesaurus, String attribute) {
74 3214 jorpiell
        XMLNode[] nodeFeatures = XMLTree.searchMultipleNode(node,"gml:featureMember");
75 8765 jjdelcerro
        namespace = "";
76
        String geomField = "";
77 3311 jorpiell
78 8765 jjdelcerro
        if ((nodeFeatures != null) && (nodeFeatures.length > 0)){
79
                if (nodeFeatures[0].getSubnodes().length > 0){
80
                        String nodeName = nodeFeatures[0].getSubnodes()[0].getName();
81
                        if (nodeName.split(":").length == 2){
82
                                namespace = nodeName.split(":")[0] + ":";
83
                        }
84
                        geomField = getGeomField(nodeFeatures[0].getSubNode(0),namespace,thesaurus);
85
                }
86
        }
87
88 3408 jorpiell
        if ((node.getName().equals("ResultCollection")) ||
89 8765 jjdelcerro
                (node.getName().equals("feature_collection"))){
90
                geomField = "position->gml:Point->gml:coordinates";
91
                geomType = "Point";
92 3566 jorpiell
            return parseWFS(nodeFeatures,thesaurus,"geographicIdentifier",geomField);
93 3311 jorpiell
        }else{
94 8765 jjdelcerro
            return parseWFS(nodeFeatures,thesaurus,namespace + attribute,geomField);
95 3311 jorpiell
        }
96 3566 jorpiell
    }
97
98
/**
99
 * It parses the a WFS answer
100
 *
101
 *
102
 * @return Array of features
103
 * @param nodeFeatures XML tree that contains the Features
104
 * @param thesaurusName FEature selected in the thesaurus list
105
 * @param attribute Attribute to do the search
106
 * @param geomField Field that contains the geometry
107
 */
108
    public Feature[] parseWFS(XMLNode[] nodeFeatures, ThesaurusName thesaurusName, String attribute, String geomField) {
109 3311 jorpiell
        Feature[] features = new Feature[nodeFeatures.length];
110 8604 jorpiell
111 3214 jorpiell
        for (int i=0 ; i<nodeFeatures.length ; i++){
112 8765 jjdelcerro
            XMLNode nodeName = XMLTree.searchNode(nodeFeatures[i],thesaurusName.getName() + "->" + attribute);
113
            String name = "";
114
115 8604 jorpiell
            if (nodeName == null){
116 8765 jjdelcerro
                if (thesaurusName.getName().split(":").length == 1){
117
                        thesaurusName.setName(namespace + thesaurusName.getName());
118
                        nodeName = XMLTree.searchNode(nodeFeatures[i],thesaurusName.getName() + "->" + attribute);
119
                }
120 8604 jorpiell
            }
121 3408 jorpiell
122 3367 jorpiell
            if (nodeName != null){
123 8765 jjdelcerro
                    if ((nodeName.getText() != null) && (!(nodeName.getText().equals("")))){
124
                            name = nodeName.getText();
125
                    } else if ((nodeName.getCdata() != null) && (!(nodeName.getCdata().equals("")))){
126
                            name = nodeName.getCdata();
127
                    }
128
            }
129
130
            String id = XMLTree.searchNodeAtribute(nodeFeatures[i],thesaurusName.getName(),"fid");
131
            String description = name;
132
            System.out.println(thesaurusName.getName() + "->" + geomField);
133
            Point2D point = getCoordinates(XMLTree.searchNodeValue(nodeFeatures[i],
134
                            thesaurusName.getName() + "->" + geomField));
135 3214 jorpiell
136
            features[i] = new Feature(id,name,description,point);
137
        }
138
139
        return features;
140 3566 jorpiell
    }
141
//cv300:entity:layer:level:elevation:color:text:clave:the_geom->gml:MultiLineString->gml:lineStringMember->gml:LineString->gml:coordinates
142
143
/**
144
 * If returns the Geom route of the XML tree
145
 *
146
 *
147
 * @return
148
 * @param prefix Typename
149
 * @param thesaurus Feature selected in the thesaurus list
150
 */
151 8765 jjdelcerro
    private String getGeomField(XMLNode node, String prefix, ThesaurusName thesaurus) {
152
        String geometryFieldName = "";
153
        XMLNode geometryNode = null;
154
            if (node == null){
155
                return null;
156
        }
157 3566 jorpiell
        for (int i=0 ; i<thesaurus.getFields().length ; i++){
158 8765 jjdelcerro
                if (isGeometry(thesaurus.getFields()[i].getType())){
159
                        geometryFieldName = thesaurus.getFields()[i].getName();
160
                }
161
        }
162
        for (int i=0 ; i<node.getNumSubNodes() ; i++){
163
                if (node.getSubNode(i).getName().endsWith(geometryFieldName)){
164
                        geometryFieldName = node.getSubNode(i).getName();
165
                        geometryNode = node.getSubNode(i);
166
                }
167
        }
168
        return getGeometryField(geometryNode,geometryNode.getName());
169
    }
170
171
    private String getGeometryField(XMLNode node,String parentNode){
172
            if (node == null){
173
                    return "";
174
            }
175
            for (int i=0 ; i<node.getNumSubNodes() ; i++){
176
                    if (node.getSubNode(i).getName().compareTo("gml:coordinates") == 0){
177
                            return parentNode + "->" + node.getSubNode(i).getName();
178
                    }else{
179
                            return parentNode + "->" + getGeometryField(node.getSubNode(i),node.getSubNode(i).getName());
180
                }
181
        }
182
            return node.getName();
183
    }
184 3566 jorpiell
185 8765 jjdelcerro
    private boolean isGeometry(String tagName){
186
            if (tagName.compareTo("gml:MultiLineStringPropertyType") == 0){
187
                    geomType = "MultiLine";
188
                    return true;
189
            }else if(tagName.compareTo("gml:LineString") == 0){
190
                    geomType = "MultiLine";
191
                    return true;
192
            }else if(tagName.compareTo("gml:MultiPolygonPropertyType") == 0){
193
                    geomType = "MultiPolygon";
194
                    return true;
195
            }else if(tagName.compareTo("gml:PointPropertyType") == 0){
196
                    geomType = "Point";
197
                    return true;
198
            }else if(tagName.compareTo("gml:GeometryPropertyType") == 0){
199
                    geomType = "Point";
200
                    return true;
201
            }
202
            return false;
203
    }
204
205 3566 jorpiell
/**
206
 * It returns a pair of coordinates of the Feature
207
 *
208
 *
209
 * @return
210
 * @param sCoordinates String that contains the coordinates
211
 */
212
    private Point2D getCoordinates(String sCoordinates) {
213 3214 jorpiell
        if (geomType.equals("MultiLine")){
214
            return getCoordinatesMultiline(sCoordinates);
215
        }
216
        if (geomType.equals("MultiPolygon")){
217
            return getCoordinatesMultiPolygon(sCoordinates);
218
        }
219
        if (geomType.equals("Point")){
220
            return getCoordinatesPoint(sCoordinates);
221
        }
222
        return null;
223 3566 jorpiell
    }
224
225
/**
226
 * It returns a pair of coordinates from a Multiline string
227
 *
228
 *
229
 * @return
230
 * @param sCoordinates String
231
 */
232
    private java.awt.geom.Point2D.Double getCoordinatesMultiline(String sCoordinates) {
233 3214 jorpiell
        return getAverage(sCoordinates.split(" "));
234 3566 jorpiell
    }
235
236
/**
237
 * It returns a pair of coordinates from a MultiPolygon string
238
 *
239
 *
240
 * @return
241
 * @param sCoordinates String
242
 */
243
    private java.awt.geom.Point2D.Double getCoordinatesMultiPolygon(String sCoordinates) {
244 3214 jorpiell
         return getAverage(sCoordinates.split(" "));
245 3566 jorpiell
    }
246
247
/**
248
 * It returns a pair of coordinates from a Point
249
 *
250
 *
251
 * @return
252
 * @param sCoordinates String
253
 */
254
    private java.awt.geom.Point2D.Double getCoordinatesPoint(String sCoordinates) {
255 3214 jorpiell
          return getAverage(sCoordinates.split(" "));
256 3566 jorpiell
    }
257
258
/**
259
 * It returns the average of a set of coordinates
260
 *
261
 *
262
 * @return Average
263
 * @param sCoordinates Coordinates: X,Y
264
 */
265
    private java.awt.geom.Point2D.Double getAverage(String[] sCoordinates) {
266 3613 jorpiell
         double x =         0.0;
267
         double y =         0.0;
268 3214 jorpiell
         for (int i=0 ; i<sCoordinates.length ; i++){
269
             x = x + Double.parseDouble(sCoordinates[i].split(",")[0]);
270
             y = y + Double.parseDouble(sCoordinates[i].split(",")[1]);
271
         }
272
         x = x / sCoordinates.length;
273
         y = y / sCoordinates.length;
274
         return new Point2D.Double(x,y);
275 3566 jorpiell
    }
276
 }