LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2014, 12:33 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
how to get a list of zip codes within a 10 mile radius


does anyone know of an api (openstreetmap perhaps) where i can get a list of zip codes within a 10 mile radius.

or is there a database i can download that has all the zip codes with longs/lats that i can calculate myself.

i am trying to read thru results from duckduckgo but maybe my search terms (title) arent good ?

i also took a look at usps.com but they dont seem to have anything useful.

Last edited by schneidz; 11-17-2014 at 12:34 PM.
 
Old 11-17-2014, 01:21 PM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 9,311

Rep: Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057Reputation: 2057
http://www.zip-codes.com/zip-code-radius-finder.asp

You can get it at the link above

Last edited by yancek; 11-17-2014 at 01:22 PM.
 
1 members found this post helpful.
Old 11-17-2014, 01:35 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569
I don't know if this will fit your needs, but do check it out:

https://developer.yahoo.com/geo/geoplanet/
 
1 members found this post helpful.
Old 11-17-2014, 03:33 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Quote:
Originally Posted by schneidz View Post
... where i can get a list of zip codes within a 10 mile radius.
Within a 10 mile radius of what? Is your input a town/state or a zip code? Is your input a single location keyed by a user prompt or a file of locations?

Daniel B. Martin
 
Old 11-17-2014, 05:25 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Original Poster
Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
a file of zipcodes. via commandline i would like to type in something like:
Code:
zipcode-radius 02144
and get back a list like:
Code:
02144
02153
02140
02155
02138
02143
02145
02156
02474
02163
...
i will check out the links above and advise on my progress... thanks so far.


edit: solved for now. using a proper api like the yahoo developer geoplanet link above is probably the best way to acheive this but i am going to cheat and use a site like http://www.searchbug.com/tools/zip-radius.aspx or http://www.zip-codes.com/zip-code-radius-finder.asp and formulate a url and scrape the results using wget.

Last edited by schneidz; 11-17-2014 at 05:37 PM.
 
Old 11-18-2014, 10:13 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Calculating distances between two points identified by latitude and longitude requires more trigonometry than I know how to code. Herewith is simpler approach which may suit your purposes. Perhaps you can extend it to use an actual radius.

My Linux coding is all self-taught and probably exhibits lapses in variable quoting and so forth. Corrections and improvements are welcomed.

Download a file of Zip Code data from http://www.boutell.com/zipcodes/

Then play with this...
Code:
#!/bin/bash   Daniel B. Martin   Nov14

#  To execute this program, launch a terminal session and enter:
#  bash /home/daniel/Desktop/LQfiles/dbm1280.bin
#
#  This program inspired by
#  http://www.linuxquestions.org/questions/programming-9/
#    how-to-get-a-list-of-zip-codes-within-a-10-mile-radius-4175525670/

# File identifiers.
   Path=${0%%.*}
 InFile=$Path"inp.txt"
OutFile=$Path"out.txt"

echo
echo "Please enter a five-digit Zip Code."
read SearchString

# For debugging convenience: if null entry, apply a default string.
if [ -z "$SearchString" ]; then SearchString="27523"; fi
    
# InFile was derived from  http://www.boutell.com/zipcodes/
# "zip","city","state","latitude","longitude","timezone","dst"
# "27523","Apex","NC","35.73663","-78.86462","-5","1"
# "02139","Cambridge","MA","42.364347","-71.10431","-5","1"

MatchRec=$(grep '^"'$SearchString'"' <$InFile)

# Handle the no-match case.
if [ -z "$MatchRec" ] ; then 
  echo "Your Zip Code specification was:" $SearchString
  echo "No matches were found."
  echo "Execution ended."
  exit
