Exemplo n.º 1
0
            def recursive_lookup(collection, suffix, used_ids, subsets):
                # get all EO objects related to this collection, excluding
                # those already searched
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]).exclude(
                        pk__in=used_ids).order_by("begin_time", "end_time")
                # apply subsets
                eo_objects = subsets.filter(eo_objects)

                selection = LayerSelection()

                # append all retrived EO objects, either as a coverage of
                # the real type, or as a subgroup.
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        selection.append(eo_object.cast(),
                                         eo_object.identifier)
                    elif models.iscollection(eo_object):
                        selection.extend(
                            recursive_lookup(eo_object, suffix, used_ids,
                                             subsets))
                    else:
                        pass

                return selection
Exemplo n.º 2
0
            def recursive_lookup(collection, suffix, used_ids, subsets):
                # get all EO objects related to this collection, excluding 
                # those already searched
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]
                ).exclude(
                    pk__in=used_ids
                ).order_by("begin_time", "end_time")
                # apply subsets
                eo_objects = subsets.filter(eo_objects)

                selection = LayerSelection()

                # append all retrived EO objects, either as a coverage of 
                # the real type, or as a subgroup.
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        selection.append(eo_object.cast(), eo_object.identifier)
                    elif models.iscollection(eo_object):
                        selection.extend(recursive_lookup(
                            eo_object, suffix, used_ids, subsets
                        ))
                    else: 
                        pass

                return selection
Exemplo n.º 3
0
    def print_object(self, eo_object, recursive=False, level=0):
        indent = INDENT * level
        eo_object = eo_object.cast()
        print("%s%s %s" % (indent, eo_object.identifier,
                                eo_object.__class__.__name__))

        if recursive and models.iscollection(eo_object):
            for sub_eo_object in eo_object.eo_objects.all():
                self.print_object(sub_eo_object, recursive, level+1)
    def execute(collection, begin_time, end_time, **kwarg):
        """ The main execution function for the process.
        """

        # get the dataset series matching the requested ID
        try:
            model = models.EOObject.objects.get(identifier=collection)
        except models.EOObject.DoesNotExist:
            raise InvalidInputValueError(
                "collection", "Invalid collection name '%s'!" % collection
            )

        if models.iscollection(model):
            model = model.cast()

            # recursive dataset series lookup
            def _get_children_ids(ds):
                ds_rct = ds.real_content_type
                id_list = [ds.id]
                for child in model.eo_objects.filter(real_content_type=ds_rct):
                    id_list.extend(_get_children_ids(child))
                return id_list

            collection_ids = _get_children_ids(model)

            # prepare coverage query set
            coverages_qs = models.Coverage.objects.filter(
                collections__id__in=collection_ids
            )
            if end_time is not None:
                coverages_qs = coverages_qs.filter(begin_time__lte=end_time)
            if begin_time is not None:
                coverages_qs = coverages_qs.filter(end_time__gte=begin_time)
            coverages_qs = coverages_qs.order_by('begin_time', 'end_time')
            coverages_qs = coverages_qs.values_list("begin_time", "end_time", "identifier", "min_x", "min_y", "max_x", "max_y")

        else:
            min_x, min_y, max_x, max_y = model.extent_wgs84
            coverages_qs = ((model.begin_time, model.end_time, model.identifier, min_x, min_y, max_x, max_y),)

        # create the output
        output = CDAsciiTextBuffer()
        writer = csv.writer(output, quoting=csv.QUOTE_ALL)
        header = ["starttime", "endtime", "bbox", "identifier"]
        writer.writerow(header)

        for starttime, endtime, identifier, min_x, min_y, max_x, max_y in coverages_qs:
            bbox = (min_x, min_y, max_x, max_y)
            writer.writerow([isoformat(starttime), isoformat(endtime), bbox, identifier])

        return output
