[GIS] How to draw a square from 2 coordinates

coordinatescsvqgisrastervector-grid

I have a CSV file with the following output

id, x min, y min, x max, y max, value
1 , 0    , 0    , 100  , 100  , 5
2 , 0,   , 100  , 100  , 200  , 10
3 , 100  , 100  , 200  , 200  , 15
4 , 100  , 0    , 200  , 100  , 20

I can plot the co-ordinate points onto my basemap fine. What I'm trying to do is tell QGIS that row 1 of the CSV has the bottom left and top right corners of a square, to draw it, and to assign it a value of 5.

I'm not even sure this is the right way to achieve my goal – which is to display a grid with different values attached to each square, and then produce a heat-map based on those values.

Thanks.

Best Answer

I would go through WKT way. You could edit your data a bit and build polygons as WKT from the corners of your boxes and save the result into csv file which you can finally import with the CSV plugin. See documentation http://docs.qgis.org/2.0/en/docs/user_manual/introduction/general_tools.html#add-delimited-text-layer

However, it is more fun to do the conversion with SQL within Spatialite. Install spatialite-gui, create an empty database and copy and execute the following SQL statements one by one in the SQL box.

create table text_to_box  (id,  x_min, y_min, x_max, y_max, value);
insert into text_to_box values (1,0,0,100,100,5);
insert into text_to_box values (2,0,100,100,200,10);
insert into text_to_box values (3,100,100,200,200,15);
insert into text_to_box values (4,100,0,200,100,20);
SELECT AddGeometryColumn('text_to_box','geometry',4326,'POLYGON',2);
update text_to_box set geometry = geomfromtext('POLYGON(('||x_min||' '||y_min||','||x_min||' '||y_max||','||x_max||' '||y_max||','||x_max||' '||y_min||','||x_min||' '||y_min||'))',4326)

When you are ready you can connect your database with QGIS and import the table. Note that I used EPSG:4326 as the coordinate system. Edit it if needed.

If you decide to build WKT without Spatialite/SQLite you can do it with Excel or OpenOffice by using the text string functions. The result should be like this (square has four corners but WKT polygon must be closed and therefore the first point must be repeated as the last point)

POLYGON((0 0,0 100,100 100,100 0,0 0))
POLYGON((0 100,0 200,100 200,100 100,0 100))
POLYGON((100 100,100 200,200 200,200 100,100 100))
POLYGON((100 0,100 100,200 100,200 0,100 0))
Related Question