1

I have written a Python script for QGIS 2.14.3 which reads in every .txt file in a folder containing hundreds of .txt files (format: "X Y Z" whitespace-separated)and converts them to ESRI shapefiles.

As a next step I want to generate contour lines for every shapefile. Therefore I have installed the Contour plugin (V.1.4.2) from https://github.com/ccrook/QGIS-Contour-Plugin

How can I call the Plugin from my script?

Can I somehow import my plugin folder to make my installed plugins accessible?

When I look into /homefolder/.qgis2/python/plugins/contour there are five .py files...

Of course I could do the whole workflow by hand with the GUI, but that would take too long.

If somebody needs the script mentioned above (which converts the .txt files to shapefiles), here is the code:

# Defines Input parameters which are displayed in the GUI
##Input_Folder=folder                                               
##Unbuffered_PolygonLayer=vector
##output_Folder=folder

import os
from qgis.core import *
from PyQt4.QtCore import *

indir = Input_Folder
outdir = output_Folder

for dirpath, dirnames, filenames in os.walk(indir):
    for file in filenames:
        if file.endswith('.txt'):
            fullname = os.path.join(dirpath, file).replace('\\', '/')
            filename = os.path.splitext(os.path.basename(fullname))[0]
            #type=(csv|regexp|whitespace|plain) , skipLines=n, useHeader=(yes|no), trimFields=(yes|no), skipEmptyFields=(yes|no), decimalPoint=c, xField=column yField=column [ If the useHeader is no (ie there are no column names), then this is the column number (with the first column as 1).]
            # crs=crsstring, spatialIndex=(yes|no)
            # see: http://qgis.org/api/2.14/classQgsVectorLayer.html

            uri = ( 'file:///%s?type=%s&skipLines=%d&useHeader=%s&trimFields=%s&skipEmptyFields=%s&decimalPoint=%s&xField=%s&yField=%s&crs=%s&spatialIndex=%s' % (fullname,   "whitespace", 0, "Yes", "Yes", "Yes", ".", 1, 2,  "EPSG:31468", "Yes"))
            layer = QgsVectorLayer(uri, "LayerName", "delimitedtext")

        if layer.isValid():
            print "Loaded Layer %s is valid..."%(fullname)

            #QgsMapLayerRegistry.instance().addMapLayer(layer)
            #QgsVectorFileWriter.writeAsVectorFormat(layer, outdir + '/' + filename + '.shp', 'CP1250', None, 'ESRI Shapefile')

        else:
            print "Failed to load Layer %s" % (fullname)
1

Import the python files at the start of your code

import file
import file2

That should give you everything from the files. Then I think you can call functions by using,

file.function

Another solution would be to use

from file import function

Then you could just use,

function

to call the function.

There's more information on this sort of thing on StackOverflow.

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.