Beispiel #1
0
def get_dataset_details_dict(id_dataset, session_role):
    """
    Return a dataset from TDatasetDetails model (with all relationships)
    return also the number of taxon and observation of the dataset
    Use for get_one datasert
    """
    q = DB.session.query(TDatasetDetails)
    q = cruved_filter(q, TDatasetDetails, session_role)
    try:
        data = q.filter(TDatasetDetails.id_dataset == id_dataset).one()
    except NoResultFound:
        return None

    dataset = data.as_dict(True)

    dataset["taxa_count"] = (
        DB.session.query(Synthese.cd_nom)
        .filter(Synthese.id_dataset == id_dataset)
        .distinct()
        .count()
    )
    dataset["observation_count"] = (
        DB.session.query(Synthese.cd_nom)
        .filter(Synthese.id_dataset == id_dataset)
        .count()
    )
    geojsonData = (
        DB.session.query(func.ST_AsGeoJSON(func.ST_Extent(Synthese.the_geom_4326)))
        .filter(Synthese.id_dataset == id_dataset)
        .first()[0]
    )
    if geojsonData:
        dataset["bbox"] = json.loads(geojsonData)
    return dataset
Beispiel #2
0
def get_acquisition_framework_details(id_acquisition_framework):
    """
    Get one AF

    .. :quickref: Metadata;

    :param id_acquisition_framework: the id_acquisition_framework
    :param type: int
    """
    af = DB.session.query(TAcquisitionFrameworkDetails).get(
        id_acquisition_framework)
    if not af:
        return None
    acquisition_framework = af.as_dict(True)

    datasets = acquisition_framework[
        "datasets"] if "datasets" in acquisition_framework else []
    dataset_ids = [d["id_dataset"] for d in datasets]
    geojsonData = (DB.session.query(
        func.ST_AsGeoJSON(func.ST_Extent(Synthese.the_geom_4326))).filter(
            Synthese.id_dataset.in_(dataset_ids)).first()[0])
    if geojsonData:
        acquisition_framework["bbox"] = json.loads(geojsonData)
    nb_data = len(dataset_ids)
    nb_taxons = (DB.session.query(Synthese.cd_nom).filter(
        Synthese.id_dataset.in_(dataset_ids)).distinct().count())
    nb_observations = (DB.session.query(Synthese.cd_nom).filter(
        Synthese.id_dataset.in_(dataset_ids)).count())
    nb_habitat = 0

    # Check if pr_occhab exist
    check_schema_query = exists(
        select([text("schema_name")
                ]).select_from(text("information_schema.schemata")).where(
                    text("schema_name = 'pr_occhab'")))

    if DB.session.query(check_schema_query).scalar() and nb_data > 0:
        query = (
            "SELECT count(*) FROM pr_occhab.t_stations s, pr_occhab.t_habitats h WHERE s.id_station = h.id_station AND s.id_dataset in \
        (" + str(dataset_ids).strip("[]") + ")")

        nb_habitat = DB.engine.execute(text(query)).first()[0]

    acquisition_framework["stats"] = {
        "nb_data": nb_data,
        "nb_taxons": nb_taxons,
        "nb_observations": nb_observations,
        "nb_habitats": nb_habitat,
    }

    if acquisition_framework:
        return acquisition_framework
    return None
Beispiel #3
0
def get_acquisition_framework_bbox(info_role, id_acquisition_framework):
    """
    Get BBOX from one AF
    .. :quickref: Metadata;
    :param id_acquisition_framework: the id_acquisition_framework
    :param type: int
    """
    datasets = TDatasets.query.filter(
        TDatasets.id_acquisition_framework == id_acquisition_framework).all()
    dataset_ids = [d.id_dataset for d in datasets]
    geojsonData = (DB.session.query(
        func.ST_AsGeoJSON(func.ST_Extent(Synthese.the_geom_4326))).filter(
            Synthese.id_dataset.in_(dataset_ids)).first()[0])
    return json.loads(geojsonData) if geojsonData else None