Exemplo n.º 5
0
    def execute(collection, begin_time, end_time, **kwarg):
        """ The main execution function for the process.
        """

        # get the dataset series matching the requested ID
        try:
            model = models.EOObject.objects.get(identifier=collection)
        except models.EOObject.DoesNotExist:
            raise InvalidInputValueError(
                "collection", "Invalid collection name '%s'!" % collection
            )

        if models.iscollection(model):
            model = model.cast()

            # recursive dataset series lookup
            def _get_children_ids(ds):
                ds_rct = ds.real_content_type
                id_list = [ds.id]
                for child in model.eo_objects.filter(real_content_type=ds_rct):
                    id_list.extend(_get_children_ids(child))
                return id_list

            collection_ids = _get_children_ids(model)

            # prepare coverage query set
            coverages_qs = models.Coverage.objects.filter(
                collections__id__in=collection_ids
            )
            if end_time is not None:
                coverages_qs = coverages_qs.filter(begin_time__lte=end_time)
            if begin_time is not None:
                coverages_qs = coverages_qs.filter(end_time__gte=begin_time)
            coverages_qs = coverages_qs.order_by('begin_time', 'end_time')
            coverages_qs = coverages_qs.envelope()
            coverages_qs = coverages_qs.values_list("begin_time", "end_time", "identifier", "envelope")

        else:
            coverages_qs = ((model.begin_time, model.end_time, model.identifier, model.footprint),)

        # create the output
        output = CDAsciiTextBuffer()
        writer = csv.writer(output, quoting=csv.QUOTE_ALL)
        header = ["starttime", "endtime", "bbox", "identifier"]
        writer.writerow(header)

        for starttime, endtime, identifier, bbox in coverages_qs:
            writer.writerow([isoformat(starttime), isoformat(endtime), bbox.extent, identifier])

        return output
Exemplo n.º 6
0
    def print_object(self, eo_object, recursive=False, suppress_type=False,
                     level=0):
        indent = INDENT * level
        eo_object = eo_object.cast()
        if not suppress_type:
            print("%s%s %s" % (indent, eo_object.identifier,
                               eo_object.__class__.__name__))
        else:
            print("%s%s" % (indent, eo_object.identifier))

        if recursive and models.iscollection(eo_object):
            for sub_eo_object in eo_object.eo_objects.all():
                self.print_object(
                    sub_eo_object, recursive, suppress_type, level+1
                )
Exemplo n.º 7
0
 def _recursive_lookup(collection):
     """ Search recursively through the nested collections
         and find the relevant EOObjects."""
     eo_objects = subsets.filter(models.EOObject.objects\
                     .filter(collections__in=[collection.pk])\
                     .exclude(pk__in=used_ids)\
                     .order_by("begin_time", "end_time", "identifier")\
                     .prefetch_related('metadata_items'))
     for eo_object in eo_objects:
         used_ids.add(eo_object.pk)
         if models.iscoverage(eo_object):
             selection.append(eo_object.cast(), _get_alias(eo_object))
         elif models.iscollection(eo_object):
             _recursive_lookup(eo_object)
         else:
             pass # TODO: Reporting of invalid EOObjects (?)
Exemplo n.º 8
0
            def recursive_lookup(collection, used_ids, suffix):
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]
                ).exclude(
                    pk__in=used_ids
                )

                result = []
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        result.append((eo_object.cast(), suffix))
                    elif models.iscollection(eo_object):
                        result.extend(
                            recursive_lookup(eo_object, used_ids, suffix)
                        )
                    else:
                        pass

                return result
Exemplo n.º 9
0
            def recursive_lookup(collection, used_ids, suffix):
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]
                ).exclude(
                    pk__in=used_ids
                )

                result = []
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        result.append((eo_object.cast(), suffix))
                    elif models.iscollection(eo_object):
                        result.extend(
                            recursive_lookup(eo_object, used_ids, suffix)
                        )
                    else:
                        pass

                return result
