Wednesday 17 October 2018

pyqgis - Automating vector data entry in QGIS


I have a vector contour data(polyline). I want to add values to each contour lines. Means I want to digitize my contour lines with proper values. Those values are with me in a paper. I'm adding those (z values) in the "id" column in the attribute table after selecting each line separately. it's taking a lot of time since there are around 20000 lines. Actually I created these contours after digitizing my raster map. (raster to vector conversion after scanning those maps)


Is there any way to add values automatically in between lines, if I select two extreme lines (contour is varying by an interval of 1 unit) or is it possible to create a GUI like that, or can I program like that in QGIS?


I'm new to this.


enter image description here


I'm adding my contour values to the "id" table.



Is there any way to program for this in pyqgis or any other way to do it?



Answer



I would suggest Add autoincremental field tool in the QGIS Processing Toolbox > Vector table.


[Step 1] Draw a dummy line


Create a new line shapefile (in this example I have created a Lines layer, with only one line, which starts at the lowest point and ends at the highest point [a1]. [a2]. I have an id field, called fid (and it has only 1 line; i.e. "fid"= 1).


[Step 1.5] Choose contours which intersects the dummy line


enter image description here


(Expression)


intersects($geometry, geometry(get_feature('Lines', 'fid', '1')))


This expression returns new layer Matching features


[Step 2] Start Add autoincremental field tool


enter image description here


Make sure that the Input layer is your [Edit] selected contour layer, not the dummy line layer. Start value (1 m in the above example) should be the height of the first contour line it intersects.


(Expression)...[a3]


line_locate_point(geometry:=geometry(get_feature('Lines', 'fid', '1')),
point:= intersection($geometry, geometry(get_feature('Lines', 'fid', '1'))))

[Step 3]


Open the attribute table of newly created Incremented layer and check the AUTO field. It seems your contour interval is 1, so you would not need further calculation. (If interval is 10m, then use the Field Calculator to multiply 10).



enter image description here


[Step 4] Repeat this process to cover whole area, then merge all output layers.




[a1] If you have already drawn the dummy line starting from the mountain top, then change the Sorting to descending. (Just tick off Sort ascending checkbox).


[a2] This line does not have to be straight.


[a3] This is just calculating the distance of intersecting contours, from the starting point of the line (along the line).


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...