Exemple #1
0
 def dereference(self, obj):
     if not obj.has_key("ref"):
         return
     for ref in obj["ref"]:
         filename = ref["filename"]
         if not filename.startswith("/"):
             filename = os.path.join(self.get_root(), filename)
         try:
             cls = PRODUCTS[ref["product"]]
         except KeyError:
             raise RuntimeError('Unknown product type "%s"' % ref["product"])
         if ref.has_key("offset"):
             product = cls(ref["filename"], self, offset=ref["offset"])
         else:
             product = cls(ref["filename"], self)
         tile = product.tile(obj["layer"], obj["zoom"], obj["x"], obj["z"])
         if obj.has_key("data"):
             if obj["format"] == "png":
                 utils.array_update(obj["data"], tile["data"])
             elif obj["format"] == "json" and obj["type"] == "geojson":
                 utils.geojson_update(obj["data"], tile["data"])
         else:
             obj["data"] = tile["data"]
         if obj.has_key("raw_data"):
             del obj["raw_data"]
     del obj["ref"]
Exemple #2
0
    def save(self, obj, append=True):
        """Save object to profile.
        
        Object is a dictionary with the following mandatory and optional fields:
        
            layer   layer name [required]
            data    numpy array (format: png) or dictionary (format: json)
            ref     list of references to product files
            zoom    zoom level
            x       x-coordinate (type: x or xz)
            z       z-coordinate (type: xz)
        
        If append is True, object is merged with the original object.
        Return the object augmented with layer fields.
        """
        # Insert layer properties to obj.
        layer = self.layer_for(obj)
        o = layer.copy()
        o.update(obj)
        obj = o

        if append:
            orig_obj = self.load(obj, dereference=False)
            if orig_obj is not None:
                # Update data.
                if orig_obj.has_key("data") and obj.has_key("data"):
                    orig_obj["data"]
                    if layer["format"] == "png":
                        utils.array_update(orig_obj["data"], obj["data"])
                    elif layer["format"] == "json" and layer["type"] == "geojson":
                        utils.geojson_update(orig_obj["data"], obj["data"])
                elif obj.has_key("data"):
                    orig_obj["data"] = obj["data"]

                # Update ref.
                if orig_obj.has_key("ref") and obj.has_key("ref"):
                    utils.ref_update(orig_obj["ref"], obj["ref"])
                elif obj.has_key("ref"):
                    orig_obj["ref"] = obj["ref"]

                obj = orig_obj
                del obj["raw_data"]

        # self.cache.store(obj)
        self.storage.store(obj)

        if obj.has_key("zoom") and obj.has_key("x"):
            self.update_availability(obj["layer"], obj["zoom"], (obj["x"], obj["x"] + 1))

        return obj