I created a grid and used it's squares and it has 4 values in attribute table (each square has left, top, bottom, and right values). Every square is a different object. I try to export file from Excel/CSV, while importing in QGIS, its shows points coordinates X filed, Y field and I have (left, top, right, bottom values) 4 points to make it grid (square).
How do I import correctly in QGIS from Excel/CSV?
4 Answers 4
You can create a virtual layer from the information in the excel file.
Add the excel or csv file to the map, and MakePolygon using the four corner coordinates:
select MakePolygon(
GeomFromText(concat('LINESTRING(',
"left",' ',"top",
',',
"left",' ',"bottom",
',',
"right",' ',"bottom",
',',
"right",' ',"top",
',',
"left",' ',"top",
')'))
) as geometry, *
from gridexport
Replace gridexport
with your table name and set the crs in the dialogue:
Either convert your coordinates to WellKnownText format, or change the layout of your table so each corner of each square has its own row, including a square ID, a point ID and its X and Y coordinates. Duplicate the first point of each square and add it as the fifth.
Your table should look as follows:
Square ID | Point ID | x | y |
---|---|---|---|
1 | 1 | 0 | 0 |
1 | 2 | 1 | 0 |
1 | 3 | 1 | 1 |
1 | 4 | 0 | 1 |
1 | 5 | 0 | 0 |
2 | 1 | 10 | 10 |
Then load your csv into QGIS, run points to path
and finally convert your lines to polygons
.
In MSExcel fill in columns A to H the corner coordinates, in column I the id that identifies the polygon, create in column J field geom, calculate geom with the following formula:
="POLYGON(("&A2&" "&B2&", "&C2&" "&D2&", "&E2&" "&F2&", "&G2&" "&H2&", "&A2&" "&B2&"))"
LLx LLy LRx LRy URx URy ULx ULy id geom
-2300 -1000 -1400 -1000 -1400 -400 -2300 -400 block1 POLYGON((-2300 -1000, -1400 -1000, -1400 -400, -2300 -400, -2300 -1000))
-1400 -1000 -500 -1000 -500 -400 -1400 -400 block2 POLYGON((-1400 -1000, -500 -1000, -500 -400, -1400 -400, -1400 -1000))
Save as TABseparated textfile, and import as WKT in QGIS: Layer > Add layer > add delimited text layer...
Add your exported text file, select File Format: custom delimiters tab, Geometry Definition: Well Known Text, Geometry Field geom and click add.
Besides the numerous solutions proposed, you could use the Geometry Generator to create the polygons.
If your CSV-file has the following form:
xmin,ymin,xmax,ymax
-0.5,-0.5,0.5,0.5
-2,-1,1,2
...
you can load it using QGIS' CSV driver (Select xmin and ymin for xy): enter image description here
Then change the "Simple Marker" styling to "Geometry Generator (Polygon/Multipolygon" and use the following expression to generate your polygons:
geom_from_wkt( 'POLYGON((' ||
"xmin"||' '||"ymin"||','||
"xmax"||' '||"ymin"||','||
"xmax"||' '||"ymax"||','||
"xmin"||' '||"ymax"||','||
"xmin"||' '||"ymin"||','||'))')
With the processiong tool "Geometry by expression" you can save the geometries into a spatial layer.
Explore related questions
See similar questions with these tags.