Example #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
Example #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
Example #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
Example #4
0
 def resolve(self, db, network_elements):
     """
     network_elements should be in consecutive order. jumps between non-contiguous
     elements will result in an interpolated join in the resulting line
     """
     session = db.session()
     try:
         q = session.query(
             func.ST_AsGeoJSON(
                 func.ST_Multi(func.ST_Union(db.RoadNetwork.geom)))).filter(
                     db.RoadNetwork.network_element.in_(network_elements))
         cut = q.one()[0]
     finally:
         session.close()
     return json.loads(cut)
Example #5
0
 def _get_path(self, db):
     gj = json.dumps({
         "crs": {
             "type": "name",
             "properties": {
                 "name": "EPSG:4326"
             }
         },
         "type": "LineString",
         "coordinates": self._coords,
     })
     session = db.session()
     try:
         return json.loads(
             session.query(
                 func.ST_AsGeoJSON(
                     func.ST_Transform(
                         func.ST_Multi(func.ST_GeomFromGeoJSON(gj)),
                         3857))).one()[0])
     finally:
         session.close()