2

I have a QgsRectangle and a network layer of lines. How is it possible to calculate the length (or percentage) of the network that lies inside the rectangle?

I found this answer that helps computing the percentage of a line inside a polygon, but I would love to avoid having to convert each rectangle in a polygon (I have almost 50000 rectangles) and iterating over that and over the whole line network (which is also some 25000 features).

Here is what I have:

from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtCore import *
from qgis.core import *
from qgis.gui import *
import itertools

registry = QgsMapLayerRegistry.instance()
network = registry.mapLayersByName('network')[0]

#all of this is to create the rectangles that I'll need to compute later

ext = network.extent()

x1 = ext.xMinimum()
x2 = ext.xMaximum()
y1 = ext.yMinimum()
y2 = ext.yMaximum()

X, Y = range(x1,x2,100), range(y1,y2,100)
XY = itertools.product(X,Y)

rectDic = {}
for x,y in XY:
    p1 = QgsPoint(x,y)
    p2 = QgsPoint(x + 1000, y + 1000)
    r = QgsRectangle(p1,p2)
    rectDic[(x,y)] = r

#and this is where I get stuck

networkPerRect = {}
for pt in rectDic:
    r = recdic[pt]
    lenOfNetwork = length of network inside r #NEED HELP HERE
    networkPerRect[pt] = lenOfNetwork
1

In this case, it's very difficult "to avoid having to convert each rectangle in a polygon ... and iterating over that and over the whole line network" because this is a geoprocessing problem. To understand which is the purpose of your code, I modified it by using a greater factor for each XY (10000) and p2 (100000) variables to reduce compute time and print the length of a network inside each generated rectangle. Code is:

from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtCore import *
from qgis.core import *
from qgis.gui import *
import itertools

registry = QgsMapLayerRegistry.instance()
network = registry.mapLayersByName('line')[0]
feats_network = [ feat for feat in network.getFeatures() ]

#all of this is to create the rectangles that I'll need to compute later

ext = network.extent()

x1 = ext.xMinimum()
x2 = ext.xMaximum()
y1 = ext.yMinimum()
y2 = ext.yMaximum()

X, Y = range(int(x1),int(x2),10000), range(int(y1),int(y2),10000)
XY = itertools.product(X,Y)

rectDic = {}
for x,y in XY:
    p1 = QgsPoint(x,y)
    p2 = QgsPoint(x + 100000, y + 100000)
    r = QgsRectangle(p1,p2)
    rectDic[(x,y)] = r

#and this is where I get stuck

#networkPerRect = {}
for i, pt in enumerate(rectDic):
    r = rectDic[pt].asWktPolygon()
    r_geom = QgsGeometry.fromWkt(r)

    lenOfNetwork = 0

    for j, feat in enumerate(feats_network):
        if feat.geometry().intersects(r_geom):
            print "rectangle {:d} intercets feature network {:d}".format(i,j)
            lenOfNetwork += feat.geometry().intersection(r_geom).length()

    print "total length", lenOfNetwork

#    lenOfNetwork = length of network inside r #NEED HELP HERE
#    networkPerRect[pt] = lenOfNetwork
#

I used next arbitrary shapefile as network layer; previously converted in multipart to additionally improve compute time.

enter image description here

After running the code at the Python Console of QGIS, I got 96 total lengths:

rectangle 0 intercets feature network 0
total length 62350.5059025
rectangle 1 intercets feature network 0
total length 88243.6468944
rectangle 2 intercets feature network 0
total length 136207.054982
rectangle 3 intercets feature network 0
total length 13782.1451098
rectangle 4 intercets feature network 0
total length 63420.2492418
rectangle 5 intercets feature network 0
total length 73010.35539
rectangle 6 intercets feature network 0
total length 11730.7783419
rectangle 7 intercets feature network 0
total length 273051.100382
rectangle 8 intercets feature network 0
total length 48209.4105307
rectangle 9 intercets feature network 0
total length 27869.0695912
rectangle 10 intercets feature network 0
total length 199036.229632
rectangle 11 intercets feature network 0
total length 2387.5570924
rectangle 12 intercets feature network 0
total length 119356.014314
rectangle 13 intercets feature network 0
total length 46886.2287155
rectangle 14 intercets feature network 0
total length 130805.109208
rectangle 15 intercets feature network 0
total length 236789.965812
total length 0
rectangle 17 intercets feature network 0
total length 156467.459214
rectangle 18 intercets feature network 0
total length 88243.6468944
rectangle 19 intercets feature network 0
total length 61032.6921494
rectangle 20 intercets feature network 0
total length 207372.499041
rectangle 21 intercets feature network 0
total length 125942.428638
rectangle 22 intercets feature network 0
total length 46212.2146533
rectangle 23 intercets feature network 0
total length 36535.2901658
rectangle 24 intercets feature network 0
total length 26522.9013592
rectangle 25 intercets feature network 0
total length 53831.7003364
rectangle 26 intercets feature network 0
total length 63420.2492418
rectangle 27 intercets feature network 0
total length 133112.135442
rectangle 28 intercets feature network 0
total length 142873.607026
rectangle 29 intercets feature network 0
total length 227979.730828
rectangle 30 intercets feature network 0
total length 76088.1421981
rectangle 31 intercets feature network 0
total length 52184.7306027
rectangle 32 intercets feature network 0
total length 95085.4988114
rectangle 33 intercets feature network 0
total length 166856.172206
rectangle 34 intercets feature network 0
total length 88243.6468944
rectangle 35 intercets feature network 0
total length 200041.062615
rectangle 36 intercets feature network 0
total length 198722.874336
rectangle 37 intercets feature network 0
total length 75539.660579
rectangle 38 intercets feature network 0
total length 197639.165497
rectangle 39 intercets feature network 0
total length 175367.94436
rectangle 40 intercets feature network 0
total length 63420.2492418
rectangle 41 intercets feature network 0
total length 295511.810162
rectangle 42 intercets feature network 0
total length 143759.204944
rectangle 43 intercets feature network 0
total length 23794.5339165
rectangle 44 intercets feature network 0
total length 43819.3115298
rectangle 45 intercets feature network 0
total length 63420.2492418
rectangle 46 intercets feature network 0
total length 316886.314413
rectangle 47 intercets feature network 0
total length 115062.507231
rectangle 48 intercets feature network 0
total length 166651.755055
rectangle 49 intercets feature network 0
total length 144842.913784
rectangle 50 intercets feature network 0
total length 237873.674652
rectangle 51 intercets feature network 0
total length 153296.825078
rectangle 52 intercets feature network 0
total length 74998.9210491
rectangle 53 intercets feature network 0
total length 286855.530294
rectangle 54 intercets feature network 0
total length 240455.65256
rectangle 55 intercets feature network 0
total length 241573.583016
total length 0
rectangle 57 intercets feature network 0
total length 78796.8389007
rectangle 58 intercets feature network 0
total length 230526.469985
rectangle 59 intercets feature network 0
total length 39263.6576086
rectangle 60 intercets feature network 0
total length 121506.005452
rectangle 61 intercets feature network 0
total length 32071.1192815
rectangle 62 intercets feature network 0
total length 99443.0721743
rectangle 63 intercets feature network 0
total length 120635.800868
rectangle 64 intercets feature network 0
total length 167382.996967
rectangle 65 intercets feature network 0
total length 148984.427218
rectangle 66 intercets feature network 0
total length 257894.718748
rectangle 67 intercets feature network 0
total length 205379.337318
rectangle 68 intercets feature network 0
total length 38209.4105307
rectangle 69 intercets feature network 0
total length 259753.38373
rectangle 70 intercets feature network 0
total length 22071.1192815
rectangle 71 intercets feature network 0
total length 238378.879479
rectangle 72 intercets feature network 0
total length 15128.3133418
rectangle 73 intercets feature network 0
total length 97280.8708923
rectangle 74 intercets feature network 0
total length 181861.744605
rectangle 75 intercets feature network 0
total length 179822.38919
rectangle 76 intercets feature network 0
total length 96883.1892213
rectangle 77 intercets feature network 0
total length 46886.2287155
rectangle 78 intercets feature network 0
total length 46547.6789725
rectangle 79 intercets feature network 0
total length 177965.919194
rectangle 80 intercets feature network 0
total length 142710.94429
rectangle 81 intercets feature network 0
total length 33806.9227231
rectangle 82 intercets feature network 0
total length 330184.031065
rectangle 83 intercets feature network 0
total length 113718.046323
rectangle 84 intercets feature network 0
total length 190834.955395
rectangle 85 intercets feature network 0
total length 66572.4565858
rectangle 86 intercets feature network 0
total length 166228.537003
rectangle 87 intercets feature network 0
total length 159786.601184
rectangle 88 intercets feature network 0
total length 315027.649431
rectangle 89 intercets feature network 0
total length 74998.9210491
rectangle 90 intercets feature network 0
total length 82244.9255883
rectangle 91 intercets feature network 0
total length 270078.151876
rectangle 92 intercets feature network 0
total length 56560.0677791
rectangle 93 intercets feature network 0
total length 51817.6266959
rectangle 94 intercets feature network 0
total length 174284.23552
rectangle 95 intercets feature network 0
total length 218973.189506

To corroborate good code performance, I took last generated rectangle (visualized with QuickWKT plugin; as it can be seen at next image) and manually determined (with measure line tool of QGIS) length lines inside it.

enter image description here

Result was as expected.

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.