Ejemplo n.º 1
0
    def _handle_file(self, filename, config):
        root = etree.parse(filename)

        start_revision = root.findtext(ns_cfg("startRevision"))
        end_revision = root.findtext(ns_cfg("endRevision"))

        remove_layers_elems = root.xpath(
            "cfg:removeConfiguration/cfg:browseLayers",
            namespaces={"cfg": ns_cfg.uri})
        add_layers_elems = root.xpath("cfg:addConfiguration/cfg:browseLayers",
                                      namespaces={"cfg": ns_cfg.uri})

        add_layers = []
        for layers_elem in add_layers_elems:
            add_layers.extend(decode_browse_layers(layers_elem))

        remove_layers = []
        for layers_elem in remove_layers_elems:
            remove_layers.extend(decode_browse_layers(layers_elem))

        # get the mapcache config xml file path to make it transaction safe
        mapcache_config = get_mapcache_seed_config(config)
        mapcache_xml_filename = mapcache_config["config_file"]

        # transaction safety here
        with FileTransaction((mapcache_xml_filename, ), copy=True):
            with transaction.commit_on_success():
                with transaction.commit_on_success(using="mapcache"):
                    for browse_layer in add_layers:
                        if models.BrowseLayer.objects.filter(
                                id=browse_layer.id).exists():
                            update_browse_layer(browse_layer, config)
                        else:
                            add_browse_layer(browse_layer, config)

                    for browse_layer in remove_layers:
                        delete_browse_layer(browse_layer, config=config)

        # set the new revision
        config = config or get_ngeo_config()

        if not config.has_section("config"):
            config.add_section("config")

        revision = int(safe_get(config, "config", "revision", 0))
        config.set("config", "revision", int(end_revision))

        write_ngeo_config()
Ejemplo n.º 2
0
    def _handle_file(self, filename, config):
        root = etree.parse(filename)

        start_revision = root.findtext(ns_cfg("startRevision"))
        end_revision = root.findtext(ns_cfg("endRevision"))

        remove_layers_elems = root.xpath("cfg:removeConfiguration/cfg:browseLayers", namespaces={"cfg": ns_cfg.uri})
        add_layers_elems = root.xpath("cfg:addConfiguration/cfg:browseLayers", namespaces={"cfg": ns_cfg.uri})

        add_layers = []
        for layers_elem in add_layers_elems:
            add_layers.extend(decode_browse_layers(layers_elem))

        remove_layers = []
        for layers_elem in remove_layers_elems:
            remove_layers.extend(decode_browse_layers(layers_elem))

        # get the mapcache config xml file path to make it transaction safe
        mapcache_config = get_mapcache_seed_config(config)
        mapcache_xml_filename = mapcache_config["config_file"]

        # transaction safety here
        with FileTransaction((mapcache_xml_filename,), copy=True):
            with transaction.commit_on_success():
                with transaction.commit_on_success(using="mapcache"):
                    for browse_layer in add_layers:
                        if models.BrowseLayer.objects.filter(id=browse_layer.id).exists():
                            update_browse_layer(browse_layer, config)
                        else:
                            add_browse_layer(browse_layer, config)

                    for browse_layer in remove_layers:
                        delete_browse_layer(browse_layer, config=config)

        # set the new revision
        config = config or get_ngeo_config()

        if not config.has_section("config"):
            config.add_section("config")

        revision = int(safe_get(config, "config", "revision", 0))
        config.set("config", "revision", int(end_revision))

        write_ngeo_config()
