jeffbrand.net

tagline in progress

Importing MaxMind’s World Cities Data to MySQL

One of the inevitable early steps in the development of any location-aware app is linking location in the form of latitude and longitude to a city, state, and country. This short tutorial will describe the few steps needed to import MaxMind’s World Cities database into a table in your own MySQL database. These steps assume you have command-line access to your MySQL server.

Get the Data

Be sure to read MaxMind’s specifications about the content and use of their data. Then, download the latest file and extract it:

$ wget http://www.maxmind.com/download/worldcities/worldcitiespop.txt.gz
$ gunzip worldcitiespop.txt.gz

Prep the Database

Connect to your database server and get the table structure ready.

$ mysql -h localhost -u dbuser -pdbpass myappdatabase

Depending on the application, it may make sense to index additional columns than the statement below.

mysql>
CREATE TABLE cities (
    country       CHAR(2),
    city_ascii    VARCHAR(100),
    city          VARCHAR(255),
    region        CHAR(2),
    population    INT UNSIGNED,
    latitude      DECIMAL(10, 6),
    longitude     DECIMAL(10, 6),
    INDEX idx_lat_long (latitude, longitude),
    INDEX idx_country (country),
    INDEX idx_region (region)
);

Import the Data

I learned about this powerful command over at tech-recipes.com. Learn more about the options for the LOAD DATA command in MySQL’s documentation.

mysql>
LOAD DATA LOCAL INFILE 'worldcitiespop.txt' INTO TABLE cities
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(country, city_ascii, city, region, population, latitude, longitude);

Now sit back and wait… At the time of this writing, the database has over 3.1 million rows. On a local windows-based development laptop it took about 8 minutes, but under a minute on a dedicated database server. This could vary depending on available system resources.

That’s it!

When the command completes the data will be ready to use. Interestingly enough, MaxMind’s page references a similar free database from GeoNames. After a brief look at the documentation, it looks like there’s a lot more data that could be useful.


State of the art in CAPTCHAs?

CAPTCHAs are a necessary evil. These days there is no lower time-investment trade-off that works as well to prevent automated submissions to comment sections, user registration, and contact forms. That being said, they are not without issues. Read the rest of this entry »


The Honda Amigo

This past winter I purchased a PF50R Honda Amigo moped. I quickly learned that this vehicle is relatively rare in America. While I was able to find parts and service manuals, most of the reference materials and parts websites are in Dutch. It seems this model is very popular in Holland and Belgium (where mine was manufactured.)

Read the rest of this entry »


Something like Spring

Perhaps it wouldn’t be fair to call it hibernating, given the mild winter.

The Iditarod is still winding down and I have a ton on my plate for the coming months, but for the first time in many months I have time to think about the road ahead.

It’s not like I have the wildest plans: scooter/moped/bicycle repair and riding are definitely in the plan. A number of other projects, personal and professional have been waiting for March to arrive and can now get some attention.


Free Wifi Diagnostic Tool: inSSIDer

inSSIDer is a handy free tool for surveying wireless access points around you. Similar to Wifi Analyzer on the Android, this tool displays signal strength and channel usage graphs for all hotspots in the area. Use it to find the channel with the least interference, identify conflicting SSIDs, or just learn more about network hardware in your area.

This app is free and open source under an Apache license and is available for Windows and Linux.