2

Can we create a buffer of distance 0.004 using python in qgis.

I tried Buffering in pyQGIS?, but it isn't working for python in qgis 2.10.1-Pisa.

enter image description here


The above is the diagram of buffer I am asking. It's located in the vector->geoprocessing tools->Buffer(s)
Any of you could help me out with the equivalent python code for the above Buffer in QGIS.
code:

from qgis.utils import iface
from qgis.analysis import QgsGeometryAnalyzer 
QgsApplication.initQgis()
uri = QgsDataSourceURI()
uri.setConnection("localhost","5432","gis_data","postgres","password")
uri.setDataSource("schema_name","table_name","wkt","")
vlayer = QgsVectorLayer(uri.uri(),"LayerName1","postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
mc = iface.mapCanvas() 
layer = mc.currentLayer()
QgsGeometryAnalyzer().buffer(layer, "file:///C:/Users/***/Downloads/aa.shp", 0.0004, False, False, -1)

Everything works fine till the LayerName1 loads and getting errot at Buffer statement the last line of code error:
Feature geometry not imported (OGR error: Pointer 'hGeom' is NULL in 'OGR_G_ImportFromWkb'. )

3
  • 1
    that error message suggests it's expecting WKB, not WKT. Is your 'wkt' field a text field with WKT, or abyte (as used by WKB)? I got your code running fine on Pisa 2.10.1 (Ubuntu) if the wkt field was of type text (created with st_asewkt(geom)), although the last line had no effect (probably because I don't have a primary key on the table I tried it on, so it wouldn't let me update the layer). Also, might want to check no NULL values in your wkt column.
    – Steven Kay
    Aug 24, 2015 at 23:41
  • My wkt is string in the format "MULTILINESTRING(( -122.02505000000001 37.338190000000004, -122.02502000000001 37.33766))". wkt is in text format and it has a primary key(i tried without a primary key too) and none of the wkt are NULL but still I am getting the same error Aug 25, 2015 at 17:04
  • might have been because the shapefile used wkb, but your db was using wkt - it was trying to update a text field in db from a binary field from shapefile. Good to know!
    – Steven Kay
    Aug 25, 2015 at 17:53

0

Browse other questions tagged or ask your own question.