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.
Multiparts to Singleparts
tool followed by theDissolve
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