5

I would like to be able to move two layers that look like you see below:

enter image description here

Basically, when I move the polygon object, to which the lines are snapped as a separate layer I want them to be intact at the end. I can't merge these layers and promote them to multipart because I need them separately. I just want to know the option, which will allow me to make the drag including these 2 layers (or more optionally).

Similar issues were potentially here:

but they didn't solve my problem.

Is it possible to do operation such as this in QGIS?

Taras
  • 32,823
  • 4
  • 66
  • 137
Geographos
  • 4,087
  • 2
  • 27
  • 88
  • 1
    An option could be to have a point layer with the external line ends, an attribute "poleID" on these points, and a virtual layer that joins the points to the "poles" and creates the lines on the fly. – JGH Dec 06 '21 at 16:29
  • I don't know how to do that. Could you clarify it with some steps? – Geographos Dec 06 '21 at 16:30
  • Ok, Thanks. Now it starts to make sense, although I can't drag these lines the same as the polygon. They remain fixed in that given location.

    Here is the link to my screenshot: https://imgur.com/gallery/vpGi5WF

    – Geographos Dec 07 '21 at 14:19

2 Answers2

5

You can use Geometry by expression to create the new lines, based on the old ones. Use this expression and adapt it to your use case - here, the line goes to the centroid of the shifted polygon:

make_line (
    start_point ($geometry), 
    centroid (
        geometry (
            get_feature_by_id(
                'shifted_polygon', 
                1
))))

This is how the dialog window looks like:

enter image description here

Here visualized with geometry generator and the same expression to show how it works: enter image description here

Here in detail with the centroids made visible: blue = orgininal, red = shifted polygon/centroid; black lines = initial lines, red dotted = created line. White dots are start_point of the lines (if they are end_points, simply change start_point to end_point on line 2 in the expression: enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • I have the new layer "Modified geometry" created, but I can't see it on my map. Is this expression valid then? – Geographos Dec 06 '21 at 17:21
  • Without sharing your data or at least posting a screenshot, it's difficult to say what happened. Did you adapt the name of the layer (shifted_polygon in my case)? Is the polygon you shift really feature no. 1? Are start-points of the line really those outside the polygon? How did you make settings in the Geometry by expression dialog: did you set output geomtetry type to line? And did you use the line layer as input? – Babel Dec 06 '21 at 17:30
  • Yes I did, but I don't know what to put in the dialog window. – Geographos Dec 07 '21 at 13:55
  • Added the dialog window to my answer – Babel Dec 07 '21 at 14:12
  • OK, now it makes sense. However, my lines remain fixed in the position. Is there something I have to do here? They do not move at all. The screenshot is here: https://imgur.com/PwDEz6A – Geographos Dec 07 '21 at 17:41
  • Image could not be loaded. Can you share the project + data? Did you try moving (pan) the map canvas? Sometimes this is needed to upadate the image. If it does not work, try end_point instead of start_point. – Babel Dec 07 '21 at 17:43
5

One option is to use an endpoint point layer, the existing pole polygon layer and to compute a virtual lines layer, that is updated each time the map is moved.

The endpoint must have a poleID attribute, that is populated of the polygon it should be connected to.

The pole layer must have a unique poleID attribute.

The virtual layer can have any attribute from the endpoint or the pole layer, in addition to create a line between the two geometries.

When you touch the map (pan, move, refresh etc), the lines are automatically recomputed. You can use the virtual layer like any other layer, or you can export it to another format if you need to persist the data.

Go the the menu layer / add layer / add-edit virtual layer and enter the following query.

select p.poleID, pt.ptID, ST_ShortestLine(p.geometry,pt.geometry)
from endPoints pt
join pole p
 on pt.poleID=p.poleID

enter image description here

after moving the pole polygon:

enter image description here

PS: this solution produces real lines that can be used for further analysis, in contrast with the excellent geometry_generator solution that produces lines for display only.

JGH
  • 41,794
  • 3
  • 43
  • 89
  • OK, I've managed with it. Unfortunately, when I move the polygon, the lines remain in the same place. I have to redo the query if I want them to be dragged where the new place of the polygon is. Is something I could do here in order to make these lines draggable in the real-time? – Geographos Dec 06 '21 at 17:46
  • @MKR pan the map after having moved the polygon (or refresh, or zoom) – JGH Dec 06 '21 at 17:47
  • Ok, now I see. Thanks. Let me play with it. – Geographos Dec 06 '21 at 17:51