### Thread: Help with a Python script

Page 1 of 2 12 Last
1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9

#### Help with a Python script

Is there someone who might be able to help me with this? I don't know much about Python, but I would like to be able to use this algoritm for a project that I am working on. Ideally, I would like it to be able to be ported to PHP, but if I can figure out how to throw numbers into, I'd be happy with it being in Python.

Here is the code that I have:

Code:
```numPoints = number of lat/lon pairs
points = array floats, lat followed by lon
points = map( lambda x: int(x/1.0E-5), points )
l = []
xo = yo = 0
for i in range(numPoints):
y = points[i<<1]
yd = y - yo
yo = y
f = (abs(yd) << 1) - (yd<0)
while 1:
e = f & 31
f >>= 5
if f:
e |= 32
l.append(chr(e+63))
if f == 0: break

x = self.points[(i<<1)+1]
xd = x - xo
xo = x
f = (abs(xd) << 1) - (xd<0)
while 1:
e = f & 31
f >>= 5
if f:
e |= 32
l.append(chr(e+63))
if f == 0: break```
From my limited knowledge it looks as though I need to import an array of values into this algorithm. l=[xxx,xxx,xxx,xxx] like so. My question is... How do I go about doing this? How do I pass the array of numbers to the Python script and then capture it's output? Can I pass the output from a PHP script into a Python one? Then pass the output from this Python script back into PHP? I've tried looking at some Python books for help, but I have not been able to find out what to do.

This code is supposed to take an array of lat and longitude values (l=[lat1,long1,lat2,long2,etc..]) and encode them into a string.

Thanks for any advice or help you can provide! Anyone willing to help me port this completely over to PHP?
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
3
Rep Power
0

#### There is a much easier way

First I wanted to say that this script has some problems. From what I understand from your description of what you think the script is supposed to do, there are easier ways. A simple method to convert floats to string is to do something like this

Code:
`"%s"%thefloat`
To get command line arguments:
Code:
```import sys
opt1 = sys.argv[0]
...```
There is also a couple of command line parsing libraries included in Python. The one I use most of the time is getopt. If you are looking to have this accessible on a web server there is a cgi module that makes creating CGI scripts easy. I don't know PHP but I would assume it would be similarly easy to convert a float to a string in PHP as well. If your using PHP it would be easier to stick with PHP rather than trying to interface a python script to a PHP script. If your looking to do your work in Python then you can do templating in Python (There are Several libs, CherryPy is my favorite) which is just as capable as PHP.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2004
Posts
461
Rep Power
24
ya that script is kinda buggy, it has psedo code mixed in with python code. Are you sure that is ment to actually run?
4. I think he included the pseudo code so that we knew what he wanted that variable to equal. He just didn't want to include any specific data.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9
Originally Posted by †Yegg†
I think he included the pseudo code so that we knew what he wanted that variable to equal. He just didn't want to include any specific data.
Yes, you are correct in that assertation.

Here is what the code is supposed to do. I am working on a project with Google Maps. That script takes lat/long coords and turns them into an encoded string that Googles uses to draw driving directions. I don't know Python that well, but I do know PHP. I have been scratching my head trying to figure it out.

If I could help with either how I can import the lat/long points into this script from a PHP script and then capture the string output, I would be forever in your debt. If not, I suppose my project can succeed without it for now.

If there would be someone willing to help me convert the algorithm to PHP, that would also be wonderful.

6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2004
Location
Atlantic City, NJ
Posts
327
Rep Power
13
Originally Posted by Absolut9
Yes, you are correct in that assertation.

Here is what the code is supposed to do. I am working on a project with Google Maps. That script takes lat/long coords and turns them into an encoded string that Googles uses to draw driving directions. I don't know Python that well, but I do know PHP. I have been scratching my head trying to figure it out.

If I could help with either how I can import the lat/long points into this script from a PHP script and then capture the string output, I would be forever in your debt. If not, I suppose my project can succeed without it for now.

If there would be someone willing to help me convert the algorithm to PHP, that would also be wonderful.

I still don't understand. You want to create a script that takes lat/long coordinates in as a string and encodes them into a string that google maps can understand? Well what exact encoding process do you have to do on the string? What format does google maps need? Can you give us some example input and output?
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9
Originally Posted by Shiner_Man
I still don't understand. You want to create a script that takes lat/long coordinates in as a string and encodes them into a string that google maps can understand? Well what exact encoding process do you have to do on the string? What format does google maps need? Can you give us some example input and output?
Well, here's some example input:
[33.823,-84.151,33.822,-84.149,33.823,-84.147]

And the output:
u`mmEtvr`OdEmKeEoK

