programmatically converting kml to image [closed]
Asked Answered
S

5

7

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?

Syracuse answered 25/8, 2010 at 17:5 Comment(0)
O
6

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()
Onionskin answered 20/10, 2010 at 15:28 Comment(1)
FYI, 404 on your second linkTranscendentalism
C
3

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.

Centering answered 27/8, 2010 at 7:33 Comment(0)
I
0

Have a look at this post "Interactive mapping with HTML5, JavaScript, and Canvas". It is about rendering KML with the HTML5 canvas element.

Hope it helps.

http://indiemaps.com/blog/2010/06/interactive-mapping-with-html5-javascript-and-canvas/

Inclusion answered 25/9, 2010 at 23:13 Comment(1)
Link is dead, unfortunatelyContra
M
0

You could conceivably use Matplotlib and its toolkit Basemap to plot the images on a map (if you can retrieve the background map image that the user wants...).

Madisonmadlen answered 10/12, 2010 at 8:16 Comment(0)
P
-1

It's not really a question of "convert"; KML's can hold all sorts of things - polygons, icons, links, info, etc.

However, KML is XML, so there's no reason why you couldn't parse the KML file with something like expat or etree to extract the polygon definitions. From there you could (for example) draw them on a PDF.

Photic answered 27/8, 2010 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.