def run(cls):

        ngw_session = DBSession()
        transaction.manager.begin()

        deviation_distance = env.compulink_deviation.settings.get('deviation_distance')

        if not deviation_distance:
            raise AssertionError('Set deviation_distance in config!')
        try:
            deviation_distance = int(deviation_distance)
        except:
            raise AssertionError('Set correct value for deviation_distance in config ! (Need int)')

        # get all focls
        fs_resources = ngw_session.query(FoclStruct).all()

        for fs in fs_resources:
            # get counts features in project layers
            project_layers = {}
            for layer in fs.children:
                layer_type = cls.get_layer_type(layer)
                if layer_type in PROCESSING_LAYER_TYPES.keys():
                    obj_count = cls.get_feat_count(layer)
                    project_layers[layer_type] = obj_count

            # real check
            for layer_type, proj_feat_count in project_layers.items():
                proj_layer = cls.get_layer_by_type(fs.children, layer_type)
                actual_layer = cls.get_layer_by_type(fs.children, 'actual_real_' + layer_type)
                actual_features = cls.get_features(actual_layer)

                for feat in actual_features:
                    if proj_feat_count > 0:
                        min_dist = cls.nearest_feat_dist(feat, proj_layer)
                        if min_dist > deviation_distance:
                            # write to layer
                            feat.fields['is_deviation'] = 1
                            feat.fields['deviation_distance'] = min_dist
                            feat.layer.feature_put(feat)

                            # write to table
                            deviation = ngw_session.query(ConstructDeviation)\
                                .filter(
                                    ConstructDeviation.focl_res_id == fs.id,
                                    ConstructDeviation.object_type == layer_type,
                                    ConstructDeviation.object_num == feat.id
                                ).first()

                            if not deviation:
                                deviation = ConstructDeviation()

                            deviation.deviation_distance = min_dist
                            deviation.focl_res_id = fs.id
                            deviation.focl_name = fs.display_name
                            deviation.focl_proj = cls.get_proj_name(fs)
                            deviation.object_type = layer_type
                            deviation.object_num = feat.id
                            ngw_session.add(deviation)

                        elif feat.fields['is_deviation'] == 1 and feat.fields['deviation_approved'] != 1:
                            feat.fields['is_deviation'] = 0
                            feat.fields['deviation_distance'] = 0
                            feat.layer.feature_put(feat)

                            # remove from table
                            deviation = ngw_session.query(ConstructDeviation) \
                                .filter(
                                ConstructDeviation.focl_res_id == fs.id,
                                ConstructDeviation.object_type == layer_type,
                                ConstructDeviation.object_num == feat.id
                            ).delete()
                    else:
                        if feat.fields['is_deviation'] == 1 and feat.fields['deviation_approved'] != 1:
                            feat.fields['is_deviation'] = 0
                            feat.fields['deviation_distance'] = 0
                            feat.layer.feature_put(feat)

                            # remove from table
                            ngw_session.query(ConstructDeviation) \
                                .filter(
                                ConstructDeviation.focl_res_id == fs.id,
                                ConstructDeviation.object_type == layer_type,
                                ConstructDeviation.object_num == feat.id
                            ).delete()


        transaction.manager.commit()
Пример #2
0
def copy_attache(feat_attache, value, oldvalue, initiator):
    req_fields = dict()
    req_fields['feature_id'] = feat_attache.feature_id
    req_fields['resource_id'] = feat_attache.resource_id
    req_fields['fileobj_id'] = feat_attache.fileobj_id
    req_fields['size'] = feat_attache.size
    req_fields['mime_type'] = feat_attache.mime_type
    req_fields[initiator.key] = value

    # not full setter
    if not all([v is not None for v in req_fields.values()]):
        return

    session = DBSession

    # get layer
    try:
        orig_res = session.query(Resource).filter(Resource.id==req_fields['resource_id']).one()

        if not orig_res or not isinstance(orig_res, VectorLayer):
            return    # not support
    except:
        return

    # check mirroring layer
    real_layer_name = orig_res.keyname
    if not real_layer_name or not real_layer_name.startswith('real_') or not orig_res.parent:
        return

    # try to get mirror res
    real_layer_name = '_'.join(real_layer_name.rsplit('_')[0:-1])


    actual_layer = None

    for lyr in orig_res.parent.children:
        if lyr.keyname:
            lyr_name = '_'.join(lyr.keyname.rsplit('_')[0:-1])
        else:
            continue
        if 'actual_' + real_layer_name == lyr_name:
            actual_layer = lyr

    if not actual_layer:
        LogEntry.error('Mirror layer was not found! (Attache) (%s)' % orig_res.keyname, component=COMP_ID, group='Mirroring', append_dt=datetime.now())
        return

    # get guid of src feature
    try:
        query = orig_res.feature_query()
        query.filter_by(id=req_fields['feature_id'])
        features = query()
        if features.total_count < 1:
            LogEntry.error('Src feature was not found! (Attache) (%s)' % orig_res.keyname, component=COMP_ID,
                           group='Mirroring', append_dt=datetime.now())
            return
        src_feat = next(features.__iter__())
        src_feat_guid = src_feat.fields['feat_guid']
    except:
        LogEntry.error('Src feature was not found! (Attache) (%s)' % orig_res.keyname, component=COMP_ID,
                       group='Mirroring', append_dt=datetime.now())
        return

    # get dest feat by guid
    try:
        query = actual_layer.feature_query()
        query.filter_by(feat_guid=src_feat_guid)
        features = query()
        if features.total_count < 1:
            LogEntry.error('Dest feature was not found! (Attache) (%s, %s)' % (orig_res.keyname, src_feat_guid), component=COMP_ID,
                           group='Mirroring', append_dt=datetime.now())
            return
        dest_feat = next(features.__iter__())
        dest_feat_id = dest_feat.id
    except:
        LogEntry.error('Dest feature was not found! (Attache) (%s, %s)' % (orig_res.keyname, src_feat_guid), component=COMP_ID,
                       group='Mirroring', append_dt=datetime.now())
        return

    # create new attache
    mirror_attache = FeatureAttachment()
    mirror_attache.resource_id = actual_layer.id
    mirror_attache.feature_id = dest_feat_id
    mirror_attache.fileobj_id = req_fields['fileobj_id']
    mirror_attache.size = req_fields['size']
    mirror_attache.mime_type = req_fields['mime_type']
    mirror_attache.name = feat_attache.name
    mirror_attache.description = feat_attache.description

    DBSession.add(mirror_attache)