GeoNames .NET WCF 客户端
用于访问 GeoNames 服务的 .NET WCF 客户端
引言
GeoNames 是一个地理数据库,根据知识共享署名许可证提供。这些数据可通过多种(30 多种)Web 服务免费访问。
Web 服务包括直接和反向地理编码、通过邮政编码查找地点、查找给定地点附近的地点、给定位置的天气观测以及查找有关邻近地点的维基百科文章等。
背景
我最近一直在使用 GeoNames 服务,既使用 JavaScript 也使用 .NET。除此之外,我们还使用 JavaScript 在 Web 上进行邮政编码自动完成功能,并使用 .NET 进行服务器端邮政编码验证。
所有这些酷炫的 GeoNames 服务肯定可以在许多不同的环境和应用程序中使用。我自己也有一些想法,我想使用 GeoNames 作为数据提供程序来开发 UI 控件和小部件。
因此,我开发了这个 WCF 客户端,以便能够以统一、简便的面向对象方式从 .NET 访问 GeoNames 服务。
Using the Code
以下是一些使用该客户端的 C# 示例。
所有服务都可以通过单个类 `GeoNamesClient` (`inherits System.ServiceModel.ClientBase
using ( GeoNamesClient client = new GeoNamesClient ( ) )
{
...
}
创建 `GeoNamesClient` 后,可以通过此对象调用任何服务。第一个示例显示如何检索地理边界框内的所有城市
double north = 44.1, south = -9.9, east = -22.4, west = 55.2;
var cities = client.GetCities ( north, south, east, west );
foreach ( GeoCity city in cities )
{
// GeoCity has many more properties to choose from...
Console.WriteLine ( "City, Name={0}, Population={1}", city.Name, city.Population );
}
下一个示例显示如何查找给定位置的天气观测结果
double latitude = 42, longitude = -2;
var response = client.FindNearbyWeather ( latitude, longitude );
GeoWeatherObservation weather = response.WeatherObservation;
// GeoWeatherObservation has many more properties to choose from...
Console.WriteLine ( "Current temperature is {0} degrees
(delivered from the {1} weather station)",
weather.Temperature, weather.StationName );
客户端还提供重载方法,用于指定文化、服务详细程度和控制每个服务返回的记录最大数量。该示例显示如何搜索与“`london`”匹配的任何地理地名,使用西班牙语文化设置(地名等),并将返回结果的数量限制为`10`
GeoToponymSearchCriteria criteria = new GeoToponymSearchCriteria ( );
criteria.Query = "london";
criteria.Culture = "es";
criteria.MaxRows = 10;
var response = client.SearchToponyms ( criteria );
foreach ( GeoToponym toponym in response )
{
// GeoToponym has many more properties to choose from...
Console.WriteLine ( "Found toponym match, name:{0}", toponym.Name );
}
更新:添加了对地理编码地址的支持。此新示例显示如何对地址进行地理编码以确定其空间坐标,然后将坐标传递给`FindNearbyPostalCodes`以查找此地址附近的邮政编码
string address = "1600 Amphitheatre Parkway Mountain View, CA 94043";
double latitude, longitude;
if ( client.TryGeocode ( address, out latitude, out longitude ) )
{
var response = client.FindNearbyPostalCodes ( latitude, longitude, 10 );
foreach ( GeoPostalCode code in response )
{
...
}
}
else
Console.WriteLine ( "-- Geocoding failed --" );
2.0.0.0 更新:添加了对根据给定的经纬度对和给定的公里半径近似边界框的支持。该公式基于WGS84系统。一个新的有用的`GetBoundingBox`方法已添加到`GeoNamesClient`类中。此方法可以与采用边界框的`IGeoNamesClient`方法(例如`GetCities`方法)结合使用。
以下是如何使用`GetBoundingBox`从经纬度对和半径获取边界框,然后将边界框传递给GetCities方法以检索此边界框内的城市的示例
double lat = 23.854, lng = 5.78;
double radius = 100; // kilometers
double north, south, east, west;
client.GetBoundingBox ( lat, lng, radius,
out north, out south, out east, out west );
var cities = client.GetCities ( north, south, east, west );
foreach ( GeoCity city in cities )
{
...
}
结论
如果您需要从 .NET 应用程序访问 GeoNames 服务,您可能需要查看我的客户端。它通过一个简洁的面向对象 API 支持所有可用的服务。
历史
- 2008-11-02 1.0.0.0
- 2008-11-04 1.5.0.0 添加了对地理编码的支持
- 2009-04-17 2.0.0.0
- 添加了对 WGS84 边界框计算的支持(阅读更新的文章)
- 修复了 Distance 属性序列化错误,方法是将类型更改为 String。Distance 属性中返回的数据实际上是 String 而不是 Double;它已经根据指定的返回文化进行了格式化。对于我可能破坏的任何现有代码,我深感抱歉。
- 修复了 `GeoStreetSegment.FeatureClasses` 的映射,现在映射到 MAF/TIGER 特征类代码
- 修复/解决了测试失败的问题