Exemplo n.º 10
0
def configuration(request):
    collections = models.Collection.objects.all()
    coverages = filter(
        lambda c: not models.iscollection(c),
        models.Coverage.objects.filter(
            visible=True, collections__isnull=True
        )
    )

    all_objects = list(chain(collections, coverages))
    start_time = min(o.begin_time for o in all_objects)
    end_time = max(o.end_time for o in all_objects)

    return render_to_response(
        'webclient/config.json', {
            "layers": all_objects,
            "start_time_full": isoformat(start_time - timedelta(days=5)),
            "end_time_full": isoformat(end_time + timedelta(days=5)),
            "start_time": isoformat(start_time),
            "end_time": isoformat(end_time)
        },
        context_instance=RequestContext(request)
    )
Exemplo n.º 11
0
def lookup_layers(layers, subsets, suffixes=None):
    """ Performs a layer lookup for the given layer names. Applies the given
        subsets and looks up all layers with the given suffixes. Returns a
        hierarchy of ``LayerSelection`` objects.
    """
    suffix_related_ids = {}
    root_group = LayerSelection(None)
    suffixes = suffixes or (None, )
    logger.debug(str(suffixes))

    for layer_name in layers:
        for suffix in suffixes:
            if not suffix:
                identifier = layer_name
            elif layer_name.endswith(suffix):
                identifier = layer_name[:-len(suffix)]
            else:
                continue

            # TODO: nasty, nasty bug... dunno where
            eo_objects = models.EOObject.objects.filter(identifier=identifier)
            if len(eo_objects):
                eo_object = eo_objects[0]
                break
        else:
            raise LayerNotDefined(layer_name)

        if models.iscollection(eo_object):
            # recursively iterate over all sub-collections and collect all
            # coverages

            used_ids = suffix_related_ids.setdefault(suffix, set())

            def recursive_lookup(collection, suffix, used_ids, subsets):
                # get all EO objects related to this collection, excluding
                # those already searched
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]).exclude(
                        pk__in=used_ids).order_by("begin_time", "end_time")
                # apply subsets
                eo_objects = subsets.filter(eo_objects)

                selection = LayerSelection()

                # append all retrived EO objects, either as a coverage of
                # the real type, or as a subgroup.
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        selection.append(eo_object.cast(),
                                         eo_object.identifier)
                    elif models.iscollection(eo_object):
                        selection.extend(
                            recursive_lookup(eo_object, suffix, used_ids,
                                             subsets))
                    else:
                        pass

                return selection

            root_group.append(
                LayerSelection(
                    eo_object, suffix,
                    recursive_lookup(eo_object, suffix, used_ids, subsets)))

        elif models.iscoverage(eo_object):
            # Add a layer selection for the coverage with the suffix
            selection = LayerSelection(None, suffix=suffix)
            if subsets.matches(eo_object):
                selection.append(eo_object.cast(), eo_object.identifier)
            else:
                selection.append(None, eo_object.identifier)

            root_group.append(selection)

    return root_group
Exemplo n.º 12
0
def lookup_layers(layers, subsets, suffixes=None):
    """ Performs a layer lookup for the given layer names. Applies the given 
        subsets and looks up all layers with the given suffixes. Returns a 
        hierarchy of ``LayerSelection`` objects.
    """
    suffix_related_ids = {}
    root_group = LayerSelection(None)
    suffixes = suffixes or (None,)
    logger.debug(str(suffixes))


    for layer_name in layers:
        for suffix in suffixes:
            if not suffix:
                identifier = layer_name
            elif layer_name.endswith(suffix):
                identifier = layer_name[:-len(suffix)]
            else:
                continue
            
            # TODO: nasty, nasty bug... dunno where
            eo_objects = models.EOObject.objects.filter(
                identifier=identifier
            )
            if len(eo_objects):
                eo_object = eo_objects[0]
                break
        else:
            raise LayerNotDefined(layer_name)

        if models.iscollection(eo_object):
            # recursively iterate over all sub-collections and collect all
            # coverages

            used_ids = suffix_related_ids.setdefault(suffix, set())

            def recursive_lookup(collection, suffix, used_ids, subsets):
                # get all EO objects related to this collection, excluding 
                # those already searched
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]
                ).exclude(
                    pk__in=used_ids
                ).order_by("begin_time", "end_time")
                # apply subsets
                eo_objects = subsets.filter(eo_objects)

                selection = LayerSelection()

                # append all retrived EO objects, either as a coverage of 
                # the real type, or as a subgroup.
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        selection.append(eo_object.cast(), eo_object.identifier)
                    elif models.iscollection(eo_object):
                        selection.extend(recursive_lookup(
                            eo_object, suffix, used_ids, subsets
                        ))
                    else: 
                        pass

                return selection

            root_group.append(
                LayerSelection(
                    eo_object, suffix,
                    recursive_lookup(eo_object, suffix, used_ids, subsets)
                )
            )

        elif models.iscoverage(eo_object):
            # Add a layer selection for the coverage with the suffix
            selection = LayerSelection(None, suffix=suffix)
            if subsets.matches(eo_object):
                selection.append(eo_object.cast(), eo_object.identifier)
            else:
                selection.append(None, eo_object.identifier)

            root_group.append(selection)

    return root_group