Ejemplo n.º 3
0
def config(request):
    try:
        status = get_status()
        config = get_ngeo_config()

        if request.method not in ("PUT", "POST"):
            raise Exception("Invalid request method '%s'." % request.method)

        if request.method == "POST":
            # "setting" new configuration, which means removing the previous one.
            action = "set"
        else:
            action = "update"

        root = etree.parse(request)

        start_revision = root.findtext(ns_cfg("startRevision"))
        end_revision = root.findtext(ns_cfg("endRevision"))

        # TODO: check current and last revision

        remove_layers_elems = root.xpath("cfg:removeConfiguration/cfg:browseLayers", namespaces={"cfg": ns_cfg.uri})
        add_layers_elems = root.xpath("cfg:addConfiguration/cfg:browseLayers", namespaces={"cfg": ns_cfg.uri})

        add_layers = []
        for layers_elem in add_layers_elems:
            add_layers.extend(decode_browse_layers(layers_elem))

        remove_layers = []
        for layers_elem in remove_layers_elems:
            remove_layers.extend(decode_browse_layers(layers_elem))

        # get the mapcache config xml file path to make it transaction safe

        mapcache_config = get_mapcache_seed_config(config)
        mapcache_xml_filename = mapcache_config["config_file"]

        # transaction safety here
        with FileTransaction((mapcache_xml_filename,), copy=True):
            with transaction.commit_on_success():
                with transaction.commit_on_success(using="mapcache"):
                    for browse_layer in add_layers:
                        if models.BrowseLayer.objects.filter(id=browse_layer.id).exists():
                            update_browse_layer(browse_layer, config)
                        else:
                            add_browse_layer(browse_layer, config)

                    for browse_layer in remove_layers:
                        delete_browse_layer(browse_layer, config)

        # set the new revision
        config = get_ngeo_config()

        if not config.has_section("config"):
            config.add_section("config")

        revision = int(safe_get(config, "config", "revision", 0))
        config.set("config", "revision", end_revision)

        write_ngeo_config()

        # return with the new revision
        return HttpResponse('<?xml version="1.0"?>\n'
            '<synchronizeConfigurationResponse>%s</synchronizeConfigurationResponse>'
            % end_revision
        )

    except Exception, e:
        logger.error("%s: %s" % (type(e).__name__, str(e)))
        logger.debug(traceback.format_exc())

        return HttpResponse(
            '<faultcode>ConfigurationError</faultcode>\n'
            '<faultstring>%s</faultstring>' % str(e), status=400
        )
Ejemplo n.º 4
0
def decode_browse_layers(browse_layers_elem, config=None):
    logger.info("Start decoding browse layer.")
    
    config = config or get_ngeo_config()
    timedimension_default = safe_get(
        config, "mapcache", "timedimension_default", "2014"
    )
    tile_query_limit_default = safe_get(
        config, "mapcache", "tile_query_limit_default", "100"
    )
    
    browse_layers = []
    for browse_layer_elem in browse_layers_elem.findall(ns_cfg("browseLayer")):
    
        opt = {
            "strategy": "inherit"
        }
        description_elem = browse_layer_elem.find(ns_cfg("description"))
        if description_elem is not None:
            opt["description"] = description_elem.text or ""
        
        related_dataset_ids_elem = browse_layer_elem.find(ns_cfg("relatedDatasetIds"))
        related_dataset_ids = [elem.text for elem in related_dataset_ids_elem]
        
        rgb_bands_elem = browse_layer_elem.find(ns_cfg("rgbBands"))
        if rgb_bands_elem is not None:
            r, g, b = map(int, rgb_bands_elem.text.split(","))
            opt["r_band"] = r; opt["g_band"] = g; opt["b_band"] = b
        
        radiometric_interval_elem = browse_layer_elem.find(ns_cfg("radiometricInterval"))
        if radiometric_interval_elem is not None:
            opt["radiometric_interval_min"] = int(radiometric_interval_elem.find(ns_cfg("min")).text)
            opt["radiometric_interval_max"] = int(radiometric_interval_elem.find(ns_cfg("max")).text)
        
        strategy_elem = browse_layer_elem.find(ns_cfg("strategy"))
        if strategy_elem is not None:
            opt["strategy"] = strategy_elem.text

        opt["timedimension_default"] = browse_layer_elem.findtext(
            ns_cfg("timeDimensionDefault")
        ) or timedimension_default
        opt["tile_query_limit"] = int(
            browse_layer_elem.findtext(ns_cfg("tileQueryLimit")) 
            or tile_query_limit_default
        )

        opt["contains_volumes"] = browse_layer_elem.findtext(ns_cfg("contains_volumes")) == "true"

        browse_layers.append(BrowseLayer(
            browse_layer_elem.get("browseLayerId"),
            browse_layer_elem.find(ns_cfg("browseType")).text,
            browse_layer_elem.find(ns_cfg("title")).text,
            browse_layer_elem.find(ns_cfg("grid")).text,
            browse_layer_elem.find(ns_cfg("browseAccessPolicy")).text,
            browse_layer_elem.find(ns_cfg("containsVerticalCurtains")).text == "true",
            int(browse_layer_elem.find(ns_cfg("highestMapLevel")).text),
            int(browse_layer_elem.find(ns_cfg("lowestMapLevel")).text),
            browse_layer_elem.find(ns_cfg("hostingBrowseServerName")).text,
            related_dataset_ids,
            **opt
        ))
    
    return browse_layers
