[OpenLayers-Users] GetFeature VS MapScript generated csv file
Alexandre Dube
adube at mapgears.com
Mon Aug 18 09:58:13 EDT 2008
Hi Gilles,
Ok, I'll try that. Thanks a lot for the help.
Alexandre
Gilles Bassière wrote:
> Hi Alexandre,
>
> I assume your service create a file on the server side. That's
> probably not what you want unless you need to store your CSV for later
> reuse. Your PHP script should output the CSV data instead of writing
> it to disk:
> - either you keep your existing script and eventually output the file
> with the PHP readfile() function (or equivalent)
> - or you can simply print your data to standard output with basic echo
> statements.
>
> In both case you may consider using the header function to specify the
> correct mime-type of the output (e.g. header("Content-type:
> text/plain"); or something like that).
>
> Hope it helps,
> Gilles
>
> Alexandre Dube wrote:
>> Hi Eric,
>>
>> My service only creates a file. When I try to read
>> response.responseText, it's empty. I could just read the file on the
>> file system but it seems to me that it's the wrong way of doing it.
>> I would prefer the standard way of returning the content. Can you
>> give me a hint on how to do it ?
>>
>> Many thanks,
>>
>> Alexandre
>>
>> Eric Lemoine wrote:
>>
>>> Hi. What do you mean when you say your php service does return
>>> anything and just create a file? Do you mean your service just creates
>>> a file on the file system? Really it should send the text in the
>>> response, so that you can most probably replace OpenLayers.Format.GML
>>> with OpenLayers.Format.Text and i'll just work. I'm hoping i've
>>> understood your question correctly. Eric
>>>
>>> 2008/8/15, Alexandre Dube <adube at mapgears.com>:
>>>
>>>> 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
>>>>
>>>> _______________________________________________
>>>> Users mailing list
>>>> Users at openlayers.org
>>>> http://openlayers.org/mailman/listinfo/users
>>>>
>>>>
>>
>>
>>
>
--
Alexandre Dubé
Mapgears
www.mapgears.com
More information about the Users
mailing list