3

I wrote a pyqgis script that should iterate over a folder with xyz files, check if the bouding box overlaps with a polygon in my QGIS project and if yes, it should copy the file into a new folder.

I don't get an error message but nothing happens either. My output Folder remains empty. I just don't see the problem. Can someone please help me find the error, I've already spent too much time on this.

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/"
 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")
 lyr.selectAll()
 lyr.boundingBoxOfSelected()
 outputPath = outDir + file
 self.iface.addVectorLayer(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')

I'm using QGIS 3.4

asked Feb 26, 2020 at 16:32

1 Answer 1

5

Type of layerOptions should be list of strings, not just string. Try to change layerOptions='GEOMETRY=AS_XYZ' as layerOptions=['GEOMETRY=AS_XYZ'].

writeAsVectorFormat documentation:

writeAsVectorFormat(..., layerOptions: Iterable[str] = [], ...)

answered Feb 26, 2020 at 19:34
4
  • The next error message is strange again. Its telling me ''QgsVectorLayer' object has no attribute 'xMinimum" But mask = self.getBoundingBox() gets the bounding box as a QgsRectangle and according to the documentation (qgis.org/api/…) I can access the xMinimum with xMinimum(). Any ideas what the problem here is? Commented Feb 26, 2020 at 19:49
  • The error is about lyr.xMinimum(), not mask.xMinimum. Commented Feb 26, 2020 at 20:33
  • 2
    Use extent = lyr.extent(), then if mask.xMinimum() <= extent.xMinimum(). Commented Feb 26, 2020 at 20:37
  • That solved the 2nd problem, thank you very much! There was also a 3rd problem that I had to use elif instead of if. But now it finally works Commented Feb 26, 2020 at 21:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.