Ejemplo n.º 5
0
def serialize_browse_layers(browse_layers, stream=None, pretty_print=False):
    if not stream:
        stream = StringIO()
    browse_layers_elem = Element(ns_cfg("browseLayers"),
                                 nsmap={"cfg": ns_cfg.uri})

    for browse_layer in browse_layers:
        bl_elem = SubElement(browse_layers_elem,
                             ns_cfg("browseLayer"),
                             attrib={"browseLayerId": browse_layer.id})

        rgb = browse_layer.r_band, browse_layer.g_band, browse_layer.b_band
        has_rgb = len(filter(lambda v: v is not None, rgb)) == 3

        ri = browse_layer.radiometric_interval_min, browse_layer.radiometric_interval_max
        has_ri = len(filter(lambda v: v is not None, ri)) == 2

        SubElement(bl_elem,
                   ns_cfg("browseType")).text = browse_layer.browse_type
        SubElement(bl_elem, ns_cfg("title")).text = browse_layer.title
        if browse_layer.description is not None:
            SubElement(bl_elem,
                       ns_cfg("description")).text = browse_layer.description
        SubElement(bl_elem, ns_cfg("grid")).text = browse_layer.grid
        SubElement(bl_elem, ns_cfg(
            "browseAccessPolicy")).text = browse_layer.browse_access_policy
        SubElement(bl_elem, ns_cfg("hostingBrowseServerName")).text = ""
        rel_ds_elem = SubElement(bl_elem, ns_cfg("relatedDatasetIds"))
        for rel_ds_id in browse_layer.related_dataset_ids:
            SubElement(rel_ds_elem, ns_cfg("datasetId")).text = rel_ds_id
        SubElement(
            bl_elem, ns_cfg("containsVerticalCurtains")
        ).text = "true" if browse_layer.contains_vertical_curtains else "false"
        if has_rgb:
            SubElement(bl_elem,
                       ns_cfg("rgbBands")).text = ",".join(map(str, rgb))

        if has_ri:
            ri_elem = SubElement(bl_elem, ns_cfg("radiometricInterval"))
            SubElement(ri_elem, ns_cfg("min")).text = str(ri[0])
            SubElement(ri_elem, ns_cfg("max")).text = str(ri[1])

        SubElement(bl_elem, ns_cfg("highestMapLevel")).text = str(
            browse_layer.highest_map_level)
        SubElement(bl_elem, ns_cfg("lowestMapLevel")).text = str(
            browse_layer.lowest_map_level)
        SubElement(bl_elem, ns_cfg("timeDimensionDefault")).text = str(
            browse_layer.timedimension_default)
        SubElement(bl_elem, ns_cfg("tileQueryLimit")).text = str(
            browse_layer.tile_query_limit)

    # TODO: encoding
    et = ElementTree(browse_layers_elem)
    et.write(stream,
             pretty_print=pretty_print,
             encoding="utf-8",
             xml_declaration=True)

    return stream
