1

I'm loading several shapefiles into QGIS using the Python console. The script works by reading a list of directories and a list of shapefiles to load from each directory, then it loads each file. Like this:

encList = "/foo/encList.txt"
shapeList = "/foo/shpList.txt"
path = "/bar/"
with open(encList) as encs:
    for enc in encs:
            with open(shapeList) as shps:
                    for shp in shps:
                            file=path+str(enc).rstrip()+'/'+str(enc).rstrip()+str(shp).rstrip()+".shp"
                            print file
                            qgis.utils.iface.addVectorLayer(file, str(enc).rstrip()+str(shp).rstrip(), 'ogr' )

                            #Use loaded layer as active layer to set the CRS
                            myLayer = qgis.utils.iface.activeLayer()
                            myLayer.setCrs(QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId))

I would like to also configure the shapefile layers as I go along. As you would if you right click on a layer and select properties.

Specifically, I would like to change the line colours and label display. In my head I'd need something like this pseudo-code added into the loop:

if shapefile == line:
   then shapefile.setColor(black)
if shapefile == points:
   then shapefile.setStyle(pointDisplacement)
   ans shapefile.setStyle.lable('nameOfPoint')

Or something like that. I have figured out how to do the conditions, I just need to understand how to change the attributes. Does anybody know of a way or a tutorial?

(I'm new to QGIS, but know my way around Python)

nmtoken
  • 13,355
  • 5
  • 38
  • 87
FredFury
  • 1,097
  • 2
  • 10
  • 19

1 Answers1

1

So after some tinkering and reading up on the most helpful comments I've managed do solve this, mostly with the help of this question (thanks arMoraer) and reading some of the reference material.

I would recommend reading this tutorial (again thanks arMoraer) which gives a little bit of information into the data structure of layers.

Here is a copy of the completed "loader" code with the conditional colour changes:

encList = "/foo/encList.txt"
shapeList = "/foo/shpList.txt"
path = "/bar/"
with open(encList) as encs:
    for enc in encs:
            with open(shapeList) as shps:
                    for shp in shps:
                            file=path+str(enc).rstrip()+'/'+str(enc).rstrip()+str(shp).rstrip()+".shp"
                            print file
                            qgis.utils.iface.addVectorLayer(file, str(enc).rstrip()+str(shp).rstrip(), 'ogr' )

                            #Use loaded layer as active layer and set the CRS
                            layer = qgis.utils.iface.activeLayer()
                            layer.setCrs(QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId))

                            # If the shape is designated as a line type
                            if(str(shp).rstrip()[-1:] == "L"):
                                symbols = layer.rendererV2().symbols()
                                symbol = symbols[0]
                                symbol.setColor(QtGui.QColor.fromRgb(0,0,0))

                            # If the shape is designated as a point type
                            if(str(shp).rstrip()[-1:] == "P"):
                                symbols = layer.rendererV2().symbols()
                                symbol = symbols[0]
                                symbol.setColor(QtGui.QColor.fromRgb(255, 77, 77))

                            qgis.utils.iface.mapCanvas().refresh() 
                            qgis.utils.iface.legendInterface().refreshLayerSymbology(layer)

Maybe this will help out a fellow newbie who got waist deep in QGIS as quickly as I did. Thank you to all of those who added comments and guidance.

FredFury
  • 1,097
  • 2
  • 10
  • 19