def stats_tms(request, t=None, stat=None, z=None, x=None, y=None, u=None, ext=None): #==# verbose = True ix = None iy = None iyf = None iz = None if u: iz, ix, iy = quadkey_to_tms(u) elif x and y and z: ix = int(x) iy = int(y) iz = int(z) if t == "regular": ify = flip_y(ix,iy,iz,256,webmercator_bbox) else: ify = iy iy = flip_y(ix,ify,iz,256,webmercator_bbox) #stats = stats_tilerequest() cache, stats = get_from_cache('default','stats_tilerequests') key = z+"/"+x+"/"+y if not stats: return None if not stat: return None image = None if key in stats['global'][stat]: blue = (256.0 * stats['global'][stat][key]) / stats['tile']['max'] image = solidTile(width=256, height=256, b=int(blue), a=128) else: image = blankTile(width=256, height=256) if image: response = HttpResponse(content_type="image/png") image.save(response, "PNG") return response else: return None
def getYValues(tileservice, tilesource, ix, iy, iz): iyf = -1 if tileservice: if tileservice["type"] == TYPE_TMS_FLIPPED or tileservice["type"] == TYPE_BING: iyf = iy iy = flip_y(ix, iyf, iz, 256, webmercator_bbox) else: iyf = flip_y(ix, iy, iz, 256, webmercator_bbox) else: if tilesource["type"] == TYPE_TMS_FLIPPED or tilesource["type"] == TYPE_BING: iyf = iy iy = flip_y(ix, iyf, iz, 256, webmercator_bbox) else: iyf = flip_y(ix, iy, iz, 256, webmercator_bbox) return (iy, iyf)
def taskRequestTile(ts, iz, ix, iy, ext, verbose=True): now = datetime.datetime.now() # Load Logging Info #log_root = settings.LOG_REQUEST_ROOT log_format = settings.LOG_REQUEST_FORMAT #if log_root and log_format: # if not os.path.exists(log_root): # os.makedirs(log_root) #if settings.LOG_ERRORS_ROOT # if not os.path.exists(log_root): # os.makedirs(log_root) indirect_file = settings.LOG_INDIRECT_ROOT+os.sep+"requests_tiles_"+now.strftime('%Y-%m-%d')+"_indirect.tsv" # Find TileSource tilesource = None tilesources = getTileSources(proxy=True) for candidate in tilesources: if candidate['id'] == ts: tilesource = candidate break if not tilesource: error_file = settings.LOG_ERRORS_ROOT+os.sep+"requests_tiles_"+now.strftime('%Y-%m-%d')+"_errors.txt" with open(error_file,'a') as f: line = "Error: Could not find tilesource for primary key "+str(ts)+"." f.write(line+"\n") return #Y is always in regualar TMS before being added to task queue iyf = flip_y(ix,iy,iz) #iy, iyf = getYValues(None,tilesource,ix,iy,iz) tile_bbox = tms_to_bbox(ix,iy,iz) #Check if requested tile is within source's extents returnBlankTile = False returnErrorTile = False intersects = True if tilesource['extents']: intersects = bbox_intersects_source(tilesource,ix,iyf,iz) if not intersects: returnBlankTile = True validZoom = 0 #Check if inside source zoom levels if tilesource['minZoom'] or tilesource['maxZoom']: if (tilesource['minZoom'] and iz < tilesource['minZoom']): validZoom = -1 elif (tilesource['maxZoom'] and iz > tilesource['maxZoom']): validZoom = 1 if validZoom != 0: #returnBlank = True returnErrorTile = True if returnBlankTile or returnErrorTile: return tile = None if iz >= settings.TILEJET['cache']['memory']['minZoom'] and iz <= settings.TILEJET['cache']['memory']['maxZoom']: #key = "{layer},{z},{x},{y},{ext}".format(layer=tilesource.name,x=ix,y=iy,z=iz,ext=ext) key = ",".join([tilesource['name'],str(iz),str(ix),str(iy),ext]) tilecache, tile = getTileFromCache( settings.CACHES['tiles']['LOCATION'], settings.CACHES['tiles'], 'tiles', key, True, GEVENT_MONKEY_PATCH=True) if not tilecache: error_file = settings.LOG_ERRORS_ROOT+os.sep+"requests_tiles_"+now.strftime('%Y-%m-%d')+"_errors.txt" with open(error_file,'a') as f: line = "Error: Could not connect to cache (tiles)." f.write(line+"\n") return if tile: if verbose: print "task / cache hit for "+key else: if verbose: print "task / cache miss for "+key with open(indirect_file,'a') as f: line = log_format.format( status='indirect', tileorigin=tilesource['origin'], tilesource=tilesource['name'], z=iz,x=ix,y=iy, ip='-', datetime=now.isoformat()) f.write(line+"\n") from urllib2 import HTTPError try: if tilesource['type'] == TYPE_TMS: tile = requestTileFromSource(tilesource,ix,iy,iz,ext,True) elif tilesource['type'] == TYPE_TMS_FLIPPED: tile = requestTileFromSource(tilesource,ix,iyf,iz,ext,True) except HTTPError, err: error_file = settings.LOG_ERRORS_ROOT+os.sep+"requests_tiles_"+now.strftime('%Y-%m-%d')+"_errors.txt" with open(error_file,'a') as f: line = "Error: HTTPError. Could not get tile ("+key+") from source." f.write(line+"\n") return except: