[OpenLayers-Users] Custom OpenLayers.Format.XML class

Chris cnaslain at gmail.com
Thu Jul 18 02:10:05 PDT 2013


Hi all,

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...

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:

<?xml version="1.0"?><wplatform version="1.0.16  Mon Mar 4 16:53:30 CET 2013 ">
<response>

  <search_result>
    <wd-34>
      <spot_id>34</spot_id>
      <spot_name>Aroca</spot_name>
      <spot_geopath>/spot/Aroca</spot_geopath>
      <gps_lat>43.427700</gps_lat>
      <gps_long>-1.667850</gps_long>
    </wd-34>
    <wd-38>
      <spot_id>38</spot_id>
      <spot_name>Ficala</spot_name>
      <spot_geopath>/spot/Ficala</spot_geopath>
      <gps_lat>43.477075</gps_lat>
      <gps_long>-1.627072</gps_long>
    </wd-38>
  </search_result>
</response>
<response_time>28</response_time>
<cache_date>2013-07-18 08:12:17</cache_date>

</wplatform>


I would like to display this XML geo-points into OpenLayers as a custom
layer.

I have seen a previous post from Bart (
http://lists.osgeo.org/pipermail/openlayers-users/2008-October/008064.html)
describing a method to build my own OpenLayers.Format class. I also found
an example of custom format in the OpenLayers wiki (
http://docs.openlayers.org/library/formats.html#creating-custom-formats)
but I failed to generate a valid output response. Here is a part of my test
code:

var map;
var WGS84 = new OpenLayers.Projection("EPSG:4326");
var WGS84_google_mercator = new OpenLayers.Projection("EPSG:900913");
...


/**
 * @requires OpenLayers/Format/XML.js
 */
OpenLayers.Format.MyTestWebservice = OpenLayers.Class(
    OpenLayers.Format.XML, {
    initialize: function(options) {
        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
        this.options = options;
    },
    read: function(data) {
        if(typeof data == "string") {
            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
        }
    var root = data.documentElement;
    var spots = root.childNodes[0].childNodes[0].childNodes;
    var features = [];
    var txt = new OpenLayers.Format.Text();
/* Try to add header like that? but this does not work either...
    var txtstr = "lat\tlon\ttitle\tdescription\ticonSize\ticonOffset\ticon";
    console.log('DEBUG XML PARSER: ' + txtstr);
    features.push(txt.read(txtstr));
*/
    for ( i=0; i < spots.length ; i++) {
       var spot = spots[i];
       var spot_name =
spot.getElementsByTagName("spot_name")[0].childNodes[0].nodeValue;
       var gps_lat =
spot.getElementsByTagName("gps_lat")[0].childNodes[0].nodeValue;
       var gps_long =
spot.getElementsByTagName("gps_long")[0].childNodes[0].nodeValue;
       var spot_geopath =
spot.getElementsByTagName("spot_geopath")[0].childNodes[0].nodeValue;
       //console.log('DEBUG XML PARSER: ' + spot_name + ' ' + gps_lat + '/'
+ gps_long + ' ' + spot_geopath);

       var txtstr = gps_lat + "\t" + gps_long + "\t" + spot_name +
"\tDESC.TEST\t21,25\t21,25\t-10,-25\thttp://
www.openlayers.org/dev/img/marker.png";
       //console.log('DEBUG XML PARSER: ' + txtstr);
       features.push(txt.read(txtstr));
    }

    console.log(features);
    return features;
    },
    CLASS_NAME: "OpenLayers.Format.MyTestWebservice"
});

...
    var w_url = "/maps/px.php?url=webservices.example.com"
    wVectorLayer = new OpenLayers.Layer.Vector("wVectorLayer", {
        strategies: [new OpenLayers.Strategy.BBOX({ratio:2, resFactor: 3})],
        projection: WGS84,
        protocol: new OpenLayers.Protocol.HTTP({
            url: w_url,
            params: {
                    type: 'webservice',
                    layers: 'scuba'
            },
            format: new OpenLayers.Format.MyTestWebservice()
        }),
        visibility: true
    });
...
map.addLayers([osm, wVectorLayer]);
...

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.

On my map, no points are shown. The Firebug Javascript console displays an
array with 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
OpenLayers.Format.Text
if I need to send a header or not with the list of the TSV columns?

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.

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?

Any help appreciated.

All the best,

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130718/751f0d7b/attachment.html>


More information about the Users mailing list