示例#1
0
 def _deserialize(self, value, attr, data, **kwargs):
     try:
         shape = asShape(value)
         two_dimension_geom = remove_third_dimension(shape)
         return from_shape(two_dimension_geom, srid=4326)
     except ValueError as error:
         raise ValidationError("Geometry error") from error
示例#2
0
def post_station(info_role):
    """
    Post one occhab station (station + habitats)

    .. :quickref: OccHab; 

    Post one occhab station (station + habitats)

    :returns: GeoJson<TStationsOcchab>
    """
    data = dict(request.get_json())
    occ_hab = None
    properties = data['properties']
    if "t_habitats" in properties:
        occ_hab = properties.pop("t_habitats")
    observers_list = None
    if "observers" in properties:
        observers_list = properties.pop("observers")

    station = TStationsOcchab(**properties)
    shape = asShape(data["geometry"])
    two_dimension_geom = remove_third_dimension(shape)
    station.geom_4326 = from_shape(two_dimension_geom, srid=4326)
    if observers_list is not None:
        observers = (
            DB.session.query(User).filter(
                User.id_role.in_(
                    list(map(lambda user: user['id_role'], observers_list))
                )).all()
        )
        for o in observers:
            station.observers.append(o)
    t_hab_list_object = []
    if occ_hab is not None:
        for occ in occ_hab:
            if occ['id_habitat'] is None:
                occ.pop('id_habitat')
            data_attr = [k for k in occ]
            for att in data_attr:
                if not getattr(THabitatsOcchab, att, False):
                    occ.pop(att)
            t_hab_list_object.append(THabitatsOcchab(**occ))

    # set habitat complexe
    station.is_habitat_complex = len(t_hab_list_object) > 1

    station.t_habitats = t_hab_list_object
    if station.id_station:
        user_cruved = get_or_fetch_user_cruved(
            session=session, id_role=info_role.id_role, module_code="OCCHAB"
        )
        # check if allowed to update or raise 403
        station.check_if_allowed(info_role, 'U', user_cruved["U"])
        DB.session.merge(station)
    else:
        DB.session.add(station)
    DB.session.commit()
    return station.get_geofeature()
示例#3
0
def add_one_place(info_role):
    user_id = info_role.id_role

    data = request.get_json()
    place_name = data["properties"]["placeName"]
    place_exists = (DB.session.query(TPlaces).filter(
        TPlaces.place_name == place_name, TPlaces.id_role == user_id).scalar())
    if place_exists:
        return {"message": "Nom du lieu déjà existant", "status": "error"}

    shape = asShape(data["geometry"])
    two_dimension_geom = remove_third_dimension(shape)
    place_geom = from_shape(two_dimension_geom, srid=4326)

    place = TPlaces(id_role=user_id,
                    place_name=place_name,
                    place_geom=place_geom)
    DB.session.add(place)
    DB.session.commit()

    return {"message": "Ajout du lieu avec succés", "status": "success"}
示例#4
0
def add_place():
    data = request.get_json()
    # FIXME check data validity!
    place_name = data["properties"]["place_name"]
    place_exists = (
        TPlaces.query
        .filter(TPlaces.place_name == place_name, TPlaces.id_role == g.current_user.id_role)
        .exists()
    )
    if db.session.query(place_exists).scalar():
        raise Conflict("Nom du lieu déjà existant")

    shape = asShape(data["geometry"])
    two_dimension_geom = remove_third_dimension(shape)
    place_geom = from_shape(two_dimension_geom, srid=4326)

    place = TPlaces(id_role=g.current_user.id_role, place_name=place_name, place_geom=place_geom)
    db.session.add(place)
    db.session.commit()

    return jsonify(place.as_geofeature())
