Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / wfsg / parsers / WfsgFeatureParser.java @ 3408

History | View | Annotate | Download (7.83 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package es.gva.cit.gazetteer.wfsg.parsers;
42

    
43
import java.awt.geom.Point2D;
44

    
45
import es.gva.cit.catalogClient.metadataXML.XMLNode;
46
import es.gva.cit.catalogClient.metadataXML.XMLTree;
47
import es.gva.cit.gazetteer.querys.Feature;
48
import es.gva.cit.gazetteer.querys.ThesaurusName;
49

    
50
/**
51
 * This class is used to parse the getFeature request
52
 * 
53
 * @author Jorge Piera Llodra (piera_jor@gva.es)
54
 */
55
public class WfsgFeatureParser {
56
    private String geomType = null;
57
    
58
    /**
59
     * It parses the answer
60
     * @param node
61
     * XML tree that contains the getFeature Answer
62
     * @param feature
63
     * FEature selected in the thesaurus list
64
     * @param attribute
65
     * Attribute to do the search
66
     * @return
67
     * Array of features
68
     */
69
    public Feature[] parse(XMLNode node,ThesaurusName feature, String attribute){
70
        XMLNode[] nodeFeatures = XMLTree.searchMultipleNode(node,"gml:featureMember");
71
        
72
        String prefix = feature.getName().split(":")[0];
73
        String geomField = getGeomField(prefix,feature);
74
            
75
        if ((node.getName().equals("ResultCollection")) ||
76
                (node.getName().equals("feature_collection"))){
77
            return parseWFS(nodeFeatures,feature,"geographicIdentifier",geomField);
78
        }else{
79
            return parseWFS(nodeFeatures,feature,prefix + ":" + attribute,geomField);
80
        }  
81
    }
82
    
83
    /**
84
     * It parses the a WFS answer
85
     * @param nodeFeatures
86
     * XML tree that contains the Features
87
     * @param feature
88
     * FEature selected in the thesaurus list
89
     * @param attribute
90
     * Attribute to do the search
91
     * @param geomField
92
     * Field that contains the geometry
93
     * @return
94
     * Array of features
95
     */
96
    public Feature[] parseWFS(XMLNode[] nodeFeatures,ThesaurusName feature, 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],feature.getName(),"fid");
101
            
102
            XMLNode nodeName = XMLTree.searchNode(nodeFeatures[i],feature.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
    
122
   
123
    
124
    
125
    /**
126
     * If returns the Geom route of the XML tree
127
     * @param prefix
128
     * Typename
129
     * @param feature
130
     * Feature selected in the thesaurus list
131
     * @return
132
     */
133
    
134
    //cv300:entity:layer:level:elevation:color:text:clave:the_geom->gml:MultiLineString->gml:lineStringMember->gml:LineString->gml:coordinates
135
    private String getGeomField(String prefix,ThesaurusName feature){
136
        if (feature.getFields() == null){
137
            geomType = "Point";  
138
            return feature.getName() + "->" + "position->gml:Point->gml:coordinates";
139
        }    
140
        
141
        for (int i=0 ; i<feature.getFields().length ; i++){
142
            if (feature.getFields()[i].getType().equals("gml:MultiLineStringPropertyType")){
143
                prefix = feature.getName() + "->" + prefix + ":" + feature.getFields()[i].getName();
144
                geomType = "MultiLine";
145
                return prefix +
146
                "->gml:MultiLineString->gml:lineStringMember->gml:LineString->" +
147
                "gml:coordinates";
148
            }
149
            
150
            if (feature.getFields()[i].getType().equals("gml:MultiPolygonPropertyType")){
151
                prefix = feature.getName() + "->" + prefix + ":" + feature.getFields()[i].getName();
152
                geomType = "MultiPolygon";
153
                return prefix +
154
                "->gml:MultiPolygon->gml:polygonMember->gml:Polygon->gml:outerBoundaryIs->" +
155
                "gml:LinearRing->gml:coordinates";
156
            }
157
            
158
            if (feature.getFields()[i].getType().equals("gml:PointPropertyType")){
159
                prefix = feature.getName() + "->" + prefix + ":" + feature.getFields()[i].getName();
160
                geomType = "Point";
161
                return prefix +
162
                "->gml:Point->gml:coordinates";
163
            }
164
          
165
            
166
            
167
        }
168
        
169
        return "";   
170
        
171
    }
172
    
173
    /**
174
     * It returns a pair of coordinates of the Feature
175
     * @param sCoordinates
176
     * String that contains the coordinates
177
     * @return
178
     */
179
    
180
    private Point2D getCoordinates(String sCoordinates){
181
        if (geomType.equals("MultiLine")){
182
            return getCoordinatesMultiline(sCoordinates);
183
        }
184
        if (geomType.equals("MultiPolygon")){
185
            return getCoordinatesMultiPolygon(sCoordinates);
186
        }
187
        if (geomType.equals("Point")){
188
            return getCoordinatesPoint(sCoordinates);
189
        }
190
        return null;
191
    }
192
    
193
   /**
194
    * It returns a pair of coordinates from a Multiline string
195
    * @param sCoordinates
196
    * String
197
    * @return
198
    */
199
    private Point2D.Double getCoordinatesMultiline(String sCoordinates){
200
        return getAverage(sCoordinates.split(" "));      
201
    }
202
    
203
    /**
204
     * It returns a pair of coordinates from a MultiPolygon string
205
     * @param sCoordinates
206
     * String
207
     * @return
208
     */
209
     private Point2D.Double getCoordinatesMultiPolygon(String sCoordinates){
210
         return getAverage(sCoordinates.split(" "));
211
     }
212
     
213
     /**
214
      * It returns a pair of coordinates from a Point
215
      * @param sCoordinates
216
      * String
217
      * @return
218
      */
219
      private Point2D.Double getCoordinatesPoint(String sCoordinates){
220
          return getAverage(sCoordinates.split(" "));
221
      }
222
     
223
    
224
     /**
225
      * It returns the average of a set of coordinates
226
      * @param sCoordinates
227
      * Coordinates: X,Y
228
      * @return
229
      * Average
230
      */
231
     private Point2D.Double getAverage(String[] sCoordinates){
232
         double x = 0.0;
233
         double y = 0.0;
234
         for (int i=0 ; i<sCoordinates.length ; i++){
235
             x = x + Double.parseDouble(sCoordinates[i].split(",")[0]);
236
             y = y + Double.parseDouble(sCoordinates[i].split(",")[1]);
237
         }
238
         x = x / sCoordinates.length;
239
         y = y / sCoordinates.length;
240
         return new Point2D.Double(x,y);
241
    }
242
    
243
}