Changing my text to list of coordinates | Newbie | | Join Date: Nov 2009
Posts: 5
| | |
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 :)
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,561
| | | 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. - # 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: - >>> 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
-
>>>
| | Newbie | | Join Date: Nov 2009
Posts: 5
| | | 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 ;)
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,561
| | | 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
| | | 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...
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,561
| | | 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: - # 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]
| | Newbie | | Join Date: Nov 2009
Posts: 5
| | | 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)...
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,561
| | | 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
| | | 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 ;)
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,561
| | | re: Changing my text to list of coordinates
No problem. I am glad to help. :)
BV
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|