Changing my text to list of coordinates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hajdukzivivjecno
    New Member
    • Nov 2009
    • 5

    Changing my text to list of coordinates

    I am new user of Python. I would like to change my .shp file that I have astext and it looks something like this (using some loop):

    [('POLYGON((5576 309.155 5075170.4966,55 76299.7617 5075169.7026,55 76297.7031 5075169.445,557 6287.5204 5075168.1705,55 76286.9311 5075171.4731,55 76286.88250176 5075171.7454858 9,5576309.13147 582 5075173.0732648 6,5576309.03444 426 5075171.7454858 9,5576309.155 5075170.4966))' ,), ('POLYGON((5576 287.5204 5075168.1705,55 76297.7031 5075169.445,557 6299.7617 5075169.7026,55 76309.155 5075170.4966,55 76309.9005 5075163.301,557 6301.9807 5075162.5819,55 76300.8652 5075162.4806,55 76297.3699 5075162.1632,55 76295.8787 5075162.0278,55 76292.9409 5075161.761,557 6288.7631 5075161.3817,55 76288.2564 5075164.0455,55 76287.5204 5075168.1705))' ,)]

    to something like this (for every polygon):

    ((5576309 5075170,5576299 5075169,5576297 5075169,5576287 5075168,5576286 5075171,5576286 5075171,5576309 5075173,5576309 5075171,5576309 5075170))

    I need that for drawing in pygame.

    I have long file like that one above and I have to get something like (int int, int int, int int) for every polygon which means I have to separate all the coordinates for each polygon in that file and get (int int, int int...) for them. I don't know if I have explained that well.

    If you can help me, I would be grateful :)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Typical data structures like tuple, list, set and dict require commas separating each value. I would parse the data into a dictionary of tuples, and if the output must be in the form you requested, create an output string from the dictionary. Assuming the input contains newline characters, read the data, parse into a dictionary, and format an output string.

    The code parses the data into a dictionary. It should be simple to format the data from here.
    Code:
    # read file into a string
    s = open(file_name).read()
    # remove unwanted characters
    s = s.replace('\n', '').replace("'", "").lstrip('[(').rstrip(',)]')
    # split string on ",),", creating a list
    sList = s.split(',),')
    
    # create empty dictionary
    dd = {}
    for i, item in enumerate(sList):
        # remove unwanted characters
        item = item.lstrip("POLYGON((").rstrip("))")
        # create each vertex pair
        pairs = [pair.split() for pair in item.split(',') if pair]
        # type cast to int and assign to dict key i
        dd[i] = [(int(float(a)), int(float(b))) for a,b in pairs]
    Output similar to this:
    Code:
    >>> for key in dd:
    ... 	print "POLYGON %d" % (key)
    ... 	for pair in dd[key]:
    ... 		print '    %s %s' % (pair)
    ... 		
    POLYGON 0
        5576309 5075170
        5576299 5075169
        5576297 5075169
        5576287 5075168
        5576286 5075171
        5576286 5075171
        5576309 5075173
        5576309 5075171
        5576309 5075170
    POLYGON 1
        5576287 5075168
        5576297 5075169
        5576299 5075169
        5576309 5075170
        5576309 5075163
        5576301 5075162
        5576300 5075162
        5576297 5075162
        5576295 5075162
        5576292 5075161
        5576288 5075161
        5576288 5075164
        5576287 5075168
    >>>

    Comment

    • hajdukzivivjecno
      New Member
      • Nov 2009
      • 5

      #3
      AttributeError

      I just copied your code and got this

      s = s.replace('\n', '').replace("'" , "").lstrip( '[(').rstrip(',)]')
      AttributeError: 'list' object has no attribute 'replace'

      I tried to change my list file to string using:

      string=''.join( something)

      but I get this:

      string=''.join( data)
      TypeError: sequence item 0: expected string, tuple found

      ... thanks for any help ;)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        File method read() returns a string object. Obviously you have a list object - therefore the error. Please look at my comments.

        BV

        Comment

        • hajdukzivivjecno
          New Member
          • Nov 2009
          • 5

          #5
          Importing from database

          I am using connection from Python to Postgre (PostGIS) database to download my files and I cannot save file with coordinates in new file and later import that in new file because it has to be connected all the time...

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            You only mentioned modifying a file. So your data is a list of tuples instead of a string. That makes a big difference! Here's the dictionary:
            Code:
            # create empty dictionary
            dd = {}
            for i, item in enumerate(data):
                # remove unwanted characters
                item = item[0].lstrip("POLYGON((").rstrip("))")
                # create each vertex pair
                pairs = [pair.split() for pair in item.split(',') if pair]
                # type cast to int and assign to dict key i
                dd[i] = [(int(float(a)), int(float(b))) for a,b in pairs]

            Comment

            • hajdukzivivjecno
              New Member
              • Nov 2009
              • 5

              #7
              Just one more question...

              Thank you very much. I am still developing my code and have you any suggestions if I would have to get for every polygon ((x,y),(z,w),(. ..,...)) to be in integers? I have to input that for each polygon to pygame (polygon)...

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                So, you want the numbers to be integers? Please read the last line of code in my previous post and the comment line before it.

                Comment

                • hajdukzivivjecno
                  New Member
                  • Nov 2009
                  • 5

                  #9
                  Sorry, I am really retarded. Sorry again and thank you very much. You helped me a lot ;)

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    No problem. I am glad to help. :)

                    BV

                    Comment

                    Working...