Skip to content

Instantly share code, notes, and snippets.

@ndimiduk
Created March 17, 2013 22:23
Show Gist options
  • Save ndimiduk/5183913 to your computer and use it in GitHub Desktop.
Save ndimiduk/5183913 to your computer and use it in GitHub Desktop.
Attempting to repro mapnik-support #7 (https://github.com/mapnik/mapnik-support/issues/7).
#!/usr/bin/env python
import io
import mapnik
from shapely.geometry import box, MultiPoint
class SimpleDatasource(mapnik.PythonDatasource):
def __init__(self):
self.points = MultiPoint([(-1.0,-1.0), (-1.0,1.0), (1.0,1.0), (1.0,-1.0)])
def extend (delta, x):
if x > 0: return x + delta
else: return x - delta
self.envelope = mapnik.Box2d(*[extend(0.1,x) for x in self.points.bounds])
super(SimpleDatasource, self).__init__(
envelope=self.envelope,
)
def features(self, query):
b = box(query.bbox.minx,query.bbox.miny,query.bbox.maxx,query.bbox.maxy)
return mapnik.PythonDatasource.wkb_features(
keys = (),
features = [(x.wkb, {}) for x in self.points if b.contains(x)]
)
def init_map():
m = mapnik.Map(256,256)
m.background_color = mapnik.Color('white')
s = mapnik.Style()
r = mapnik.Rule()
r.symbols.append(mapnik.PointSymbolizer())
s.rules.append(r)
m.append_style('point_style', s)
ds = mapnik.Python(factory='SimpleDatasource')
l = mapnik.Layer('points')
l.datasource = ds
l.styles.append('point_style')
m.layers.append(l)
return m
if __name__=='__main__':
map = init_map()
map.zoom_all()
mapnik.render_to_file(map, 'mapnik_rendered.png')
im = mapnik.Image(256,256)
mapnik.render(map,im)
im.save('im_rendered.png', 'png')
buf = im.tostring('png')
with open('manually.png', 'wb') as f:
f.write(buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment