开发者

How to display multiple placemarks and paths?

开发者 https://www.devze.com 2023-03-07 11:38 出处:网络
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 (w

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号