2

I have two layers, one contains polygon while another consist of line. something like below figure:

polygon and line

Now I want to update line attribute column value through polygon attribute column only when, line is inside or contains or overlap 90% in the polygon. Like above picture it is depicted that polygon contains line almost 60%. How can I check that?

I only know that there are two function intersect and contains both cannot do this job. I tried it with contains but it didn't work as polygon provide area while line provide length so I can't compare both.

2

Next code calculates percentages of total length line that is inside of each polygon layer feature. These results, along with indices of each feature involved (line, polygon), are printed at the Python Console of QGIS. It's also visualized the memory layer of respective intersections.

registry = QgsMapLayerRegistry.instance()

line = registry.mapLayersByName('line')
polygon = registry.mapLayersByName('polygon8')

feats_lines = [ feat for feat in line[0].getFeatures() ]
feats_polygons = [ feat for feat in polygon[0].getFeatures() ]

crs = line[0].crs()
epsg = crs.postgisSrid()

uri = "Linestring?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,
                           'lines',
                           'memory')

prov = mem_layer.dataProvider()

for i, feat_line in enumerate(feats_lines):
    for j, feat_polygon in enumerate(feats_polygons):
        if feat_polygon.geometry().intersects(feat_line.geometry()):
            geom = feat_polygon.geometry().intersection(feat_line.geometry())
            print i, j, (geom.length()/feat_line.geometry().length())*100
            feat = QgsFeature()
            feat.setAttributes([i])
            feat.setGeometry(geom)
            prov.addFeatures( [feat] )

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

To try out my approach, I used shapefiles of next image:

enter image description here

After running the code, I got:

enter image description here

0

I would do this as two separate queries - first select all the lines that are within the polygon layer (using the Spatial Query plugin) - update all the selected lines with polygon attributes. Then select all the lines that intersect the polygons and test for the length of the intersection, where that is > 90% (or whatever) select those lines and then update those lines with the required attributes.

  • " test for the length of the intersection," how you will do that – Muhammad Faizan Khan Sep 29 '16 at 4:23
0

You can do it by below workflow:

  • Clip all lines by polygons (each line should have an ID)
  • Create a spatial Join to get all lines inside polygons, then you will get a one to many table (one polygon with one or many lines inside)
  • Create a relation between the result of spatial join and the main lines layer where the ID of lines is the key.
  • Finally the result will have 2 columns: length of clipped line and the length before clipping, then you can use Field calculator to get the percentage (ClippedLength/RealLength)*100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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