Has anyone done something in python/zope to allow for searching by number of miles from a zip code? I see this on many sites and wonder how to incorporate it into my site. sean
On Wed, 2003-06-11 at 20:06, Sean Kelley wrote:
Has anyone done something in python/zope to allow for searching by number of miles from a zip code?
I did something like this several years ago, but not in Python. It isn't difficult if you're OK being accurate to within a mile or two. First thing, you get a data set that gives the map coordinates for each post office. USGS maintains a massive set of data about landmarks that includes this data, though it's probably available elsewhere as well. Once you have this data, you close your eyes and assume that since there's a 1:1 relationship between zip codes and post offices, you now have a map point that's somewhat central for each zip. Then you swear to yourself never to subject this hypothesis to actual testing. :-) You could just calculate the hypotenuse of a right triangle to get the distance between two map points, but first you have to deal with a problem: there are a fixed number of feet per degree of longitude, but the number of feet per degree of latitude varies considerably, particularly once Alaska and Hawaii are factored in. You could pick an average land distance for each degree of latitude or calculate the latitude distance based on the average of the longitudes being compared. I wouldn't be surprised if you could find a table that gives these figures. I suppose you could even include calculations for the curvature of the Earth, but given that you're only going to be accurate to a couple miles anyway, it's not going to make much difference at most intervals. HTH, Dylan
You proably should use a spherical earth model to compute distances. Database search with a "distance from" key is difficult and requires specialized mechanisms. There is a significant literature if you really need the general mechanism. Here we've assumed that you have a way of generating a small list of candidates and want to rank them by geographic distance. A way different problem. On 12 Jun 2003, Dylan Reinhardt wrote:
On Wed, 2003-06-11 at 20:06, Sean Kelley wrote:
Has anyone done something in python/zope to allow for searching by number of miles from a zip code?
I did something like this several years ago, but not in Python. It isn't difficult if you're OK being accurate to within a mile or two.
First thing, you get a data set that gives the map coordinates for each post office. USGS maintains a massive set of data about landmarks that includes this data, though it's probably available elsewhere as well.
Once you have this data, you close your eyes and assume that since there's a 1:1 relationship between zip codes and post offices, you now have a map point that's somewhat central for each zip. Then you swear to yourself never to subject this hypothesis to actual testing. :-)
You could just calculate the hypotenuse of a right triangle to get the distance between two map points, but first you have to deal with a problem: there are a fixed number of feet per degree of longitude, but the number of feet per degree of latitude varies considerably, particularly once Alaska and Hawaii are factored in.
You could pick an average land distance for each degree of latitude or calculate the latitude distance based on the average of the longitudes being compared. I wouldn't be surprised if you could find a table that gives these figures.
I suppose you could even include calculations for the curvature of the Earth, but given that you're only going to be accurate to a couple miles anyway, it's not going to make much difference at most intervals.
On Thu, 2003-06-12 at 09:29, Dennis Allison wrote:
You proably should use a spherical earth model to compute distances.
Depending on your accuracy requirements. The way I read the original post, the goal was to constrain search results to within some number of miles from a given zip code. If that's being used for figuring out how much fuel to put on an airplane, you're going to want far more accurate figures. :-) But if the purpose of this is to get some proxy for driving distance, there's no compelling reason to be any more accurate than within a mile or so... distance "as the crow flies" is only a bare approximation of driving distance anyway.
Database search with a "distance from" key is difficult and requires specialized mechanisms.
Depending, again, on how good the results need to be. Assuming your *best* search would be defined by point p and some radius r, you could get results *almost* as good by searching for points bounded by p +/- r. A select consisting of two simple range criteria is pretty easy to set up and virtually indistinguishable from *correct* results in a decent range of probable applications. Of course, you *could* use that method as a first pass before performing more expensive computation on the results. But just this one pass might be enough to know what things are *about* X miles from some point. You won't win any comp sci awards for this process, but it's a pragmatic solution that's good enough for many cases. Your mileage may vary. :-) FWIW, Dylan
Thanks for the thought on this. I realized that what I really need is a return of all of the zip codes within x miles of a zip code. In other words, show me all records (from zcatalog or sql) which match the results of a radius search where zip codes in the radius are returned. Dylan Reinhardt wrote:
On Thu, 2003-06-12 at 09:29, Dennis Allison wrote:
You proably should use a spherical earth model to compute distances.
Depending on your accuracy requirements.
The way I read the original post, the goal was to constrain search results to within some number of miles from a given zip code.
If that's being used for figuring out how much fuel to put on an airplane, you're going to want far more accurate figures. :-)
But if the purpose of this is to get some proxy for driving distance, there's no compelling reason to be any more accurate than within a mile or so... distance "as the crow flies" is only a bare approximation of driving distance anyway.
Database search with a "distance from" key is difficult and requires specialized mechanisms.
Depending, again, on how good the results need to be.
Assuming your *best* search would be defined by point p and some radius r, you could get results *almost* as good by searching for points bounded by p +/- r. A select consisting of two simple range criteria is pretty easy to set up and virtually indistinguishable from *correct* results in a decent range of probable applications.
Of course, you *could* use that method as a first pass before performing more expensive computation on the results. But just this one pass might be enough to know what things are *about* X miles from some point.
You won't win any comp sci awards for this process, but it's a pragmatic solution that's good enough for many cases. Your mileage may vary. :-)
FWIW,
Dylan
Thanks for the thought on this. I realized that what I really need is a return of all of the zip codes within x miles of a zip code. In other words, show me all records (from zcatalog or sql) which match the results of a radius search where zip codes in the radius are returned.
I'm reminded of a digital logic design class I once took. We had a problem where given a certain set of inputs, we had to calculate the outputs. It was to be done in a single (small) programmable chip. Being a software engineer, I did my state diagrams and whatnot, came up with the logical formula, and put together the gates that solved the inputs as they came through. A friend of mine, being of a more practical mindset, solved all the states himself and made what was basically a lookup table. It fit in the chip, and produced exactly the same outputs. What's more, it took a lot less work on his part. My solution may have been more elegant, but given the nature of the problem I'm convinced his was the better solution. What's my point? There's a fixed and not-too-large number of post offices. Make a table of the distances between all pairs of POs (or ZIP code centers), sort of like the cities table on a road map. I'm not exactly sure how big this'll be, but I'll bet it's not that big in modern computing's scope. You could use a sparse table and only include those pairs within a 'reasonable' distance. Then, given a post office, get the column of the distance to all others and return the zip codes of those within your range. Your table can be generated by whatever method is easiest to you (probably an existing geo-manipulation package), and I bet lookup will be a lot faster (and easier to write) than computation. By "table" what do I mean? Doesn't matter to me: a global Python array (in memory), a ZODB bag of tuplish objects, or a relational database could all do the job. --jcc
On Thu, 2003-06-12 at 12:23, Sean Kelley wrote:
Thanks for the thought on this. I realized that what I really need is a return of all of the zip codes within x miles of a zip code. In other words, show me all records (from zcatalog or sql) which match the results of a radius search where zip codes in the radius are returned.
This could be decently approximated by finding all the POs that are about double the search distance from the target PO. That should do a fair job of estimating which neighbor zips your radius would probably include. Then use the selected zip codes as a filter for your next search. This is, of course, assuming you're not planning on going to the bother of actually finding with where the *real* zip code borders are. Again, this isn't probably the *correct* solution, but it may prove useful enough for your purposes. Dylan
participants (4)
-
Dennis Allison -
Dylan Reinhardt -
J Cameron Cooper -
Sean Kelley