Hello Slawomir,<br><br>That's crafty! I didn't know you could do that trick ("open('data:...')". Good to know. :)<br><br>-z<br><br><div class="gmail_quote">On Fri, Jan 21, 2011 at 9:32 AM, Slawomir Messner <span dir="ltr"><<a href="mailto:slawomir.messner@staff.uni-marburg.de">slawomir.messner@staff.uni-marburg.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div text="#000000" bgcolor="#ffffff">
Hi,<br>
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.<br>
<pre style="font-family: consolas;">exportToFormat: <span style="color: blue;">function</span> (layer, format) {
<span style="color: blue;">if</span> (layer <span style="color: blue;">instanceof</span> OpenLayers.Layer.Vector && layer.features.length == 0) {
<span style="color: blue;">return</span>;
}
<span style="color: blue;">var</span> formatText = <span style="color: blue;">null</span>;
<span style="color: blue;">var</span> mimeType = <span style="color: blue;">null</span>;
<span style="color: blue;">if</span> (format == <span style="color: maroon;">'svg'</span>) {
mimeType = <span style="color: maroon;">"image/svg+xml"</span>;
formatText = document.getElementById(<a href="http://layer.id" target="_blank">layer.id</a>).innerHTML;
} <span style="color: blue;">else</span> <span style="color: blue;">if</span> (format == <span style="color: maroon;">'kml'</span>) {
mimeType = <span style="color: maroon;">"application/vnd.google-earth.kml+xml"</span>;
<span style="color: blue;">var</span> format = <span style="color: blue;">new</span> OpenLayers.Format.KML(
{ extractAttributes: <span style="color: blue;">true</span>,
extractTracks: <span style="color: blue;">true</span>,
internalProjection : <span style="color: blue;">new</span> OpenLayers.Projection(<span style="color: maroon;">"EPSG:900913"</span>),
externalProjection : <span style="color: blue;">new</span> OpenLayers.Projection(<span style="color: maroon;">"EPSG:4326"</span>)
}
);
<span style="color: blue;">var</span> features = <span style="color: blue;">new</span> Array();
<span style="color: blue;">var</span> operation = <span style="color: blue;">new</span> OpenLayers.Operation.CopyFeatureToLayer();
<span style="color: blue;">for</span> (<span style="color: blue;">var</span> i = 0; i < layer.features.length; i++) {
<span style="color: blue;">var</span> newfeature = <span style="color: blue;">new</span> OpenLayers.Feature.Vector(layer.features[i].geometry.clone());
newfeature.style = layer.features[i].style || layer.styleMap.createSymbolizer(layer.features[i], layer.features[i].renderIntent || <span style="color: maroon;">'default'</span>);
newfeature.attributes = operation.clone(layer.features[i].attributes);
features.push(newfeature);
}
formatText = format.write(features);
}
<span style="color: blue;">if</span> (formatText != <span style="color: blue;">null</span>) {
open(<span style="color: maroon;">"data:"</span> + mimeType + <span style="color: maroon;">","</span> + encodeURIComponent(<span style="color: maroon;">"<?xml version=\"1.0\" encoding=\"UTF-8\"?> "</span> + formatText));
}
},</pre>
<br>
<br>
Am 21.01.2011 15:17, schrieb David Zwarg:
<div><div></div><div class="h5"><blockquote type="cite">Hello Mohammed,<br>
<br>
Look at the OpenLayers.Format classes.<br>
<br>
Here's a link to the geojson format example: <a href="http://dev.openlayers.org/apidocs/files/OpenLayers/Format/GeoJSON-js.html" target="_blank">http://dev.openlayers.org/apidocs/files/OpenLayers/Format/GeoJSON-js.html</a><br>
<br>
You will probably do something like:<br>
<br>
<span style="font-family: courier new,monospace; color: rgb(0, 0, 153);">var format = new OpenLayers.Format.GeoJSON();</span><br style="font-family: courier new,monospace; color: rgb(0, 0, 153);">
<br style="font-family: courier new,monospace; color: rgb(0, 0, 153);">
<span style="font-family: courier new,monospace; color: rgb(0, 0, 153);">// given that 'layer' is a vector layer attached to the
map with some features:</span><br style="font-family: courier new,monospace; color: rgb(0, 0, 153);">
<span style="font-family: courier new,monospace; color: rgb(0, 0, 153);">var jsonstring = format.write( layer.features );</span><br style="font-family: courier new,monospace; color: rgb(0, 0, 153);">
<br style="font-family: courier new,monospace; color: rgb(0, 0, 153);">
<span style="font-family: courier new,monospace; color: rgb(0, 0, 153);">// given that 'txtarea' is an HTML textarea element
somewhere in your page to display the geojson:</span><br style="font-family: courier new,monospace; color: rgb(0, 0, 153);">
<span style="font-family: courier new,monospace; color: rgb(0, 0, 153);">txtarea.value = jsonstring;</span><br>
<br>
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.<br>
<br>
-z<br>
<br>
<div class="gmail_quote">On Fri, Jan 21, 2011 at 8:51 AM, Mohammed
Rashad <span dir="ltr"><<a href="mailto:mohammedrashadkm@gmail.com" target="_blank">mohammedrashadkm@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
How can I save a OpenLayers.Layer.Vector to a gml, geojson or
any openlayer supported formats without using
<div> WFS or WFS- T( without geoserver,postgis);</div>
<div><br>
</div>
<div>Is it possible?<br clear="all">
<br>
-- <br>
Rashad<br>
</div>
<br>
_______________________________________________<br>
Dev mailing list<br>
<a href="mailto:Dev@lists.osgeo.org" target="_blank">Dev@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/openlayers-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/openlayers-dev</a><br>
<br>
</blockquote>
</div>
<br>
<pre><fieldset></fieldset>
_______________________________________________
Dev mailing list
<a href="mailto:Dev@lists.osgeo.org" target="_blank">Dev@lists.osgeo.org</a>
<a href="http://lists.osgeo.org/mailman/listinfo/openlayers-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/openlayers-dev</a>
</pre>
</blockquote>
<br>
</div></div></div>
<br>_______________________________________________<br>
Dev mailing list<br>
<a href="mailto:Dev@lists.osgeo.org">Dev@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/openlayers-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/openlayers-dev</a><br>
<br></blockquote></div><br>