I'm actually working on a way to print parts of a Polyline according to both specified paper size and scale. The paper should be rotated in order to maximize the visualisation comfort, and this must be automated.

Note : I'm using QGIS 2.18.x, and can't migrate to 3.x due to major internal extensions that haven't been switched from Python 2.x to 3.x yet.

Here are the steps I followed so far:

  • Get the line length, and divide it by the page width in order to know the number of pages to print.
  • Divide line length by this number to get the average distance of cable to print on each page (in order to avoid the last page to be partially used)
  • Subdivide the polyline using the average distance. This is where I'm actually stuck

I found a way to get the points where I should cut, but I didn't found any function to subdivide a line using a distance. (Apart from splitGeometry(), which doesn't works with a list of points. It doesn't make them collide the line. Also giving a polyline version of this list cuts too much, as it splits at each interesection of the lines, not only the specified points)

I only succeed to get the points that should cut the line with this code :

pointsList = []  
for i in range (1, pagesNb):  
    point = line.interpolate(dist * i)  
    pointsList.append(point.asPoint())
  • Make a Convex Hull of each subline and search for the longuest included line

  • Make the orientation of the page parallel to this line and adjust the page using the centroid and the page dimensions

Is there a function to cut a line by a ratio, a number of segments or by a list of points?

I'm also interested in any hints/improvements on this method.

I am aware that a minimumOrientedBoundingBox() function exists, the problem is that it's not present in the QGIS version I use.

Nethertheless, if there is a way to catch the sourcecode of it and use it, I will also take that as a solution (not the one present on the toolbox and that can be called with processing, only the PyQGIS function)

Unfortunately there's no method in the QGIS API to do this easily. You'd need to write your own, which steps along the vertices in the LineStrings and:

  1. checks to see if the whole segment should be included in the chopped line subsection (i.e. the total distance traversed so far + segment length < desired distance). If so, add it to the output LineString.

  2. If not, use an interpolated point along the segment for the remaining distance required, and add this subsection to your output LineString. Then, start the next output LineString at this interpolated midpoint and continue.

(If you wanted to go the c++ route, I'm sure that a pull request adding this method to the QGIS API would be very much appreciated!)

  • Thanks for your help, I'm not on the c++ route yet, but I will post my python code here. – Chris Prolls Aug 10 at 7:02
  • A generic function should follow your steps, but as I stated I already know the points where I should cut, my function will just need those points and the line then – Chris Prolls Aug 10 at 7:10
  • Actually - you triggered my interest. So the good news is 3.4 will have this function native in the API: github.com/qgis/QGIS/pull/7587 – ndawson Aug 10 at 8:38
  • I will not switch to the new version until 2 months, but I'm happy that you will add it to the API, I will use it during migration. Thanks for keeping QGis up-to-date ! I'm testing my code right now, I will post it when I have checked the different behaviors. – Chris Prolls Aug 10 at 14:28

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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