Can somebody help me with loading a dxf file as a vector layer with PyQGIS?

Is QgsDataSourceURI an option? I've tried it with this code, but it didn't work.

uri = QgsDataSourceURI()
uri.setDataSource('', 'entities', 'OGR_GEOMETRY', "OGR_GEOMETRY='POINT'", 'fid')

layer = QgsVectorLayer(uri.uri(), 'prova', 'ogr')

print 'isValid:', layer.isValid()

1 Answer 1


The solution to my question is:

layer = QgsVectorLayer(fitxer, 'prova', 'ogr')

This another sample loads all geometry types:

    #Load the dfx file to get the sublayers.
    vlayer = QgsVectorLayer(dfx_file, 'layer_test', 'ogr')
    subLayers = vlayer.dataProvider().subLayers()

    #For each sublayer, diferent type of geometry, load a layer to map.
    for subLayer in subLayers:
        #Extract the geometry type
        geom_type = subLayer.split(':')[-1]
        #Set the path
        uri = "%s|layername=entities|geometrytype=%s" % (dfx_file, geom_type,)
        #Name for sub layer
        dfx_file_name = os.path.splitext(os.path.basename(dfx_file))[0]
        layer_name = "%s - %s" % (dfx_file_name,geom_type,)
        #Create layer
        sub_vlayer = QgsVectorLayer(uri, layer_name, 'ogr')
        #Add layer to map
  • This has been flagged as low quality. It might be because it just contains code. While one can compare the original code you tried and the new code which may indeed solve the problem, it would improve the answer if you could include some additional description of what you changed and why in order to get things to work.
    – Chris W
    Feb 27, 2015 at 23:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.