Exemplo n.º 13
0
    def handle(self, request):
        decoder = WMS13GetLegendGraphicDecoder(request.GET)

        layer_name = decoder.layer
        coverage_id = decoder.coverage

        suffixes = (None, "_bands", "_outlines")
        for suffix in suffixes:
            try:
                if len(suffix or "") == 0:
                    identifier = layer_name
                else:
                    layer_name[-len(suffix):]
                eo_object = models.EOObject.objects.get(identifier=identifier)
                break
            except models.EOObject.DoesNotExist:
                pass
        else:
            raise InvalidParameterException(
                "No such layer '%s'." % layer_name, "layer"
            )


        if models.iscollection(eo_object):
            def recursive_lookup(collection, used_ids, suffix):
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]
                ).exclude(
                    pk__in=used_ids
                )

                result = []
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        result.append((eo_object.cast(), suffix))
                    elif models.iscollection(eo_object):
                        result.extend(
                            recursive_lookup(eo_object, used_ids, suffix)
                        )
                    else:
                        pass

                return result

            used_ids = set()
            coverages = recursive_lookup(eo_object, used_ids, suffix)
            collection = eo_object

            if coverage_id:
                for coverage in coverages:
                    if coverage.identifier == coverage_id:
                        coverages = ((coverage, suffix),)
                        break
                else:
                    raise InvalidParameterException(
                        "Layer '%s' does not contain a coverage with ID '%s'.", 
                        "coverage"
                    )
        else:
            collection = None
            coverages = ((eo_object.cast(), suffix),)

        layer_selection = LayerSelection(
            collection.identifier if collection else None
        )
        layer_selection.extend(coverages)

        result, _ = self.renderer.render(layer_selection, request.GET.items())
        return to_http_response(result)
Exemplo n.º 14
0
    def handle(self, request):
        decoder = WMS13GetLegendGraphicDecoder(request.GET)

        layer_name = decoder.layer
        coverage_id = decoder.coverage

        suffixes = self.renderer.suffixes
        for suffix in suffixes:
            try:
                if len(suffix or "") == 0:
                    identifier = layer_name
                else:
                    identifier = layer_name[-len(suffix):]
                eo_object = models.EOObject.objects.get(identifier=identifier)
                break
            except models.EOObject.DoesNotExist:
                pass
        else:
            raise InvalidParameterException(
                "No such layer '%s'." % layer_name, "layer"
            )

        if models.iscollection(eo_object):
            def recursive_lookup(collection, used_ids, suffix):
                eo_objects = models.EOObject.objects.filter(
                    collections__in=[collection.pk]
                ).exclude(
                    pk__in=used_ids
                )

                result = []
                for eo_object in eo_objects:
                    used_ids.add(eo_object.pk)

                    if models.iscoverage(eo_object):
                        result.append((eo_object.cast(), suffix))
                    elif models.iscollection(eo_object):
                        result.extend(
                            recursive_lookup(eo_object, used_ids, suffix)
                        )
                    else:
                        pass

                return result

            used_ids = set()
            coverages = recursive_lookup(eo_object, used_ids, suffix)
            collection = eo_object

            if coverage_id:
                for coverage in coverages:
                    if coverage.identifier == coverage_id:
                        coverages = ((coverage, suffix),)
                        break
                else:
                    raise InvalidParameterException(
                        "Layer '%s' does not contain a coverage with ID '%s'.",
                        "coverage"
                    )
        else:
            collection = None
            coverages = ((eo_object.cast(), suffix),)

        layer_selection = LayerSelection(
            collection if collection else None
        )
        layer_selection.extend(coverages)

        result, _ = self.renderer.render(layer_selection, request.GET.items())
        return to_http_response(result)
