使用 Python 将 Geojson 转换为 kml

Converting Geojson to kml using Python

我也可以将它 kml/kmz 转换为 geojson,从 geojson 创建 kml..

我尝试使用以下库

Shapely
kml2geojson
geojson

源代码如下..

import json
import simplekml


with open('file.geojson') as f:
    data = json.load(f)
kml = simplekml.Kml()
for feature in data['features']:
    if feature['geometry']['type'] == 'Polygon':
        kml.newpolygon(name=name,
                       description='test',
                       outerboundaryis=feature['geometry']['coordinates'][0])
    elif feature['geometry']['type'] == 'LineString':
        kml.newlinestring(name=name,
                          description='test',
                          coords=feature['geometry']['coordinates'])

    elif feature['geometry']['type'] == 'Point':
        kml.newpoint(name=name,
                     description='test',
                     coords=[feature['geometry']['coordinates']])
kml.save('file.kml')

终于可以使用

将geojson文件转换为kml
import simplekml
import simplekml
import json

with open("myfile.geojson") as f:
    data = json.load(f)
kml = simplekml.Kml()
for feature in data['features']:
    geom = feature['geometry']
    geom_type = geom['type']
    if geom_type == 'Polygon':
        kml.newpolygon(name='test',
                       description='test',
                       outerboundaryis=geom['coordinates'][0])
    elif geom_type == 'LineString':
        kml.newlinestring(name='test',
                          description='test',
                          coords=geom['coordinates'])
    elif geom_type == 'Point':
        kml.newpoint(name='test',
                     description='test',
                     coords=[geom['coordinates']])
    else:
        print("ERROR: unknown type:", geom_type)
kml.save('kml_file.kml')

这对我来说很好..