someone_2014

go back in an array - python code

Discussion created by someone_2014 on Feb 10, 2014
Dear all,

I am working on a python code which gives back the vertices of all polygones within a shape file. Maybe you know the code which already exists in the internet. I have to change it a bit because, it should also write the number of vertices in a line above the vertices. Actually I think my problem is quite simple, however I am not very familiar with programming. So I am going to show you the problem.

First of all I want to give you an expression of what the output should be:

First polygone with of vertices: n #exterior ring
P1
.
.
Pn
headline with number of vertices of interior ring: m # pf the first polygon
P1
.
.
Pm


However, my output file looks as the following:
First polygone with of vertices: n #exterior ring
P1
.
.
Pn
Second polygone with of vertices: n #exterior ring
P1
.
.
Pn


This is the code so far...

 # step through each vertex in the feature
                while pnt:

                    outLine = str(round((pnt.x), 1)) + " " + str(round((pnt.y), 1)) + "\n"   # str(pnt_count)
                    if sepchar == "": outFile.write(outLine)
                    else: outFile.write(outLine.replace(".", sepchar))
                    pnt = part.next()
                    pnt_count += 1

                    #If pnt is null, either the part is finished or there is an interior ring.                     
                    if not pnt:
                        pnt = part.next()

                        if pnt:
                            pnt_zaehler = 0

                            while pnt:
                                pnt = part.next()
                                pnt_zaehler += 1
                            pnt_zaehler_neu = pnt_zaehler


                            outFile.write("interiorRing " + str(rechts) + " " + str(links) + " " + str(vertex) + " pnt_zahler:" + 
                                             str (pnt_zaehler) + "\n")

                partnum += 1


After writing the outFile ("interiorRing " etc.) I want to go back in the array and want to put out the vertices of the interior ring.

As I understand a polygone with interior rings is build up as follow. [[pnt, pnt, pnt], , [pnt, pnt,pnt]]. A space is the end of the exterior ring and the start of the interior ring.
In my code above I put a "pnt_zaehler_neu" to count the vertices of the interior ring. This is working. But as it is a while-loop I am going already to the end of the polygon.
Therefore, I need to get back to the last beginning of the interior ring that I can put out the vertices again. with an while-loop which I want to put before the row outFile.write ....

I hope that my question is cleare and hope that you can give me some help. Thank you very much!

Outcomes