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.
import pandas as pd
import simplekml
import pprint
import numpy as np
kml = simplekml.Kml()
###LOADING THE .csv FILE WITH ALL THE COORDINATES (USING QGIS)
df = pd.read_csv('C:\\Users\\file.csv')
###ADDING A COLUMN "altitude" WITH RANDOM VALUES FROM 200 TO 2000
df['altitude']=df.groupby('name').name.transform(lambda x: np.random.randint(200,2000))
###CALLING THE COLUMNS OF INTEREST
df=df[['longitude', 'latitude', 'altitude', 'name']]
###CREATING A DICTIONARY OF DATAFRAMES (ONE FOR EACH DISTRICT)
dict_dataframes=dict(tuple(df.groupby('name')))
###CALLING EACH DATAFRAME FROM THE DICTIONARY
for name, df in dict_dataframes.items():
###CREATING A LIST OF TUPLES WITH THE COLUMNS OF THE DATAFRAME
tuples = [tuple(x) for x in df.values]
extrude=1
altitudemode = simplekml.AltitudeMode.relativetoground
pol = kml.newpolygon(name = name, description="District of " + name, outerboundaryis=tuples, extrude=extrude, altitudemode=altitudemode)
pol.style.linestyle.color = simplekml.Color.honeydew
pol.style.linestyle.width = 3
pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.navy)
###SAVING THE FILE
kml.save('C:\\Users\\3d_file.kml')