友情提示,您閱讀本篇博文的先決條件如下:
1、本文示例基于Microsoft SQL Server 2008 R2調測。
2、具備 Transact-SQL 程式設計經驗和使用 SQL Server Management Studio 的經驗。
3、熟悉或了解Microsoft SQL Server 2008中的空間資料類型。
4、具備相應(比如OGC規範、KML規範)的GIS專業理論知識。
5、GeoRss訂閱技術以及其他相關知識。
GeoRSS是一種描述和查明網際網路内容所在實體位置的方法。通過使用GeoRSS,搜尋Web站點或者與地理位置有關的項目就成為可能。GeoRSS利用地理辨別語言(GML),即利用可擴充标記語言 (Extensible Markup Language, XML)存儲和傳輸地理資料的方法。原始的GML模型以由World Wide Web聯盟(W3C)所開發的資源描述架構(RDF)為基礎。GML保持着RDF的許多特性,包括智能代理和一個用于描述和查詢資料的标準文法。
GeoRSS 是在 RSS 訂閱源中包含地理空間資料時所用的一個标準,它定義了一種名為 GeoRSS GML 的特定格式,用來在訂閱源中包含 GML 格式的資料。用戶端應用程式可以訂閱 GeoRSS 訂閱源,訂閱方式與訂閱正常 RSS 訂閱源相同。可以輕松地将 GeoRSS 格式的資料導入Microsoft Bing Maps、Google Maps中,同樣也可以将空間資料庫中的空間資料釋出為GeoRss訂閱後快速的在GIS中呈現,本篇将介紹如何基于微軟Bing Maps for Silverlight中呈現GeoRss訂閱的空間資料。
一、釋出空間資料到GeoRss
二、建立GeoRss閱讀器
建立GeoRss閱讀器的目的是為了動态的請求GeoRss位址,将GeoRss資料解析為自己想要的資料結構,如下便是根據自己的需求結合GeoRss定義的一種資料結構樣例。
using System.Collections.Generic;
using Microsoft.Maps.MapControl;
namespace GeoRss.Map.GeoRssUtils
{
public class GeoRssItem
{
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public string PubData { get; set; }
public LocationCollection Locatios { get; set; }
}
}
核心原理就是使用WebClient動态的發起http請求,将傳回的GeoRss資料通過Linq To XML的方式解析為對象結構的資料。其實作非常簡單,不做具體分析,詳細代碼如下所示:
using System;
using System.Net;
using System.Xml.Linq;
using System.Linq;
using System.Windows;
public delegate void DownloadGeoRssCompletedEventHandler(List<GeoRssItem> items);
public delegate void DownloadGeoRssExceptionEventHandler(Exception e);
public class GeoRssReader
public GeoRssReader()
{
wc = new WebClient();
wc.DownloadStringCompleted += WebClientDownloadGeoRssCompleted;
}
public GeoRssReader(Uri uri)
: this()
this.uri = uri;
public GeoRssReader(Uri uri, DownloadGeoRssCompletedEventHandler evh)
: this(uri)
DownloadGeoRssCompleted += evh;
public Uri uri { get; set; }
public event DownloadGeoRssCompletedEventHandler DownloadGeoRssCompleted;
public event DownloadGeoRssExceptionEventHandler DownloadGeoRssException;
public void ReadAsync()
if (DownloadGeoRssCompleted.Target != null)
{
wc.DownloadStringAsync(uri);
}
#region _private
private readonly WebClient wc;
private void WebClientDownloadGeoRssCompleted(object sender, DownloadStringCompletedEventArgs e)
try
XNamespace nsXml = "http://www.w3.org/2005/Atom";
XNamespace nsGeorss = "http://www.georss.org/georss";
XNamespace nsGeo = "http://www.w3.org/2003/01/geo/wgs84_pos#";
XNamespace nsMedia = "http://search.yahoo.com/mrss/";
var items = from item in XElement.Parse(e.Result).Descendants("item")
select new GeoRssItem
{
Title = (item.Element("title") != null) ? item.Element("title").Value : null,
Link = (item.Element("link") != null) ? item.Element("link").Value : null,
Description = (item.Element("description") != null) ? item.Element("description").Value : null,
PubData = (item.Element("pubDate") != null) ? item.Element("pubDate").Value : null,
Locatios = ParserLocations(XElement.Parse(item.LastNode.ToString().Replace(":", "X")).Value)
};
if (DownloadGeoRssCompleted.Method != null)
{
DownloadGeoRssCompleted.Invoke(items.ToList());
}
catch (Exception ex)
if (DownloadGeoRssException.Method != null)
DownloadGeoRssException.Invoke(ex);
else
throw;
private LocationCollection ParserLocations(string points)
LocationCollection lc = new LocationCollection();
string[] ps = points.Split(' ');
for (int i = 0; i < ps.Length; i+=2)
lc.Add(new Location(double.Parse(ps[i]), double.Parse(ps[i + 1])));
return lc;
#endregion
三、基于SLBM呈現GeoRss資料
引入Bing Maps Silverlight Control的控件庫,定義一個專門的MapLayer圖層來呈現GeoRss資料,其Silverlight前台的代碼如下。
<Grid x:Name="LayoutRoot" Background="White">
<map:Map x:Name="map" Margin="0,0,0,0" CredentialsProvider="{StaticResource MyCredentials}"
ScaleVisibility="Visible"
CopyrightVisibility="Collapsed">
<map:MapLayer Name="mlayer"></map:MapLayer>
</map:Map>
</Grid>
應用程式加載的過程中使用上面所開發完成的GeoRss閱讀器進行資料讀取并解析,随後将結果呈現在Bing Maps Silverlight Control的應用中。代碼如下:
public MainPage()
InitializeComponent();
string url = "http://localhost:32484/SHBuildingGeoHandler.ashx";
GeoRssReader reader = new GeoRssReader(new Uri(url, UriKind.RelativeOrAbsolute));
reader.DownloadGeoRssCompleted+=new DownloadGeoRssCompletedEventHandler(reader_DownloadGeoRssCompleted);
reader.ReadAsync();
void reader_DownloadGeoRssCompleted(List<GeoRssItem> items)
//System.Diagnostics.Debug.WriteLine(items.Count);
foreach (var item in items)
MapPolygon mp = new MapPolygon();
mp.Locations = item.Locatios;
mp.Fill = new SolidColorBrush(Colors.Red);
this.mlayer.Children.Add(mp);
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauETMiJmZzMmN3MWNiZTO0E2YyQ2MjR2N2MWMkFjNjlDZfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.jpg)
四、相關資料
版權說明
本文屬原創文章,歡迎轉載且注明文章出處,其版權歸作者和部落格園共有。為了儲存作者的創作熱情,請在轉載後的明顯位置标記本文出處。
作 者:Beniao