Reputation: 9778
I would use the API Geonames, but my application won't have access to the internet. So it has to run stand-alone. I see there are many files from Geonames.org to download, but I don't see any software to aid in being able to do Reverse Geocoding. I want to provide the lat/lon and have it return the country code. I'm thinking of downloading the data and put it into a MySQL database.
What algorithm is used for Reverse Geocoding so I can make use of it with the geonames.org downloads. My project is being written in PHP. Thanks!
Upvotes: 3
Views: 2367
Reputation: 7228
Geonames provides data for most countries. This data can be imported into a MySQL database. This can be searched for using lat lng.
The problem is that there is probably too much data in the files for your requirements. You can eliminate all the fields except lat,lng&country code for all the countries required and then combine them in 1 record.
MySQL query
SELECT country code
FROM XX
WHERE lat
BETWEEN 55 AND 55.5 AND lng
BETWEEN -2
AND -1.5
This will pull in data over a "Box" of 111kM. You can change the range to suit.
From the table below you can see that the 111kM "box" is only valid at the equator(0°). for other latitudes the lng range would need to be increased.
lat lng
0° 110.574 km 111.320 km
15° 110.649 km 107.551 km
30° 110.852 km 96.486 km
45° 111.132 km 78.847 km
60° 111.412 km 55.800 km
75° 111.618 km 28.902 km
90° 111.694 km 0.000 km
Upvotes: 3
Reputation: 12592
The algorithm you are looking for is more a data structure then a complicated formula. Mysql supports R-tree and spatial index queries. The concept is a hierarchical tree with the lat long pairs as a bounding box the root of the tree has the entire world in the bounding box. A Kd-tree can be also good. In rare case you want to look for a quadkey or a space filling curve. It's a fractal and reduces the dimension to a number. The formula is H(x,y) = H(x) + H(y). The fractal also preseve some proximity information which I don't know how it's solved with a R-tree.
Upvotes: 1
Reputation: 4478
geonames downloads has mainly in .txt
files, these can be imported via SQLYog to the mysql database
Table Structure
CREATE DATABASE geonames;
USE geonames;
CREATE TABLE geoname (
geonameid int PRIMARY KEY,
name varchar(200),
asciiname varchar(200),
alternatenames varchar(4000),
latitude decimal(10,7),
longitude decimal(10,7),
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 int,
elevation int,
gtopo30 int,
timezone varchar(40),
moddate date
) CHARACTER SET utf8;
MYsql query to import data to this table
LOAD DATA INFILE '/path/to/your/file/geoname.txt' INTO TABLE `geoname`;
After all is set you can query this table against lat/long to get country name
Here is the link you can refer to
http://sgowtham.net/blog/2009/07/29/importing-geonames-org-data-into-mysql/
http://forum.geonames.org/gforum/posts/list/732.page
Hope this helps!
Upvotes: 2