1

I'm using PyKml module in order to form kml inside my Python script. I want to display path which consists of array of coordinates and also display all points as placemarks. Currently, I'm trying (without success) to do it following way

 doc = K.kml(
        K.Document(
            K.Placemark(
                 K.Point(
                     K.name("pl1"),
                    K.coordinates("52.4858, 25.9218, 1051.05105105")
                ) 
            ),
            K.Placemark(
                K.name("path1"),
                K.LineStyle(
                    K.color(0x7f00ffff),
                    K.width(10)
                ),
                K.LineString(
                    K.coordinates(
                        coord_str
                    )
                )
            )
        )
    )

Path looks OK, but when I start adding Placemarks, Google Maps displays only first one. What should I use to display all Placemarks on my path? Do I need some sort of metaprogramming(i.e. add placemarks in object definition automatically)? Or perhaps something else?

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
qutron
  • 1,710
  • 4
  • 18
  • 30

1 Answers1

1

This should let you iterate over the objects and associate each point with the lines it terminates:

from pykml.factory import KML_ElementMaker as K
from lxml import etree

#line_points here comes from a geojson object
data = json.loads(open('tib.json').read())
line_points = data['features'][0]['geometry']['coordinates']

_doc = K.kml()

doc = etree.SubElement(_doc, 'Document')

for i, item in enumerate(line_points):
    doc.append(K.Placemark(
        K.name('pl'+str(i+1)),
        K.Point(
            K.coordinates(
                str(item).strip('[]').replace(' ', '')
                )
        )
    )
)

doc.append(K.Placemark(
    K.name('path'),
    K.LineStyle(
        K.color('#00FFFF'),
        K.width(10)
    ),
    K.LineString(
        K.coordinates(
            ' '.join([str(item).strip('[]').replace(' ', '') for item in line_points])
        )
    )
))

s = etree.tostring(_doc)

print s

where line_points is a list of lists like this, with the coordinates:

[[-134.15611799999999, 34.783318000000001, 0],
 [-134.713527, 34.435267000000003, 0],
 [-133.726201, 36.646867, 0],
 [-132.383655, 35.598272999999999, 0],
 [-132.48034200000001, 36.876308999999999, 0],
 [-131.489846, 36.565426000000002, 0],...

Here (http://sfgeo.org/data/contrib/tiburon.html) is an example of output, jsfiddle of it here: http://jsfiddle.net/bvmou/aTkpN/7/ but there is a problem with the api key when viewed publicly, try on your local machine.

unmounted
  • 33,530
  • 16
  • 61
  • 61
  • well, coord_str is a string, I meant, I'd like to emphasize points of a line. LineString gives you just line and I'd like to see segments of this line – qutron May 19 '11 at 12:29
  • Sorry for the misread, tried to delete and reanswer but had to resuscitate this one. – unmounted May 21 '11 at 04:21