How to retrieve a list of small towns around a larger town?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.comwww.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}
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.
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.
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.
#!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.