1

I'm attempting to export the coordinates of the vertices of a vector layer in QGIS 2.8.9-Wien. I need them to be in a specific format for a simulation program to load them. The documentation for this simulation program says the following.

"The geometry file format is adopted from the “ungenerate” command of the ArcGIS geographic information system (other GIS packages can also use this format). “Ungenerate” exports the geometry of a polygon system as an ASCII text file."

The documentation then shows the format as so:

  1      2923.3222656      2084.7429199
  2917.0759277      2079.6772461
  2926.8261719      2089.6940918
  2929.4162598      2089.6560059
  2918.4470215      2078.3823242
  2917.0759277      2079.6772461
END
     3      2936.2717285      2087.4851074
  2929.4162598      2089.6560059
  2942.2133789      2087.4470215
  2942.0610352      2084.7048340
  2930.1779785      2087.5612793
  2929.4162598      2089.6560059
END
     4      2937.0334473      2081.5056152
  2942.0610352      2084.7048340
  2941.4516602      2076.0590820
  2934.9006348      2073.7739258
  2930.1779785      2087.5612793
  2942.0610352      2084.7048340
END
END

It is a ID number for each shape (1,2,3,...) then the X,Y coordinates of the vertices listed in pairs and finally "END" after all are listed.

Currently I can use the MMQGIS plugin to export a similar format which I reformat in excel. I want to automate the process using the graphical modeler so I'm wondering a) is there a way to export from QGIS to get this format directly and b) is that method available in the graphical modeler.

0

I ended up writing my own plugin to do it. Here's that part that does the actual work:

    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
        layer_list.append(layer.name())
    self.dlg.comboBox.addItems(layer_list)

    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # get the layer that is selected form ther drop down menu
        selectedLayerIndex = self.dlg.comboBox.currentIndex()
        layer = layers[selectedLayerIndex]
        # get ther opuput file name form ther box
        fileName = self.dlg.lineEdit.text()
        outputFile = open(fileName, 'w')
        # make a blank string called coords
        coords = ''
        # get ther list of features form ther layer
        features = layer.getFeatures()

        # itterate over the features
        for feature in features:
            geom = feature.geometry()
            # get the vertices
            pt = geom.asPolygon()

            # check if the list is populated
            check = 1
            try:
                trash = pt[0]
            except IndexError:
                check = 0

            if check:
                #get ther number of vertices
                length = len(pt[0])
                # get ther feature id
                number = feature.id()
                # write the feature ID
                coords = coords + str(number) 
                #write out the coordinated
                for i in range(0, (length-1)):
                    coords = coords + "\t" + str(pt[0][i].x())+ "\t" + str(pt[0][i].y()) + "\n"
                coords = coords + "END\n"
        coords = coords + "END\n"
        #write to the file
        unicodeLine = coords.encode('utf-8')
        outputFile.write(unicodeLine)
        outputFile.close()

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.