C# 如何从SharePoint Bing地图的点值安全地解析地理坐标(纬度、经度)?
我们正在使用SharePoint列表存储位置信息。该列表使用Bing地图组件,允许编辑SharePoint列表的用户在Bing地图上选择位置。在内部,位置的地理坐标系存储为某种C# 如何从SharePoint Bing地图的点值安全地解析地理坐标(纬度、经度)?,c#,regex,ssis,geocoding,string-parsing,C#,Regex,Ssis,Geocoding,String Parsing,我们正在使用SharePoint列表存储位置信息。该列表使用Bing地图组件,允许编辑SharePoint列表的用户在Bing地图上选择位置。在内部,位置的地理坐标系存储为某种点对象 当我使用SharePoint列表适配器查询列表时,我会得到一个带有如下所示地理坐标的字符串(在SSIS Data Viewer中) 这是粘贴的实际数据(供参考) 我在SSIS脚本组件中使用C#来提取纬度和经度值,对于我正在处理的每一行的每个值,该值位于点(和)之间 这是我的密码。它起作用了 public over
点
对象
当我使用SharePoint列表适配器查询列表时,我会得到一个带有如下所示地理坐标的字符串(在SSIS Data Viewer中)
这是粘贴的实际数据(供参考)
我在SSIS脚本组件中使用C#来提取纬度
和经度
值,对于我正在处理的每一行的每个值,该值位于点(
和)
之间
这是我的密码。它起作用了
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.
}
}
这是输出
问题
我知道我现有的代码可以工作,但我想知道是否有更好、更优雅、更不容易出错的方法来实现这一点。因为子字符串
的硬编码起点和拆分
中的数组项并不理想,并且容易出现索引自动异常
等。当然纬度
和经度
将是空的,但我认为有更好的方法
让我知道!谢谢你 我将使用以下类似的方法来获取纬度
和经度
。您必须对其进行修改,以使其与您的代码配合使用,但这应该足以让您产生想法
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+)”
,它为输入字符串括号内的双精度创建两个匹配组。谢谢Spencer!这很好地工作,我认为正则表达式将是解决方案。我创建了一个dotNetFiddle来测试它。像老板一样工作=>
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;