Download Census.gov Tiger/Line shape files (2009 Edition)

by justin carlson on 12/21/2009
PHPUse the php script or wget example to pull down census.gov Tiger/Line shape files. It would be easier to just use wget, but I'm using windows.

If you can use wget, do:

wget -A "*.zip" -r ftp://ftp2.census.gov/geo/tiger/TIGER2009/

Otherwise, you can use this simple php script on the command line:

<?php

/**
 * Resumable directory download, recursive.
 * Quick snippet to download tiger/line shape files.
 *
 * Call on the command line: php foo.php ftp://ftp2.census.gov/geo/tiger/TIGER2009/
 *
 */

archget($argv[1]);

function archget($path, $original = null, $depth = "\t") {

    if (substr($path,-1)!='/') {
        $path.= '/';
    }
   
    if (is_null($original)) {
        $original = $path;
    }
   
    $handle = opendir($path);
    while ( $file = readdir($handle) ) {
        if (is_dir($path . $file)) {
           
            echo $depth . "Entering directory: " . $path . $file . '/' . "\r\n";
           
            $target = str_replace($original, '', $path . $file);
            if (! is_dir($target)) {
                mkdir($target);
            }
           
            archlist($path . $file . '/', $original, $depth . "\t");
       
        } else {
           
            $target = str_replace($original, '', $path . $file);
            if (file_exists($target) && filesize($target) != filesize($path . $file)) {
                unlink($target);
            }
            if (! file_exists($target)) {
               
                echo $depth . " downloading $target\r\n";
                $ch = curl_init($path . $file);
                $fp = fopen($target, "w");
                curl_setopt($ch, CURLOPT_FILE, $fp);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_exec($ch);
                echo curl_close($ch);
                fclose($fp);
           
            }
       
        }
    }

}
?>

Trackback

Trackback URL for this entry: http://www.tehuber.com/trackback.php?id=20091221134405604

No trackback comments for this entry.
Download Census.gov Tiger/Line shape files (2009 Edition) | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.