1

I have a python program which can extract all the nodes of any polygon to a text file.

import ogr, os, sys
import xlrd
control_file = "W:/04 Tools/07 MapMaker/Control File_Shapefile to Text file.xls"
workbook = xlrd.open_workbook(control_file)
sheet = workbook.sheet_by_index(0)
sheet.nrows
sheet.ncols
r = 1
for row in range(r,sheet.nrows):
    input_folder = sheet.cell_value(row,0)
    input_shapefile = sheet.cell_value(row,1)
    output_folder = sheet.cell_value(row,2)
    output_textfile = sheet.cell_value(row,3)
    fn = str(input_folder) + str(input_shapefile)
    outfile = str(output_folder) + str(output_textfile)
    driver = ogr.GetDriverByName('ESRI Shapefile')
    ds = ogr.Open(fn) 
    if not ds: 
        raise IOError("Could not open '%s'"%fn)
    numLayers = ds.GetLayerCount()
    print 'The file contains ', numLayers, ' Layers'
    layer = ds.GetLayer(0)
    print layer.GetName(), ' contains ', layer.GetFeatureCount(), ' features'
    f=layer.GetFeatureCount()
    print("Number of features = " + str(f))
    v=0
    s=1
    file = open(outfile,'w')
    file.write('\n' 'Sl No' '\t' 'X' '\t' 'Y' '\t' 'Z' '\t' 'Polygon ID' '\n')
    for v in range(f):
        print("\n\nStarting the process of extracting nodes for the feature " + str(v))
        feature= layer.GetFeature(v)
        geometry= feature.GetGeometryRef()
        print ' Feature contains the Geometry', geometry.GetGeometryName()
        print ' It contains', geometry.GetGeometryCount(), geometry.GetGeometryName()
        for g in range(geometry.GetGeometryCount()):
            ring = geometry.GetGeometryRef(0)
            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
        print("Completed the process of extracting nodes for the feature " + str(v) + "\n\n")

This python code will take the input shapefile (folder details) and output text file details from an excel file. The details (X, Y, Z, Polygon ID) of all the nodes for all the polygons will be written in the text file.

The file contains  1  Layers
AUS_adm1  contains  2  features
Number of features = 2


Starting the process of extracting nodes for the feature 0
Feature contains the Geometry MULTIPOLYGON
 It contains 7 MULTIPOLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
MULTIPOLYGON  contains the Geometry POLYGON
 It contains 0  points in a  POLYGON
Completed the process of extracting nodes for the feature 0

Starting the process of extracting nodes for the feature 1
Feature contains the Geometry POLYGON
 It contains 1 POLYGON
POLYGON  contains the Geometry LINEARRING
 It contains 3274  points in a  LINEARRING
Completed the process of extracting nodes for the feature 1

Unfortuantely, I am only able to get the output of feature 1 in the text file. I am not getting the output of feature 0 in the text file.

I think that the actual issue is that this python program is executing perfectly for Geometry POLYGON and it is not working for Geometry MULTIPOLYGON (for Geometry MULTIPOLYGON, the output is 'contains 0 points in a POLYGON').

I have tested this python program for other features with Geometry MULTIPOLYGON and it is not working. But for features with Geometry POLYGON and Geometry LINEARRING, its working fine.

How can I solve this issue for features with Geometry MULTIPOLYGON ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.