ASP.NET VB KML 生成器

发布于 2024-10-12 10:55:47 字数 6984 浏览 5 评论 0原文

下面是一个生成 KMZ 文件的处理程序 (ashx) 文件(可以通过不使用 Ionic 压缩类(Codeplex 上的 Ionic = DotNetZip)来生成 KML)。该处理程序使用 SQL 表来填充地点标记,但它是 ashx 处理程序的一个很好的示例使用、自定义图标使用、正确压缩文件以及填充位置标记通过使用 context.Response.OutputStream,这在 .NET 引擎中非常高效。

<%@ WebHandler Language="VB" Class="YourKML" %>

Imports System
Imports System.Web
Imports System.Web.Configuration
Imports System.Xml
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Types
Imports MySql.Data.MySqlClient
Imports Ionic.Zip

Public Class YourKML : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.AppendHeader("Connection", "close")
        'Response.ContentType = "application/vnd.google-earth.kml+xml"
        'Response.ContentEncoding = System.Text.Encoding.UTF8
        context.Response.ContentType = "application/vnd.google-earth.kmz"
        context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)

        Dim outzip As New ZipOutputStream(context.Response.OutputStream)
        outzip.EnableZip64 = Zip64Option.Never
        outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
        outzip.PutNextEntry("doc.kml")

        Dim kmlout As New XmlTextWriter(outzip, System.Text.Encoding.UTF8)
        kmlout.WriteStartDocument()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("kml")
        kmlout.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Document")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteElementString("name", "doc.kml")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Style")
        kmlout.WriteAttributeString("id", "yoursym")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("IconStyle")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Icon")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteElementString("href", "http://youriconimage")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("hotSpot")
        kmlout.WriteAttributeString("x", "0.5")
        kmlout.WriteAttributeString("y", "0.5")
        kmlout.WriteAttributeString("xunits", "fraction")
        kmlout.WriteAttributeString("yunits", "fraction")
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        PlacesKML(kmlout)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndDocument()
        kmlout.Flush()
        outzip.Close()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

    Sub PlacesKML(ByVal kmlout As XmlTextWriter)
        Using connection As New SqlConnection(WebConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString)
            connection.Open()
            Dim cmd As New SqlCommand("SELECT yourkey, Location FROM yourtable", connection)
            Dim positrs As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            Dim curkey As String = ""
            Dim comment As StringBuilder = New StringBuilder()
            Dim loc As New SqlGeography()
            With positrs
                While .Read()
                    Dim yourkey As String = .GetString(.GetOrdinal("yourkey"))
                    If String.IsNullOrEmpty(yourkey) Then Continue While
                    If yourkey <> curkey Then
                        If Not String.IsNullOrEmpty(curkey) Then
                            kmlout.WriteStartElement("Placemark")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("name", curkey)
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteStartElement("description")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteCData(comment.ToString())
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Description
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("styleUrl", "#yoursym")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteStartElement("Point")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("coordinates", loc.Long.ToString() & ", " & loc.Lat.ToString() & ", 0")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Point
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Place Mark
                            kmlout.WriteWhitespace(vbCrLf)
                        End If
                        curkey = yourkey
                        comment.Length = 0
                        comment.Append(yourkey)
                        loc = .GetProviderSpecificValue(.GetOrdinal("Location"))
                    Else
                        comment.Append("<br/>").Append(yourkey)
                    End If
                End While
                If Not String.IsNullOrEmpty(curkey) Then
                    kmlout.WriteStartElement("Placemark")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("name", curkey)
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteStartElement("description")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteCData(comment.ToString())
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Description
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("styleUrl", "#yoursym")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteStartElement("Point")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("coordinates", loc.Long.ToString() & ", " & loc.Lat.ToString() & ", 0")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Point
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Place Mark
                    kmlout.WriteWhitespace(vbCrLf)
                End If
            End With
            positrs.Close()
        End Using
    End Sub
End Class

