如何从 SharePoint Bing 地图 POINT 值安全地解析地理坐标(纬度、经度)?
How to safely parse Geo-coordinates (Latitude , Longitude) from a SharePoint Bing Maps POINT Value?
我们正在使用 SharePoint 列表来存储位置信息。该列表使用 Bing 地图组件,允许用户编辑 SharePoint 列表 select Bing 地图上的位置。在内部,位置的地理坐标存储为某种 Point
对象。
当我使用 SharePoint 列表适配器查询列表时,我得到一个带有地理坐标的 String
,如下所示(这在 SSIS 数据查看器 ).
这里是实际粘贴的数据(供大家参考)
POINT (-96.082211 41.209486)
POINT (-96.099613 41.277599)
POINT (-96.104125 41.233525)
POINT (-96.120664 41.266411)
POINT (-96.1287887 41.205003)
我在 SSIS 脚本组件中使用 C# 来提取 Latitude
和 Longitude
值,每个值的每个值都在 POINT (
和 )
之间我正在处理的行。
这是我的代码。有效。
public override void SourceRows_ProcessInputRow(SourceRowsBuffer Row)
{
double latitude;
double longitude;
try
{
if (!Row.Maps_IsNull)
{
string[] point = Row.Maps.Substring(7, Row.Maps.Length - 8).Split(' ');
if (Double.TryParse(point[1], out latitude))
Row.Latitude = latitude;
if (Double.TryParse(point[0], out longitude))
Row.Longitude = longitude;
}
}
catch (Exception ex)
{
// TODO: log error
Console.WriteLine( ex.Message);
// at this point, the latitude and longitude for the row will be null.
}
}
这是输出。
问题
我知道我现有的代码可以工作,但我想知道是否有更好、更优雅且不易出错的方法来执行此操作。因为Substring
的硬编码起点和Split
中的数组项并不理想,容易出现IndexOutOfRangeException
等。当然Latitude
和Longitude
将只是空的,但我认为有更好的方法。
告诉我!谢谢!!!
我会使用类似下面的方法来获取 Latitude
和 Longitude
。您必须对其进行修改才能使其与您的代码一起使用,但它应该足以让您了解想法。
string input = "POINT (-96.082211 41.209486)";
Match match = Regex.Match(input, @"\((-?\d+.?\d+) (-?\d+.?\d+)\)");
string lat = match.Groups[1].Value;
string lon = match.Groups[2].Value;
关键是正则表达式模式 "\((-?\d+.?\d+) (-?\d+.?\d+)\)"
,它为输入字符串括号内的双打创建两个匹配组。
我们正在使用 SharePoint 列表来存储位置信息。该列表使用 Bing 地图组件,允许用户编辑 SharePoint 列表 select Bing 地图上的位置。在内部,位置的地理坐标存储为某种 Point
对象。
当我使用 SharePoint 列表适配器查询列表时,我得到一个带有地理坐标的 String
,如下所示(这在 SSIS 数据查看器 ).
这里是实际粘贴的数据(供大家参考)
POINT (-96.082211 41.209486)
POINT (-96.099613 41.277599)
POINT (-96.104125 41.233525)
POINT (-96.120664 41.266411)
POINT (-96.1287887 41.205003)
我在 SSIS 脚本组件中使用 C# 来提取 Latitude
和 Longitude
值,每个值的每个值都在 POINT (
和 )
之间我正在处理的行。
这是我的代码。有效。
public override void SourceRows_ProcessInputRow(SourceRowsBuffer Row)
{
double latitude;
double longitude;
try
{
if (!Row.Maps_IsNull)
{
string[] point = Row.Maps.Substring(7, Row.Maps.Length - 8).Split(' ');
if (Double.TryParse(point[1], out latitude))
Row.Latitude = latitude;
if (Double.TryParse(point[0], out longitude))
Row.Longitude = longitude;
}
}
catch (Exception ex)
{
// TODO: log error
Console.WriteLine( ex.Message);
// at this point, the latitude and longitude for the row will be null.
}
}
这是输出。
问题
我知道我现有的代码可以工作,但我想知道是否有更好、更优雅且不易出错的方法来执行此操作。因为Substring
的硬编码起点和Split
中的数组项并不理想,容易出现IndexOutOfRangeException
等。当然Latitude
和Longitude
将只是空的,但我认为有更好的方法。
告诉我!谢谢!!!
我会使用类似下面的方法来获取 Latitude
和 Longitude
。您必须对其进行修改才能使其与您的代码一起使用,但它应该足以让您了解想法。
string input = "POINT (-96.082211 41.209486)";
Match match = Regex.Match(input, @"\((-?\d+.?\d+) (-?\d+.?\d+)\)");
string lat = match.Groups[1].Value;
string lon = match.Groups[2].Value;
关键是正则表达式模式 "\((-?\d+.?\d+) (-?\d+.?\d+)\)"
,它为输入字符串括号内的双打创建两个匹配组。