corrija os erros: # Python Console
Use iface to access QGIS API interface or type '?' for more info
Security warning: typing commands from an untrusted source can harm your computer
import os
import processing
from qgis.core import (
QgsVectorLayer,
QgsProject
)
def csv_to_shapefile(folder_path):
# Listar todos os arquivos .csv na pasta especificada
for filename in os.listdir(folder_path):
if filename.endswith(“.csv”):
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 4
if filename.endswith(“.csv”):
^
SyntaxError: invalid character '“' (U+201C)
# Construir o caminho completo do arquivo
file_path = os.path.join(folder_path, filename)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'folder_path' is not defined
# Assume que a primeira coluna é longitude (x) e a segunda coluna é latitude (y)
# Note: Você pode precisar ajustar ‘xField’ e ‘yField’ de acordo com o seu arquivo CSV
uri = f"file:///{file_path}?delimiter=,&xField=longitude&yField=latitude&crs=epsg:4326&spatialIndex=no&subsetIndex=no&watchFile=no"
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'file_path' is not defined
layer_name = os.path.splitext(filename)[0] # Remove a extensão do arquivo para usar como nome da camada
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'filename' is not defined
# Carregar o arquivo .csv como uma camada
csv_layer = QgsVectorLayer(uri, layer_name, “delimitedtext”)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
csv_layer = QgsVectorLayer(uri, layer_name, “delimitedtext”)
^
SyntaxError: invalid character '“' (U+201C)
if not csv_layer.isValid():
print(f"Erro ao carregar o arquivo {filename}“)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 2
print(f"Erro ao carregar o arquivo {filename}“)
^
SyntaxError: EOL while scanning string literal
continue
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
SyntaxError: 'continue' not properly in loop
# Adicionar a camada ao projeto QGIS
QgsProject.instance().addMapLayer(csv_layer)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'csv_layer' is not defined
# Construir o caminho do arquivo de saída Shapefile
shp_path = os.path.join(folder_path, f”{layer_name}.shp")
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
shp_path = os.path.join(folder_path, f”{layer_name}.shp")
^
SyntaxError: invalid character '”' (U+201D)
# Converter a camada para Shapefile
processing.run(“native:reprojectlayer”, {
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
processing.run(“native:reprojectlayer”, {
^
SyntaxError: invalid character '“' (U+201C)
‘INPUT’: csv_layer,
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
‘INPUT’: csv_layer,
^
SyntaxError: invalid character '‘' (U+2018)
‘TARGET_CRS’: ‘EPSG:4326’,
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
‘TARGET_CRS’: ‘EPSG:4326’,
^
SyntaxError: invalid character '‘' (U+2018)
‘OUTPUT’: shp_path
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
‘OUTPUT’: shp_path
^
SyntaxError: invalid character '‘' (U+2018)
})
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
})
^
SyntaxError: unmatched '}'
print(f"Shapefile criado: {shp_path}")
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'shp_path' is not defined
Caminho para sua pasta contendo arquivos CSV
folder_path = “E:/1_Trabalho_remoto/CBC/Ocorrencia/Categorias_Avaliadas”
Traceback (most recent call last):
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 178, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 106, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 93, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS331.3\apps\Python39\lib\codeop.py", line 143, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "", line 1
folder_path = “E:/1_Trabalho_remoto/CBC/Ocorrencia/Categorias_Avaliadas”
^
SyntaxError: invalid character '“' (U+201C)