Ejemplo n.º 6
0
def serialize_browse_layers(browse_layers, stream=None, pretty_print=False):
    if not stream:
        stream = StringIO()
    browse_layers_elem = Element(ns_cfg("browseLayers"), 
                                 nsmap={"cfg": ns_cfg.uri})
    
    for browse_layer in browse_layers:
        bl_elem = SubElement(
            browse_layers_elem, ns_cfg("browseLayer"), 
            attrib={"browseLayerId": browse_layer.id}
        )
        
        rgb = browse_layer.r_band, browse_layer.g_band, browse_layer.b_band
        has_rgb = len(filter(lambda v: v is not None, rgb)) == 3
        
        ri = browse_layer.radiometric_interval_min, browse_layer.radiometric_interval_max
        has_ri = len(filter(lambda v: v is not None, ri)) == 2 
        
        SubElement(bl_elem, ns_cfg("browseType")).text = browse_layer.browse_type
        SubElement(bl_elem, ns_cfg("title")).text = browse_layer.title
        if browse_layer.description is not None:
            SubElement(bl_elem, ns_cfg("description")).text = browse_layer.description
        SubElement(bl_elem, ns_cfg("grid")).text = browse_layer.grid
        SubElement(bl_elem, ns_cfg("browseAccessPolicy")).text = browse_layer.browse_access_policy
        SubElement(bl_elem, ns_cfg("hostingBrowseServerName")).text = ""
        rel_ds_elem = SubElement(bl_elem, ns_cfg("relatedDatasetIds"))
        for rel_ds_id in browse_layer.related_dataset_ids:
            SubElement(rel_ds_elem, ns_cfg("datasetId")).text = rel_ds_id
        SubElement(bl_elem, ns_cfg("containsVerticalCurtains")).text = "true" if browse_layer.contains_vertical_curtains else "false"
        SubElement(bl_elem, ns_cfg("contains_volumes")).text = "true" if browse_layer.contains_volumes else "false"
        if has_rgb:
            SubElement(bl_elem, ns_cfg("rgbBands")).text = ",".join(map(str, rgb))
        
        if has_ri:
            ri_elem = SubElement(bl_elem, ns_cfg("radiometricInterval"))
            SubElement(ri_elem, ns_cfg("min")).text = str(ri[0])
            SubElement(ri_elem, ns_cfg("max")).text = str(ri[1])
        
        SubElement(bl_elem, ns_cfg("highestMapLevel")).text = str(browse_layer.highest_map_level)
        SubElement(bl_elem, ns_cfg("lowestMapLevel")).text = str(browse_layer.lowest_map_level)
        SubElement(bl_elem, ns_cfg("timeDimensionDefault")).text = str(browse_layer.timedimension_default)
        SubElement(bl_elem, ns_cfg("tileQueryLimit")).text = str(browse_layer.tile_query_limit)
    
    # TODO: encoding
    et = ElementTree(browse_layers_elem)
    et.write(stream, pretty_print=pretty_print, encoding="utf-8", 
             xml_declaration=True)
    
    return stream
Ejemplo n.º 7
0
def config(request):
    try:
        status = get_status()
        config = get_ngeo_config()

        if request.method not in ("PUT", "POST"):
            raise Exception("Invalid request method '%s'." % request.method)

        if request.method == "POST":
            # "setting" new configuration, which means removing the previous one.
            action = "set"
        else:
            action = "update"

        root = etree.parse(request)

        start_revision = root.findtext(ns_cfg("startRevision"))
        end_revision = root.findtext(ns_cfg("endRevision"))

        # TODO: check current and last revision

        remove_layers_elems = root.xpath(
            "cfg:removeConfiguration/cfg:browseLayers",
            namespaces={"cfg": ns_cfg.uri})
        add_layers_elems = root.xpath("cfg:addConfiguration/cfg:browseLayers",
                                      namespaces={"cfg": ns_cfg.uri})

        add_layers = []
        for layers_elem in add_layers_elems:
            add_layers.extend(decode_browse_layers(layers_elem))

        remove_layers = []
        for layers_elem in remove_layers_elems:
            remove_layers.extend(decode_browse_layers(layers_elem))

        # get the mapcache config xml file path to make it transaction safe

        mapcache_config = get_mapcache_seed_config(config)
        mapcache_xml_filename = mapcache_config["config_file"]

        # transaction safety here
        with FileTransaction((mapcache_xml_filename, ), copy=True):
            with transaction.commit_on_success():
                with transaction.commit_on_success(using="mapcache"):
                    for browse_layer in add_layers:
                        if models.BrowseLayer.objects.filter(
                                id=browse_layer.id).exists():
                            update_browse_layer(browse_layer, config)
                        else:
                            add_browse_layer(browse_layer, config)

                    for browse_layer in remove_layers:
                        delete_browse_layer(browse_layer, config=config)

        # set the new revision
        config = get_ngeo_config()

        if not config.has_section("config"):
            config.add_section("config")

        revision = int(safe_get(config, "config", "revision", 0))
        config.set("config", "revision", int(end_revision))

        write_ngeo_config()

        # return with the new revision
        return HttpResponse(
            '<?xml version="1.0"?>\n'
            '<synchronizeConfigurationResponse>%s</synchronizeConfigurationResponse>'
            % end_revision)

    except Exception, e:
        logger.error("%s: %s" % (type(e).__name__, str(e)))
        logger.debug(traceback.format_exc())

        return HttpResponse('<faultcode>ConfigurationError</faultcode>\n'
                            '<faultstring>%s</faultstring>' % str(e),
                            status=400)
