def dotile(z, x, y): x_str = "%s" % x y_str = "%s" % y z_str = "%s" % z p0 = gprj.fromPixelToLL((x * 256, (y + 1) * 256), z) p1 = gprj.fromPixelToLL(((x + 1) * 256, y * 256), z) print z, x, y, p0, p1 # render a new tile and store it on filesystem c0 = prj.forward(Coord(p0[0], p0[1])) c1 = prj.forward(Coord(p1[0], p1[1])) bbox = Envelope(c0.x, c0.y, c1.x, c1.y) bbox.width(bbox.width() * 2) bbox.height(bbox.height() * 2) m.zoom_to_box(bbox) if not os.path.isdir(tile_dir + z_str): os.mkdir(tile_dir + z_str) if not os.path.isdir(tile_dir + z_str + '/' + x_str): os.mkdir(tile_dir + z_str + '/' + x_str) tile_uri = tile_dir + z_str + '/' + x_str + '/' + y_str + '.png' im = Image(512, 512) render(m, im) im = fromstring('RGBA', (512, 512), rawdata(im)) im = im.crop((128, 128, 512 - 128, 512 - 128)) fh = open(tile_uri, 'w+b') im.save(fh, 'PNG', quality=100) command = "convert -colors 255 %s %s" % (tile_uri, tile_uri) call(command, shell=True)
def GetMap(self, params): if params["bbox"][0] >= params["bbox"][2]: raise OGCException("BBOX values don't make sense. minx is greater than maxx.") if params["bbox"][1] >= params["bbox"][3]: raise OGCException("BBOX values don't make sense. miny is greater than maxy.") if params.has_key("styles") and len(params["styles"]) != len(params["layers"]): raise OGCException("STYLES length does not match LAYERS length.") m = Map(params["width"], params["height"]) if params.has_key("transparent") and params["transparent"] == "FALSE": m.background = params["bgcolor"] else: m.background = Color(0, 0, 0, 0) maplayers = self.mapfactory.layers mapstyles = self.mapfactory.styles for layername in params["layers"]: try: layer = maplayers[layername] except KeyError: raise OGCException('Layer "%s" not defined.' % layername, "LayerNotDefined") for stylename in layer.styles: if stylename in mapstyles.keys(): m.append_style(stylename, mapstyles[stylename]) else: raise ServerConfigurationError( 'Layer "%s" refers to non-existent style "%s".' % (layername, stylename) ) m.layers.append(layer) m.zoom_to_box(Envelope(params["bbox"][0], params["bbox"][1], params["bbox"][2], params["bbox"][3])) im = Image(params["width"], params["height"]) render(m, im) im = fromstring("RGBA", (params["width"], params["height"]), rawdata(im)) fh = StringIO() im.save(fh, PIL_TYPE_MAPPING[params["format"]], quality=100) fh.seek(0) return Response(params["format"], fh.read())
def dotile(z,x,y): x_str = "%s" % x y_str = "%s" % y z_str = "%s" % z p0 = gprj.fromPixelToLL((x * 256, (y+1) * 256),z) p1 = gprj.fromPixelToLL(((x+1) * 256, y* 256),z) print z,x,y,p0,p1 # render a new tile and store it on filesystem c0 = prj.forward(Coord(p0[0],p0[1])) c1 = prj.forward(Coord(p1[0],p1[1])) bbox = Envelope(c0.x,c0.y,c1.x,c1.y) bbox.width(bbox.width() * 2) bbox.height(bbox.height() * 2) m.zoom_to_box(bbox) if not os.path.isdir(tile_dir + z_str): os.mkdir(tile_dir + z_str) if not os.path.isdir(tile_dir + z_str + '/' + x_str): os.mkdir(tile_dir + z_str + '/' + x_str) tile_uri = tile_dir + z_str + '/' + x_str + '/' + y_str + '.png' im = Image(512, 512) render(m, im) im = fromstring('RGBA', (512, 512), rawdata(im)) im = im.crop((128,128,512-128,512-128)) fh = open(tile_uri,'w+b') im.save(fh, 'PNG', quality=100) command = "convert -colors 255 %s %s" % (tile_uri,tile_uri) call(command, shell=True)
def GetMap(self, params): m = self._buildMap(params) im = Image(params['width'], params['height']) render(m, im) im = fromstring('RGBA', (params['width'], params['height']), rawdata(im)) fh = StringIO() im.save(fh, PIL_TYPE_MAPPING[params['format']], quality=100) fh.seek(0) return Response(params['format'], fh.read())
def GetMap(self, params): if params['bbox'][0] >= params['bbox'][2]: raise OGCException( "BBOX values don't make sense. minx is greater than maxx.") if params['bbox'][1] >= params['bbox'][3]: raise OGCException( "BBOX values don't make sense. miny is greater than maxy.") if params.has_key('styles') and len(params['styles']) != len( params['layers']): raise OGCException('STYLES length does not match LAYERS length.') m = Map(params['width'], params['height']) if params.has_key('transparent') and params['transparent'] == 'FALSE': m.background = params['bgcolor'] else: m.background = Color(0, 0, 0, 0) maplayers = self.mapfactory.layers mapstyles = self.mapfactory.styles for layername in params['layers']: try: layer = maplayers[layername] except KeyError: raise OGCException('Layer "%s" not defined.' % layername, 'LayerNotDefined') for stylename in layer.styles: if stylename in mapstyles.keys(): m.append_style(stylename, mapstyles[stylename]) else: raise ServerConfigurationError( 'Layer "%s" refers to non-existent style "%s".' % (layername, stylename)) m.layers.append(layer) m.zoom_to_box( Envelope(params['bbox'][0], params['bbox'][1], params['bbox'][2], params['bbox'][3])) im = Image(params['width'], params['height']) render(m, im) im = fromstring('RGBA', (params['width'], params['height']), rawdata(im)) fh = StringIO() im.save(fh, PIL_TYPE_MAPPING[params['format']], quality=100) fh.seek(0) return Response(params['format'], fh.read())
def render_tiles(bbox, mapfile, tile_dir, mapkoordfile, write_to_disk, minZoom=1, maxZoom=18, img_ext='', name="unknown"): print "Render_tiles(", bbox, "\n ", \ mapfile, " ", tile_dir, "\n ", \ "minZoom=%d" % minZoom, "maxZoom=%d" % maxZoom, name, img_ext, ")" if write_to_disk: fh_mapkoord = open(mapkoordfile, "a") if fh_mapkoord == 0: sys.exit("Can not open map_koord.txt.") if not os.path.isdir(tile_dir): os.mkdir(tile_dir) else: fh_mapkoord = open(mapkoordfile, "r") if fh_mapkoord == 0: sys.exit("Can not open map_koord.txt.") gprj = GoogleProjection(maxZoom + 1) #m = Map(2 * 256,2 * 256) m = Map(1280, 1024) load_map(m, mapfile) #prj = Projection("+proj=merc +datum=WGS84") # What follows is from /usr/share/proj/esri.extra, EPSG:900913 # "Chris' funny epsgish code for the google mercator" prj = Projection( "+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" ) ll0 = (bbox[0], bbox[3]) ll1 = (bbox[2], bbox[1]) for z in range(minZoom, maxZoom + 1): if z == 9: print "CAUTION: Mercator projection begins to be noticeably distorted at this zoom level." elif z < 9: print "WARNING: Mercator projection is very distorted at this zoom level." px0 = gprj.fromLLtoPixel(ll0, z) px1 = gprj.fromLLtoPixel(ll1, z) for x in range(int(px0[0] / 640.0), int(px1[0] / 640.0) + 1): for y in range(int(px0[1] / 512.0), int(px1[1] / 512.0) + 1): p0 = gprj.fromPixelToLL((x * 640.0, (y + 1) * 512.0), z) p1 = gprj.fromPixelToLL(((x + 1) * 640.0, y * 512.0), z) actual_scale = calc_scale((p0[1] + p1[1]) / 2, z) # render a new tile and store it on filesystem c0 = prj.forward(Coord(p0[0], p0[1])) c1 = prj.forward(Coord(p1[0], p1[1])) bbox = Envelope(c0.x, c0.y, c1.x, c1.y) bbox.width(bbox.width() * 2) bbox.height(bbox.height() * 2) m.zoom_to_box(bbox) # check if we have directories in place zoom = "%s" % z str_x = "%s" % x str_y = "%s" % y if write_to_disk: if not os.path.isdir(tile_dir + zoom): os.mkdir(tile_dir + zoom) if not os.path.isdir(tile_dir + zoom + '/' + str_x): os.mkdir(tile_dir + zoom + '/' + str_x) tile_uri = tile_dir + zoom + '/' + str_x + '/' + str_y + img_ext tile_path = "mapnik/" + zoom + '/' + str_x + '/' + str_y + img_ext exists = '' if os.path.isfile(tile_uri): exists = " [exists]" empty = '' bytes = os.stat(tile_uri)[6] # FIXME: I don't thing this bytes test actually works... if bytes == 137: empty = "Empty Tile" else: im = Image(1280, 1024) render(m, im) im = fromstring('RGBA', (1280, 1024), im.tostring()).convert("RGB") #im = im.crop((128,128,512-128,512-127)) if write_to_disk: fh = open(tile_uri, 'w+b') if img_ext == '.png': im.save(fh, 'PNG', optimize=True) else: im.save(fh, 'JPEG', quality=85) # 'convert' is a program from the Imagemagick package command = "convert -type optimize %s %s" % (tile_uri, tile_uri) call(command, shell=True) fh_mapkoord.write(tile_path + " ") fh_mapkoord.write(str((p0[1] + p1[1]) / 2) + " ") fh_mapkoord.write(str((p0[0] + p1[0]) / 2) + " ") fh_mapkoord.write(str(actual_scale)) fh_mapkoord.write(" " + str(p0[1]) + " " + str(p0[0])) fh_mapkoord.write(" " + str(p1[1]) + " " + str(p1[0])) fh_mapkoord.write("\n") bytes = os.stat(tile_uri)[6] empty = "[created]" if bytes == 137: empty = "Empty Tile" else: empty = "[simulation]" print name, "[%d-%d]: " % (minZoom,maxZoom), \ "zoom:%2d " % z, "scale=1:%.1f " % actual_scale, \ "x:%5d " % x, "y:%5d " % y, \ " p:(%.7f, %.7f)/(%.7f, %.7f)" % (p0[0],p0[1],p1[0],p1[1]), \ exists, empty fh_mapkoord.close()
def render_tiles(bbox, mapfile, tile_dir, mapkoordfile, write_to_disk, minZoom=1,maxZoom=18, img_ext='', name="unknown"): print "Render_tiles(", bbox, "\n ", \ mapfile, " ", tile_dir, "\n ", \ "minZoom=%d" % minZoom, "maxZoom=%d" % maxZoom, name, img_ext, ")" if write_to_disk: fh_mapkoord = open(mapkoordfile, "a") if fh_mapkoord == 0: sys.exit("Can not open map_koord.txt.") if not os.path.isdir(tile_dir): os.mkdir(tile_dir) else: fh_mapkoord = open(mapkoordfile, "r") if fh_mapkoord == 0: sys.exit("Can not open map_koord.txt.") gprj = GoogleProjection(maxZoom+1) #m = Map(2 * 256,2 * 256) m = Map(1280,1024) load_map(m,mapfile) #prj = Projection("+proj=merc +datum=WGS84") # What follows is from /usr/share/proj/esri.extra, EPSG:900913 # "Chris' funny epsgish code for the google mercator" prj = Projection("+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") ll0 = (bbox[0],bbox[3]) ll1 = (bbox[2],bbox[1]) for z in range(minZoom,maxZoom + 1): if z == 9: print "CAUTION: Mercator projection begins to be noticeably distorted at this zoom level." elif z < 9: print "WARNING: Mercator projection is very distorted at this zoom level." px0 = gprj.fromLLtoPixel(ll0,z) px1 = gprj.fromLLtoPixel(ll1,z) for x in range(int(px0[0]/640.0),int(px1[0]/640.0)+1): for y in range(int(px0[1]/512.0),int(px1[1]/512.0)+1): p0 = gprj.fromPixelToLL((x * 640.0, (y+1) * 512.0),z) p1 = gprj.fromPixelToLL(((x+1) * 640.0, y * 512.0),z) actual_scale = calc_scale( (p0[1] + p1[1])/2, z) # render a new tile and store it on filesystem c0 = prj.forward(Coord(p0[0],p0[1])) c1 = prj.forward(Coord(p1[0],p1[1])) bbox = Envelope(c0.x,c0.y,c1.x,c1.y) bbox.width(bbox.width() * 2) bbox.height(bbox.height() * 2) m.zoom_to_box(bbox) # check if we have directories in place zoom = "%s" % z str_x = "%s" % x str_y = "%s" % y if write_to_disk: if not os.path.isdir(tile_dir + zoom): os.mkdir(tile_dir + zoom) if not os.path.isdir(tile_dir + zoom + '/' + str_x): os.mkdir(tile_dir + zoom + '/' + str_x) tile_uri = tile_dir + zoom + '/' + str_x + '/' + str_y + img_ext tile_path = "mapnik/" + zoom + '/' + str_x + '/' + str_y + img_ext exists= '' if os.path.isfile(tile_uri): exists= " [exists]" empty= '' bytes = os.stat(tile_uri)[6] # FIXME: I don't thing this bytes test actually works... if bytes == 137: empty = "Empty Tile" else: im = Image(1280, 1024) render(m, im) im = fromstring('RGBA', (1280, 1024), im.tostring()).convert("RGB") #im = im.crop((128,128,512-128,512-127)) if write_to_disk: fh = open(tile_uri,'w+b') if img_ext == '.png': im.save(fh, 'PNG', optimize=True) else: im.save(fh, 'JPEG', quality=85) # 'convert' is a program from the Imagemagick package command = "convert -type optimize %s %s" % (tile_uri,tile_uri) call(command, shell=True) fh_mapkoord.write(tile_path + " ") fh_mapkoord.write(str((p0[1] + p1[1]) / 2) + " ") fh_mapkoord.write(str((p0[0] + p1[0]) / 2) + " ") fh_mapkoord.write(str(actual_scale)) fh_mapkoord.write(" " + str(p0[1]) + " " + str(p0[0])) fh_mapkoord.write(" " + str(p1[1]) + " " + str(p1[0])) fh_mapkoord.write("\n") bytes = os.stat(tile_uri)[6] empty= "[created]" if bytes == 137: empty = "Empty Tile" else: empty = "[simulation]" print name, "[%d-%d]: " % (minZoom,maxZoom), \ "zoom:%2d " % z, "scale=1:%.1f " % actual_scale, \ "x:%5d " % x, "y:%5d " % y, \ " p:(%.7f, %.7f)/(%.7f, %.7f)" % (p0[0],p0[1],p1[0],p1[1]), \ exists, empty fh_mapkoord.close()