473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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((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 :)
Nov 1 '09 #1
9 3800
bvdet
2,851 Expert Mod 2GB
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. >>> 
Nov 2 '09 #2
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 ;)
Nov 2 '09 #3
bvdet
2,851 Expert Mod 2GB
File method read() returns a string object. Obviously you have a list object - therefore the error. Please look at my comments.

BV
Nov 2 '09 #4
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...
Nov 2 '09 #5
bvdet
2,851 Expert Mod 2GB
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]
Nov 2 '09 #6
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)...
Nov 4 '09 #7
bvdet
2,851 Expert Mod 2GB
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.
Nov 4 '09 #8
Sorry, I am really retarded. Sorry again and thank you very much. You helped me a lot ;)
Nov 4 '09 #9
bvdet
2,851 Expert Mod 2GB
No problem. I am glad to help. :)

BV
Nov 4 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: flupke | last post by:
Hi, i'm trying to convert my java console app to a python gui. Now, the only problem i seem to have at the moment are the resizers for the layout. It seems that for the purpose of what i'm...
14
by: Sims | last post by:
Hi, I know this is slightly OT but before i can get to the programming part i want to make sure that i have the structure covered. I have a 2D map and items in my list/vector will be lines on...
7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
4
by: Eduard Witteveen | last post by:
Hello, I want to make a hooverbox, which is shown when the mousepointer is not moved for a amount of time. When the hooverbox is shown, i will do a server request to retrieve the information...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
2
by: John Smith | last post by:
Hey folks, I've got a combobox (DropDown List) in a windows form application which is bound to a DataSet. It's value is set to a numeric ID. It's visible text is set to a date. I need to make...
2
by: sasperilla | last post by:
Hi, I would like to find the coordinates of a word inside a div or span tag. I know you can find the coordinates of a tag inside the DOM, but can you get the coordinates of a portion of the...
0
by: mingke | last post by:
Hi.... I'm having difficulties with my codes to list coordinates (in my example codes is 4x4 pixel). I tried two different codes and the results are different.Basically, I tried to declare the sub...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.