Hi all,<br><br>I'm pretty a newbe with OpenLayers and I think I'm starting with a complicated case; need some help to better understand the core of the system...<br><br>I want to create a map with a layer comming from a webservice that generates an XML response. This service was not designed to be used for map display (originally for mobile), then the XML output is not a standard Open GIS format but do contains geo elements including name, lat, long, desc (url)... Example of the XML response:<br>
<pre id="line1"><span class="pi"><?xml version="1.0"?></span><span>
<span id="line2"></span></span><span><<span class="start-tag">wplatform</span> <span class="attribute-name">version</span>="<a class="attribute-value">1.0.16 Mon Mar 4 16:53:30 CET 2013 </a>"><br></span><span></span><span><<span class="start-tag">response</span>><br>
</span><span></span><span><<span class="start-tag">search_result</span>></span><span></span><span><br> <<span class="start-tag">wd-34</span>></span><span></span><span></span><span><br></span><span> </span><span></span><span><<span class="start-tag">spot_id</span>></span><span>34</span><span></<span class="end-tag">spot_id</span>><br>
</span><span> </span><span></span><span><<span class="start-tag">spot_name</span>></span><span>Aroca</span><span></<span class="end-tag">spot_name</span>><br></span><span><span> </span><<span class="start-tag">spot_geopath</span>></span><span>/spot/Aroca</span><span></<span class="end-tag">spot_geopath</span>></span><span></span><span><br>
</span><span><span> </span><<span class="start-tag">gps_lat</span>></span><span>43.427700</span><span></<span class="end-tag">gps_lat</span>></span><span></span><span><br></span><span><span> </span><<span class="start-tag">gps_long</span>></span><span>-1.667850</span><span></<span class="end-tag">gps_long</span>></span><span></span><span><br>
</span><span><span> </span></<span class="end-tag">wd-34</span>></span><span></span><span><br> <<span class="start-tag">wd-38</span>><br></span><span> </span><span></span><span><<span class="start-tag">spot_id</span>></span><span>38</span><span></<span class="end-tag">spot_id</span>></span><span></span><span><br>
</span><span><span> </span><<span class="start-tag">spot_name</span>></span><span>Ficala</span><span></<span class="end-tag">spot_name</span>></span><span></span><span><br></span><span><span> </span><<span class="start-tag">spot_geopath</span>></span><span>/spot/Ficala</span><span></<span class="end-tag">spot_geopath</span>><br>
</span><span> </span><span></span><span><<span class="start-tag">gps_lat</span>></span><span>43.477075</span><span></<span class="end-tag">gps_lat</span>><br></span><span> </span><span></span><span><<span class="start-tag">gps_long</span>></span><span>-1.627072</span><span></<span class="end-tag">gps_long</span>></span><span></span><span><br>
</span><span><span> </span></<span class="end-tag">wd-38</span>></span><span></span><span><br> </span><span></span><span></<span class="end-tag">search_result</span>><br></span><span></span><span></<span class="end-tag">response</span>><br>
</span><span></span><span><<span class="start-tag">response_time</span>></span><span>28</span><span></<span class="end-tag">response_time</span>><br></span><span></span><span><<span class="start-tag">cache_date</span>></span><span>2013-07-18 08:12:17</span><span></<span class="end-tag">cache_date</span>></span><span></span><span><br>
</<span class="end-tag">wplatform</span>></span><span>
<span id="line3"></span></span></pre><br>I would like to display this XML geo-points into OpenLayers as a custom layer.<br><br>I have seen a previous post from Bart (<a href="http://lists.osgeo.org/pipermail/openlayers-users/2008-October/008064.html">http://lists.osgeo.org/pipermail/openlayers-users/2008-October/008064.html</a>) describing a method to build my own OpenLayers.Format class. I also found an example of custom format in the OpenLayers wiki (<a href="http://docs.openlayers.org/library/formats.html#creating-custom-formats">http://docs.openlayers.org/library/formats.html#creating-custom-formats</a>) but I failed to generate a valid output response. Here is a part of my test code:<br>
<br>var map;<br>var WGS84 = new OpenLayers.Projection("EPSG:4326");<br>var WGS84_google_mercator = new OpenLayers.Projection("EPSG:900913");<br>...<br><br><br>/**<br> * @requires OpenLayers/Format/XML.js<br>
*/<br>OpenLayers.Format.MyTestWebservice = OpenLayers.Class(<br> OpenLayers.Format.XML, {<br> initialize: function(options) {<br> OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);<br> this.options = options;<br>
},<br> read: function(data) {<br> if(typeof data == "string") {<br> data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);<br> }<br> var root = data.documentElement;<br>
var spots = root.childNodes[0].childNodes[0].childNodes;<br> var features = [];<br> var txt = new OpenLayers.Format.Text();<br>/* Try to add header like that? but this does not work either...<br> var txtstr = "lat\tlon\ttitle\tdescription\ticonSize\ticonOffset\ticon";<br>
console.log('DEBUG XML PARSER: ' + txtstr);<br> features.push(txt.read(txtstr));<br>*/ <br> for ( i=0; i < spots.length ; i++) {<br> var spot = spots[i];<br> var spot_name = spot.getElementsByTagName("spot_name")[0].childNodes[0].nodeValue;<br>
var gps_lat = spot.getElementsByTagName("gps_lat")[0].childNodes[0].nodeValue;<br> var gps_long = spot.getElementsByTagName("gps_long")[0].childNodes[0].nodeValue;<br> var spot_geopath = spot.getElementsByTagName("spot_geopath")[0].childNodes[0].nodeValue;<br>
//console.log('DEBUG XML PARSER: ' + spot_name + ' ' + gps_lat + '/' + gps_long + ' ' + spot_geopath);<br> <br> var txtstr = gps_lat + "\t" + gps_long + "\t" + spot_name + "\tDESC.TEST\t21,25\t21,25\t-10,-25\thttp://<a href="http://www.openlayers.org/dev/img/marker.png">www.openlayers.org/dev/img/marker.png</a>";<br>
//console.log('DEBUG XML PARSER: ' + txtstr);<br> features.push(txt.read(txtstr));<br> }<br><br> console.log(features);<br> return features;<br> },<br> CLASS_NAME: "OpenLayers.Format.MyTestWebservice"<br>
}); <br><br>...<br> var w_url = "/maps/px.php?url=<a href="http://webservices.example.com">webservices.example.com</a>"<br> wVectorLayer = new OpenLayers.Layer.Vector("wVectorLayer", {<br> strategies: [new OpenLayers.Strategy.BBOX({ratio:2, resFactor: 3})],<br>
projection: WGS84,<br> protocol: new OpenLayers.Protocol.HTTP({<br> url: w_url,<br> params: {<br> type: 'webservice',<br> layers: 'scuba'<br>
},<br> format: new OpenLayers.Format.MyTestWebservice()<br> }),<br> visibility: true<br> });<br>...<br>map.addLayers([osm, wVectorLayer]);<br>...<br><br>The result in the console shows that the XML response is parsed well. I can access all nodes information (name, long, lat etc.). I tried to generate what I think was the simplest output format (OpenLayers.Format.Text) so I just build an array of Text objects, then return this array.<br>
<br>On my map, no points are shown. The Firebug Javascript console displays an array with <span class="objectBox objectBox-array "><span class="objectBox objectBox-array opened"><span class="arrayProperties "><span class="objectBox objectBox-string ">OpenLayers.Layer.Vector objects (which seems good). I don't really know at this point what is wrong. What output format I really have to return and how I can debug this. I also have no idea for the </span></span></span></span>OpenLayers.Format.Text if I need to send a header or not with the list of the TSV columns?<br>
<br>I guess it would be much better if the webservice could render a KML or other "known" format. A workaround would be to work on this but just for fun, I would like to make it work like this.<br><br>Other question, does this method (Custom Format and DOM XML manipulation) is good from a performance point of view compared to a built-in KML format?<br>
<br>Any help appreciated.<br><br>All the best,<br><br>Chris<br>