1

I have a building Layer(Full of Polygons).I want to get centroids of polygons and save them as point layer.I am using python console within Qgis 3.0. I have tried below code.I got my Centroid Layer(but only for some polygon,not ALL).But I want the centroid for all Polygons.

vectorLyr=QgsVectorLayer("C:/Test/demand/Demand.shp", "BuildingLayer", "ogr")
vectorLyr.isValid()
import processing
processing.run("native:centroids", {'INPUT':'C:/Test/demand/Demand.shp','ALL_PARTS':False,'OUTPUT':'C:/Test/demand/centroid.shp'})
#{'OUTPUT': <qgis._core.QgsVectorLayer object at 0x000000000EF9C438>}

I am getting below error

traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.0\apps\Python36\lib\code.py", line 91, in   runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "C:/PROGRA~1/QGIS3~1.0/apps/qgis/./python/plugins\processing\tools\general.py", line 84, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
File "C:/PROGRA~1/QGIS3~1.0/apps/qgis/./python/plugins\processing\core\Processing.py", line 190, in runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: There were errors executing the algorithm.

I have added visualization of my output centroid for some polygon,NOT FOR ALL

  • Try to check validity of you polygons (vector -> geometry tools), maybe these polygons have invalid geometry? – Jane Sep 18 '18 at 7:18
  • What do you mean by Invalid Geometry ? – ps1 Sep 18 '18 at 8:44
  • @Jane I checked some of them are wrong , but still for majority of them its not showing centroid – ps1 Sep 18 '18 at 8:57
  • use v.clean from processing toolbox to clean topology. Zoom in to one without centroid and look through their vertices to understand what else with geometry of the polygon. - maybe duplicate vertices, self-intersections, etc. QGIS centroid algorithm is very sensitive to initial geometry – Jane Sep 18 '18 at 12:05
  • @Jane I even tried that , but still not working. This is dropbox link please can you check . I can't proceed dropbox.com/sh/0kj4z0txefynfdf/AACfGXZvi-Mz3PAH-TxJ0ouFa?dl=0 – ps1 Sep 18 '18 at 16:29

Your Answer

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

Browse other questions tagged or ask your own question.