LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-13-2013, 01:31 AM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
How to retrieve a list of small towns around a larger town?


I guess I've got a brain lock. I need to find the towns around someplace within a 50 mile radius. I would like to type in the name of the town and get back a list of all the town around it. Something in a form I can feed to another function that will search a database.

Example:
Someone is playing music in Conroe Tx. If I was going to be in Houston and queried the data base it would miss the Conroe date, because I was looking in Houston.

Does anyone know of an open source code snippet that would do this, also a open source data base.
I have searched Google. W3C, and php.net. What I have found deals with zip codes, or longitudes and latitudes. I don't know these for across the nation.

Any help? Even a new search pattern. I've used up all my word combinations.
 
Old 01-13-2013, 02:22 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
umm...I'm belgium...here we have postal codes. Of course, you've got that too...but, is there a way to "correlate" them? I dont know the system that was used to set up postal codes where you live...so this is just a suggestion...

An example (bear with me, these are belgian names, these may come over as names from an other planet...sometimes belgium exists on an other planet... )

Hasselt has postal code 3500
Wimmertingen, a few miles further on, has 3501 as postal code

Use this site to look up a number and just add/subtract one (or more) to see the neighboring towns...on our local google maps...

Just a hint, it is sunday morning here, so the fog has'nt lifted yet

Thor

Last edited by ButterflyMelissa; 01-13-2013 at 02:23 AM.
 
Old 01-13-2013, 05:04 PM   #3
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
Postal codes / zip codes have been my problem. Zip codes are the logical way to distribute mail. But not everyone know these code if they don't write to them regulary. I need to deal in the names of the towns.
For example:
I live about 60 miles out side of Houston. A check of the postal codes within a 50 mile radius produces 84 zip codes for 23 cities/towns.

A booking agent for musician Sara Hickman schedules his client into Doesy Doe Music Cafe in Conroe Texas tomorrow night.
He would then go to my webpage and enter the following into the database,
Date State City Artist Name Artist Website Venues Website

A fans company sents them to Houston for a 3 day meeting. The fan would then go to my website and type in:
Date State City
My search function would then take the 'fans input' and search my database to see who was playing. The returned information would look something like this:
03/21/2013 Tx Conroe Sara Hickman www.sarahickman.com www.doesydoemusiccaffe.com

A search for Houston would miss this eventhough it lies within the 50 mile radius.

I need a list of near by towns I can plug into a varable 'City' and loop through the database. And the open sourse code for my website that will allow me to access the cityname database, if it has to exist at another website. {Or something I can reverse engineer}
 
Old 01-13-2013, 05:42 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
That is a pretty valuable database you are looking for, and perhaps it is available for a stiff price.

If you enter "latitude longitude conroe,tx" in google, you get the result in a nice format, and quite readable in the source:
Code:
<div class="vk_cxp">
<div class="vk_ans vk_dgy answer_predicate">30.3117° N, 95.4558° W</div>
<div class="vk_gy vk_sh answer_subject">Conroe, Coordinates</div>
</div>
Once you have these coordinates it is a piece of cake to do some mathematics and get the distance.

It must be possible to script something, although my first attempt with wget was not succesful. Myabe Lynx does better.

jlinkels
 
1 members found this post helpful.
Old 01-15-2013, 05:29 PM   #5
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Sounds a bit of a logistical nightmare to be honest, especially when looking opensource/free. For example, how do you define what a "small town" is? Within a 50 mile radius of my home there are probably hundreds of small villages, towns, cities etc, however I wouldn't want to see all those - I'd be interested in the major ones. So you need the appropriate granularity in your database from the start.
 
Old 01-15-2013, 05:44 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Just because you want it to DISPLAY the town name, doesn't mean you need to WORK in town names. Work in what's convenient for you, and then convert these to town names for display.

For example, if you can find a site or database that will give you all postal codes within a xx mile radius of a given postal code, then you're 99% of the way there. Just take the input town name, convert it to a postal code, check the database for all nearby postal codes, then convert those back into town names. Same goes for lat/lon. I can pretty much guarantee you that any code/database you find that does what you're looking for does it this way already.

For a brute force approach, you can work in lat/lon easily. Spend some time building up a database of lat/lon coordinates for all cities/towns (yes this will take a while, but this is the hard part). Once you have that database, you can convert from the input town name to its lat/lon, find all lat/lons in the database that are within xx miles, then display the corresponding names.

Last edited by suicidaleggroll; 01-15-2013 at 05:48 PM.
 
1 members found this post helpful.
Old 01-15-2013, 06:27 PM   #7
lcole
Member
 
Registered: Aug 2003
Location: Oregon
Distribution: Ubuntu
Posts: 51

Rep: Reputation: 18
Here is a web app and provider that may help you.
http://www.zip-codes.com/zip-code-ra....x=29&srch.y=7
 
1 members found this post helpful.
Old 01-15-2013, 06:46 PM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Well maybe this helps:
http://geonames.usgs.gov/domestic/download_data.htm

jlinkels
 
1 members found this post helpful.
Old 01-15-2013, 10:53 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
A crude attempt...

Go to http://www.boutell.com/zipcodes/ and click on Click here to download the zip code database.

Unpack that file. The contents look like this ...
Code:
"zip","city","state","latitude","longitude","timezone","dst"
"27519","Cary","NC","35.76812","-78.829548","-5","1"
"27520","Clayton","NC","35.635275","-78.4489","-5","1"
"27521","Coats","NC","35.408375","-78.66964","-5","1"
"27522","Creedmoor","NC","36.111937","-78.69122","-5","1"
"27523","Apex","NC","35.73663","-78.86462","-5","1"
Make this your input file.

Run this program.
Code:
#!/bin/bash
#  Daniel B. Martin   Jan13
#
#  To execute this program, launch a terminal session and enter:
#  bash /home/daniel/Desktop/LQfiles/dbm586.bin
#
#  This program inspired by
#  http://www.linuxquestions.org/questions/programming-9/
#    how-to-retrieve-a-list-of-small-towns-around-a-larger-town-4175445359/
 

Path='/home/daniel/Desktop/LQfiles/dbm586'
 InFile=$Path"inp.txt"
OutFile=$Path"out.txt"
  Work1=$Path"w1.txt"

echo
echo "Please enter a City and State in this format:"
echo "   New York, NY   or   Chicago, IL"
read InString

# For debugging convenience:
# Test for null entry 
    if [ -z "$InString" ]; then InString="Wake Forest, NC"; fi
    
echo "The input string is: " $InString
SearchString=$(sed 's/, /","/' <<< $InString)
SearchString=$(sed 's/^/\"/' <<< $SearchString)
# echo "The search string is=>"$SearchString"<="


# InFile was derived from  http://www.boutell.com/zipcodes/
# "zip","city","state","latitude","longitude","timezone","dst"
# "27519","Cary","NC","35.76812","-78.829548","-5","1"
# "27520","Clayton","NC","35.635275","-78.4489","-5","1"
# "27521","Coats","NC","35.408375","-78.66964","-5","1"
# "27522","Creedmoor","NC","36.111937","-78.69122","-5","1"
# "27523","Apex","NC","35.73663","-78.86462","-5","1"

grep "$SearchString" $InFile > $Work1
# echo; echo "Work1..."; cat $Work1


 Lat=$(head -n1 $Work1 |cut -d\" -f8  |cut -c 1-3)
Long=$(head -n1 $Work1 |cut -d\" -f10 |cut -c 1-4)
# echo "Lat=>"$Lat"<="
# echo "Long=>"$Long"<="

echo "Towns near" $InString "..." > $OutFile
 grep '\'$Long $InFile \
|grep \"$Lat           \
|cut -d\" -f4-6        \
|sed 's/","/, /'       \
|sort -u               \
>> $OutFile

echo; echo "OutFile..."; cat $OutFile

echo; echo "Normal end of job."; echo
exit 0
Daniel B. Martin
 
Old 01-15-2013, 11:28 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Yahoo Geoplanet lets you query for the neighbours of a given "place".

It's free. Check it out.

The queries that it permit should also cover your need to convert towns to zip codes.

Last edited by dugan; 01-15-2013 at 11:36 PM.
 
Old 01-16-2013, 05:22 PM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Dafydd View Post
I need to find the towns around someplace within a 50 mile radius. I would like to type in the name of the town and get back a list of all the town around it. Something in a form I can feed to another function that will search a database.
Go to http://www.boutell.com/zipcodes/ and click on Click here to download the zip code database.

Unpack that file. The contents look like this ...
Code:
"zip","city","state","latitude","longitude","timezone","dst"
"27519","Cary","NC","35.76812","-78.829548","-5","1"
"27520","Clayton","NC","35.635275","-78.4489","-5","1"
"27521","Coats","NC","35.408375","-78.66964","-5","1"
"27522","Creedmoor","NC","36.111937","-78.69122","-5","1"
"27523","Apex","NC","35.73663","-78.86462","-5","1"
Make this your input file.

Run this program.
Code:
#!/bin/bash
#  Daniel B. Martin   Jan13
#
#  To execute this program, launch a terminal session and enter:
#  bash /home/daniel/Desktop/LQfiles/dbm586.bin
#
#  This program inspired by
#  http://www.linuxquestions.org/questions/programming-9/
#    how-to-retrieve-a-list-of-small-towns-around-a-larger-town-4175445359/
 
Path='/home/daniel/Desktop/LQfiles/dbm586'
 InFile=$Path"inp.txt"
OutFile=$Path"out.txt"
  Work1=$Path"w1.txt"

echo
echo "Please enter a City and State in this format:  City, ST"
echo "  Examples:  New York, NY  or  Chicago, IL"
read InString

# For debugging convenience: if null entry, apply a default string.
if [ -z "$InString" ]; then InString="Wake Forest, NC"; fi
    
SearchString=$(sed 's/, /","/' <<< $InString)
SearchString=$(sed 's/^/\"/' <<< $SearchString)

# 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"

# Find all lines which match the user-specified town.
grep -i "$SearchString" $InFile > $Work1
 
# Handle the no-match case.
LineCount=$(wc -l $Work1 |cut -d ' ' -f1)
if [ "$LineCount" -eq "0" ] ; then 
  echo "Your Town, ST specification was:" $InString
  echo "No matches were found."
  echo "Check the spelling and try again."
  echo "Remember, the comma between Town and ST is required."
  exit
fi
 
# There may be multiple matches.
# echo "Calculate ALat,  the average latitude,"
# echo "      and ALong, the average longitude."
 ALat=$(awk -F \" '{sum+=$8}  END {print sum/NR}' $Work1)
ALong=$(awk -F \" '{sum+=$10} END {print sum/NR}' $Work1)

# Calculate the relative distances by ...
#  - squaring the Delta-Latitude
#  - squaring the Delta-Longitude
#  - summing those squares
# Sort on relative distance in ascending order
# Keep 300 lines with shortest distances
# Eliminate redundant town names
# Toss the town specified as the InString
# Coff = Cut-off value
Coff=35
echo "The" $Coff "towns nearest" $InString \
     "(ranked nearest to farthest)." > $OutFile
awk -F \" -v ALat=$ALat -v ALong=$ALong \
  '{if (NF>0)
    print (($8-ALat)^2)+(($10-ALong)^2) " " $4$5" "$6}' \
    $InFile           \
|sort -k1,1 -n        \
|head -n300           \
|cut -d " " -f2-      \
|grep -vi "$InString" \
|awk '!_[$1]++'       \
|head -n$Coff         \
>> $OutFile
echo; cat $OutFile

echo; echo "Normal end of job."; echo
exit 0
You will have to change the file identifiers (InFile, OutFile, etc.) to suit your own computer.

Daniel B. Martin
 
  


Reply



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
How to build pcitweak (small util in much larger, apparently broken package) rich1 Linux - Newbie 0 07-03-2012 12:42 PM
How to retrieve a list of installed programs with aptitude? lauterx Linux - Newbie 2 09-22-2011 12:27 PM
Greetings from a small town in Ontario jalupp LinuxQuestions.org Member Intro 2 07-14-2010 07:48 PM
LXer: Multinationals test small town Indians LXer Syndicated Linux News 0 09-01-2006 01:54 PM
compiling kernel for larger scatter gather list? niverson Linux - Software 0 09-20-2004 10:02 PM

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

All times are GMT -5. The time now is 10:01 PM.

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