python - Can I use simplekml shared style and also change the style of individual points? -


i've written script in python convert csv file kml file using simplekml python package. it's not finished yet needs scale , color points based on 1 of data values. right i'm playing around if/else see if can use shared style, edit color , scale of each point (i plan on changing use range of colors, i'm trying figure out works , doesn't). data has 5000 rows, wanted use shared style keep resulting kml short possible, use loop assign color , scale along schema data.

my problem this: if/else executes, changes shared style icon color lime green. result every single point lime green. there way use shared style , edit color , scale without overwriting shared style? if remove shared style, colors work expected kml file huge. i'm new python, learned last week. or tips appreciated.

edit: seems if cannot set out shared style there. can use if/else compare , assign colors works if rid of shared style. think overwrites everything. if there way however, make output files smaller (with shared style 4mb, without 7mb , know used larger datasets in future).

here code reference:

import simplekml import csv import math  kml = simplekml.kml() style = simplekml.style() #creates shared style points style.iconstyle.icon.href ='http://maps.google.com/mapfiles/kml/shapes/target.png' #can change desired icon url style.iconstyle.scale = 1 schema = kml.newschema(name= '') #creates schema schema.newsimplefield(name= 'realization', type = 'string', display name = 'realization') schema.newsimplefield(name= 'diameter', type = 'string', displayname = 'diameter') schema.newsimplefield(name= 'density', type = 'string', displayname = 'density') schema.newsimplefield(name= 'strength', type = 'string', displayname = 'strength') schema.newsimplefield(name= 'velocity', type = 'string', displayname = 'velocity (mps)') schema.newsimplefield(name= 'entry', type = 'string', displayname = 'entry angle') schema.newsimplefield(name= 'casualties', type = 'float', displayname = 'casualties') schema.newsimplefield(name= 'damagesource', type = 'string', displayname = 'damage source') schema.newsimplefield(name= 'blastrad', type = 'string', displayname = 'blast radius')  #loads csv , sets delimiter myfile = open(raw_input("enter file path: ")) data = csv.dictreader(myfile, delimiter = ',') #create new kml file  row in data:   cas_log = float(row[' casualties ']) + 1 #change 0 values 1   newlog = math.log10(cas_log)   row[' casualties '] = newlog #changes values log10   pnt = kml.newpoint(name = "", coords = [(float(row[' longitude ']),float(row['  latitude ']))])   pnt.style = style #assigns shared style every point   pnt.extendeddata.schemadata.schemaurl = schema.id #assigns schema data each point display in   pnt.extendeddata.schemadata.newsimpledata('realization', row['realization '])   pnt.extendeddata.schemadata.newsimpledata('diameter', row[' diameter '])   pnt.extendeddata.schemadata.newsimpledata('density', row[' density '])   pnt.extendeddata.schemadata.newsimpledata('strength', row[' strength '])   pnt.extendeddata.schemadata.newsimpledata('velocity', row[' velocity_mps '])   pnt.extendeddata.schemadata.newsimpledata('entry', row[' entryangle '])   pnt.extendeddata.schemadata.newsimpledata('casualties', row[' casualties '])   pnt.extendeddata.schemadata.newsimpledata('damagesource', row[' damagesource'])   pnt.extendeddata.schemadata.newsimpledata('blastrad', row[' blastradmajor_m '])   if row[' casualties '] == 0.0: # color test      pnt.style.iconstyle.color = 'ffff00ff' #magenta   else:      pnt.style.iconstyle.color = 'ff32cd32' #lime green   kml.save("csv2kml.kml") #saves new kml file name in user directory  print "file created." 

to use shared styles in simplekml need create style each of colors refer variable on point based on criteria color in case log of number of casualties.

create multiple shared styles in kml

style1 = simplekml.style() #creates shared style points style1.iconstyle.color = 'ffff00ff' #magenta style1.iconstyle.icon.href ='http://maps.google.com/mapfiles/kml/shapes/target.png' #can change desired icon url style1.iconstyle.scale = 1  style2 = simplekml.style() #creates shared style points style2.iconstyle.color = 'ff32cd32' #lime green style2.iconstyle.icon.href ='http://maps.google.com/mapfiles/kml/shapes/target.png' #can change desired icon url style2.iconstyle.scale = 1 

next, assign style point based on color test

if row['casualties'] >= 5.0: # color test    pnt.style = style1 # magenta else:    pnt.style = style2 # lime green 

Comments