def expose_ev(self, widget, event): if(widget.allocation.width != self.width or widget.allocation.height != self.height ): #debug("Rrresize!") self.width = widget.allocation.width self.height = widget.allocation.height cr = widget.window.cairo_create() if self.old_center_coord != self.center_coord or self.old_zoom != self.zoom: #print "Recentered!" xy = projections.from4326(self.center_coord,"EPSG:3857") xy1 = projections.to4326((xy[0]-40075016.*(0.5**(self.zoom))/self.tiles.tilewidth*self.width, xy[1]-40075016.*(0.5**(self.zoom))/self.tiles.tileheight*self.height), "EPSG:3857") xy2 = projections.to4326((xy[0]+40075016.*(0.5**(self.zoom))/self.tiles.tilewidth*self.width, xy[1]+40075016.*(0.5**(self.zoom))/self.tiles.tileheight*self.height), "EPSG:3857") self.bbox = (xy1[0],xy1[1],xy2[0],xy2[1]) self.tilebox = projections.tile_by_bbox(self.bbox, self.zoom, "EPSG:3857") self.old_center_coord = self.center_coord self.old_zoom = self.zoom from_tile_x, from_tile_y, to_tile_x, to_tile_y = self.tilebox dx = 1.*(from_tile_x - int(from_tile_x))*self.tiles.tilewidth dy = 1.*(from_tile_y - int(from_tile_y))*self.tiles.tileheight print dx,dy #print self.dx, self.dy onscreen_tiles = set() for x in range (int(from_tile_x), int(to_tile_x)+1): for y in range (int(to_tile_y), int(from_tile_y)+1): onscreen_tiles.add((self.zoom,x,y)) self.tiles.onscreen = onscreen_tiles for z,x,y in onscreen_tiles: tile = self.tiles[(self.zoom,x,y)] #print dx+(x-from_tile_x)*self.tiles.tilewidth-self.width #print dy+(y-from_tile_y)*self.tiles.tileheight-self.height #cr.set_source_surface(tile, int(self.dx-dx+(x-int(from_tile_x))*self.tiles.tilewidth-self.width), int(self.dy-dy-(int(from_tile_y)-y)*self.tiles.tileheight+self.height)) cr.set_source_surface(tile, int(self.dx-dx+(x-int(from_tile_x))*self.tiles.tilewidth), int(self.dy-dy-(int(from_tile_y)-y)*self.tiles.tileheight+self.height)) cr.paint()
def update_surface_by_center(self, lonlat, zoom, style): self.zoom = zoom xy = projections.from4326(lonlat, self.proj) xy1 = projections.to4326((xy[0]-40075016*0.5**self.zoom/256*self.w, xy[1]-40075016*0.5**self.zoom/256*self.h), self.proj) xy2 = projections.to4326((xy[0]+40075016*0.5**self.zoom/256*self.w, xy[1]+40075016*0.5**self.zoom/256*self.h), self.proj) bbox = (xy1[0],xy1[1],xy2[0],xy2[1]) debug (bbox) return self.update_surface(bbox, zoom, style)
def update_surface_by_center(self, lonlat, zoom, style): self.zoom = zoom xy = projections.from4326(lonlat, self.proj) xy1 = projections.to4326( (xy[0] - 40075016 * 0.5**self.zoom / 256 * self.w, xy[1] - 40075016 * 0.5**self.zoom / 256 * self.h), self.proj) xy2 = projections.to4326( (xy[0] + 40075016 * 0.5**self.zoom / 256 * self.w, xy[1] + 40075016 * 0.5**self.zoom / 256 * self.h), self.proj) bbox = (xy1[0], xy1[1], xy2[0], xy2[1]) debug(bbox) return self.update_surface(bbox, zoom, style)
def expose_ev(self, widget, event): if (widget.allocation.width != self.width or widget.allocation.height != self.height): # debug("Rrresize!") self.width = widget.allocation.width self.height = widget.allocation.height cr = widget.window.cairo_create() if self.old_center_coord != self.center_coord or self.old_zoom != self.zoom: # print "Recentered!" xy = projections.from4326(self.center_coord, "EPSG:3857") xy1 = projections.to4326( (xy[0] - 40075016. * (0.5**(self.zoom)) / self.tiles.tilewidth * self.width, xy[1] - 40075016. * (0.5**(self.zoom)) / self.tiles.tileheight * self.height), "EPSG:3857") xy2 = projections.to4326( (xy[0] + 40075016. * (0.5**(self.zoom)) / self.tiles.tilewidth * self.width, xy[1] + 40075016. * (0.5**(self.zoom)) / self.tiles.tileheight * self.height), "EPSG:3857") self.bbox = (xy1[0], xy1[1], xy2[0], xy2[1]) self.tilebox = projections.tile_by_bbox(self.bbox, self.zoom, "EPSG:3857") self.old_center_coord = self.center_coord self.old_zoom = self.zoom from_tile_x, from_tile_y, to_tile_x, to_tile_y = self.tilebox dx = 1. * (from_tile_x - int(from_tile_x)) * self.tiles.tilewidth dy = 1. * (from_tile_y - int(from_tile_y)) * self.tiles.tileheight print dx, dy # print self.dx, self.dy onscreen_tiles = set() for x in range(int(from_tile_x), int(to_tile_x) + 1): for y in range(int(to_tile_y), int(from_tile_y) + 1): onscreen_tiles.add((self.zoom, x, y)) self.tiles.onscreen = onscreen_tiles for z, x, y in onscreen_tiles: tile = self.tiles[(self.zoom, x, y)] # print dx+(x-from_tile_x)*self.tiles.tilewidth-self.width # print dy+(y-from_tile_y)*self.tiles.tileheight-self.height # cr.set_source_surface(tile, int(self.dx-dx+(x-int(from_tile_x))*self.tiles.tilewidth-self.width), int(self.dy-dy-(int(from_tile_y)-y)*self.tiles.tileheight+self.height)) cr.set_source_surface( tile, int(self.dx - dx + (x - int(from_tile_x)) * self.tiles.tilewidth), int(self.dy - dy - (int(from_tile_y) - y) * self.tiles.tileheight + self.height)) cr.paint()
def screen2lonlat(self, x, y): lo1, la1, lo2, la2 = self.bbox_p debug("%s %s - %s %s" % (x, y, self.w, self.h)) debug(self.bbox_p) return projections.to4326((1. * x / self.w * (lo2 - lo1) + lo1, la2 + (1. * y / (self.h) * (la1 - la2))), self.proj)
def twms_main(req): resp = "" data = req srs = data.get("srs", data.get("SRS", "EPSG:4326")) content_type = "image/png" #layer = data.get("layers",data.get("LAYERS", config.default_layers)).split(",") width=0 height=0 req_bbox = () if data.get("bbox",data.get("BBOX",None)): req_bbox = tuple(map(float,data.get("bbox",data.get("BBOX",req_bbox)).split(","))) req_bbox = projections.to4326(req_bbox, srs) req_bbox, flip_h = bbox.normalize(req_bbox) box = req_bbox height = int(data.get("height",data.get("HEIGHT",height))) width = int(data.get("width",data.get("WIDTH",width))) z = bbox.zoom_for_bbox (box, (height, width), {"proj":"EPSG:3857"}, min_zoom = 1, max_zoom = 25,max_size = (10000,10000)) res = RasterTile(width, height, z, db) res.update_surface(box, z, style) image_content = StringIO.StringIO() res.surface.write_to_png(image_content) resp = image_content.getvalue() return (OK, content_type, resp)
def release_ev(self, widget, event): if event.button == 1: #debug("Stop drag") self.drag = False self.timer.stop() #debug("dd: %s,%s "%(self.dx, self.dy)) x = event.x y = event.y lo1, la1, lo2, la2 = projections.from4326(self.bbox, "EPSG:3857") print lo1, la1, lo2, la2 #self.center_coord = projections.to4326((0.5*(self.width+self.dx)/self.width*(lo1-lo2)+lo2, la1+(0.5*(self.height+self.dy)/self.height*(la2-la1))),"EPSG:3857") self.center_coord = projections.to4326((0.5*(self.width+2*self.dx)/self.width*(lo1-lo2)+lo2, la1+(0.5*(self.height+2*self.dy)/self.height*(la2-la1))),"EPSG:3857") #self.rastertile.screen2lonlat(self.rastertile.w/2 - self.dx, self.rastertile.h/2 - self.dy); self.dx = 0 self.dy = 0 self.redraw()
def release_ev(self, widget, event): if event.button == 1: # debug("Stop drag") self.drag = False self.timer.stop() # debug("dd: %s,%s "%(self.dx, self.dy)) x = event.x y = event.y lo1, la1, lo2, la2 = projections.from4326(self.bbox, "EPSG:3857") print lo1, la1, lo2, la2 # self.center_coord = projections.to4326((0.5*(self.width+self.dx)/self.width*(lo1-lo2)+lo2, la1+(0.5*(self.height+self.dy)/self.height*(la2-la1))),"EPSG:3857") self.center_coord = projections.to4326( (0.5 * (self.width + 2 * self.dx) / self.width * (lo1 - lo2) + lo2, la1 + (0.5 * (self.height + 2 * self.dy) / self.height * (la2 - la1))), "EPSG:3857") # self.rastertile.screen2lonlat(self.rastertile.w/2 - self.dx, self.rastertile.h/2 - self.dy); self.dx = 0 self.dy = 0 self.redraw()
def screen2lonlat(self, x, y): lo1, la1, lo2, la2 = self.bbox_p debug ("%s %s - %s %s"%(x,y,self.w, self.h)) debug(self.bbox_p) return projections.to4326( (1.*x/self.w*(lo2-lo1)+lo1, la2+(1.*y/(self.h)*(la1-la2))),self.proj)