5

I have a shapefile that contains 10s of thousands of individual four-sided polygons, so I have 5x that in vertices. I need to be able to update the coordinates of every single vertex based on a new list of vertex coordinates. To clarify, this isn't a reprojection or a simple transform, it's an overwrite of the existing polygon vertex coordinates with a different set of coordinates.

I'd like to be able to extract a list of the vertex coordinates, then perform the edits in the list outside of QGIS, then import it back into the shapefile so that the polygon locations update. Is that possible? Or is there a similar method that might do the same?

As an example, just to describe it visually, it could look something like this for just two polygons (black poly is with the 'current' coordinates, purple poly is with the 'updated' coordinated):

enter image description here

If this is a coding thing, might it work with something like this:

for f in layer.getFeatures():
  f.geometry().asPoint().y() = .... 
  f.geometry().asPoint().x() = ....

Where the .... refers to an external source for the coordinates.

6
  • 1
    No, you have five times that in vertices -- A quadrilateral has a fifth vertex with the same as the first to close the ring.
    – Vince
    Commented Nov 3, 2021 at 18:17
  • Thanks, five vertices per poly. The question is still the same though! Commented Nov 3, 2021 at 18:20
  • The best way to do this is with a filter paradigm: Input the original and output a new shapefile with modified values. This should be a basic coding task (same as a copy for all attributes but the geometry), but it's still a coding task (and therefore should have an attempt at code in the question).
    – Vince
    Commented Nov 3, 2021 at 18:22
  • Thanks, I have basic coding skills but have never attempted any in QGIS, I have no idea where to start or which parts of the language would allow me to access the coordinates etc.. in the data. :edit: adding first attempt at code ideas in Q. Commented Nov 3, 2021 at 18:29
  • Sql could help you update the geom of your existing polygon. Other solution if you don't want to code will be to export in a table as wkt apply modification on it with excel or what you want and then import it again. Commented Nov 3, 2021 at 18:40

3 Answers 3

1

With the QGIS field calculator it is possible to update the geometry.

Below an example, I add a WKT (text) field and populate it with the expression:

geom_to_wkt ($ geometry, 2);

I add a second WKT_update field and populate it with the WKT string of the new polygon.

Finally, I update the geometry using the expression:

geom_from_wkt ("wkt_2")

enter image description here

after update:

enter image description here

it is therefore necessary to have a field populated in WKT format of the new updated polygon

1

There are probably better solutions (that will be provided), nevertheless, I am giving IMHO the most intuitive approach

Step 1. Extract coordinates from polygon vertices, follow any of the suggested solutions in Extracting Latitude/Longitude from polygon vertices in QGIS

Step 2. Export as a CSV file

Step 3. Apply changes in that CSV file

Step 4. Import the modified CSV file back into QGIS

1

The method that I used was a combination of the suggestions. Exporting as WKT, as initially suggested by @CorentinLemaitre, was how I did it. I edited the WKT in Excel and then re-imported that to QGIS.

This looks like it will be almost exactly the same method as @Taras, except with a slightly different way of getting the coordinates out. @Pigreco's method looks like a more sophisticated way of doing the same thing with WKT files within QGIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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