10

I have a CSV file with the coordinates of the vertices of my polygons (in the picture they correspond to "left", "right", "top", "bottom"). Actually is a grid of polygons. How can I import the file in QGIS as polygons and not as points?

enter image description here

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Oct 31, 2020 at 10:48

2 Answers 2

7

You can do it with expressions, even loading your excel file, without converting to .csv, load your excel file as if it was a vectorial layer

On the process toolbox > vector geometry > geometry by expression. enter image description here

enter image description here

The expression, although it looks a little long, is simply to write a wkt polygon using the fields, then you convert it to geometry. enter image description here

Simply run this expression

 geom_from_wkt( concat('POLYGON((',to_string("left")+' '+to_string("top")+',',to_string("right")+' '+to_string("top")+', ', to_string("right")+' '+to_string("bottom")+',', to_string("left")+' '+to_string("bottom"),'))

You will get a result like this:

enter image description here

answered Oct 31, 2020 at 13:04
6

I'm assuming you want the rectangles with diagonal (left,top) and (right,bottom) as the required polygons.

You could import the CSV as points, say x=left, y=top, and then use processing (or even expressions) which access right and bottom as attributes and transform to the desired rectangles.

However, it will be faster to preprocess within Excel (or whatever your spreadsheet program is) by combining the attributes into a WKT Polygon string, which unambiguously specifies the polygons in a standard format.

In cell G2 (or row 2 of a new column if G is used), put ="POLYGON(("&B2&" "&C2&", "&D2&" "&C2&", "&D2&" "&E2&", "&B2&" "&E2&", "&B2&" "&C2&"))" and copy it down. It generates a polygon WKT string that goes around the points clockwise. You can then import the CSV taking the geometry from this column and QGIS will interpret it as a polygon (rectangle) for each line.

answered Oct 31, 2020 at 11:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.