Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically converting kml to image [closed]

Are there any open source libraries (preferably python) that will convert a kml file to an image file?

I have an open source web-based application that allows users to draw shapes on a Google Earth Map, and I would like to provide them with a pdf that contains their map with the shapes that they have drawn.

Right now the users are provided instructions for using either Print Screen or exporting the kml, but the former seems a little lame and the latter doesn't give them an image unless they have access to other software.

Is this a pipe dream?

like image 808
sfletche Avatar asked Aug 25 '10 17:08

sfletche


People also ask

What is MultiGeometry?

The MultiGeometry element groups geometries together in the same Placemark element. This allows the geometries to share the same styling and to appear as one item in a list such as the My Places pane in Google Earth.

What is the difference between KML and KMZ?

KML is an open standard of the Open Geospatial Consortium (OGC). KML can include both raster and vector data, and the file includes symbolization. KML files are like HTML, and only contains links to icons and raster layers. A KMZ file combines the images with the KML into a single zipped file.


2 Answers

I recently did something like this with Mapnik. After installing Mapnik with all optional packages, this Python script can export a path from a KML file to a PDF or bitmap graphic:

#!/usr/bin/env python

import mapnik
import cairo

m = mapnik.Map(15000, 15000, "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs") # end result: OpenStreetMap projection
m.background = mapnik.Color(0, 0, 0, 0)

bbox = mapnik.Envelope(-10000000, 2000000, -4000000, -19000000) # must be adjusted
m.zoom_to_box(bbox)

s = mapnik.Style()
r = mapnik.Rule()

polygonSymbolizer = mapnik.PolygonSymbolizer()
polygonSymbolizer.fill_opacity = 0.0
r.symbols.append(polygonSymbolizer)

lineSymbolizer = mapnik.LineSymbolizer(mapnik.Color('red'), 1.0)
r.symbols.append(lineSymbolizer)

s.rules.append(r)
m.append_style('My Style',s)

lyr = mapnik.Layer('path', '+init=epsg:4326')
lyr.datasource = mapnik.Ogr(file = './path.kml', layer = 'path')
lyr.styles.append('My Style')
m.layers.append(lyr)

# mapnik.render_to_file(m,'./path.png', 'png')

file = open('./path.pdf', 'wb')
surface = cairo.PDFSurface(file.name, m.width, m.height)
mapnik.render(m, surface)
surface.finish()
like image 164
Ole Begemann Avatar answered Nov 14 '22 21:11

Ole Begemann


See http://mapnik.org/faq/, it supports OGR formats (KML is one of them). Mapnik has python bindings and is easy to use.

Cairo renderer supports PDF and SVG, you just need to set everything up correctly.

like image 33
alxx Avatar answered Nov 14 '22 22:11

alxx