I need to create a .kml file from a dataframe with more than 800 districts. This is what I have done so far:
1) Read a .csv file (FIG 1) using PANDAS
2) Creat a new dataframe by choosing only the first 3 columns (longitude, latitude, altitude)
3) Create a list of tuples from the dataframe
4) Create a .kml file and do some styling (colors)
All this proceure work great ONLY when there is 1 district. Now I need to do the same but with more than 800 districts. In FIG 2, it is shown an example with 2 districts (ACTONO and AILSACRAIGO).
When converting the dataframe to a list of tuples, how can make "python" know that there are many districts?
I believe these lines have to be improved:
a) Here I will need a list of tuples (one for each district)
#Converting the dataframe to a list of tuples
tuples = [tuple(x) for x in df_modify.values]
b) And here, "outboundaryies" will have to change for each of the tuples
pol = kml.newpolygon(name= 'ACTONO', description= 'Acton County',
outerboundaryis=tuples, extrude=extrude, altitudemode=altitudemode)
This is all the code:
CODE FOR .CSV WITH 1 DISTRICT
import pandas
import simplekml
kml = simplekml.Kml()
#Using PANDAS to read .csv and chosing the first 3 columns
df = pandas.read_csv('C:\\Users\\disa_ONTshp.csv')
df_modify=df.iloc[:, [0,1,2]]
#Converting the dataframe to a list of tuples
tuples = [tuple(x) for x in df_modify.values]
#Creating a .kml file
extrude=1
altitudemode = simplekml.AltitudeMode.relativetoground
pol = kml.newpolygon(name= 'ACTONO', description= 'Acton County',
outerboundaryis=tuples, extrude=extrude, altitudemode=altitudemode)
#Styling colors
pol.style.linestyle.color = simplekml.Color.green
pol.style.linestyle.width = 5
pol.style.polystyle.color = simplekml.Color.changealphaint(100,
simplekml.Color.green)
#Saving
kml.save("Polygon Styling.kml")
FIG 1 (1 DISTRICT)
FIG 2 (2 DISTRICTS)
This is the answer. What I needed was a dictionary of dataframes.