473,804 Members | 3,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing my text to list of coordinates

5 New Member
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 :)
Nov 1 '09 #1
9 3816
bvdet
2,851 Recognized Expert Moderator Specialist
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
hajdukzivivjecno
5 New Member
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 Recognized Expert Moderator Specialist
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
hajdukzivivjecno
5 New Member
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 Recognized Expert Moderator Specialist
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
hajdukzivivjecno
5 New Member
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 Recognized Expert Moderator Specialist
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
hajdukzivivjecno
5 New Member
Sorry, I am really retarded. Sorry again and thank you very much. You helped me a lot ;)
Nov 4 '09 #9
bvdet
2,851 Recognized Expert Moderator Specialist
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
2072
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 trying to do, specifying the coordinates is easier that fighting with the layout resizers. 1) I have a screen split in 2. Left side is a textcontrol where logging will end up. All text will be appended to the textcontrol. Ideally this should allow...
14
1634
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 the map, with a start and finish point within the map, so an item would have a start pos S(x,y) and an end pos E(x,y). so my structure would be something like
7
2381
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 location of them in the array /list. Hence the output being some list of lists, containing groups of indices of the storage array that point to the same node index. This is obviously a trivial problem, but if my storage list is large and the set of...
5
2960
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, I have used individual entry fields for each variable. I would now like to use text area boxes to simplify the data entry (this way, data can be produced by another program--FORTRAN, "C", etc.--but analyzed online, so long as it is first...
4
1757
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 for the hooverbox. I was thinking of using document.onmousemove and a infinit running while loop comparing the mouse positions. Is this the way to solve it?
10
15137
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
2
4281
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 it so that the first item shows the words "Most Recent" instead of the date. I've tried setting the SelectedItem, Text, and SelectedText of the list box to "Most Recent", when the SelectedIndex changes to 0, but nothing happens.
2
1904
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 text inside that element in javascript? If the user highlights or selects text in a div or span can I get the indicies that make up that selected piece of text? What API of
0
2149
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 coordinates for x and y, and then print it. But, if I make a change by declaring sub coordinates for x and print it, and the declare sub coordinate for y and print it, the results are different. I have tried to calculated manually, the right...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.