Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 10th, 2005, 04:28 PM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation 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
June 10th, 2005, 06:27 PM
 stormcoder
Registered User

Join Date: Jun 2005
Posts: 3
Time spent in forums: 28 m 14 sec
Reputation 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
June 10th, 2005, 07:41 PM
 CyBerHigh
Contributing User

Join Date: Jun 2004
Posts: 461
Time spent in forums: 4 Days 12 h 36 m 28 sec
Reputation 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?
__________________
My Site:
http://www.coryhardman.com

#4
June 10th, 2005, 08:30 PM
 Yegg`
Contributing User

Join Date: Dec 2004
Location: Meriden, Connecticut
Posts: 1,797
Time spent in forums: 3 Weeks 6 Days 21 h 54 m 27 sec
Reputation Power: 154
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
June 11th, 2005, 12:06 AM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation Power: 9
Quote:
 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
June 11th, 2005, 09:55 AM
 Shiner_Man
Contributing User

Join Date: Jan 2004
Location: Atlantic City, NJ
Posts: 327
Time spent in forums: 2 Days 8 h 24 m 5 sec
Reputation Power: 12
Quote:
 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. Thanks againin advance!

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?
__________________
I'll learn this stuff someday.

#7
June 11th, 2005, 10:06 AM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation Power: 9
Quote:
 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
June 13th, 2005, 07:24 AM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation Power: 9
Bump. Anyone?

#9
June 13th, 2005, 09:42 AM
 Shiner_Man
Contributing User

Join Date: Jan 2004
Location: Atlantic City, NJ
Posts: 327
Time spent in forums: 2 Days 8 h 24 m 5 sec
Reputation Power: 12
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
June 13th, 2005, 09:57 AM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation Power: 9
Quote:
 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
June 13th, 2005, 10:59 AM
 Shiner_Man
Contributing User

Join Date: Jan 2004
Location: Atlantic City, NJ
Posts: 327
Time spent in forums: 2 Days 8 h 24 m 5 sec
Reputation Power: 12
Quote:
 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
June 13th, 2005, 11:34 AM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation Power: 9
Quote:
 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
June 13th, 2005, 11:56 AM
 Shiner_Man
Contributing User

Join Date: Jan 2004
Location: Atlantic City, NJ
Posts: 327
Time spent in forums: 2 Days 8 h 24 m 5 sec
Reputation Power: 12
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
June 13th, 2005, 12:16 PM
 Absolut9
Contributing User

Join Date: Jun 2005
Posts: 30
Time spent in forums: 10 h 20 m 32 sec
Reputation Power: 9
Quote:
 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
June 13th, 2005, 12:26 PM
 Shiner_Man
Contributing User

Join Date: Jan 2004
Location: Atlantic City, NJ
Posts: 327
Time spent in forums: 2 Days 8 h 24 m 5 sec
Reputation Power: 12
Quote:
 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).

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with a Python script