2

I have already discovered this class, but I am confused. If I am doing a analysis on the current layer, I don't know why it is not displaying on the map.

lyr =iface.mapCanvas()
layer = lyr.currentLayer()
QgsGeometryAnalyzer().centroids(layer,'new route\new shape.shp',False)

I haven't got any errors, it just does nothing. When I execute the code in the QGIS Text Editor, I click on "Run" and executes the file. Nothing appears on the map and I don't see any new shapefile in the folder written.

1 Answer 1

2

Because you need to load manually the layer on the Map View of QGIS. For example, in my case of the next shapefile:

enter image description here

I ran your script (slightly modified to be tested on my system):

from qgis.analysis import QgsGeometryAnalyzer
lyr =iface.mapCanvas()
layer = lyr.currentLayer()
QgsGeometryAnalyzer().centroids(layer,'/home/zeito/pyqgis_data/centroid_pol2.shp', False) 

and when I loaded the 'centroid_pol2' shapefile (at the specified path) it could be observed:

enter image description here

5
  • Hello xunilk, thank you for your answer. I have been watching this question too. My problem, as well, is that in the folder appears nothing. It is a system's folder, not external. I refresh it and it doesn't have anything
    – user64823
    Apr 15, 2016 at 8:39
  • The issue it is probably the 'path'. Above syntax is for a Linux system. Adapt for Windows if it is your case. For example: C:/pyqgis_data/centroid_pol2.shp
    – xunilk
    Apr 15, 2016 at 8:44
  • Yes, I have been trying in Windows, and set all the paths correctly. Well... this is amazing, I have been trying in "D:\..." disk, it didn't worked. And now, I have created a new folder in "C:\..." Finally it worked. Thank you very much. But now, I am confused, why does it work in one path but not in the other one?
    – user64823
    Apr 15, 2016 at 8:49
  • The correct path is D:/... or D:\\...
    – xunilk
    Apr 15, 2016 at 8:52
  • Can't attach an image, but well, I have been using "/" or "\\" for a long time, until I discovered that, in QGIS Essen, there isn't any problem (I load layers, typically using QgsVectorLayer, and QgsMapLayerRegistry, QgsDataProvider...) writing "\". 'C:\folder\my_shp.shp'
    – user64823
    Apr 15, 2016 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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