2

I am working on a script that imports a .csv layer that has lat long coordinates, which I then want to buffer in feet.
I've created a vector layer from the .csv file and tried to set the coordinate system of the vector layer to a local system with units in feet, but when I buffer that layer I don't get the expected output - it looks like its buffering in degrees. Is there a way to specify buffer units? I understand the units are determine by the input layer which I have tried to set to the local system.
What am I doing wrong?

from qgis.core import *
from qgis.analysis import *
from PyQt4.QtCore import *
from PyQt4.QtGui import QInputDialog
from qgis.utils import iface
import os

csvFile = QInputDialog.getText(None,'Enter .csv path','Enter filepath for csv:')

s = QSettings()
oldValidation = s.value("/Projections/defaultBehaviour")
s.setValue("/Projections/defaultBehaviour", "useGlobal" )

InFlnm= os.path.split(csvFile[0])[1]
InDrPth= os.path.split(csvFile[0])[0].replace('\\','/')
InFlPth="file:///"+InDrPth+'/'+InFlnm

csvImport = InFlPth+"?delimiter=%s&xField=%s&yField=%s" % (",", "Longitud", "Latitud")
shp = QgsVectorLayer(csvImport, InFlnm,"delimitedtext")
shp.setCrs(QgsCoordinateReferenceSystem(2239, QgsCoordinateReferenceSystem.EpsgCrsId))

s.setValue("/Projections/defaultBehaviour", oldValidation)

buffFile = QInputDialog.getText(None,'Enter output filepath for buffer','Enter filepath for buffer:')
buffNm = os.path.split(buffFile[0])[1]
buffDir = os.path.split(buffFile[0])[0].replace('\\','/')

QgsMapLayerRegistry.instance().addMapLayer(shp)
mc = iface.mapCanvas() 
layer = mc.currentLayer()

QgsGeometryAnalyzer().buffer(layer, buffDir+'/'+buffNm, 60, False, False, -1)
2

I realized I needed to set the coordinate system of shp to WGS84, then write out to a new file and specify the local coordinate system - then add that layer to the map and buffer it. Worked! Here is my updated code:

from qgis.core import *
from qgis.analysis import *
from PyQt4.QtCore import *
from PyQt4.QtGui import QInputDialog
from qgis.utils import iface
import os
csvFile = QInputDialog.getText(None,'Enter .csv path','Enter filepath for csv:')

s = QSettings()
oldValidation = s.value("/Projections/defaultBehaviour")
s.setValue("/Projections/defaultBehaviour", "useGlobal" )

InFlnm= os.path.split(csvFile[0])[1]
InDrPth= os.path.split(csvFile[0])[0].replace('\\','/')
InFlPth="file:///"+InDrPth+'/'+InFlnm

csvImport = InFlPth+"?delimiter=%s&xField=%s&yField=%s" % (",", "Longitud", "Latitud")
shp = QgsVectorLayer(csvImport, InFlnm,"delimitedtext")
shp.setCrs(QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId))

s.setValue("/Projections/defaultBehaviour", oldValidation)

buffFile = QInputDialog.getText(None,'Enter output filepath for buffer','Enter filepath for buffer:')
buffNm = os.path.split(buffFile[0])[1]
buffDir = os.path.split(buffFile[0])[0].replace('\\','/')

outPoints = 'c:/Temp/qgis/GPS_locations.shp'
QgsVectorFileWriter.writeAsVectorFormat(shp, outPoints, "CP1250", sr, "ESRI Shapefile")
shpFt = QgsVectorLayer(outPoints, 'GPS_locations',"ogr")

QgsMapLayerRegistry.instance().addMapLayer(shpFt)
mc = iface.mapCanvas() 
layer = mc.currentLayer()

QgsGeometryAnalyzer().buffer(layer, buffDir+'/'+buffNm, 60, False, False, -1)
  • I know it is outdated now, but someone may need it as I did: above didn't work for me, and instead I first reprojected the geometry to some meter-based CRS, then did buffering and then reprojected back again to WGS-84 and after then I added the feature to the layer. – adamczi Sep 19 '16 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.