Connecting Tech Pros Worldwide Help | Site Map

Changing my text to list of coordinates

Newbie
 
Join Date: Nov 2009
Posts: 5
#1: 3 Weeks Ago
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((5576309.155 5075170.4966,5576299.7617 5075169.7026,5576297.7031 5075169.445,5576287.5204 5075168.1705,5576286.9311 5075171.4731,5576286.88250176 5075171.74548589,5576309.13147582 5075173.07326486,5576309.03444426 5075171.74548589,5576309.155 5075170.4966))',), ('POLYGON((5576287.5204 5075168.1705,5576297.7031 5075169.445,5576299.7617 5075169.7026,5576309.155 5075170.4966,5576309.9005 5075163.301,5576301.9807 5075162.5819,5576300.8652 5075162.4806,5576297.3699 5075162.1632,5576295.8787 5075162.0278,5576292.9409 5075161.761,5576288.7631 5075161.3817,5576288.2564 5075164.0455,5576287.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's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#2: 2 Weeks Ago

re: Changing my text to list of coordinates


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.
Expand|Select|Wrap|Line Numbers
  1. # read file into a string
  2. s = open(file_name).read()
  3. # remove unwanted characters
  4. s = s.replace('\n', '').replace("'", "").lstrip('[(').rstrip(',)]')
  5. # split string on ",),", creating a list
  6. sList = s.split(',),')
  7.  
  8. # create empty dictionary
  9. dd = {}
  10. for i, item in enumerate(sList):
  11.     # remove unwanted characters
  12.     item = item.lstrip("POLYGON((").rstrip("))")
  13.     # create each vertex pair
  14.     pairs = [pair.split() for pair in item.split(',') if pair]
  15.     # type cast to int and assign to dict key i
  16.     dd[i] = [(int(float(a)), int(float(b))) for a,b in pairs]
Output similar to this:
Expand|Select|Wrap|Line Numbers
  1. >>> for key in dd:
  2. ...     print "POLYGON %d" % (key)
  3. ...     for pair in dd[key]:
  4. ...         print '    %s %s' % (pair)
  5. ...         
  6. POLYGON 0
  7.     5576309 5075170
  8.     5576299 5075169
  9.     5576297 5075169
  10.     5576287 5075168
  11.     5576286 5075171
  12.     5576286 5075171
  13.     5576309 5075173
  14.     5576309 5075171
  15.     5576309 5075170
  16. POLYGON 1
  17.     5576287 5075168
  18.     5576297 5075169
  19.     5576299 5075169
  20.     5576309 5075170
  21.     5576309 5075163
  22.     5576301 5075162
  23.     5576300 5075162
  24.     5576297 5075162
  25.     5576295 5075162
  26.     5576292 5075161
  27.     5576288 5075161
  28.     5576288 5075164
  29.     5576287 5075168
  30. >>> 
Newbie
 
Join Date: Nov 2009
Posts: 5
#3: 2 Weeks Ago

re: Changing my text to list of coordinates


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 ;)
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#4: 2 Weeks Ago

re: Changing my text to list of coordinates


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

BV
Newbie
 
Join Date: Nov 2009
Posts: 5
#5: 2 Weeks Ago

re: Changing my text to list of coordinates


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...
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#6: 2 Weeks Ago

re: Changing my text to list of coordinates


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:
Expand|Select|Wrap|Line Numbers
  1. # create empty dictionary
  2. dd = {}
  3. for i, item in enumerate(data):
  4.     # remove unwanted characters
  5.     item = item[0].lstrip("POLYGON((").rstrip("))")
  6.     # create each vertex pair
  7.     pairs = [pair.split() for pair in item.split(',') if pair]
  8.     # type cast to int and assign to dict key i
  9.     dd[i] = [(int(float(a)), int(float(b))) for a,b in pairs]
Newbie
 
Join Date: Nov 2009
Posts: 5
#7: 2 Weeks Ago

re: Changing my text to list of coordinates


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)...
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#8: 2 Weeks Ago

re: Changing my text to list of coordinates


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.
Newbie
 
Join Date: Nov 2009
Posts: 5
#9: 2 Weeks Ago

re: Changing my text to list of coordinates


Sorry, I am really retarded. Sorry again and thank you very much. You helped me a lot ;)
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#10: 2 Weeks Ago

re: Changing my text to list of coordinates


No problem. I am glad to help. :)

BV
Reply