[Qgis-user] Refactoring line offset script - help

Richard Duivenvoorde rdmailings at duif.net
Thu Jun 24 11:30:11 PDT 2021


On 6/24/21 3:22 PM, Ryan Peel wrote:
> I cannot for the life of me refactor this code to get it to work for the latest version of QGIS.  Has anyone successfully been able to do this or could help?  The article is a few years old and I don't know if the author is still there to help.
> 
> https://gis.stackexchange.com/questions/239129/shifting-display-of-overlapping-lines-in-qgis/239370#239370 <https://gis.stackexchange.com/questions/239129/shifting-display-of-overlapping-lines-in-qgis/239370#239370>

Hi Ryan, 

I fixed both versions see below.
I left the old code in comments for reference, it's just a couple of lines.
Attached a screenie for the version which explodes all vertices and the one only using the first and last vertex.

Not guaranteeing it will always work (for one I think you will need single lines, not multilines), but as shown it works with me here...

Will try to put in on stackexchange too

Hope this helps:

Regards,

Richard Duivenvoorde

########################
# ALL vertices
########################

from qgis.core import *
from qgis.gui import *
from math import sin, cos, radians

@qgsfunction(args='auto', group='Custom')
def draw_wires(angle, percentage, curr_feat, layer_name, feature, parent):

    def wires(polyline, new_angle, percentage, referenced_columns=[]):
        for x in range(0, len(polyline)-1):
            vertices = []
            first_point = polyline[x]
            second_point = polyline[x +1]
            #seg = QgsGeometry.fromPolyline([first_point, second_point])
            seg = QgsGeometry.fromPolylineXY([first_point, second_point])            
            len_feat = seg.length()
            frac_len = percentage * len_feat
            limb = frac_len/cos(radians(new_angle))
            tmp_azim = first_point.azimuth(second_point)
            angle_1 = radians(90 - (tmp_azim+new_angle))
            dist_x, dist_y = (limb * cos(angle_1), limb * sin(angle_1))
            #point_1 = QgsPoint(first_point[0] + dist_x, first_point[1] + dist_y)
            point_1 = QgsPointXY(first_point[0] + dist_x, first_point[1] + dist_y)
            angle_2 = radians(90 - (tmp_azim-new_angle))
            dist_x, dist_y = (limb * cos(angle_2), limb * sin(angle_2))
            #point_2 = QgsPoint(second_point[0] - dist_x, second_point[1] - dist_y)
            point_2 = QgsPointXY(second_point[0] - dist_x, second_point[1] - dist_y)
            tmp_azim = second_point.azimuth(first_point)
            angle_3 = radians(90 - (tmp_azim+new_angle))
            dist_x, dist_y = (limb * cos(angle_3), limb * sin(angle_3))
            #point_3 = QgsPoint(second_point[0] + dist_x, second_point[1] + dist_y)
            point_3 = QgsPointXY(second_point[0] + dist_x, second_point[1] + dist_y)
            angle_4 = radians(90 - (tmp_azim-new_angle))
            dist_x, dist_y = (limb * cos(angle_4), limb * sin(angle_4))
            #point_4 = QgsPoint(first_point[0] - dist_x, first_point[1] - dist_y)
            point_4 = QgsPointXY(first_point[0] - dist_x, first_point[1] - dist_y)
            vertices.extend([first_point, point_1, point_2, second_point, point_3, point_4, first_point])
            #tempGeom = QgsGeometry.fromPolyline(vertices)
            tempGeom = QgsGeometry.fromPolylineXY(vertices)
            num.append(tempGeom)
        return num

    #layer = QgsMapLayerRegistry.instance().mapLayersByName(layer_name)[0]
    layer = QgsProject().instance().mapLayersByName(layer_name)[0]

    all_feats = {}
    index = QgsSpatialIndex()
    for ft in layer.getFeatures():
        index.insertFeature(ft)
        all_feats[ft.id()] = ft

    first = True

    tmp_geom = curr_feat.geometry()
    polyline = tmp_geom.asPolyline()
    idsList = index.intersects(tmp_geom.boundingBox())
    occurrences = 0
    for id in idsList:
        test_feat = all_feats[id]
        test_geom = test_feat.geometry()
        if tmp_geom.equals(test_geom):
            occurrences += 1
    if occurrences & 0x1:
        num = [tmp_geom]
    else:
        num = []

    rapp = occurrences/2
    i=2
    new_angle = angle

    while i <= occurrences:
        draw=wires(polyline, new_angle, percentage)
        i += 2
        new_angle -= new_angle/rapp
    first = True
    for h in num:
        if first:
            geom = QgsGeometry(h)
            first = False
        else:
            geom = geom.combine(h)
    return geom


