开发者

get kmz file from controller

开发者 https://www.devze.com 2023-02-19 06:15 出处:网络
I am trying to use some code taken here: to respond with a kmz file given a GET request. This is my code:

I am trying to use some code taken here:

to respond with a kmz file given a GET request. This is my code:

public void GetKMZ()
{
    this.Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=GoogleMap.kmz");
        this.Response.ContentType = "application/vnd.google-earth.kmz";

        this.Response.AppendHeader("Content-Encoding", "kmz");

    byte[] bytes = null;
    MemoryStream memStream = new MemoryStream();
    XmlTextWriter xmlTW = new XmlTextWriter(memStream,Encoding.UTF8);

    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();

    xmlTW.WriteStartElement("kml");
    xmlTW.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
    xmlTW.WriteStartElement("Document");
        xmlTW.WriteStartElement("Style");
        xmlTW.WriteAttributeString("id", "s1");
        xmlTW.WriteStartElement("LineStyle");
        xmlTW.WriteElementString("color", "7f0000ff");
        xmlTW.WriteElementString("width", "3");
        xmlTW.WriteEndElement();
        xmlTW.WriteEndElement();

        xmlTW.WriteElementString("name", "Chicago Transit Map");
        xmlTW.WriteElementString("description", "Chicago Transit Authority train lines");

        xmlTW.WriteStartElement("Placemark");
            xmlTW.WriteElementString("styleUrl", "#s1");
            xmlTW.WriteElementString("name", "Chicago Transit Map");
            xmlTW.WriteStartElement("LineString");
                xmlTW.WriteElementString("altitudeMode", "relative");
                xmlTW.WriteElementString("coordinates", "-87.89289951324463,41.97881025520548,0 -87.89184808731079,41.97788506340239,0 -87.89150476455688,41.97762983571196,0");

            xmlTW.WriteEndElement();
            xmlTW.WriteEndElement();

        xmlTW.WriteEndElement();

    xmlTW.WriteEndElement(); //Document

    xmlTW.WriteEndDocument(); // kml
    xmlTW.Close();

    bytes = memStream.ToArray(); // vs .GetBuffer();

    MemoryStream memStream2 = new MemoryStream();
    using (ZipOutputStream gzOs = new ZipOutputStream(memStream2))
    {
    ZipEntry entry = new ZipEntry("GoogleMap.kml");
    gzOs.SetLevel(9);
    gzOs.PutNextEntry(entry);
    gzOs.Write(bytes, 0, bytes.Length);
    gzOs.CloseEntry();
    gzOs.Close();
 开发者_运维百科   }
    this.Response.Clear();
    this.Response.BinaryWrite(memStream2.ToArray());
    this.Response.End();
}

The produced kmz file works if I unzip it as kml file using winrar (i.e. I can open it in google map). Unfortunately, google earth/map does not like the kmz file the GET request produces. I am using ASP.NET MVC 3. Could the zip dll (compiled for .net 2.0):

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

be a problem?

Thanks.

Christian


Things seem to work with DotNetZip:

// http://stackoverflow.com/questions/4717605/asp-net-vb-kml-generator
[AcceptVerbs(HttpVerbs.Get)]
public void GetKMZ()
{
    Response.AppendHeader("Connection", "close");
    Response.ContentType = "application/vnd.google-earth.kmz";
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

    ZipOutputStream outzip = new ZipOutputStream(Response.OutputStream);
    outzip.EnableZip64 = Zip64Option.Never;
    outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    outzip.PutNextEntry("doc.kml");

    XmlTextWriter xmlTW = new XmlTextWriter(outzip,Encoding.UTF8);

    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();

    xmlTW.WriteStartElement("kml");
    xmlTW.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
    xmlTW.WriteStartElement("Document");
        xmlTW.WriteStartElement("Style");
        xmlTW.WriteAttributeString("id", "s1");
        xmlTW.WriteStartElement("LineStyle");
        xmlTW.WriteElementString("color", "7f0000ff");
        xmlTW.WriteElementString("width", "3");
        xmlTW.WriteEndElement();
        xmlTW.WriteEndElement();

        xmlTW.WriteElementString("name", "Chicago Transit Map");
        xmlTW.WriteElementString("description", "Chicago Transit Authority train lines");

        xmlTW.WriteStartElement("Placemark");
            xmlTW.WriteElementString("styleUrl", "#s1");
            xmlTW.WriteElementString("name", "Chicago Transit Map");
            xmlTW.WriteStartElement("LineString");
                xmlTW.WriteElementString("altitudeMode", "relative");
                xmlTW.WriteElementString("coordinates", "-87.89289951324463,41.97881025520548,0 -87.89184808731079,41.97788506340239,0 -87.89150476455688,41.97762983571196,0");

            xmlTW.WriteEndElement();
            xmlTW.WriteEndElement();

        xmlTW.WriteEndElement();

    xmlTW.WriteEndElement(); //Document

    xmlTW.WriteEndDocument(); // kml

    xmlTW.Flush();
    outzip.Close();
} 


Slight variation of csetzkorn's, and a little more generic (works in a ashx mode, takes kml as string)

    using Ionic.Zip;

public void KMLtoKMZ(HttpContext context, string kml)
        {




            context.Response.ContentType = "application/vnd.google-earth.kmz";
            context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

            ZipOutputStream outzip = new ZipOutputStream(context.Response.OutputStream);
            outzip.EnableZip64 = Zip64Option.Never;
            outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            outzip.PutNextEntry("doc.kml");
            StreamWriter sw = new StreamWriter(outzip);
            sw.Write(kml);
            sw.Flush();

            outzip.Flush();
            outzip.Close();



        }
0

精彩评论

暂无评论...
验证码 换一张
取 消