2
\$\begingroup\$

I am new to Python programing language. I have created the codes, based on this solution.

I prepared two simple programs:

import simplekml

List2 = [ [ 'Placemark','old file', 51.500152, -0.126236 ] ] # description, lat, lon
List3 = [ [ 'New placemark','new file', 51.600152, -0.136236 ] ]

kml = simplekml.Kml()
for row in List2:
    kml.newpoint(name=row[0], description=row[1],
    coords=[(row[3], row[2])])  # lon, lat, optional height
for row in List3:
    kml.newpoint(name=row[0], description=row[1],
    coords=[(row[3], row[2])])  

kml.save("test2.kml")

and from another file here:

import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Kirstenbosch", description="Description", coords=[(18.432314,-33.988862)])  # lon, lat,optional height
kml.newpoint(name="Kirstenbosch2", description="<b> Botanic garden </b> in South Africa", coords= 
 [(18.532314,-33.788862)])
kml.save("output/botanicalgarden2.kml")

I know that this code (simplekml tool) can be used for the bulk .kml data, as it was mentioned here.

Could you tell me how to write this code not repeatedly, as I did? I would like to have a multitude of place mark locations within one list/new point, when possible.

\$\endgroup\$
1
  • \$\begingroup\$ TIL that mathematical tools take (lon, lat), instead of (lat, lon), because it maps to the common way to put (x, y) coordinates for plotting... \$\endgroup\$
    – Graipher
    Mar 8, 2020 at 8:49

1 Answer 1

3
\$\begingroup\$

In your first code you already have everything you need, you just don't use it. List2 and List3 are each lists of points to create. But they only contain one point, so your for loop does nothing exciting, it only runs once.

You can simply add more points to any of the lists, here I take the first as an example:

import simplekml

points = [['Placemark', 'old file', 51.500152, -0.126236],
          ['New placemark', 'new file', 51.600152, -0.136236]] # description, lat, lon

kml = simplekml.Kml()
for point in points:
    kml.newpoint(name=point[0], description=point[1],
                 coords=[(point[3], point[2])])  # lon, lat, optional height

kml.save("test2.kml")

You could make this a bit more readable by using a collections.namedtuple for the markers. This way your comments become superfluous:

from collections import namedtuple
import simplekml

Marker = namedtuple("Marker", ["name", "description", "lat", "lon"])

points = [Marker('Placemark', 'old file', 51.500152, -0.126236),
          Marker('New placemark', 'new file', 51.600152, -0.136236)]

kml = simplekml.Kml()
for point in points:
    kml.newpoint(name=point.name, description=point.description,
                 coords=[(point.lon, point.lat)])

kml.save("test2.kml")

Note that I followed Python's official style-guide, PEP8, when naming my variables. Variables and functions should be lower_case and only classes (such as the namedtuple class I create) should be in PascalCase. PEP8 also recommends putting a space after commas and not to use unnecessary whitespace.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.