For csv to kml file creation using python,first we need to install simplekml from python by command

Code for Csv to kml file creation using python:

pip install simplekml
import csv import simplekml from polycircles import polycircles # initialize list c=[] kml=simplekml.Kml() doc = kml.newdocument(name='Sheet1') #Open CSV file with open('KML.csv', mode ='r')as file: csvfile = csv.reader(file) for lines in csvfile: c.append(lines) i=1 while i<len(c): sn=doc.newpoint(name=c[i][2],description=c[i][3],coords=[(c[i][1],c[i][0])]) if int(c[i][2])>-95: sn.style.iconstyle.icon.href='http://maps.google.com/mapfiles/kml/paddle/grn-blank.png' elif -110<int(c[i][2])<=-95: sn.style.iconstyle.icon.href='http://maps.google.com/mapfiles/kml/paddle/pink-blank.png' else: sn.style.iconstyle.icon.href='http://maps.google.com/mapfiles/kml/paddle/A.png' i+=1 inner_polycircle = polycircles.Polycircle(latitude=float(c[1][5]), longitude=float(c[1][6]), radius=1000, number_of_vertices=36) outer_polycircle = polycircles.Polycircle(latitude=float(c[1][5]), longitude=float(c[1][6]), radius=998, number_of_vertices=36) tn=doc.newpoint(name="Picnic",coords=[(c[1][6],c[1][5])]) tn.style.iconstyle.icon.href='http://maps.google.com/mapfiles/kml/shapes/picnic.png' pol = kml.newpolygon(name="Circle Measure",outerboundaryis=inner_polycircle.to_kml(),innerboundaryis=inner_polycircle.to_kml()) kml.save("file.kml")

polycircle module is used for making circle radius of 1Km.Above code will give KML file picnic icon within radius of 1km circle and plot latitude and longitude on map where >-95 icon will be green color and between -95 to -110 pink and for <-110 it will be red.

CSV file should be in given format

CSV file format

Output will be .kml file which can be viewed on google earth.

csv to KML file creation using python
1.CSV to KML file creation using python
to fetch data of kml file Write below code kml.kml()

By SC

Leave a Reply

Your email address will not be published. Required fields are marked *