1

I need to load geometry from csv and then use it to calculate hub distance. I write the following script, but it doesn't work. What am I doing wrong?

 sys.path.append('/usr/share/qgis/python/plugins/')
 from processing.core.Processing import Processing
 from processing.tools import *
 qgs = QgsApplication([], True)

 QgsApplication.setPrefixPath("/usr", True)

 Processing.initialize()
 QgsApplication.initQgis()
 layer = QgsVectorLayer('from_excel.csv', 'from_excel', "delimitedtext")

 layer.setCrs(QgsCoordinateReferenceSystem(32365, QgsCoordinateReferenceSystem.EpsgCrsId))



layer2 = QgsVectorLayer('/home/raven/work/UpWork/Inputs/Cenefas.shp', 'Cenefas', 'ogr')
layer2.setCrs(QgsCoordinateReferenceSystem(32365, QgsCoordinateReferenceSystem.EpsgCrsId))

general.runalg("qgis:distancetonearesthub", layer, layer2, 'test', 0, 0, 'HubOutput')

QgsApplication.exitQgis()

Then I've bunch or errors:

  1. Warning: Not all input layers use the same CRS.
  2. Fields of width 255 truncated to 254 'NoneType' object has no attribute 'boundingBox'
  3. Segmentation fault (core dumped)
1

I think there's a few issues:

  1. I think you need to replace qgs = QgsApplication([], True) with qgs = QApplication([], True).

  2. When loading a csv file, you need to set the relevant parameters such as the delimiter and (assuming you want to load it as a point layer) set the X Y fields. However, I found that calling this point layer into the algorithm fails as the path of the file also contains those settings. So perhaps it is best to import the csv as a layer and export this to a shapefile. Then call this shapefile into the algorithm.

  3. I don't think EPSG:32365 exists, was this a typo?


The following code works for me using EPSG:27700 (note that I tested this on QGIS 2.18.2 for Windows 7 64-bit, you can adjust the paths accordingly):

qgs = QApplication([], True)
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

sys.path.append('/usr/share/qgis/python/plugins/')
from processing.core.Processing import Processing
from processing.tools import *
Processing.initialize()

uri = '/home/raven/work/UpWork/Inputs/from_excel.csv?delimiter=,&crs=epsg:27700&xField=x&yField=y'
point_layer = QgsVectorLayer(uri, 'from_excel', 'delimitedtext')
point_path = '/home/raven/work/UpWork/Inputs/point.shp'
QgsVectorFileWriter.writeAsVectorFormat(point_layer, point_path, "utf-8", None, "ESRI Shapefile")

layer = QgsVectorLayer(point_path, 'point', 'ogr')

layer2 = QgsVectorLayer('/home/raven/work/UpWork/Inputs/Cenefas.shp', 'Cenefas', 'ogr')
layer2.setCrs(QgsCoordinateReferenceSystem(27700, QgsCoordinateReferenceSystem.EpsgCrsId))

general.runalg("qgis:distancetonearesthub", layer, layer2, 'test', 0, 0, 'HubOutput')

QgsApplication.exitQgis()
qgs.exit()
| improve this answer | |
  • Joseph, thank your for answer, but anyway I've bunch of errors: Warning: Not all input layers use the same CRS. This can cause unexpected results. Warning 1: Field HubName of width 255 truncated to 254. float division by zero See log for more details Segmentation fault (core dumped) – Александр Захаров May 3 '17 at 16:24
  • @АлександрЗахаров - If you want to perform spatial analysis, the layers should already be reprojected to the correct CRS. Setting the CRS as you did in your code will not reproject the data which probably explains why you keep getting the CRS warning. Either load the layer in QGIS and use the Save As... option to save it as a new shapefile with the new CRS or use the general.runalg("qgis:reprojectlayer") algorithm in your code. The field HubName warning can be ignored, not sure about the segmentation fault. Try commenting out QgsApplication.exitQgis() and qgs.exit(). – Joseph May 4 '17 at 9:05

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.