Search code examples
google-earth

drawing lines using csv file of lat/long on google earth


I have a csv file containing lat/long like this

xx.xxxxx, yy.yyyyy
xx.xxxxx, yy.yyyyy
xx.xxxxx, yy.yyyyy
xx.xxxxx, yy.yyyyy
xx.xxxxx, yy.yyyyy

How to draw lines on google earth using this csv file?


Solution

  • You can use the code below to create a KML file for use with Google Maps or Google Earth. It assumes your CSV file is called yourCSV.csv

    #!/bin/bash
    
    # Output KML header
    cat<<EOF
    <?xml version="1.0" encoding="UTF-8"?> 
    <kml xmlns="http://earth.google.com/kml/2.0">
    <Document>
    <Placemark> 
     <LineString>
      <coordinates>
    EOF
    
    # Read in CSV and append a zero altitude to each line  
    sed s/$/,0.0/ yourCSV.csv 
    
    cat<<EOF
      </coordinates>
     </LineString>
    </Placemark>
    </Document>
    </kml>
    EOF
    

    Save this in a file called CSV2KML then do this to make it executable and run it to make a file called mylines.kml:

    chmod +x CSV2KML
    ./CSV2KML > mylines.kml
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?> 
    <kml xmlns="http://earth.google.com/kml/2.0">
    <Document>
    <Placemark> 
    <LineString>
    <coordinates>
    xx.xxxxx, yy.yyyyy,0.0
    xx.xxxxx, yy.yyyyy,0.0
    xx.xxxxx, yy.yyyyy,0.0 
    xx.xxxxx, yy.yyyyy,0.0
    </coordinates>
    </LineString>
    </Placemark>
    </Document>
    </kml>
    

    If you want the line to be red, for example, change the last part so it looks like this:

    </coordinates>
    </LineString>
    <Style> 
    <LineStyle>  
    <color>#ff0000ff</color>
    </LineStyle> 
    </Style>
    </Placemark>
    </Document>
    </kml>