示例#5
0
def insertOrUpdateOneReleve(info_role):
    """
    Route utilisée depuis l'appli mobile => depreciée et non utilisée par l'appli web
    Post one Occtax data (Releve + Occurrence + Counting)

    .. :quickref: Occtax; Post one Occtax data (Releve + Occurrence + Counting)

    **Request JSON object:**

    .. sourcecode:: http

        {
        "geometry":
            {"type":"Point",
            "coordinates":[0.9008789062500001,47.14489748555398]},
            "properties":
                {
                "id_releve_occtax":null,"id_dataset":1,"id_digitiser":1,"date_min":"2019-05-09","date_max":"2019-05-09","hour_min":null,"hour_max":null,"altitude_min":null,"altitude_max":null,"meta_device_entry":"web","comment":null,"id_nomenclature_obs_technique":316,"observers":[1],"observers_txt":null,"id_nomenclature_grp_typ":132,
                "t_occurrences_occtax":[{
                    "id_releve_occtax":null,"id_occurrence_occtax":null,"id_nomenclature_obs_technique":41,"id_nomenclature_bio_condition":157,"id_nomenclature_bio_status":29,"id_nomenclature_naturalness":160,"id_nomenclature_exist_proof":81,"id_nomenclature_observation_status":88,"id_nomenclature_blurring":175,"id_nomenclature_source_status":75,"determiner":null,"id_nomenclature_determination_method":445,"cd_nom":67111,"nom_cite":"Ablette =  <i> Alburnus alburnus (Linnaeus, 1758)</i> - [ES - 67111]","meta_v_taxref":null,"sample_number_proof":null,"comment":null,
                "cor_counting_occtax":[{
                    "id_counting_occtax":null,"id_nomenclature_life_stage":1,"id_nomenclature_sex":171,"id_nomenclature_obj_count":146,"id_nomenclature_type_count":94,"id_occurrence_occtax":null,"count_min":1,"count_max":1
                    }]
                }]
            }
        }

    :returns: GeoJson<TRelevesOccurrence>
    """

    releveRepository = ReleveRepository(TRelevesOccurrence)
    data = dict(request.get_json())
    occurrences_occtax = None
    if "t_occurrences_occtax" in data["properties"]:
        occurrences_occtax = data["properties"]["t_occurrences_occtax"]
        data["properties"].pop("t_occurrences_occtax")
    observersList = None
    if "observers" in data["properties"]:
        observersList = data["properties"]["observers"]
        data["properties"].pop("observers")

    # Test et suppression des propriétés inexistantes de TRelevesOccurrence
    attliste = [k for k in data["properties"]]
    for att in attliste:
        if not getattr(TRelevesOccurrence, att, False):
            data["properties"].pop(att)

    releve = TRelevesOccurrence(**data["properties"])
    shape = asShape(data["geometry"])
    two_dimension_geom = remove_third_dimension(shape)
    releve.geom_4326 = from_shape(two_dimension_geom, srid=4326)

    if observersList is not None:
        observers = DB.session.query(User).filter(User.id_role.in_(observersList)).all()
        for o in observers:
            releve.observers.append(o)

    for occ in occurrences_occtax:
        cor_counting_occtax = []
        if "cor_counting_occtax" in occ:
            cor_counting_occtax = occ["cor_counting_occtax"]
            occ.pop("cor_counting_occtax")

        # Test et suppression
        #   des propriétés inexistantes de TOccurrencesOccurrence
        attliste = [k for k in occ]
        for att in attliste:
            if not getattr(TOccurrencesOccurrence, att, False):
                occ.pop(att)
        # pop the id if None. otherwise DB.merge is not OK
        if "id_occurrence_occtax" in occ and occ["id_occurrence_occtax"] is None:
            occ.pop("id_occurrence_occtax")
        occtax = TOccurrencesOccurrence(**occ)

        for cnt in cor_counting_occtax:
            # Test et suppression
            # des propriétés inexistantes de CorCountingOccurrence
            attliste = [k for k in cnt]
            for att in attliste:
                if not getattr(CorCountingOccurrence, att, False):
                    cnt.pop(att)
            # pop the id if None. otherwise DB.merge is not OK
            if "id_counting_occtax" in cnt and cnt["id_counting_occtax"] is None:
                cnt.pop("id_counting_occtax")
            countingOccurrence = CorCountingOccurrence(**cnt)
            occtax.cor_counting_occtax.append(countingOccurrence)
        releve.t_occurrences_occtax.append(occtax)

    # if its a update
    if releve.id_releve_occtax:
        # get update right of the user
        user_cruved = get_or_fetch_user_cruved(
            session=session, id_role=info_role.id_role, module_code="OCCTAX"
        )
        update_code_filter = user_cruved["U"]
        # info_role.code_action = update_data_scope
        user = UserRigth(
            id_role=info_role.id_role,
            value_filter=update_code_filter,
            code_action="U",
            id_organisme=info_role.id_organisme,
        )
        releve = releveRepository.update(releve, user, shape)
    # if its a simple post
    else:
        # set id_digitiser
        releve.id_digitiser = info_role.id_role
        if info_role.value_filter in ("0", "1", "2"):
            # Check if user can add a releve in the current dataset
            allowed = releve.user_is_in_dataset_actor(info_role)
            if not allowed:
                raise InsufficientRightsError(
                    "User {} has no right in dataset {}".format(
                        info_role.id_role, releve.id_dataset
                    ),
                    403,
                )
        DB.session.add(releve)
    DB.session.commit()
    DB.session.flush()

    return releve.get_geofeature()