Script for Downloading NYS DHES LiDAR and Creating Contours and Hillshades

Here is a brief PHP script I use to download LiDAR data from NYGIS website. You simply supply it a list of quads on the command line like php getlidar.php u_3592573775_1m_DEM.img u_3600073175_1m_DEM.img then execute the below script. This script requires PHP and the dbase extension. If you use PHP7, you can download a version of dbase for PHP7 .

#!/usr/bin/php -q
 array_shift($argv); // remove program name

$lidarDir = "/home/andy/Documents/GIS.Data/lidar";

$tomerge = "";
$merged = "";
$conDir = "/tmp";

foreach ($argv as $quad) {
$wgetCommand = "wget -c -P $lidarDir \"ftp://ftporthos.dhses.ny.gov/lidar/nysdop/dem/$quad\"";
#echo "$wgetCommand\n";
system($wgetCommand);

$tomerge .= "$lidarDir/$quad ";

$merged = "merge".substr($quad,1,-4);
}

$mergeLidarCommand = "gdalwarp -co \"COMPRESS=LZW\" $tomerge $conDir/$merged.tif -r average -overwrite";
#echo "$mergeLidarCommand\n";
system($mergeLidarCommand);

// produce hillshade
system("gdaldem hillshade {$conDir}/{$merged}.tif {$conDir}/{$merged}hillshade.tif -z 2.0 -s 1.0 -az 315.0 -alt 45.0 -compute_edges");

// produce deeper hillshade
system("gdaldem hillshade {$conDir}/{$merged}.tif {$conDir}/{$merged}hillshade.6x.tif -z 6.0 -s 1.0 -az 315.0 -alt 45.0 -compute_edges &");

$minor = 5;
$major = 25;
$minorMeters = $minor * 0.3048;
$majorMeters = $major * 0.3048;

// produce contours
if (file_exists("{$conDir}/{$merged}{$major}ft-contour.shp")) {
unlink("{$conDir}/{$merged}{$major}ft-contour.shp");
}

if (file_exists("{$conDir}/{$merged}{$minor}ft-contour.shp")) {
unlink("{$conDir}/{$merged}{$minor}ft-contour.shp");
}

system("gdal_contour -a elev -i {$majorMeters} {$conDir}/{$merged}.tif {$conDir}/{$merged}{$major}ft-contour.shp");
system("gdal_contour -a elev -i {$minorMeters} {$conDir}/{$merged}.tif {$conDir}/{$merged}{$minor}ft-contour.shp");

// convert contour elevation to feet from meters

foreach (array("{$conDir}/{$merged}{$minor}ft-contour.dbf", "{$conDir}/{$merged}{$major}ft-contour.dbf") as $file) {
$db = dbase_open("$file", 2);

if ($db) {
$record_numbers = dbase_numrecords($db);
for ($i = 1; $i <= $record_numbers; $i++) {

// gets the old row
$row = dbase_get_record($db, $i);

// Update the date field with feet instead of meters
$row[1] = ceil($row[1] * 3.280839);

// remove the 'deleted' entry
unset($row['deleted']);

// replace record and save
dbase_replace_record($db, $row, $i);
}
}

dbase_close($db);
}

Leave a Reply

Your email address will not be published. Required fields are marked *