1

I am trying to use part of a script to add points from a delimited text file, however, when I use it the layers are not loaded on the screen and not saved in the path indicated as .shp

import os
import qgis.core

indir = '/home/leo/Área de Trabalho/sle/'
outdir = '/home/leo/Área de Trabalho/sle/'
for root, dirs, files in os.walk(indir):
    for file in files:
        if file.endswith('.txt'):
            fullname = os.path.join(root, file).replace('\\', '/')
            filename = os.path.splitext(os.path.basename(fullname))[0]
            uri = 'file:///%s?crs=%s&delimiter=%s&xField=%s&yField=%s&decimal=%s' % (fullname, 'EPSG:4326', ',', 'field2', 'field1', '.')
            layer = QgsVectorLayer(uri, 'pontos_' + str(filename), 'delimitedtext')
            QgsVectorFileWriter.writeAsVectorFormat(layer, "pontos_" + str(file) + ".shp", "utf-8", layer.crs(), "ESRI Shapefile")
            QgsProject.instance().addMapLayer(layer)

when I do:

print(layer)
<QgsMapLayer: 'pontos_caminho_partículas_3' (delimitedtext)>

In this case, I want to read several .txt files, with coordinates and add these points as shape in QGIS. I am using QGIS 3.10.

5
  • Please use translation software so that your question is posted in English.
    – Vince
    Commented Apr 3, 2020 at 3:14
  • Ty for the tip!
    – Léo
    Commented Apr 3, 2020 at 3:21
  • Please take a look at the following thread, it might answer your question.
    – GforGIS
    Commented Apr 3, 2020 at 3:34
  • Can you add a sample of the txt file as text in your question, for example top 5 rows including header
    – Bera
    Commented Apr 3, 2020 at 5:35
  • Yes, but it's not working
    – Léo
    Commented Apr 3, 2020 at 16:14

0

Your Answer

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

Browse other questions tagged or ask your own question.