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
1 Answer 1
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] = [], ...)
-
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 withxMinimum()
. Any ideas what the problem here is?DGIS– DGIS2020年02月26日 19:49:40 +00:00Commented Feb 26, 2020 at 19:49 -
The error is about
lyr.xMinimum()
, notmask.xMinimum
.Kadir Şahbaz– Kadir Şahbaz2020年02月26日 20:33:54 +00:00Commented Feb 26, 2020 at 20:33 -
2Use
extent = lyr.extent()
, thenif mask.xMinimum() <= extent.xMinimum()
.Kadir Şahbaz– Kadir Şahbaz2020年02月26日 20:37:03 +00:00Commented 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 worksDGIS– DGIS2020年02月26日 21:13:36 +00:00Commented Feb 26, 2020 at 21:13