########################
# only the start and end
########################

from qgis.core import *
from qgis.gui import *
from math import sin, cos, radians

@qgsfunction(args='auto', group='Custom')
def draw_wires2(angle, percentage, curr_feat, layer_name, feature, parent):

    def wires2(polyline, new_angle, percentage):
        vertices = []
        len_feat = polyline.length()
        frac_len = percentage * len_feat
        limb = frac_len/cos(radians(new_angle))
        tmp_azim = first_point.azimuth(second_point)
        angle_1 = radians(90 - (tmp_azim+new_angle))
        dist_x, dist_y = (limb * cos(angle_1), limb * sin(angle_1))
        #point_1 = QgsPoint(first_point[0] + dist_x, first_point[1] + dist_y)
        point_1 = QgsPointXY(first_point[0] + dist_x, first_point[1] + dist_y)
        angle_2 = radians(90 - (tmp_azim-new_angle))
        dist_x, dist_y = (limb * cos(angle_2), limb * sin(angle_2))
        #point_2 = QgsPoint(second_point[0] - dist_x, second_point[1] - dist_y)
        point_2 = QgsPointXY(second_point[0] - dist_x, second_point[1] - dist_y)
        tmp_azim = second_point.azimuth(first_point)
        angle_3 = radians(90 - (tmp_azim+new_angle))
        dist_x, dist_y = (limb * cos(angle_3), limb * sin(angle_3))
        #point_3 = QgsPoint(second_point[0] + dist_x, second_point[1] + dist_y)
        point_3 = QgsPointXY(second_point[0] + dist_x, second_point[1] + dist_y)        
        angle_4 = radians(90 - (tmp_azim-new_angle))
        dist_x, dist_y = (limb * cos(angle_4), limb * sin(angle_4))
        #point_4 = QgsPoint(first_point[0] - dist_x, first_point[1] - dist_y)
        point_4 = QgsPointXY(first_point[0] - dist_x, first_point[1] - dist_y)
        vertices.extend([first_point, point_1, point_2, second_point, point_3, point_4, first_point])
        #tempGeom = QgsGeometry.fromPolyline(vertices)
        tempGeom = QgsGeometry.fromPolylineXY(vertices)
        num.append(tempGeom)

    #layer = QgsMapLayerRegistry.instance().mapLayersByName(layer_name)[0]
    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    all_feats = {}
    index = QgsSpatialIndex()
    for ft in layer.getFeatures():
        index.insertFeature(ft)
        all_feats[ft.id()] = ft
    first = True
    tmp_geom = curr_feat.geometry()
    #coords = tmp_geom.asMultiPolyline()
    #if coords: 
    if tmp_geom.isMultipart():
        coords = tmp_geom.asMultiPolyline()
        new_coords = [QgsPointXY(x, y) for x, y in z for z in coords]
    else:
        coords = tmp_geom.asPolyline()
        new_coords = [QgsPointXY(x, y) for x, y in coords]
    first_point = new_coords[0]
    second_point = new_coords[-1]
    #polyline=QgsGeometry.fromPolyline([first_point, second_point])
    polyline=QgsGeometry.fromPolylineXY([first_point, second_point])
    idsList = index.intersects(tmp_geom.boundingBox())
    occurrences = 0
    for id in idsList:
        test_feat = all_feats[id]
        test_geom = test_feat.geometry()
        if tmp_geom.equals(test_geom):
            occurrences += 1
    if occurrences & 0x1:
        num = [polyline]
    else:
        num = []

    rapp = occurrences/2
    i=2
    new_angle = angle

    while i <= occurrences:
        draw=wires2(polyline, new_angle, percentage)
        i += 2
        new_angle -= new_angle/rapp
    first = True
    for h in num:
        if first:
            geom = QgsGeometry(h)
            first = False
        else:
            geom = geom.combine(h)
    return geom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lines.jpeg
Type: image/jpeg
Size: 29434 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210624/6f6d5098/attachment-0001.jpeg>


More information about the Qgis-user mailing list