Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id: FeatureCollection.js 3882 2008-02-27 15:41:33Z gjvoosten $
4
*/
5

    
6
// Ensure this object's dependancies are loaded.
7
mapbuilder.loadScript(baseDir+"/model/ModelBase.js");
8

    
9
/**
10
 * Stores a GML Feature or FeatureCollection as defined by the
11
 * Open Geospatial Consortium (http://www.opengeospatial.org/).
12
 *
13
 * @constructor
14
 * @base ModelBase
15
 * @author Cameron Shorter
16
 * @requires Sarissa
17
 * @param modelNode The model's XML object node from the configuration document.
18
 * @param parent    The parent model for the object.
19
  */
20
function FeatureCollection(modelNode, parent) {
21
  // Inherit the ModelBase functions and parameters
22
  ModelBase.apply(this, new Array(modelNode, parent));
23

    
24
  this.featureTagName = this.getProperty("mb:featureTagName", "topp:CITY_NAME");
25
  this.coordsTagName = this.getProperty("mb:coordsTagName", "//gml:coordinates");
26
  this.nodeSelectXpath = this.getProperty("mb:nodeSelectXpath");
27
  this.coordSelectXpath = this.getProperty("mb:coordSelectXpath", "topp:the_geom/gml:MultiPoint/gml:pointMember/gml:Point/gml:coordinates");
28

    
29
  // Namespace to use when doing Xpath queries, usually set in config file.
30
  //if (!this.namespace){
31
  //  this.namespace = "xmlns:gml='http://www.opengis.net/gml' xmlns:wfs='http://www.opengis.net/wfs'";
32
  //}
33

    
34
  /**
35
   * convert coordinates in the GML document to the SRS of the map container, 
36
   * if required.  The coordinate values are replaced in the GML document.
37
   * @param objRef Pointer to this object.
38
   */
39
  this.convertCoords = function(objRef) {
40
  
41
    if( objRef.doc && objRef.containerModel && objRef.containerModel.doc ) {
42
		  var coordNodes = objRef.doc.selectNodes( objRef.coordsTagName );
43
		  if (coordNodes.length>0 && objRef.containerModel) {
44
		    var srsNode = coordNodes[0].selectSingleNode("ancestor-or-self::*/@srsName");
45
		    if( srsNode && (srsNode.nodeValue.toUpperCase() != objRef.containerModel.getSRS().toUpperCase()) ) {
46
		      var sourceProj = new OpenLayers.Projection(srsNode.nodeValue);
47
		      objRef.setParam("modelStatus",mbGetMessage("convertingCoords"));
48
		      var containerProj = new OpenLayers.Projection(objRef.containerModel.getSRS());
49
		      for (var i=0; i<coordNodes.length; ++i) {
50
		        var coords = getNodeValue(coordNodes[i]);
51
		        var coordsArray = coords.split(' ');
52
		        var newCoords = '';
53
		        for (var j=0; j<coordsArray.length; ++j) {
54
		          var xy = coordsArray[j].split(',');
55
		          if (xy.length==2) {
56
		            var pt=new OpenLayers.Geometry.Point(xy[0],xy[1]);
57
                pt.transform(sourceProj,containerProj);
58
		            newCoords += pt.x + ',' + pt.y + ' ';
59
		          }
60
		        }
61
		        coordNodes[i].firstChild.nodeValue=newCoords;
62
		      }
63
		    }
64
      }
65
    }
66
  }
67
  
68
  /**
69
    * Called when the OWSContext gets loaded
70
    */
71
  this.loadWfsRequests = function(objRef) {
72
    //alert( "FeatureCollection loadModel:"+(new XMLSerializer()).serializeToString(objRef.containerModel.doc))
73
    // we need to retrieve all the features
74
    if( objRef.containerModel.doc != null) {
75
      //alert( "FeatureCollection loadModel:"+(new XMLSerializer()).serializeToString(objRef.containerModel.doc))
76
      var featureTypes = objRef.containerModel.doc.selectNodes("/wmc:OWSContext/wmc:ResourceList/wmc:FeatureType");
77
      if( featureTypes.length > 0 ) {
78
        for( var i=0; i<featureTypes.length; i++) {
79
          var httpPayload = new Object();        
80
        
81
          var wfsFeature = featureTypes[i]
82
          //alert( "feature:"+ (new XMLSerializer()).serializeToString(wfsFeature) )
83
          
84
          var server = wfsFeature.selectSingleNode("wmc:Server")
85
          //alert( "server:"+ (new XMLSerializer()).serializeToString(server) )
86
          var onlineResource = server.selectSingleNode("wmc:OnlineResource")
87
          //alert( "onlineResource:"+ (new XMLSerializer()).serializeToString(onlineResource) )
88
          httpPayload.method = onlineResource.getAttribute("method")
89
          httpPayload.url = onlineResource.getAttribute("xlink:href")
90
          //alert( "server:"+ httpPayload.method + " " + httpPayload.url )
91
          
92
          var query = wfsFeature.selectSingleNode("wfs:GetFeature")
93
          //alert( "query2:"+ (new XMLSerializer()).serializeToString( query ))
94
          httpPayload.postData = (new XMLSerializer()).serializeToString( query )
95
          
96
          // This does not work on IE for some reaso
97
          // wfsFeature.model = objRef;
98
          objRef.wfsFeature = wfsFeature
99
          objRef.newRequest(objRef,httpPayload);
100

    
101
          //objRef.containerModel.setParam('addLayer', wfsFeature);
102
          //var sld = wfsFeature.selectSingleNode("wmc:StyleList")
103
        }
104
      }
105
    }
106
  }
107
  
108
  this.addFirstListener("loadModel",this.convertCoords,this);
109
  
110
  if( this.containerModel ) this.containerModel.addListener("loadModel",this.loadWfsRequests,this);
111

    
112
  /**
113
   * Change a feature's visibility.
114
   * @param featureName The name of the feature to set the hidden value for
115
   * @param hidden, 1=hidden, 0=not hidden.
116
   */
117
  this.setHidden=function(featureName, hidden){
118
    this.hidden = hidden;
119
    this.callListeners("hidden", featureName);
120
  }
121

    
122
  /**
123
   * Geta feature's visibility.
124
   * @param featureName The name of the feature to set the hidden value for
125
   * @return hidden value, true=hidden, false=not hidden.
126
   */
127
  this.getHidden=function(layerName){
128
    return this.hidden;
129
  }
130
  this.hidden = false;
131

    
132
  /**
133
   * Returns the list of nodes selected by the nodeSelectpath.  These nodes
134
   * will be the individual feature members from the collection.
135
   * @return list of nodes selected 
136
   */
137
  this.getFeatureNodes = function() {
138
    return this.doc.selectNodes(this.nodeSelectXpath);
139
  }
140

    
141
  /**
142
   * Returns a label for a node in the feature list
143
   * @param featureNode the feature node to selectfrom
144
   * @return a label for this feature
145
   */
146
  this.getFeatureName = function(featureNode) {
147
    var labelNode = featureNode.selectSingleNode(this.featureTagName);   //TBD: set this dynamically
148
    return labelNode?getNodeValue(labelNode):mbGetMessage("noRssTitle");
149
  }
150

    
151
  /**
152
   * Returns an ID value for a node in the feature list
153
   * @param featureNode the feature node to selectfrom
154
   * @return ID for this feature
155
   */
156
  this.getFeatureId = function(featureNode) {
157
    return featureNode.getAttribute("fid")?featureNode.getAttribute("fid"):"no_id";
158
  }
159

    
160
  /**
161
   * Returns a point geometry for the feature
162
   * @param featureNode the feature node to select from
163
   * @return the geometric point for the node
164
   */
165
  this.getFeaturePoint = function(featureNode) {
166
    var coords = featureNode.selectSingleNode(this.coordSelectXpath);
167
    if (coords) {
168
      var point = getNodeValue(coords).split(',');
169
      return point
170
    } else {
171
      return new Array(0,0);  //or some other error to return?
172
    }
173
  }
174
  
175
 /**
176
   * Returns a GML geometry for the feature
177
   * @param featureNode the feature node to select from
178
   * @return the GML for the node
179
   */
180
  this.getFeatureGeometry = function(featureNode) {
181
    var geometryTag = featureNode.selectSingleNode(this.coordsTagName);
182
    if( geometryTag != null )
183
      return geometryTag.firstChild;
184
    else {
185
      alert(mbGetMessage("invalidGeom", (new XMLSerializer()).serializeToString(featureNode)));
186
    }
187
  }
188
}
(4-4/19)