4

I want to save all the x, y, z coordinates of all the nodes (vertices) of a polygon shapefile to a text file using Python programming in QGIS Python Console.

import ogr, os, sys
fn = r'E:\Sreeraj\Thailand\Level 0\Thailand.shp'
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = ogr.Open(fn) 
numLayers = ds.GetLayerCount()
print 'The file contains ', numLayers, ' Layers'
layer = ds.GetLayer(0)
print layer.GetName(), ' contains ', layer.GetFeatureCount(), ' features'
f=layer.GetFeatureCount()
v=0
s=1
file = open(r'E:\Sreeraj\Thailand\Thailand_Level 0.txt','w')
file.write('Sl No' '\t' 'X' '\t' 'Y' '\t' 'Z' '\t' 'Polygon ID' '\n')
for v in range(f):
    feature= layer.GetFeature(v)
    geometry= feature.GetGeometryRef()
    print ' Feature contains the Geometry', geometry.GetGeometryName()
    print ' It contains', geometry.GetGeometryCount(), geometry.GetGeometryName()
    ring = geometry.GetGeometryRef(r)
    print geometry.GetGeometryName(), ' contains the Geometry', ring.GetGeometryName()
    print ' It contains', ring.GetPointCount(), ' points in a ', ring.GetGeometryName()
    pointsX = []; pointsY = []; pointsZ = []
    numpoints = ring.GetPointCount()
    for p in range(numpoints):
        lon, lat, z = ring.GetPoint(p)
        pointsX.append(lon)
        pointsY.append(lat)
        pointsZ.append(z)
        x = [str(s), '\t', str(lon), '\t', str(lat), '\t', str(z), '\t', str(v), '\n']
        file.writelines(x)
        s=s+1

So, I have given above the complete python code. This is perfectly working for extracting the nodes / vertices of Thailand states, districts, etc.

But, when I have a shapefile (here, Thailand.shp) with only 1 single polygon, that is the country boundary; then this code is not working. I am not getting any error, but I am also not getting the output x, y, z coordinates of each node of this particular country boundary shapefile which is just a single polygon.

Given below is the output which I am getting.

The file contains  1  Layers
Thailand  contains  1  features
Feature contains the Geometry MULTIPOLYGON
It contains 595 MULTIPOLYGON
MULTIPOLYGON  contains the Geometry POLYGON
It contains 0  points in a  POLYGON

And I am getting the text file output which is empty.

This above python code is not working only in the case of country boundary shapefile (single polygon). If I take a shapefile with all the states, districts of Thailand; then the above code is working.

When I tried this same code for the states shapefile, I got the given below output and all the x,y,z values of all the nodes were saved in the output text file.

The file contains  1  Layers
THA_Adm1_GISTA_plyg_v5  contains  77  features
Feature contains the Geometry MULTIPOLYGON
It contains 2 MULTIPOLYGON
MULTIPOLYGON  contains the Geometry POLYGON
It contains 0  points in a  POLYGON
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 25015  points in a  LINEARRING
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 16116  points in a  LINEARRING
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 41120  points in a  LINEARRING
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 33636  points in a  LINEARRING
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 16774  points in a  LINEARRING
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 19105  points in a  LINEARRING
Feature contains the Geometry MULTIPOLYGON
It contains 155 MULTIPOLYGON
MULTIPOLYGON  contains the Geometry POLYGON
It contains 0  points in a  POLYGON
Feature contains the Geometry POLYGON
It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
It contains 8501  points in a  LINEARRING
..........................................................
// and many lines of outputs like this. This is correct. 

But for the boundary shapefile (single polygon), I am not getting output.

  • 1
    @Marco I have updated my question with the output of the states shapefile. Please go through that. The solution to this issue may be something related to the polygon geometry. – Sreeraj Jan 29 '18 at 14:44
  • 2
    @Sreeraj - Your boundary shapefile seems to be a multipolygon rather than a polygon. Try using the Multiparts to Singleparts tool followed by the Dissolve tool. This should ensure your boundary shapefile only contains a single polygon and see how your code runs. – Joseph Jan 29 '18 at 14:50
  • 1
    @Joseph It didn't work. I got the same output as I mentioned in the question, that is "The file contains 1 Layers, Thailand_DissolveSingleparts contains 1 features, Feature contains the Geometry MULTIPOLYGON, It contains 595 MULTIPOLYGON, MULTIPOLYGON contains the Geometry POLYGON, It contains 0 points in a POLYGON". I used the Multiparts to Singleparts tool followed by the Dissolve tool; and then I executed my code. Still, I haven't got the output. – Sreeraj Jan 30 '18 at 6:13
  • 2
    @Joseph When I tried with the output shapefile of Multiparts to Singleparts tool (without further doing Dissolve tool), it worked. I got the necessary output. Your idea was correct. Thank You. – Sreeraj Jan 30 '18 at 10:41
  • 1
    @Joseph Yes; when I executed this code for the Multiparts to Singleparts tool output with 595 features. Unfortunately, the code doesn't work if there is only one feature (that is with the dissolved output shapefile). I don't know the reason for this !! – Sreeraj Jan 30 '18 at 11:33
1

The solution to this issue is to use the Multiparts to Singleparts tool in order to convert the curent multipolygon boundary shapefile to a polygon shapefile. Then take this output shapefile of the country boundary as the input to the python program. Then, the python program will get executed properly.

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.