WELCOME TO SINLEB.COM

  The Pirate Party (PiratPartiet , Sweden)


BACK TO SCRIPTS



GEOLOCATION BASED ON GEONAME




What’s this Geolocation based on Geoname Feature About? The GeoNames dataset provides a list of global placenames, their location: latitude and longitude and some additional information such as population and so on.
It is published under the Creative Commons Attribution 3.0 License, which allows you to use the data for your own purposes.
You can download the data by country, or the entire global dataset.



First up, lets get the data from the geonames download page:
http://download.geonames.org/export/
At the very bottom of this page you can see the TOS

Terms and Conditions

-Free : GeoNames data is free. (September 2011)

-License:
CC-by licence (creative commons attributions license).
You should give credit to GeoNames when using data or web services with a link
or another reference to GeoNames.

-Commercial usage is allowed


-The data is provided "as is" without warranty or any representation of accuracy,
timeliness or completeness..................

DOWNLOAD


GeoNames database dump can be downloaded in the form of a large worldwide text file (allCountries.zip):
http://download.geonames.org/export/dump/allCountries.zip

Separated download:
http://download.geonames.org/export/dump/
Postal codes are available as a separate download:
http://download.geonames.org/export/zip/

Note the download is around 200mb (so, expect it to take a little while)

Once the database is downloaded, unzip it:
You should now have a text file called allCountries.txt weighing in at just under 900mb.

MySQL


Next we have to load it into a MySQL database.
In the code posted below, it is assumed you have a table called 'geoname'.

CREATE TABLE IF NOT EXISTS `geoname` (
geonameid int,
name varchar(200),
asciiname varchar(200),
alternatenames varchar(8000),
latitude float,
longitude float,
fclass char(1),
fcode varchar(10),
country varchar(2),
cc2 varchar(60),
admin1 varchar(20),
admin2 varchar(80),
admin3 varchar(20),
admin4 varchar(20),
population bigint,
elevation int,
gtopo30 int,
timezone varchar(40),
moddate date
);

--------------

UPLOAD


Now, you can import the data using LOAD DATA LOCAL INFILE .

Since allCountries.txt weighing is just around 900mb. the import may take a little
while depending on the speed of your computer and server.
So I did it country by country (If we do not, it will become a long, drawn-out issue!!)


When it is completed, you should have ~7.5 million records in your geoname table.

Example : How to upload se.txt to geoname database

Follow these steps:
1- Launch: http://download.geonames.org/export/dump/
2- Download : SE.zip (sweden)
3- Unzip & Upload: se.txt to geoname dir
Internet:
http://www.your_domain.com/geoname/se.txt

Path:
/home/here_your_name/public_html/geoame/se.txt


4- Open phpmy admin
5- Click on geoname table
6- Click on SQL (console)

7-Paste this code:

LOAD DATA LOCAL INFILE '~/public_html/geoname/se.txt'
INTO TABLE geoname(geonameid,name,asciiname,alternatenames,latitude,longitude,
fclass,fcode,country,cc2, admin1,admin2,admin3,admin4,population,
elevation,gtopo30,timezone,moddate);
Be careful of blank spaces (preformatted text!)
LOAD DATA LOCAL INFILE '~/public_html/geoname/se.txt' INTO TABLE geoname(geonameid,name,asciiname,alternatenames,latitude,longitude,fclass,fcode,country,cc2,admin1,admin2,admin3,admin4,population,elevation,gtopo30,timezone,moddate);

Note*
The path needs to reflect the file location on the server.
Usually , this works: '~/public_html/path_to/your_file.txt'

if don't,try something like this:
------------
Example 1 :
'/home/here_your_user/public_html/directory_file_is_in/file_name.txt'
-----------------
Example 2:
if you want to upload a file named se.txt located in a dir. called geoname:
public_html/geoname/se.txt

'/home/here_your_name/public_html/geoame/se.txt'
---------------



Page generated in 0.692194 seconds
Wed, 22 Feb 2012 - 14:39 Server Time
Script Created by Sinleb.com TOS