Click here to Skip to main content
12,067,278 members (55,297 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML CSV KML
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.

-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
Posted 31-May-12 2:54am
mzrax476

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
mzrax 31-May-12 9:19am
   
VJ Reddy

Thanks

I've taken what you've done to look like this.

string filePath = @"C:\test.csv";
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);
System.IO.StreamWriter objWriter;
objWriter = new StreamWriter(filePath, true);
objWriter.WriteLine(csvOutput);
objWriter.Close();

This is great. I really appreciate it.

Thanks Again.
VJ Reddy 31-May-12 10:36am
   
You're welcome and thank you for accepting the solution :)
The kmlInput can be read from the file using
string kmlInput = System.IO.File.ReadAllText(filePath);
provided the file contains the required input
then

csvOutput can be written to file using
System.IO.File.WriteAllText(outFilePath, csvOutput);
umarabbas 14-Nov-12 6:36am
   
Good
losmac 31-May-12 9:28am
   
Short and to the point, my 5!
VJ Reddy 31-May-12 10:37am
   
Thank you, losmac :)
Björn Ranft 31-May-12 15:36pm
   
Correct, my 5!Regards
VJ Reddy 31-May-12 19:49pm
   
Thank you, Björn :)
Espen Harlinn 1-Jun-12 4:16am
   
Well answered :-D
VJ Reddy 1-Jun-12 5:50am
   
Thank you, Espen :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 31 May 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100