0

I wanted to change the code into for-loop so that I can change the style for each point.

Code below is working fine without for-loop:

import simplekml
import pandas as pd

excel_file = 'sample.xlsx'
df=pd.read_excel(excel_file)
kml = simplekml.Kml()
df.apply(lambda X: kml.newpoint( coords=[( X["Long"],X["Lat"])]) ,axis=1)
kml.save(path = "data.kml")

I wanted to do it in for-loop so that I can put style to each point, but my for-loop is not working

import simplekml
import pandas as pd
kml = simplekml.Kml()
style = simplekml.Style()
excel_file = 'sample1.xlsx'
df=pd.read_excel(excel_file)
y=df.Long
x=df.Lat

MinLat=int(df.Lat.min())
MaxLat=int(df.Lat.max())
MinLong=int(df.Long.min())
MaxLong=int(df.Long.max())
multipnt =kml.newmultigeometry()

for long in range(MinLong,MaxLong):  # Generate longitude values
    for lat in  range(MaxLat,MinLat): # Generate latitude values
         multipnt.newpoint(coords=[(y,x)])
        #kml.newpoint(coords=[(y,x)])
kml.save("Point Shared Style.kml")
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
Issac_n
  • 89
  • 1
  • 2
  • 13

1 Answers1

1

If want to iterate over a collection of points in an Excel file and add them to a single Placemark as a MultiGeometry using a for-loop then try this.

import simplekml
import pandas as pd

kml = simplekml.Kml()
style = simplekml.Style()
excel_file = 'sample1.xlsx'
df = pd.read_excel(excel_file)
multipnt = kml.newmultigeometry()
for row in df.itertuples(index=False):
    multipnt.newpoint(coords=[(row.Lat, row.Long)])
kml.save("PointSharedStyle.kml")

If want to generate a point grid every decimal degree for the bounding box of the points then you would try the following:

import simplekml
import pandas as pd

kml = simplekml.Kml()
style = simplekml.Style()
excel_file = 'sample1.xlsx'
df = pd.read_excel(excel_file)

MinLat = int(df.Lat.min())
MaxLat = int(df.Lat.max())
MinLong = int(df.Long.min())
MaxLong = int(df.Long.max())

for long in range(MinLong, MaxLong+1):  # Generate longitude values
    for lat in range(MinLat, MaxLat+1): # Generate latitude values
        multipnt.newpoint(coords=[(long, lat)])
        #kml.newpoint(coords=[(long,lat)])

kml.save("PointSharedStyle.kml")

Note the Style is assigned to the placemark not the geometry so the MultiGeometry can only be assigned a single Style for all points. If want a different style for each point then need to create one placemark per point and assign each with its own Style.

For help setting styles, see https://simplekml.readthedocs.io/en/latest/styles.html

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75