I am trying to write a shapefile from a Shapely geometry using PyQGIS in the QGIS 2.6.0 console.
Lines of code before the OGR method to write shapefile:
extnt = poly.extent() # gives a QtRectangle output -> extnt
x_min = extnt.xMinimum()
x_max = extnt.xMaximum()
y_min = extnt.yMinimum()
y_max = extnt.yMaximum()
A_bbox = QgsPoint(x_min, y_min)
B_bbox = QgsPoint(x_max, y_min)
C_bbox = QgsPoint(x_max, y_max)
D_bbox = QgsPoint(x_min, y_max)
where poly is a polygon shapefile.
Source for the Code below :- How to write Shapely geometries to shapefiles? and GitHub post
linelyr = ogr.Geometry(ogr.wkbLineString)
linelyr.AddPoint(x_min, y_min)
linelyr.AddPoint(x_min, y_max)
out_line = ogr.Geometry(ogr.wkbLineString)
out_line.AddGeometry(linelyr)
out_line.ExportToWkb
driver = ogr.GetDriverByName("Esri Shapefile")
ds = driver.CreateDataSource("outputlocation.shp")
layr1 = ds.CreateLayer('', None, ogr.wkbLineString)
layr1.CreateField(ogr.FieldDefn('id', ogr.OFTInteger))
defn = layr1.GetLayerDefn()
feat = ogr.Feature(defn)
feat.SetField('id', 1)
geom = ogr.CreateGeometryFromWkb(out_line.wkb) # or maybe just 'outline'
feat.SetGeometry(geom)
layr1.CreateFeature(feat)
ds = layr1 = feat = geom = None
The shapefile is created, along with all the necessary files, meaning no errors. But the shapefile, when opened in QGIS has no features. Just a constructed attribute table.
Pls assist…
P. S. Any Other solution besides OGR usage also works, as long as it can be coded in the Python console of QGIS
Best Answer
Some remarks because you mix many things ( if you want yo use your script outside the console of QGIS, install the Python module GDAL (osgeo) in your Python installation: it is installed in the Python version of QGIS if you are on Windows).
1) There are no Shapely geometries in your script, only
QgsPoint(x_min, y_min)
, ...linelyr = ogr.Geometry(ogr.wkbLineString)
, ...2)
out_line
is unnecessary becauselinelyr
is already an ogr LineString (and your creation ofout_line
gives invalid geometries (empty LineStrings = LineString(LineString))3) In the same way, you don't need
geom = ogr.CreateGeometryFromWkb(out_line.wkb)
because as said,out_line
orlinelyr
are already ogr LineStrings.3) At the end of the process, with ogr, if you not close the resulting shapefile, it remains empty.
So:
If you want to use Shapely:
If you want to use PyQGIS: