Tuesday, May 1, 2012

Where are the gazetteers?

Last time I checked it was 2012 and given all the excitement about open data in the US government, I would have expected the USGS (or anyone) providing a friendly GNIS based gazetteer service. I send the service a place name and a state and it returns a coordinate pair.


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

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
view raw gnis.sql hosted with ❤ by GitHub
And here's a quick ruby script to query the database.

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
view raw gnis_query.rb hosted with ❤ by GitHub