8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9
Bump. Anyone?
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2004
Location
Atlantic City, NJ
Posts
327
Rep Power
13
I breifly looked over the link you gave. Its just too much info for me to go through but from what I understand you take in longitude and latitude, apply some sort of algorithim to it, and thats gives you the output you need. Right? You still haven't anwered my question before; What exact encoding process do you have to do on the string(longitude/latitude)?
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9
Originally Posted by Shiner_Man
I breifly looked over the link you gave. Its just too much info for me to go through but from what I understand you take in longitude and latitude, apply some sort of algorithim to it, and thats gives you the output you need. Right? You still haven't anwered my question before; What exact encoding process do you have to do on the string(longitude/latitude)?
That is what I am trying to figure out. It looks like they shift some of the bits around, ala the following code:

Code:
```e = f & 31
f >>= 5
if f:
e |= 32
l.append(chr(e+63))
if f == 0: break```
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2004
Location
Atlantic City, NJ
Posts
327
Rep Power
13
Originally Posted by Absolut9
That is what I am trying to figure out. It looks like they shift some of the bits around, ala the following code:

Code:
```e = f & 31
f >>= 5
if f:
e |= 32
l.append(chr(e+63))
if f == 0: break```
That is some bizarre code. I'm not sure if its sudo code or what. I don't know what f & 31 gives or e|= 32. I think f >> is a bit shift change but thats about all I can get from it. Sorry.
12. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9
Originally Posted by Shiner_Man
That is some bizarre code. I'm not sure if its sudo code or what. I don't know what f & 31 gives or e|= 32. I think f >> is a bit shift change but thats about all I can get from it. Sorry.
It's not that bizarre. It's just shifting bits and augmenting assignment operators.

There are quite a few actually...

Code:
`+=,  	-=,  	*=,  	/=,  	//=,  	%=,  	**=,  	|=,  	&=,  	^=,  	>>=,  	<<=`
I guess I'll just attempt to port this to PHP. I believe that PHP offers the same type of thing Python does with augmented assignment operators.
13. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2004
Location
Atlantic City, NJ
Posts
327
Rep Power
13
Okay so basically you have a script like this:

Code:
```points = [33.823, -84.151, 33.822, -84.149, 33.823, -84.147]
points = map(lambda x: int(x/1.0E-5), points)

numPoints = len(points)/2
l=[]
xo = yo = 0

for i in range(numPoints):
y = points[i<<1]
yd = y -yo
yo = y
f = (abs(yd) << 1) - (yd<0)
while 1:
e = f & 31
f >>= 5
if f:
e |= 32
l.append(chr(e+63))
if f == 0: break

x = points[(i<<1)+1]
xd = x - xo
xo = x
f = (abs(xd) << 1) - (xd<0)
while 1:
e = f & 31
f >>= 5
if f:
e |= 32
l.append(chr(e+63))
if f == 0: break```
and you want to know how to pass this list values into the points variable? Their are a bunch of ways to this. I guess the question is, where do you get these values from?

P.S. I still don't know what the majority of that code above is suppose to do. I only copied the example you gave and fixed a few things.
14. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2005
Posts
30
Rep Power
9
Originally Posted by Shiner_Man
and you want to know how to pass this list values into the points variable? Their are a bunch of ways to this. I guess the question is, where do you get these values from?

P.S. I still don't know what the majority of that code above is suppose to do. I only copied the example you gave and fixed a few things.
I would like to know how to pass the points array into this script. Let the alrorithm do its work and then return the encoded string (which i presume is stored in L[]).

I have a question though.. What does this line do?

Code:
`points = map(lambda x: int(x/1.0E-5), points)`
Also.. I see that you changed:
Code:
`x = self.points[(i<<1)+1]`
to:
Code:
`x = points[(i<<1)+1]`
What the difference? What does the self.variable do?
15. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2004
Location
Atlantic City, NJ
Posts
327
Rep Power
13
Originally Posted by Absolut9
I would like to know how to pass the points array into this script. Let the alrorithm do its work and then return the encoded string (which i presume is stored in L[]).

I have a question though.. What does this line do?

Code:
`points = map(lambda x: int(x/1.0E-5), points)`
Also.. I see that you changed:
Code:
`x = self.points[(i<<1)+1]`
to:
Code:
`x = points[(i<<1)+1]`
What the difference? What does the self.variable do?

map simply applies a function(here it is the lambda function) to each item in the sequence. I think that lambda takes out the decimal but I'm not sure off the top of my head.

self is used in functions which appear inside classes(which are then called methods). In this case, I think whoever wrote this had that part of code in a method somewhere but put it into the script for some reason. Since all this code isn't in a method, the self part is useless(and causes and error).
Page 1 of 2 12 Last