def assignMmaxfromMelettiDataset(layer, mmax_data): """Assign Mmax from Meletti data set to area source zone layer.""" provider = layer.dataProvider() # create missing attributes (if required) for attribute_list in features.AREA_SOURCE_ATTRIBUTES_ALL: utils.getAttributeIndex(provider, attribute_list, create=True) values = {} attribute_map = utils.getAttributeIndex(provider, MMAX_ATTRIBUTES) id_idx, id_type = attribute_map[features.AREA_SOURCE_ATTR_ID['name']] mmax_idx, mmax_type = \ attribute_map[features.AREA_SOURCE_ATTR_MMAX['name']] provider.select() provider.rewind() for zone_idx, zone in utils.walkValidPolygonFeatures(provider): zone_id = int(zone[id_idx].toInt()[0]) # mmax from Meletti data set try: mmax = mmax_data[zone_id]['mmax'] except Exception: continue try: values[zone.id()] = {mmax_idx: QVariant(mmax)} except Exception, e: error_str = \ "error in attribute: zone_idx: %s, zone_id: %s, mmax: %s, %s" % ( zone_idx, zone.id(), mmax, e) raise RuntimeError, error_str
def assignAttributesFromBackgroundZones(layer, background_layer, attributes_in, ui_mode=True): """Copy attributes from background zone layer.""" provider = layer.dataProvider() provider_back = background_layer.dataProvider() # create missing attributes (if required) for attribute_list in features.AREA_SOURCE_ATTRIBUTES_ALL: utils.getAttributeIndex(provider, attribute_list, create=True) values = {} attribute_map = utils.getAttributeIndex(provider, attributes_in) provider.select() provider.rewind() for zone_idx, zone in utils.walkValidPolygonFeatures(provider): attributes = {} skipZone = False # get mmax and mcdist from background zones polygon, vertices = utils.polygonsQGS2Shapely((zone, )) centroid = polygon[0].centroid copy_attr = getAttributesFromBackgroundZones(centroid, provider_back, attributes_in, ui_mode=ui_mode) for attr_idx, attr_dict in enumerate(attributes_in): (curr_idx, curr_type) = attribute_map[attr_dict['name']] # if one of the attribute values is None, skip zone if copy_attr[attr_idx] is None: skipZone = True break try: # attributes are of type QVariant attributes[curr_idx] = copy_attr[attr_idx] except Exception, e: error_str = \ "error in attribute: curr_idx: %s, zone_idx: %s, attr_idx: %s, %s" % ( curr_idx, zone_idx, attr_idx, e) raise RuntimeError, error_str if skipZone is False: values[zone.id()] = attributes
def assignAttributesFromBackgroundZones(layer, background_layer, attributes_in, ui_mode=True): """Copy attributes from background zone layer.""" provider = layer.dataProvider() provider_back = background_layer.dataProvider() # create missing attributes (if required) for attribute_list in features.AREA_SOURCE_ATTRIBUTES_ALL: utils.getAttributeIndex(provider, attribute_list, create=True) values = {} attribute_map = utils.getAttributeIndex(provider, attributes_in) provider.select() provider.rewind() for zone_idx, zone in utils.walkValidPolygonFeatures(provider): attributes = {} skipZone = False # get mmax and mcdist from background zones polygon, vertices = utils.polygonsQGS2Shapely((zone,)) centroid = polygon[0].centroid copy_attr = getAttributesFromBackgroundZones(centroid, provider_back, attributes_in, ui_mode=ui_mode) for attr_idx, attr_dict in enumerate(attributes_in): (curr_idx, curr_type) = attribute_map[attr_dict['name']] # if one of the attribute values is None, skip zone if copy_attr[attr_idx] is None: skipZone = True break try: # attributes are of type QVariant attributes[curr_idx] = copy_attr[attr_idx] except Exception, e: error_str = \ "error in attribute: curr_idx: %s, zone_idx: %s, attr_idx: %s, %s" % ( curr_idx, zone_idx, attr_idx, e) raise RuntimeError, error_str if skipZone is False: values[zone.id()] = attributes