I have two line-layers: TLM-layer and SEGMENT-layer The TLM-layer has much more segments than the SEGMENT-layer (example: x – z – x in TLM but only red in SEGMENT-layer), but they have the same vertices (except segment a which is only in the SEGMENT-layer)

I'd like to add a new attribute to the SEGMENT-layer called object_kat (integer) which should be defined as:
blue = "object_kat" = 1
yellow = "object_kat" = 2
red = "object_kat" = 3
violet = "object_kat" = NULL

If a segment in the SEGMENT-layer contains more than one segment of the TLM-layer (as example red in SEGMENT-layer and x – z – x in the TLM-layer), the object_type of the longest segment should be used (e.g. x -> red -> "object_kat" = 3 and z which would be yellow and "object_kat" = 2 should be ignored because it's shorter)

I think I should use following steps:

  1. which object_type's (y -> "object_type" = '3', z -> "object_type" = '2', x -> "object_type" = '1') of the TLM-layer are within a segment of the SEGMENT-layer (blue, yellow, red, violet)
  2. which of these is longest
  3. add the value of it (blue -> 1, yellow -> 2, red -> 3, in the new "object_kat" attribute (integer) of the SEGMENT-layer

I'd like to have this as easy as possible using an expression in the Field Calculator in QGIS

enter image description here

  • the size of the dataset can be different, as it is a process I have to do over and over again in different regions. the perpendicular lines should symbolise the start and end of the segments. and the segments are different road types.
    – mymymy
    Feb 18, 2022 at 12:54
  • For x-z-x, "object_kat" = 3. Because the object_type of the longest segment which is x. OK. So, is it 3, because "total x" is the longest (I mean length(x+x) > length(z)), or because any of segment is bigger than the others regardless of total length of any letter? I mean this: i.stack.imgur.com/dQ4LA.png Feb 18, 2022 at 16:27
  • that's a very good question @KadirŞahbaz I think it should be the sum which makes it the longest. There is also a threshold which we use before, so segments longer than a number are splitted before manually.
    – mymymy
    Feb 25, 2022 at 9:12

1 Answer 1


You can use this Python script.

tlm_layer_name = "tlm_layer"
segment_layer_name = "segment_layer"

tlm_layer = QgsProject.instance().mapLayersByName(tlm_layer_name)[0]
seg_layer = QgsProject.instance().mapLayersByName(segment_layer_name)[0]

# add object_kat field if not exist
field_index = seg_layer.fields().indexFromName("object_kat")
if field_index==-1:
    seg_layer.dataProvider().addAttributes([QgsField("object_kat", QVariant.String, len=3)])
    field_index = seg_layer.fields().indexFromName("object_kat")

buffer_distance = 0.1 # 0.1 unit 
# if CRS unit is degree, use 0.000001

attr_map = {}
for seg in seg_layer.getFeatures():    
    buffer = seg.geometry().buffer(buffer_distance, 5)    
    intersected_tlms = [t for t in tlm_layer.getFeatures() if t.geometry().within(buffer)]
    len_ = len(intersected_tlms)
    if len_ == 1:
        longest = intersected_tlms[0]
    elif len_ > 1 :
        longest = sorted(intersected_tlms, key=lambda x: x.geometry().length(), reverse=True)[0]
        print(f"no intersecting tlm for segment id: {seg.id()}")
    attr_map.update({seg.id(): {field_index: longest["object_type"]}})

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.