def find_duplicate_value(self, source_value, source_field_type, target_value_dict, target_field_type): """ Check if source_value is in target layer. First, as is, and if necessary as a converted value. :param source_value: single value from the source layer :param source_field_type: QVariant.Type :param target_value_dict: dict of unique values in the target layer. We only use keys in this function. :param target_field_type: QVariant.Type :return: Whether the source_value is duplicated in the target layer and, if so, also the target value """ # Direct comparison if source_field_type == target_field_type: if source_value in target_value_dict: return True, source_value else: return False, None # We first need to convert types before comparing... qvariant_value = QVariant(source_value) res_can_convert = qvariant_value.canConvert(target_field_type) if res_can_convert: res_convert = qvariant_value.convert(target_field_type) if res_convert: if qvariant_value.value() in target_value_dict: return True, qvariant_value.value() return False, None
def xyz_json_to_feat(feat_json, fields): """ Convert xyz geojson to feature, given fields """ names = fields.names() qattrs = list() # handle xyz id v = feat_json.get(XYZ_ID, "") val = QVariant(v) qattrs.append([QGS_XYZ_ID, val]) if QGS_XYZ_ID not in names: fields.append(make_field(QGS_XYZ_ID, val)) props = feat_json.get("properties") if isinstance(props, dict): rename_special_props(props) # rename fid in props attrs = list(_attrs(props)) for k, v in attrs: val = QVariant(v) # if not val.isValid(): # val = QVariant("") if not val.type() in valid_fieldTypes: for cast in [QVariant.Int, QVariant.String]: if val.canConvert(cast): val.convert(cast) break if not val.type() in valid_qvariant: print_qgis("Invalid type", k, val.typeName()) continue if k not in names: fields.append(make_field(k, val)) qattrs.append([k, val]) feat = QgsFeature(fields) for k, v in qattrs: feat.setAttribute(k, v) geom = feat_json.get("geometry") if geom is not None: s = json.dumps(geom) geom_ = QgsGeometry.fromWkt( ogr.CreateGeometryFromJson(s).ExportToWkt()) feat.setGeometry(geom_) return feat
def _single_feature(feat_json, fields): # adapt to existing fields feat = QgsFeature() names = fields.names() if QGS_ID in names: names.remove(QGS_ID) names_normal = list(map(normal_field_name, names)) qattrs = list() # handle xyz id v = feat_json.get(XYZ_ID, "") val = QVariant(v) qattrs.append([QGS_XYZ_ID, val]) if QGS_XYZ_ID not in names: fields.append(make_field(QGS_XYZ_ID, val)) props = feat_json.get("properties") if not props is None: attrs = list(_attrs(props)) for k, v in attrs: val = QVariant(v) if not val.isValid(): val = QVariant("") # if not val.type() in valid_qvariant: if not val.type() in valid_fieldTypes: for cast in [QVariant.Int, QVariant.String]: if val.canConvert(cast): val.convert(cast) break if not val.type() in valid_qvariant: print_qgis("Invalid type", k, val.typeName()) continue if k not in names_normal: k = unique_field_name(k, len(fields)) fields.append(make_field(k, val)) else: idx = names_normal.index(k) k = names[idx] qattrs.append([k, val]) feat.setFields(fields) for k, v in qattrs: feat.setAttribute(k, v) return feat