Here is a handler (ashx) file that generates KMZ files (can generate KML by not using the Ionic compression classes (Ionic = DotNetZip at Codeplex). This handler uses an SQL table to populate the place marks but is a good example of ashx handler use, custom icon use, properly compressing the file, and populating place marks. By using context.Response.OutputStream, this is very efficient within the .NET engine.

<%@ WebHandler Language="VB" Class="YourKML" %>

Imports System
Imports System.Web
Imports System.Web.Configuration
Imports System.Xml
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Types
Imports MySql.Data.MySqlClient
Imports Ionic.Zip

Public Class YourKML : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.AppendHeader("Connection", "close")
        'Response.ContentType = "application/vnd.google-earth.kml+xml"
        'Response.ContentEncoding = System.Text.Encoding.UTF8
        context.Response.ContentType = "application/vnd.google-earth.kmz"
        context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)

        Dim outzip As New ZipOutputStream(context.Response.OutputStream)
        outzip.EnableZip64 = Zip64Option.Never
        outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
        outzip.PutNextEntry("doc.kml")

        Dim kmlout As New XmlTextWriter(outzip, System.Text.Encoding.UTF8)
        kmlout.WriteStartDocument()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("kml")
        kmlout.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Document")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteElementString("name", "doc.kml")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Style")
        kmlout.WriteAttributeString("id", "yoursym")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("IconStyle")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Icon")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteElementString("href", "http://youriconimage")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("hotSpot")
        kmlout.WriteAttributeString("x", "0.5")
        kmlout.WriteAttributeString("y", "0.5")
        kmlout.WriteAttributeString("xunits", "fraction")
        kmlout.WriteAttributeString("yunits", "fraction")
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        PlacesKML(kmlout)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndDocument()
        kmlout.Flush()
        outzip.Close()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

    Sub PlacesKML(ByVal kmlout As XmlTextWriter)
        Using connection As New SqlConnection(WebConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString)
            connection.Open()
            Dim cmd As New SqlCommand("SELECT yourkey, Location FROM yourtable", connection)
            Dim positrs As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            Dim curkey As String = ""
            Dim comment As StringBuilder = New StringBuilder()
            Dim loc As New SqlGeography()
            With positrs
                While .Read()
                    Dim yourkey As String = .GetString(.GetOrdinal("yourkey"))
                    If String.IsNullOrEmpty(yourkey) Then Continue While
                    If yourkey <> curkey Then
                        If Not String.IsNullOrEmpty(curkey) Then
                            kmlout.WriteStartElement("Placemark")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("name", curkey)
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteStartElement("description")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteCData(comment.ToString())
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Description
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("styleUrl", "#yoursym")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteStartElement("Point")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("coordinates", loc.Long.ToString() & ", " & loc.Lat.ToString() & ", 0")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Point
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Place Mark
                            kmlout.WriteWhitespace(vbCrLf)
                        End If
                        curkey = yourkey
                        comment.Length = 0
                        comment.Append(yourkey)
                        loc = .GetProviderSpecificValue(.GetOrdinal("Location"))
                    Else
                        comment.Append("<br/>").Append(yourkey)
                    End If
                End While
                If Not String.IsNullOrEmpty(curkey) Then
                    kmlout.WriteStartElement("Placemark")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("name", curkey)
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteStartElement("description")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteCData(comment.ToString())
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Description
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("styleUrl", "#yoursym")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteStartElement("Point")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("coordinates", loc.Long.ToString() & ", " & loc.Lat.ToString() & ", 0")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Point
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Place Mark
                    kmlout.WriteWhitespace(vbCrLf)
                End If
            End With
            positrs.Close()
        End Using
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-10-19 10:55:47

这是一个例子,不是一个问题。希望它能帮助其他想要使用 VB.net 和 ASP.NET 创建 kml/kmz 文件的人。

This is an example, not a question. Hopefully it will help others looking to use VB.net and ASP.NET to create kml/kmz files.

小ぇ时光︴ 2024-10-19 10:55:47

我还有另一个严格使用 SharpKml 的例子。

public void ProcessRequest(HttpContext context)
{

    context.Response.AppendHeader("Connection", "close");
    context.Response.ContentType = "application/vnd.google-earth.kmz";
    context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    context.Response.AddHeader("Content-Disposition", "attachment; filename=mykmz.kmz");

    Point point = new Point();
    point.Coordinate = new Vector(-13.163959, -72.545992);

    Placemark placemark = new Placemark();
    placemark.Geometry = point;
    placemark.Name = "Machu Picchu";

    KmlFile kml = KmlFile.Create(placemark, false);

    KmzFile kmz = KmzFile.Create(kml);
    kmz.Save(context.Response.OutputStream);

}

I have another example strictly using SharpKml.

public void ProcessRequest(HttpContext context)
{

    context.Response.AppendHeader("Connection", "close");
    context.Response.ContentType = "application/vnd.google-earth.kmz";
    context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    context.Response.AddHeader("Content-Disposition", "attachment; filename=mykmz.kmz");

    Point point = new Point();
    point.Coordinate = new Vector(-13.163959, -72.545992);

    Placemark placemark = new Placemark();
    placemark.Geometry = point;
    placemark.Name = "Machu Picchu";

    KmlFile kml = KmlFile.Create(placemark, false);

    KmzFile kmz = KmzFile.Create(kml);
    kmz.Save(context.Response.OutputStream);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文