def _query(self, uri): """Call a solr uri using json""" logger.info("Solr query: %s" % uri) if type(uri) == unicode: uri = uri.encode('utf-8') response = fetchers.fetch(uri + "&wt=json", timeout=self.timeout) if response.status != 200: raise DataSourceError( "Http call '%s' failed!: %s" % (uri, response.status) ) data = None try: data = json.loads(response.body.decode('utf-8')) except Exception, ex: logger.warning("Exception parsing json response: %s" % response.body) logger.exception(ex)
def tile_image(layer, z, x, y, start_time, again=False, trybetter=True, real=False): """ Returns asked image. again - is this a second pass on this tile? trybetter - should we try to combine this tile from better ones? real - should we return the tile even in not good quality? """ x = x % (2**(z - 1)) if y < 0 or y >= (2**(z - 1)): return None if not bbox.bbox_is_in(projections.bbox_by_tile(z, x, y, layer["proj"]), layer.get("data_bounding_box", config.default_bbox), fully=False): return None global cached_objs, cached_hist_list if "prefix" in layer: if (layer["prefix"], z, x, y) in cached_objs: return cached_objs[(layer["prefix"], z, x, y)] if layer.get("cached", True): local = config.tiles_cache + layer[ "prefix"] + "/z%s/%s/x%s/%s/y%s." % (z, x / 1024, x, y / 1024, y) ext = layer["ext"] if "cache_ttl" in layer: for ex in [ext, "dsc." + ext, "ups." + ext, "tne"]: f = local + ex if os.path.exists(f): if (os.stat(f).st_mtime < (time.time() - layer["cache_ttl"])): os.remove(f) gpt_image = False try: "trying to create local cache directory, if it doesn't exist" os.makedirs("/".join(local.split("/")[:-1])) except OSError: pass if not os.path.exists(local + "tne") and not os.path.exists(local + "lock"): if os.path.exists(local + ext): # First, look for tile in cache try: im1 = Image.open(local + ext) im1.is_ok = True return im1 except IOError: if os.path.exists(local + "lock"): return None else: os.remove(local + ext) # # Cached tile is broken - remove it if layer["scalable"] and ( z < layer.get("max_zoom", config.default_max_zoom) ) and trybetter: # Second, try to glue image of better ones if os.path.exists(local + "ups." + ext): try: im = Image.open(local + "ups." + ext) im.is_ok = True return im except IOError: pass ec = ImageColor.getcolor( layer.get("empty_color", config.default_background), "RGBA") ec = (ec[0], ec[1], ec[2], 0) im = Image.new("RGBA", (512, 512), ec) im1 = tile_image(layer, z + 1, x * 2, y * 2, start_time) if im1: im2 = tile_image(layer, z + 1, x * 2 + 1, y * 2, start_time) if im2: im3 = tile_image(layer, z + 1, x * 2, y * 2 + 1, start_time) if im3: im4 = tile_image(layer, z + 1, x * 2 + 1, y * 2 + 1, start_time) if im4: im.paste(im1, (0, 0)) im.paste(im2, (256, 0)) im.paste(im3, (0, 256)) im.paste(im4, (256, 256)) im = im.resize((256, 256), Image.ANTIALIAS) if layer.get("cached", True): try: im.save(local + "ups." + ext) except IOError: pass im.is_ok = True return im if not again: if "fetch" in layer: delta = (datetime.datetime.now() - start_time) delta = delta.seconds + delta.microseconds / 1000000. if (config.deadline > delta) or (z < 4): im = fetchers.fetch(z, x, y, layer) # Try fetching from outside if im: im.is_ok = True return im if real and (z > 1): im = tile_image(layer, z - 1, int(x / 2), int(y / 2), start_time, again=False, trybetter=False, real=True) if im: im = im.crop((128 * (x % 2), 128 * (y % 2), 128 * (x % 2) + 128, 128 * (y % 2) + 128)) im = im.resize((256, 256), Image.BILINEAR) im.is_ok = False return im else: if "fetch" in layer: delta = (datetime.datetime.now() - start_time) delta = delta.seconds + delta.microseconds / 1000000. if (config.deadline > delta) or (z < 4): im = fetchers.fetch(z, x, y, layer) # Try fetching from outside if im: im.is_ok = True return im
def tile_image (layer, z, x, y, start_time, again=False, trybetter = True, real = False): """ Returns asked image. again - is this a second pass on this tile? trybetter - should we try to combine this tile from better ones? real - should we return the tile even in not good quality? """ x = x % (2 ** (z-1)) if y<0 or y >= (2 ** (z-1)): return None if not bbox.bbox_is_in(projections.bbox_by_tile(z,x,y,layer["proj"]), layer.get("data_bounding_box",config.default_bbox), fully=False): return None global cached_objs, cached_hist_list if "prefix" in layer: if (layer["prefix"], z, x, y) in cached_objs: return cached_objs[(layer["prefix"], z, x, y)] if layer.get("cached", True): local = config.tiles_cache + layer["prefix"] + "/z%s/%s/x%s/%s/y%s."%(z, x/1024, x, y/1024,y) ext = layer["ext"] if "cache_ttl" in layer: for ex in [ext, "dsc."+ext, "ups."+ext, "tne"]: f = local+ex if os.path.exists(f): if (os.stat(f).st_mtime < (time.time()-layer["cache_ttl"])): os.remove(f) gpt_image = False try: "trying to create local cache directory, if it doesn't exist" os.makedirs("/".join(local.split("/")[:-1])) except OSError: pass if not os.path.exists(local+"tne") and not os.path.exists(local+"lock"): if os.path.exists(local+ext): # First, look for tile in cache try: im1 = Image.open(local+ext) im1.is_ok = True return im1 except IOError: if os.path.exists(local+"lock"): return None else: os.remove(local+ext) # # Cached tile is broken - remove it if layer["scalable"] and (z<layer.get("max_zoom", config.default_max_zoom)) and trybetter: # Second, try to glue image of better ones if os.path.exists(local+"ups."+ext): try: im = Image.open(local+"ups."+ext) im.is_ok = True return im except IOError: pass ec = ImageColor.getcolor(layer.get("empty_color", config.default_background), "RGBA") ec = (ec[0],ec[1],ec[2],0) im = Image.new("RGBA", (512, 512), ec) im1 = tile_image(layer, z+1,x*2,y*2, start_time) if im1: im2 = tile_image(layer, z+1,x*2+1,y*2, start_time) if im2: im3 = tile_image(layer, z+1,x*2,y*2+1, start_time) if im3: im4 = tile_image(layer, z+1,x*2+1,y*2+1, start_time) if im4: im.paste(im1,(0,0)) im.paste(im2,(256,0)) im.paste(im3,(0,256)) im.paste(im4,(256,256)) im = im.resize((256,256),Image.ANTIALIAS) if layer.get("cached", True): try: im.save(local+"ups."+ext) except IOError: pass im.is_ok = True return im if not again: if "fetch" in layer: delta = (datetime.datetime.now() - start_time) delta = delta.seconds + delta.microseconds/1000000. if (config.deadline > delta) or (z < 4): im = fetchers.fetch(z,x,y,layer) # Try fetching from outside if im: im.is_ok = True return im if real and (z>1): im = tile_image(layer, z-1, int(x/2), int(y/2), start_time, again=False, trybetter=False, real=True) if im: im = im.crop((128 * (x % 2), 128 * (y % 2), 128 * (x % 2) + 128, 128 * (y % 2) + 128)) im = im.resize((256,256), Image.BILINEAR) im.is_ok = False return im else: if "fetch" in layer: delta = (datetime.datetime.now() - start_time) delta = delta.seconds + delta.microseconds/1000000. if (config.deadline > delta) or (z < 4): im = fetchers.fetch(z,x,y,layer) # Try fetching from outside if im: im.is_ok = True return im