[OpenLayers-Dev] saving a vector file permanently

Slawomir Messner slawomir.messner at staff.uni-marburg.de
Fri Jan 21 09:32:10 EST 2011


Hi,
we have/had the same "issue"(luckily we could change to server-side 
formatting). If you call the function below with 
"exportToFormat(mylayer,'kml')" you will get a download dialogue(tested 
with FF 3.6-4.0), !!but!! the write-function of OpenLayers.Format.KML(I 
think with json it's the same) is not perfect because style info is 
missing, if you're will enhance it.

exportToFormat:function  (layer, format) {
           if  (layerinstanceof  OpenLayers.Layer.Vector&&  layer.features.length == 0) {
               return;
           }
           var  formatText =null;
           var  mimeType =null;
           if  (format =='svg') {
               mimeType ="image/svg+xml";
               formatText = document.getElementById(layer.id).innerHTML;
           }else  if  (format =='kml') {
               mimeType ="application/vnd.google-earth.kml+xml";
               var  format =new  OpenLayers.Format.KML(
               { extractAttributes:true,
                   extractTracks:true,
                   internalProjection :new  OpenLayers.Projection("EPSG:900913"),
                   externalProjection :new  OpenLayers.Projection("EPSG:4326")
               }
               );
               var  features =new  Array();
               var  operation =new  OpenLayers.Operation.CopyFeatureToLayer();
               for  (var  i = 0; i<  layer.features.length; i++) {
                   var  newfeature =new  OpenLayers.Feature.Vector(layer.features[i].geometry.clone());
                   newfeature.style = layer.features[i].style || layer.styleMap.createSymbolizer(layer.features[i], layer.features[i].renderIntent ||'default');
                   newfeature.attributes = operation.clone(layer.features[i].attributes);
                   features.push(newfeature);
               }
               formatText = format.write(features);
           }
           if  (formatText !=null) {
               open("data:"  + mimeType +","  + encodeURIComponent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>  "  + formatText));
           }
       },



Am 21.01.2011 15:17, schrieb David Zwarg:
> Hello Mohammed,
>
> Look at the OpenLayers.Format classes.
>
> Here's a link to the geojson format example: 
> http://dev.openlayers.org/apidocs/files/OpenLayers/Format/GeoJSON-js.html
>
> You will probably do something like:
>
> var format = new OpenLayers.Format.GeoJSON();
>
> // given that 'layer' is a vector layer attached to the map with some 
> features:
> var jsonstring = format.write( layer.features );
>
> // given that 'txtarea' is an HTML textarea element somewhere in your 
> page to display the geojson:
> txtarea.value = jsonstring;
>
> There's no way to save the file directly via openlayers (opening a 
> "Save File..." dialog), you have to do some copy & pasting from the 
> browser.
>
> -z
>
> On Fri, Jan 21, 2011 at 8:51 AM, Mohammed Rashad 
> <mohammedrashadkm at gmail.com <mailto:mohammedrashadkm at gmail.com>> wrote:
>
>     How can I save a OpenLayers.Layer.Vector to a gml, geojson or any
>     openlayer supported formats without using
>      WFS or WFS- T( without geoserver,postgis);
>
>     Is it possible?
>
>     -- 
>     Rashad
>
>     _______________________________________________
>     Dev mailing list
>     Dev at lists.osgeo.org <mailto:Dev at lists.osgeo.org>
>     http://lists.osgeo.org/mailman/listinfo/openlayers-dev
>
>
>
> _______________________________________________
> Dev mailing list
> Dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20110121/447f71d0/attachment-0001.html


More information about the Dev mailing list