[OpenLayers-Users] GetFeature VS MapScript generated csv file

Alexandre Dube adube at mapgears.com
Fri Aug 15 16:02:31 EDT 2008


Hi all,

    In my application, I dynamically build a <select> element with 
cities as options using a GetFeature request and reading from GML format 
to generate it.

    It works perfectly but is kinda slow because of the size of the GML 
format.  A dev and friend of mine suggested that I should generate my 
own text file using phpMapScript.  It was quite easy to do and works 
perfectly.  It generates a file which uses the same format as the 
OpenLayers.Format.Text ( tab separated values, coordinates and title as 
city name ).

    Now I want to replace the GML format by Text format but don't really 
understand how this must be done.

    I tried this :
        OpenLayers.loadURL("/bdga/csvCityBuilder.php", '', this, setCities);
    and the file is created...  but my php file is not a standard 
request.  It doesn't return anything, it only creates a file.

    I could load the generated file with standard javascript function, 
but I was wondering if there is already something in OpenLayers I could 
use, like the following ( which obviously isn't working ).  Maybe I'm 
missing something.
        oText = new OpenLayers.Format.Text();
        oFeatures = oText.read(response.responseText);

    Bellow is the GML version of it which works fine.  Any help would be 
appreciated.

// a GetFeature on layer BDGA_HABIT_P_POINT to get all cities and their
// coordinates.  See setCites function for more info.
sUrl = sMSURL;
sUrl += "?map=";
sUrl += sBDGAMapPath;
sUrl += "&service=WFS&version=1.0.0&request=GetFeature";
sUrl += "&TYPENAME=BDGA_HABIT_P_POINT";
OpenLayers.loadURL(sUrl, '', this, setCities);

// add one option per city to a select html element with value equal to
// the city coordinates ( longitude, latitude )
function setCities(response) {
    oGML =  new OpenLayers.Format.GML();
    oFeatures = oGML.read(response.responseText);

    var aCities = new Array();

    // put the results in an array first to be able to sort it
    for (var key in oFeatures){
        var szValue = "";
        szValue += oFeatures[key].geometry.x;
        szValue += ",";
        szValue += oFeatures[key].geometry.y

        aCities[key] = {
          cityName: oFeatures[key].attributes['HAP_NM_TOP'],
          lonlat: szValue
        };
    }

    // sort the array by city name
    aCities.sort(sortByCityName);

    // create on option per city and add it to the select element
    var oSelect=document.getElementById("zoomToCity");
    for (i=0; i<aCities.length; i++){
        var oOption = document.createElement('option');
        oOption.text=aCities[i]['cityName'];
        oOption.value=aCities[i]['lonlat'];
        try
        {
            oSelect.add(oOption,null); // standards compliant
        }    
        catch(ex)
        {
            oSelect.add(oOption); // IE only
        }
    }

    // The list has finished to be created, so enable it and remove the 
first
    // option that contained the "please wait" msg
    oSelect.disabled = "";
    oSelect.remove(0);
}

// returns the aCities array sorted by cityName
function sortByCityName(a, b) {
    var x = a.cityName.toLowerCase();
    var y = b.cityName.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


-- 
Alexandre Dubé
Mapgears
www.mapgears.com




More information about the Users mailing list