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?
QGIS – How to Import CSV as Polygon in QGIS
csvpolygonqgis
Related Solutions
Your approach is good, but you could make things clearer using dictionaries instead of list and the csv module allows it.
Moreover, your script use two loops while it is possible to simplify using only one (the second loop is redundant, one line of the csv file = one record of the shapefile).
1) With dictionaries:
Reading the csv file:
with open('your.csv', 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
print row
{'xr': '22.5', 'yb': '31.353634', 'name': 'some Name', 'xl': '-25.3125', 'yt': '47.517193'}
{'xr': '-0.703125', 'yb': '74.40216', 'name': 'another Name', 'xl': '-103.359375', 'yt': '80.87282'}
You can now use row['xr'] or row['name'] instead of row[n] (more explicit)
So your script becomes:
import csv
polyName, polyPart = [],[]
with open('your.csv', 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
bl = [float(row['xl']),float(row['yb'])]
tl = [float(row['xl']),float(row['yt'])]
br = [float(row['xr']),float(row['yb'])]
tr = [float(row['xr']),float(row['yt'])]
parr = [tl, tr, br, bl, tl]
polyName.append(row['name'])
polyPart.append(parr)
Writing the polygon shapefile
If you look at PyShpDocs you can see that:
A polygon is defined by:
w = shapefile.Writer(shapefile.POLYGON)
w.line(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
and the script, as you wrote is:
import shapefile
# create the Polygon shapefile
w = shapefile.Writer(shapefile.POLYGON)
# the field
w.field('name','C',maxStringLength)
# write the polygons in the shapefile
for part,name in zip(polyPart, polyName):
w.poly(parts=[part])
w.record(name)
#save the shapefile
w.save('your.shp')
2) Final solution using only one loop
But the second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile without using the polyName and polyPart lists).
w = shapefile.Writer(shapefile.POLYGON)
w.field('name','C',50)
with open('your.csv', 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
bl = [float(row['xl']),float(row['yb'])]
tl = [float(row['xl']),float(row['yt'])]
br = [float(row['xr']),float(row['yb'])]
tr = [float(row['xr']),float(row['yt'])]
parr = [tl, tr, br, bl, tl]
w.poly(parts=[parr])
w.record(row['name'])
w.save("your.shp')
Result in QGIS:
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))
Best Answer
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.
The expression, although it looks a little long, is simply to write a wkt polygon using the fields, then you convert it to geometry.
Simply run this expression
You will get a result like this: