1

Pythonスクリプト内でkmlを形成するために、PyKmlモジュールを使用しています。座標の配列で構成されるパスを表示し、すべてのポイントを目印として表示したい。現在、私は(成功せずに)次のようにそれをやろうとしています

 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
                    )
                )
            )
        )
    )

パスは問題ないように見えますが、目印を追加し始めると、Googleマップには最初の目印しか表示されません。パス上のすべての目印を表示するには、何を使用すればよいですか?ある種のメタプログラミングが必要ですか(つまり、オブジェクト定義に目印を自動的に追加します)?それとも何か他のもの?

4

1 に答える 1

1

これにより、オブジェクトを反復処理し、各ポイントをそれが終了する線に関連付けることができます。

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

ここline_pointsで、座標を含むこのようなリストのリストは次のとおりです。

[[-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],...

ここ(http://sfgeo.org/data/contrib/tiburon.html)は出力の例であり、そのjsfiddleはここにあります:http://jsfiddle.net/bvmou/aTkpN/7/しかし、公開されている場合のAPIキーは、ローカルマシンで試してください。

于 2011-05-19T10:03:40.130 に答える