Exemplo n.º 15
0
 def _get_children_ids(obj):
     id_list = [obj.id]
     for child in obj.eo_objects.all():
         if iscollection(child):
             id_list.extend(_get_children_ids(child.cast()))
     return id_list
Exemplo n.º 16
0
def lookup_layers(layers, subsets, suffixes=None):
    """ Performs a layer lookup for the given layer names. Applies the given
        subsets and looks up all layers with the given suffixes. Returns a
        list of ``LayerSelection`` objects.
    """
    suffixes = suffixes or (None,)
    logger.debug("Tested suffixes: %s", suffixes)

    # ------------------------------------------------------------------------
    # local closures:

    def _lookup_eoobject(layer_name):
        """ Search an EOObject matching the given layer name. """
        for suffix in suffixes:
            if not suffix:
                identifier = layer_name
            elif layer_name.endswith(suffix):
                identifier = layer_name[:-len(suffix)]
            else: # no match found
                continue
            eo_objects = list(models.EOObject.objects.filter(identifier=identifier))
            if len(eo_objects) > 0:
                return eo_objects[0], suffix
        raise LayerNotDefined(layer_name)

    def _get_wms_view(eo_object):
        """ Get an EOObject used for the WMS view. If there is no WMS view
            object the closure returns the input object.
        """
        try:
            md_item = eo_object.metadata_items.get(semantic="wms_view")
            return models.EOObject.objects.get(identifier=md_item.value)
        except ObjectDoesNotExist:
            return eo_object

    def _get_alias(eo_object):
        """ Get an EOObject alias, i.e., an identifier of the EOObject
            the given EOOobject provides the WMS view to.
        """
        try:
            return eo_object.metadata_items.get(semantic="wms_alias").value
        except ObjectDoesNotExist:
            return None

    def _recursive_lookup(collection):
        """ Search recursively through the nested collections
            and find the relevant EOObjects."""
        eo_objects = subsets.filter(models.EOObject.objects\
                        .filter(collections__in=[collection.pk])\
                        .exclude(pk__in=used_ids)\
                        .order_by("begin_time", "end_time", "identifier")\
                        .prefetch_related('metadata_items'))
        for eo_object in eo_objects:
            used_ids.add(eo_object.pk)
            if models.iscoverage(eo_object):
                selection.append(eo_object.cast(), _get_alias(eo_object))
            elif models.iscollection(eo_object):
                _recursive_lookup(eo_object)
            else:
                pass # TODO: Reporting of invalid EOObjects (?)

    # ------------------------------------------------------------------------

    selections = []

    # NOTE: The lookup is performed on a set of unique layer names. This has
    #       no effect on the rendering order of the layer as this is determined
    #       by the WMS request handled by the mapserver.

    for layer_name in set(layers):
        # get an EOObject and suffix matching the layer_name
        eoo_src, suffix = _lookup_eoobject(layer_name)

        # get EOObject holding the WMS view
        eoo_wms = _get_wms_view(eoo_src)

        # prepare the final EOObject(s) selection
        selection = LayerSelection(eoo_src, suffix)

        if models.iscollection(eoo_wms): # EOObject is a collection
            used_ids = set()
            # recursively look-up the coverages
            _recursive_lookup(eoo_wms)

        elif models.iscoverage(eoo_wms): # EOObject is a coverage
            # append to the selection if the coverage matches the subset
            if subsets.matches(eoo_wms):
                selection.append(eoo_wms.cast(), eoo_src.identifier)

        else:
            pass # TODO: Reporting of invalid EOObjects (?)

        selections.append(selection)

    return selections