def GetMap(self, params): m = self._buildMap(params) im = Image(params['width'], params['height']) render(m, im) format = PIL_TYPE_MAPPING[params['format']] return Response(params['format'].replace('8',''), im.tostring(format))
def process(self, tile): #from lat,lng bbox to mapnik bbox p0 = self.map_proj.forward(Coord(tile.bbox[0][1],tile.bbox[0][0])) p1 = self.map_proj.forward(Coord(tile.bbox[1][1],tile.bbox[1][0])) bbox = Box2d(p0,p1) image = Image(tile.size[0],tile.size[1]) features = None result = self._check_region(bbox) if result is not None: if result[0]: result[1].resize(image.width(),image.height()) result[1].zoom_to_box(bbox) render(result[1],image) features = self.save_rendered_metadata(result[1], tile.size, tile.dimensions) else : mq_logging.info("COMPOSITE MAP: %s" % result[2]) default_image = Image(tile.size[0],tile.size[1]) # mask style self.mask_map.resize(image.width(),image.height()) self.mask_map.zoom_to_box(bbox) render(self.mask_map,image) # default style self.default_map.resize(default_image.width(),default_image.height()) self.default_map.zoom_to_box(bbox) render(self.default_map,default_image) features = self.save_rendered_metadata(self.default_map, tile.size, tile.dimensions) # composite DST_OUT default_image.composite(image,CompositeOp.dst_out) # current style result[1].resize(image.width(),image.height()) result[1].zoom_to_box(bbox) image.set_alpha(0) render(result[1],image) if features is not None: features.features.extend(self.save_rendered_metadata(result[1], tile.size, tile.dimensions).features) else: features = self.save_rendered_metadata(result[1], tile.size, tile.dimensions) # blend image.blend(0,0,default_image,1.0) else : # use default style self.default_map.resize(image.width(),image.height()) self.default_map.zoom_to_box(bbox) render(self.default_map,image) features = self.save_rendered_metadata(self.default_map, tile.size, tile.dimensions) #convert to PIL image image = PIL.Image.frombuffer('RGBA', (image.width(), image.height()), image.tostring(), 'raw', 'RGBA', 0, 3) return RenderResult.from_image(tile, image, features)
def main(argv): b = "-180,-90,180,90" i = "" o = "" l = "-1" try: opts, args = getopt.getopt(argv,"hb:i:o:l:",["bbox","inputXml","output","layerGrid"]) except getopt.GetoptError: print 'tilep.py -b <bbox> -i <inputXml> -o <output> -l <layerGrid>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'tilep.py -b <bbox> -i <inputXml> -o <output> -l <layerGrid>' sys.exit() elif opt in ("-b", "--bbox"): b = arg elif opt in ("-i", "--inputXml"): i = arg elif opt in ("-o", "--output"): o = arg elif opt in ("-l", "--layerGrid"): l = arg box = [] for s in b.split(",") : box.append(float(s)) geo_extent = Box2d(box[0],box[1],box[2],box[3]) geo_proj = Projection('+init=epsg:4326') merc_proj = Projection('+init=epsg:3857') transform = ProjTransform(geo_proj,merc_proj) merc_extent = transform.forward(geo_extent) mp = mapnik2.Map(256,256) #sys.exit(i) if i.find('http') >= 0: xmlurl=urllib2.urlopen(i) xmlstr=xmlurl.read() mapnik2.load_map_from_string(mp,xmlstr) xmldoc = minidom.parseString(xmlstr) else: mapnik2.load_map(mp, i) xmldoc = minidom.parse(i) mp.zoom_to_box(merc_extent) printed = 0 if o == 'png': im = Image(mp.width,mp.height) mapnik2.render(mp,im) fd, path = tempfile.mkstemp() os.write(fd,im.tostring('png')) os.fsync(fd) print path elif o == 'json': printed = 1 else: image = o+".png" if not os.path.exists(os.path.dirname(image)): try: os.makedirs(os.path.dirname(image)) except: pass mapnik2.render_to_file(mp, image) if l == '-1': print image lgrids = [] if l != '-1': for s in l.split(",") : lgrids.append(int(s)) else: sys.exit() itemlist = xmldoc.getElementsByTagName('Layer') fields = [] resolution = 4 #Pixel resolution of output. printed = 0 for ly in lgrids : dat = itemlist[ly].getElementsByTagName('Datasource')[0] par = dat.getElementsByTagName('Parameter') for s in par : if s.attributes['name'].value == 'fields': text = s.childNodes[0].nodeValue.encode("utf-8") #print "fields "+text fields = text.split(",") if s.attributes['name'].value == 'resolution': res = s.childNodes[0].nodeValue.encode("utf-8") #print "resolution "+res resolution = int(res) layer_index = ly #First layer on the map - index in m.layers key = "__id__" #Field used for the key in mapnik2 (should probably be unique) enfix = "" if ly > 0: enfix = "_"+str(ly) d = mapnik2.render_grid(mp, layer_index, key, resolution, fields) #returns a dictionary d = "grid("+json.dumps(d)+")" if o == 'json' and printed == 0: print d printed = 1 elif o == 'png': printed = 1 else: print d f = open(o+enfix+".json",'wb') f.write(d) f.close()
def GetMap(self, params): m = self._buildMap(params) im = Image(params['width'], params['height']) render(m, im) format = PIL_TYPE_MAPPING[params['format']] return Response(params['format'].replace('8', ''), im.tostring(format))
def GetMap(self, params): m = self._buildMap(params) im = Image(params['width'], params['height']) render(m, im) return Response(params['format'], im.tostring(PIL_TYPE_MAPPING[params['format']]))