将KML坐标转换为CSV或XML [英] Convert KML coordinates to CSV or XML

查看:1477
本文介绍了将KML坐标转换为CSV或XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将KML文件的坐标转换/解析为一种格式(CSV或XML),以后可以用于导入其他程序中.

我在KML文件中有多边形,我正在尝试将坐标与其他格式不同的程序重复使用.

在我的KML文件中,我的坐标看起来像这样.

<坐标> -86.36762,37.31916,0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0

如何搜索KML文件并将坐标转换为这样的格式

-86.36762,37.31916,0
-86.43890,37.31916,0
-88.96934,32.11572,0
-84.51434,32.68596,0
-82.87490,35.85792,0
-86.36762,37.31916,0

并使用C#将其保存为CSV?

任何帮助将非常感激.我能够找到将CSV转换为KML的方法,但没有其他问题.

谢谢

解决方案

如果要求是to break将坐标集分成几行,则Regex class的Replace 方法可用于替换前面没有逗号的空间如下所示:

 字符串 kmlInput =  @"  -86.36762,37.31916,0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0";
    字符串 csvOutput = Regex.Replace(kmlInput,
                     @" " ,
                    RegexOptions.CultureInvariant);
    Console.WriteLine(csvOutput);

//  csvOutput 
// -86.36762,37.31916,0 
// -86.43890,37.31916,0 
// -88.96934,32.11572,0 
// -84.51434,32.68596,0 
// -82.87490,35.85792,0 
// -86.36762,37.31916,0  


Is there a way to convert/parse a KML file''s coordinates into a format (CSV or XML) that I could later use to import into other programs.

I have polygons in KML files and I''m trying to reuse the coordinates with other programs in different formats.

In my KML file I have coordinates that look like this.

<coordinates>-86.36762,37.31916,0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0

How do I search the KML file and convert the coordinates to a format like this

-86.36762,37.31916,0
-86.43890,37.31916,0
-88.96934,32.11572,0
-84.51434,32.68596,0
-82.87490,35.85792,0
-86.36762,37.31916,0

and save it in a CSV using C#?

Any help would be much appreciated. I''m able to find ways to convert CSV to KML no problem but what about the other way around.

Thanks

解决方案

If the requirement is to break the set of coordinates into separate lines, then the Replace method of Regex class can be used to replace the spaces which are not preceded by or followed by a comma as shown below:

string kmlInput = @"-86.36762, 37.31916 , 0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0 ";
    string csvOutput = Regex.Replace(kmlInput,
                    @"(?<=[^,]+)\s+(?=[^,]+)","\n",
                    RegexOptions.CultureInvariant);
    Console.WriteLine (csvOutput);

//csvOutput
//-86.36762, 37.31916 , 0
//-86.43890,37.31916,0
//-88.96934,32.11572,0
//-84.51434,32.68596,0
//-82.87490,35.85792,0
//-86.36762,37.31916,0


这篇关于将KML坐标转换为CSV或XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