You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将从KML提取的格式特殊的坐标字符串转换为数组?

处理KML提取的坐标字符串转数组的最优方法

从KML文件的<coordinates>节点提取的字符串通常带有多余的换行、空格,比如你拿到的:

string coordinates = "\n            34.8802346,31.9673726,0\n          ";

下面是几种高效的转换方法:

方法一:LINQ链式处理(简洁高效)

先清理首尾空白,分割后直接转换为数值数组:

string coordinates = "\n            34.8802346,31.9673726,0\n          ";
double[] coordArray = coordinates
    .Trim() // 移除首尾的换行、空格
    .Split(',') // 按逗号分割成字符串片段
    .Select(double.Parse) // 将每个片段转为double类型
    .ToArray();

如果要避免格式错误导致的异常,可以用安全转换版本:

List<double> coordList = new List<double>();
foreach (string segment in coordinates.Trim().Split(','))
{
    if (double.TryParse(segment, out double value))
    {
        coordList.Add(value);
    }
}
double[] coordArray = coordList.ToArray();

提取KML坐标的优化建议

你当前提取坐标的代码是取<Point>节点的Value,其实可以直接定位到<coordinates>节点,避免Point下其他内容干扰:

string coordinates = placeMark
    .Elements(ns + "Point")
    .Elements(ns + "coordinates")
    .Select(node => node.Value)
    .FirstOrDefault();

内容的提问来源于stack exchange,提问作者OrLevi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 11:31:14