0

Firstly, I load layers from gpkg (GeoPackage). Secondly, I need to create a vector layer from one loaded layer which is named stops. Second point I cant do it, because I need to mark a column, that one is x field and the other is y field and assign EPSG code.

This part of code doesnt work and I don't know if it's possible to do this way (specifically uri variable):

gpkg_name = os.path.splitext(os.path.basename(path2))[0]
path_to_gpkg = os.path.join(os.path.dirname(path2), gpkg_name + '.gpkg')
conn = ogr.Open(path_to_gpkg)

for i in conn:
    layer=iface.addVectorLayer(path_to_gpkg + "|layername=" + i.GetName(), i.GetName(), 'ogr')
    print(i.GetName())
    if i.GetName()== 'stops':
        uri = (path_to_gpkg + "|layername=" + i.GetName() + ",&xField=stop_lon" + ",&yField=stop_lat" + ",&crs=epsg:4326").format(i)
        print(uri)
        name = i.GetName()
        #os.path.splitext(os.path.basename(i))[0]
        layer = QgsVectorLayer(uri, name, 'delimitedtext')
        print(layer)
        QgsProject.instance().addMapLayer(layer)

There is a whole code:

from qgis.core import *
from osgeo import ogr
import os.path
import shutil


path2=r'C:\Users\Martin\Desktop\gpkg' #rewrite path

if not os.path.exists(path2):
    os.mkdir(path2)

files = []
    # r=root, d=directories, f = files
for r, d, f in os.walk(path2):
        for file in f:
            exten = os.path.splitext(os.path.basename(file))[1]
            if exten == '.txt':
                files.append(os.path.join(r, file))

options = QgsVectorFileWriter.SaveVectorOptions()
firstt = True
for file in files: 
    uri = 'file:///{}?delimiter=,'.format(file)
    name = os.path.splitext(os.path.basename(file))[0]
    layer = QgsVectorLayer(uri, name, 'delimitedtext')
    if firstt == True:
        options = QgsVectorFileWriter.SaveVectorOptions()
        options.driverName = 'GPKG'
        options.layerName = "_".join(layer.name().split(' '))
        error_message = QgsVectorFileWriter.writeAsVectorFormat(layer,path2,options)
        firstt=False
    else:
        options = QgsVectorFileWriter.SaveVectorOptions()
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer 
        options.driverName = 'GPKG' 
        options.layerName = "_".join(layer.name().split(' '))
        #print(options.layerName)
        error_message = QgsVectorFileWriter.writeAsVectorFormat(layer,path2,options)
        if error_message:
            print(layer.name(), error_message)

gpkg_name = os.path.splitext(os.path.basename(path2))[0]
path_to_gpkg = os.path.join(os.path.dirname(path2), gpkg_name + '.gpkg')
conn = ogr.Open(path_to_gpkg)

for i in conn:
    layer=iface.addVectorLayer(path_to_gpkg + "|layername=" + i.GetName(), i.GetName(), 'ogr')
    print(i.GetName())
    if i.GetName()== 'stops':
        uri = (path_to_gpkg + "|layername=" + i.GetName() + ",&xField=stop_lon" + ",&yField=stop_lat" + ",&crs=epsg:4326").format(i)
        print(uri)
        name = i.GetName()
        #os.path.splitext(os.path.basename(i))[0]
        layer = QgsVectorLayer(uri, name, 'delimitedtext')
        print(layer)
        QgsProject.instance().addMapLayer(layer)
1
  • 1
    what is displayed when you do it?
    – Ian Turton
    May 8 '20 at 16:13
0

There is a solution.

from qgis.core import *
from osgeo import ogr
import os.path

path2=r'C:\Users\Martin\Documents\gpkg' #rewrite path
if not os.path.exists(path2):
    os.mkdir(path2)
#print(path2) 
files = []
    # r=root, d=directories, f = files
    
for r, d, f in os.walk(path2):
        for file in f:
            exten = os.path.splitext(os.path.basename(file))[1]
            if exten == '.txt':
                files.append(os.path.join(r, file))
options = QgsVectorFileWriter.SaveVectorOptions()
firstt = True
for file in files: 
    uri = 'file:///{}?delimiter=,'.format(file)
    name = os.path.splitext(os.path.basename(file))[0]
    layer = QgsVectorLayer(uri, name, 'delimitedtext')
    if firstt == True:
        options = QgsVectorFileWriter.SaveVectorOptions()
        options.driverName = 'GPKG'
        options.layerName = "_".join(layer.name().split(' '))
        error_message = QgsVectorFileWriter.writeAsVectorFormat(layer,path2,options)
        firstt=False
    else:
        options = QgsVectorFileWriter.SaveVectorOptions()
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer 
        options.driverName = 'GPKG' 
        options.layerName = "_".join(layer.name().split(' '))
        #print(options.layerName)
        if layer.name()== 'stops':
            uri = 'file:///{}?delimiter=,&xField=stop_lon&yField=stop_lat&crs=epsg:4326'.format(file)
            layer_stops = QgsVectorLayer(uri, layer.name(), 'delimitedtext')
            error_message = QgsVectorFileWriter.writeAsVectorFormat(layer_stops,path2,options)
            gpkg_name = os.path.splitext(os.path.basename(path2))[0]
            path_to_gpkg = os.path.join(os.path.dirname(path2), gpkg_name + '.gpkg')
            path_to_stops=path_to_gpkg + "|layername=" + layer.name()
            layer_stops_to_map = QgsVectorLayer(path_to_stops, layer.name(), "ogr")
            QgsProject.instance().addMapLayer(layer_stops_to_map)
        else:
            error_message = QgsVectorFileWriter.writeAsVectorFormat(layer,path2,options)
            gpkg_name = os.path.splitext(os.path.basename(path2))[0]
            path_to_gpkg = os.path.join(os.path.dirname(path2), gpkg_name + '.gpkg')
            path_to_layer=path_to_gpkg + "|layername=" + layer.name()
            other_layer = QgsVectorLayer(path_to_layer, layer.name(), "ogr")
            QgsProject.instance().addMapLayer(other_layer) 
1
  • You answered your question and you can accept it. It would be nice for future readers if you could explain in text not just code how you fixed your issue. At the moment you have a wall of code answer, and the question isn't much better.
    – nmtoken
    Nov 9 '20 at 13:25

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.