Take the 2-minute tour ×

I have an XML file that contains a number of points with their longitude and latitude.

My python code at the moment gets the nearest point by simply looping through the XML file, finding the nearest, in miles or whatever, then comparing it with the previous closest point. If its nearer then I assign the variable the value of this new point. So everything is working in that regard.

Now, what I want to do is actually store the closest 2 or 3 points. How do I go about doing this? The XML file isn't ordered by closest, and besides, the users location will change each time a request is made. Can I do this with an XML file or will I perhaps have to look into storing the data is SQL Server or MySQL?

Thanks for the help. PS, the sample code is on www.eoinzy.com if anyone is interested. This is part of a college project.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You should store in a list of tuples (for example) all the point pairs and their distances as you parse de xml file.

mypoints = [(distance12, x1, x2),...,(distancenm, xn, xm)]
three_closer = mypoints[:3]

Adapting this to your code:

mypoints = []
for row in rows:
     # Get coords for current record
     curr_coords = row.getAttribute("lat") + ',' + row.getAttribute("lng")
     # Get distance
     tempDistance = distance.distance(user_coords, curr_coords).miles
     mypoints.append((tempDistance, row))

#the three closest points:
mythree_shorter = mypoints[0:3]
for distance, row in mythree_shorter:
    shortestStation = json.dumps(
                            {'number': row.getAttribute("number"),
                             'address': row.getAttribute("address"),
                             'lat': row.getAttribute("lat"),
                             'lng': row.getAttribute("lng"),
                             'open': row.getAttribute("open")},
    save_in_some_way(shortestStation)   #maybe writing to a file?
share|improve this answer
Thanks for the help! I don't think writing to a file is feasible as this data will be consumed by an iPhone. I'm not quite understanding the 2nd for() loop. How does it know to only get the 3 nearest? I'm assuming its done in mypoints[0:3] but my python is only basic. I'll test it anyway and let you know. –  eoinzy Apr 22 '11 at 22:41
Thanks Joaquin! I used some of your code and got it working! The only thing missing was a "+=" on shortestStation, so the way I have it now is shortestStation = shortestStation+json.dumps()..... Thanks again!! –  eoinzy Apr 22 '11 at 22:55
the three nearest are stored in mythree_sorter and they are taken by the loop in order. The second for loop is there to save the three closer points serialized as json strings somewhere (a list, a file) or maybe to send them somewhere (it's up to you)... –  joaquin Apr 22 '11 at 22:59
OK, I see. Note that: save_in_some_way(shortestStation) <=> shortestStation = shortestStation + json.dumps(...) –  joaquin Apr 22 '11 at 23:02

Here's a solution that will work for any number of points:

closest = points[:NUM_CLOSEST]
for point in points[NUM_CLOSEST:]:
    if point.distance < closest[-1].distance:
        closest[-1] = point

Obviously, a bit pseudo-cody. The sort() calls will probably need an argument so they are sorted in a useful way, and you'll probably want a function to calculate the distance to replace the distance member.

share|improve this answer
Thanks for the reply! Sort() was the way to go! –  eoinzy Apr 22 '11 at 22:53

Your Answer


