2

I'm trying to export tables from a PostGIS database into MapInfo TAB files, but only certain rows and columns. I have my rows exporting right, using a query, but I'm stuck on just getting the columns I'm interested in. I thought the

request.setSubsetOfAttributes(["obj","name","railway"],vlayer.pendingFields())

below would do the trick, but I'm still getting all of the original fields in my output.

    if table == "line_table":
        fields = vlayer.pendingFields()

        railway = ["Railway", r""""railway" != ''"""]
        request = QgsFeatureRequest()
        request.setFilterExpression(railway[1])
                   request.setSubsetOfAttributes(["obj","name","railway"],vlayer.pendingFields())

        selectedFeatures = vlayer.getFeatures(request)

        selectedRows = []  
        for feature in selectedFeatures:
           selectedRows.append(feature.id())

        vlayer.setSelectedFeatures(selectedRows)

        output = outputBase + "/" + railway[0] + "_" + database + ".tab"

        QgsVectorFileWriter.writeAsVectorFormat(vlayer, output, encoding, coordsys, "MapInfo File", True)
        print "Translation of " + railway[0] + "_" + database + ".tab successful."
1

Using comment by @LuigiPirelli, I did end up getting it to work by using a memory layer with just the fields I wanted, then populating those fields with the values in the original table.

    if table == "line_table":
        fields = vlayer.pendingFields()

        railway = ["Railway", r""""railway" != ''"""]
        request = QgsFeatureRequest()
        request.setFilterExpression(railway[1])

        rails = QgsVectorLayer("LineString?crs=epsg:3395", "rails", "memory")
        if not rails.isValid(): raise Exception("Failed to create memory layer")
        pr = rails.dataProvider()
        rails.startEditing()
        pr.addAttributes([QgsField("name", QVariant.String),QgsField("railway", QVariant.String)])

        selectedRows = []
        selectedFeatures = vlayer.getFeatures(request)

        for feature in selectedFeatures:
            selectedRows.append(feature.id())
            newFeature = QgsFeature()
            newFeature.setGeometry(feature.geometry())
            newFeature.setAttributes([feature.attribute("name"), feature.attribute("railway")])
            pr.addFeatures([newFeature])

        rails.commitChanges()
        rails.updateExtents()
        vlayer.setSelectedFeatures(selectedRows)
        rails.setSelectedFeatures(selectedRows)
        mapLayer = QgsMapLayerRegistry.instance().addMapLayer(rails)

        output = outputBase + "/" + database + "/" + railway[0] + ".tab"
        QgsVectorFileWriter.writeAsVectorFormat(mapLayer, output, encoding, coordsys, "MapInfo File", False)

        print "Translation of " + outputBase + database + "/" + railway[0] + ".tab successful."

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.