Sure there's Google Maps API, geonames.org, Nominatim, The National Map and a whole host of other services that require agreeing to a end user license, compile a number of data sources into one, return more than I need or want, not open data, not open source and on and on.
In less time than I spent searching for a service, I rolled my own gazetteer of sorts. I downloaded the GNIS national file and created a sqlite database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sqlite3 GNIS | |
create table national (feature_id int, | |
feature_name varchar(120), | |
feature_class varchar(50), | |
state_alpha varchar(2), | |
state_numeric varchar(2), | |
county_name varchar(100), | |
county_numeric varchar(3), | |
primary_lat_dms varchar(7), | |
primary_lon_dms varchar(8), | |
primary_lat_dec real, | |
primary_lon_dec real, | |
source_lat_dms varchar(7), | |
source_lon_dms varchar(8), | |
source_lat_dec real, | |
source_lon_dec real, | |
elevation_meters integer, | |
elevation_feet integer, | |
map_name varchar(100), | |
date_created date, | |
date_edited date); | |
.import ./NationalFile_20120416.txt national |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sqlite3' | |
db = SQLite3::Database.new( "GNIS" ) | |
city = ARGV[0] | |
state = ARGV[1] | |
query = "select primary_lat_dec, primary_lon_dec from national where feature_class="+%Q["Populated Place"] + " and feature_name=" + %Q["#{city}"] + " and state_alpha=" + %Q["#{state}"] | |
row = db.get_first_row( query ) | |
if row != nil | |
puts row.join(",") | |
else | |
puts "not found" | |
end |