fi

 Lat=$(cut -d\" -f8  <<<$MatchRec)
Long=$(cut -d\" -f10 <<<$MatchRec)

# Calculate the relative distances by ...
#  - squaring the Delta-Latitude
#  - squaring the Delta-Longitude
#  - summing those squares
#  - taking the square root of those squares
# Sort on relative distance in ascending order
# Keep "Coff" lines with shortest distances
# Coff = Cut-off value
Coff=50
echo "The" $Coff "Zip Codes nearest" $SearchString \
     "(ranked nearest to farthest)." > $OutFile
awk -F \" -v Lat=$Lat -v Long=$Long \
  '{print sqrt((($8-Lat)^2)+(($10-Long)^2)),
     " "$2" "$4$5" "$6}'  \
    $InFile               \
|sort -nk1                \
|cut -d" " -f2-           \
|head -n$Coff             \
>> $OutFile
echo; cat $OutFile

echo; echo "Normal end of job."; echo
exit 0
Daniel B. Martin

Last edited by danielbmartin; 11-18-2014 at 11:02 AM. Reason: Minor code change
 
1 members found this post helpful.
Old 11-19-2014, 08:20 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Original Poster
Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
i decided to not be lazy so i loaded the csv from dans link above into a mysql table and run this calculation:
Code:
select zip, (acos(cos(radians(42.399546)) * cos(radians(latitude)) * cos(radians(-71.121650) - radians(longitude)) + sin(radians(42.399546)) * sin(radians(latitude))) * 3958.755) as distance
from zipcodes
having distance < 10
order by distance;
http://stackoverflow.com/questions/2...ngitude-points

so now in order to know the part in purple i would first have to query:
Code:
select latitude, longitude
from zipcodes
where zip = 02144
does anyone know of a way to consolidate the 2 queries ? i tried sub-query but i dont think that would work since sub-query cant supply more than one answer and i need 2 ?

thanks,

Last edited by schneidz; 11-19-2014 at 08:25 PM.
 
Old 11-19-2014, 08:29 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
[QUOTE=schneidz;5272203]
Code:
(acos(cos(radians(42.399546)) * cos(radians(latitude)) * cos(radians(-71.121650) - radians(longitude)) + sin(radians(42.399546)) * sin(radians(latitude))) * 3958.755)
When writing dbm1280.bin I tried to code this as part of an awk but could not find an arccos function. Is there one?

Daniel B. Martin
 
Old 11-19-2014, 08:47 PM   #9
jefro
Moderator
 
Registered: Mar 2008
Posts: 20,677

Rep: Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334
Wonder if some GIS package would do that? You'd have to have a free gis data set that includes us and zip code bounties. I've seen that stuff but usually it's for sale. Bet someone offers it free if you keep looking. I'd guess USPS has it but doubt it's free. All the real estate places have access to it somewhere up the chain.
 
Old 11-19-2014, 09:08 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Original Poster
Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
[QUOTE=danielbmartin;5272210]
Quote:
Originally Posted by schneidz View Post
Code:
(acos(cos(radians(42.399546)) * cos(radians(latitude)) * cos(radians(-71.121650) - radians(longitude)) + sin(radians(42.399546)) * sin(radians(latitude))) * 3958.755)
When writing dbm1280.bin I tried to code this as part of an awk but could not find an arccos function. Is there one?

Daniel B. Martin
this is buggy for me:
Code:
[schneidz@hyper Downloads]$ cat zip.tmp
"02101","Boston","MA","42.370567","-71.026964","-5","1"
"90001","Los Angeles","CA","33.972914","-118.24878","-8","1"
"33101","Miami","FL","25.779076","-80.19782","-5","1"
"98060","Seattle","WA","47.432251","-121.803388","-8","1"
[schneidz@hyper Downloads]$ echo $Lat,$Long
42.370567,-71.026964
[schneidz@hyper Downloads]$ awk -F \" -v Lat=$Lat -v Long=$Long 'function asin(x) { return atan2(x, sqrt(1-x*x)) } function acos(x) { return atan2(sqrt(1-x*x), x) } function atan(x) { return atan2(x,1) } {print acos(sin(Lat)*sin($8)+cos(Lat)*cos($8)*cos($10-Long))*3958.755  "," " "$2" "$4$5" "$6}' zip.tmp
5.899e-05, 02101 Boston, MA
8693.96, 90001 Los Angeles, CA
8620.08, 33101 Miami, FL
4856.59, 98060 Seattle, WA
bos <-> la should be about 2590 miles but i think that its the formula is fubar... same results with ms-excel (i am pretty sure the trig functions arent part of libre-office because i get #value errors).

Last edited by schneidz; 11-19-2014 at 09:13 PM.
 
Old 11-23-2014, 04:26 AM   #11
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 89

Rep: Reputation: Disabled
Example: ./zipcodes.sh zipcode miles
Code:
$ ./zipcodes.sh 78758 4
78727
78752
78753
78754
78757
78758
78759
Code:
#!/bin/bash
# ./zipcodes.sh zipcode miles
# it appears that the max limit for miles is 30.

curl "www.zip-codes.com/zip-code-radius-finder.asp?zip1=${1}&zipMilesHigh=${2}&print=true" 2>/dev/null |
egrep "[0-9][0-9][0-9][0-9][0-9]</a>" |
sed 's/<\/a>.*$//' |
sed "s/.*asp'>//" |
sort

Last edited by nbritton; 11-23-2014 at 04:43 AM.
 
2 members found this post helpful.
Old 04-23-2018, 03:54 PM   #12
jamesmela
LQ Newbie
 
Registered: Apr 2018
Posts: 2

Rep: Reputation: Disabled
https://zipcodedownload.com/Products/Category/APIs

You can get it at the link above
 
Old 04-23-2018, 05:49 PM   #13
jefro
Moderator
 
Registered: Mar 2008
Posts: 20,677

Rep: Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334
Hello jamesmela, welcome to LQ.

LQ usually tries to limit links to commercial products and posting to older threads unless it substantially improves the thread.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
List of dmesg Error Codes worm5252 Linux - Enterprise 5 08-27-2012 06:31 AM
List of Process status codes and meanings? tgp1994 Linux - Newbie 1 04-02-2010 04:53 PM
Aterm - a way to store it's settings (getting rid of a mile long argument list) szp Linux - Newbie 1 03-07-2007 04:18 PM
Zip gives error "-bash: /usr/bin/zip: Argument list too long" konathamsrinu Programming 3 07-06-2006 10:08 AM
Chmod Codes list AmdMhz Linux - Software 12 02-05-2004 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration