polygon showing up in Google Earth - simpleKML

1.5k Views Asked by At

background - I'm trying to create a circular polygon and add it to a kml using simpleKML.

The kml knows that there should be a polygon added, and it has the proper colour, width, and description, but whenever I zoom to the location it leads me to coordinates 0,0 and no polygon.

My code to create the polygon looks like:

pol = kml.newpolygon(name=pnt.name) 
pol.description = ("A buffer for " + pnt.name)
pol.innerboundaryis = [newCoord]
pol.style.linestyle.color = simplekml.Color.green
pol.style.linestyle.width = 5
pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)

where 'newCoord' is a 2D array with all of the lat/long information stored in it.

Because I thought the array might not format the data properly I tried to form a simple triangular polygon using the code:

pol1 = kml.newpolygon(name=pnt.name) 
pol1.innerboundaryis = [(46.714,-75.6667),(44.60796,-74.502),(46.13910,-74.57411),(46.714,-75.6667)]
pol1.style.linestyle.color = simplekml.Color.green
pol1.style.linestyle.width = 5
pol1.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)

but it has the same issue as the first.

I've tried forming the polygon with both .innerboundaryis() and .outerboundaryis() without success and I'm running out of ideas.

edit: I should add that I'm opening the kml file in Google Earth

1

There are 1 best solutions below

1
On

There is almost no documentation on this issue online so I figured I would post the answer to my question for anyone who has this issue in the future.

This is the code that I used that got the polygon working.

    newCoords = []
    pol = kml.newpolygon(name=pnt.name) 
    pol.description = ("A buffer for " + pnt.name)

    if pnt.name in bufferList:
        bufferRange = input('Enter the buffer range. ' )
        for i in range(360):
            newCoords.append( ( math to calculate Lat, math to calculate Long ) )
            pol.outerboundaryis.coords.addcoordinates([newCoords[i]])


        pol.style.linestyle.color = simplekml.Color.green
        pol.style.linestyle.width = 5
        pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)

You need to put your coordinates into a list before adding them to the polygon's outer boundary using the 'coords.addcoordinates()' function. Additionally it must be a one dimensional list, so both the latitude and longitude coordinate must be stored in the same place.

You can input floats directly with '.outerboundaryis()', example:

pol.outerboundaryis = [(18.333868,-34.038274), (18.370618,-34.034421),
                       (18.350616,-34.051677),(18.333868,-34.038274)]

But '.addcoordinates()' only accepts lists and integers.