1

I have the following code:

def moveFiles(self):
    mask = self.getBoundingBox() #BoundingBox of polygon layer
    inputDir = self.dlg.input_folder.text() #directory of xyz files - "Users/denni/Desktop/xyz_data"
    outDir = self.dlg.output_folder.text()  #output directory - "C:/Users/denni/Desktop/output/"
    os.chdir(inputDir)
    crs = "EPSG:25832"

    for file in glob.glob("*.xyz"):
        uri = "file:///" + inputDir +"/"+ file + "?type=csv&delimiter=%s&crs=%s&xField=%s&yField=%s" % (" ", crs, "field_1", "field_2")
        name = file.replace('.xyz', '')
        lyr = QgsVectorLayer(uri, name, "delimitedtext")
        outputPath = outDir + file

        if mask.xMinimum() <= lyr.xMinimum() <= mask.xMaximum() and mask.yMinimum() <= lyr.yMinimum() <= mask.yMaximum():
            QgsVectorFileWriter.writeAsVectorFormat(lyr, outputPath, 'utf-8', lyr.crs(), "CSV", layerOptions=['GEOMETRY=AS_XYZ'])
        if mask.xMinimum() <= lyr.xMaximum() <= mask.xMaximum() and mask.yMinimum() <= lyr.yMinimum() <= mask.yMaximum():
            QgsVectorFileWriter.writeAsVectorFormat(lyr, outputPath, 'utf-8', lyr.crs(), "CSV", layerOptions=['GEOMETRY=AS_XYZ'])
        if mask.xMinimum() <= lyr.xMaximum() <= mask.xMaximum() and mask.yMinimum() <= lyr.yMaximum() <= mask.yMaximum():
            QgsVectorFileWriter.writeAsVectorFormat(lyr, outputPath, 'utf-8', lyr.crs(), "CSV", layerOptions=['GEOMETRY=AS_XYZ'])
        if mask.xMinimum() <= lyr.xMinimum() <= mask.xMaximum() and mask.yMinimum() <= lyr.yMaximum() <= mask.yMaximum():
            QgsVectorFileWriter.writeAsVectorFormat(lyr, outputPath, 'utf-8', lyr.crs(), "CSV", layerOptions=['GEOMETRY=AS_XYZ'])

and the following error message:

'QgsVectorLayer' object has no attribute 'xMinimum'

In the first line, I get the polygon.boundingBoxOfSelected() of a polygon. boundingBoxOfSelected() returns a QgsRectangle according to the documentation . And according to this documentation, I should be able to access the functions xMinimum() and xMaximum().

Thus, I don't understand the error message. What am I doing wrong?

  • 2
    Rectangle indeed has xMinimum, but you compare with a layer object. – Andreas Müller Feb 26 at 20:38
  • yes you are right! lyr.xMinimum() doesn't work becaue lyr is not a QgsRectangle. 2nd Problem was that I needed to use elif instead of if If you post an answer I can accept it. – DGIS Feb 26 at 21:12
3

The error is about lyr.xMinimum(), not mask.xMinimum. QgsVectorLayer has not xMinimum method as the error states. But extent method of QgsVectorLayer object gives you a QgsRectangle object. Then you can use xMinimum method using that object.

Use like this

extent = lyr.extent()
if mask.xMinimum() <= extent.xMinimum() <= mask.xMaximum() ....
| improve this answer | |

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.