Ejemplo n.º 8
0
def decode_browse_layers(browse_layers_elem, config=None):
    logger.info("Start decoding browse layer.")

    config = config or get_ngeo_config()
    timedimension_default = safe_get(config, "mapcache",
                                     "timedimension_default", "2014")
    tile_query_limit_default = safe_get(config, "mapcache",
                                        "tile_query_limit_default", "100")

    browse_layers = []
    for browse_layer_elem in browse_layers_elem.findall(ns_cfg("browseLayer")):

        opt = {"strategy": "inherit"}
        description_elem = browse_layer_elem.find(ns_cfg("description"))
        if description_elem is not None:
            opt["description"] = description_elem.text or ""

        related_dataset_ids_elem = browse_layer_elem.find(
            ns_cfg("relatedDatasetIds"))
        related_dataset_ids = [elem.text for elem in related_dataset_ids_elem]

        rgb_bands_elem = browse_layer_elem.find(ns_cfg("rgbBands"))
        if rgb_bands_elem is not None:
            r, g, b = map(int, rgb_bands_elem.text.split(","))
            opt["r_band"] = r
            opt["g_band"] = g
            opt["b_band"] = b

        radiometric_interval_elem = browse_layer_elem.find(
            ns_cfg("radiometricInterval"))
        if radiometric_interval_elem is not None:
            opt["radiometric_interval_min"] = int(
                radiometric_interval_elem.find(ns_cfg("min")).text)
            opt["radiometric_interval_max"] = int(
                radiometric_interval_elem.find(ns_cfg("max")).text)

        strategy_elem = browse_layer_elem.find(ns_cfg("strategy"))
        if strategy_elem is not None:
            opt["strategy"] = strategy_elem.text

        harvesting_source_elem = browse_layer_elem.find(
            ns_cfg("harvestingSource"))
        if harvesting_source_elem is not None:
            opt["harvesting_source"] = harvesting_source_elem.text

        opt["timedimension_default"] = browse_layer_elem.findtext(
            ns_cfg("timeDimensionDefault")) or timedimension_default
        opt["tile_query_limit"] = int(
            browse_layer_elem.findtext(ns_cfg("tileQueryLimit"))
            or tile_query_limit_default)

        browse_layers.append(
            BrowseLayer(
                browse_layer_elem.get("browseLayerId"),
                browse_layer_elem.find(ns_cfg("browseType")).text,
                browse_layer_elem.find(ns_cfg("title")).text,
                browse_layer_elem.find(ns_cfg("grid")).text,
                browse_layer_elem.find(ns_cfg("browseAccessPolicy")).text,
                browse_layer_elem.find(
                    ns_cfg("containsVerticalCurtains")).text == "true",
                int(browse_layer_elem.find(ns_cfg("highestMapLevel")).text),
                int(browse_layer_elem.find(ns_cfg("lowestMapLevel")).text),
                browse_layer_elem.find(ns_cfg("hostingBrowseServerName")).text,
                related_dataset_ids, **opt))

    return browse_layers