/******************************************************************************* * FILE: * * Downloads USGS geotiff quads from http://www.archive.org/ * * @date 21/02/2012 * */ using System; using System.IO; using System.Net; using System.Collections.Generic; namespace usgsdown { class MainClass { public static int Main (string[] args) { string rootDir, stateCode, stateName; char gll, glh; int startLat, startLon, width, height; int gnl, gnh; stateCode = "id"; startLat = 42; startLon = 111; width = 1; height = 1; gll = 'a'; glh = 'h'; gnl = 1; gnh = 8; rootDir = "/home/kas/maps/maps/geotiff/usgs_24k"; rootDir = "/media/Expansion Drive/maps/maps/geotiff/usgs_24k"; foreach (string s in args) { if (s.StartsWith("dir=") && s.Length > 4) { rootDir = s.Substring(4); } else if (s.StartsWith("state=") && s.Length > 6) { stateCode = s.Substring(6); } else if (s.StartsWith("lat=") && s.Length > 4) { startLat = int.Parse(s.Substring(4)); } else if (s.StartsWith("lon=") && s.Length > 4) { startLon = int.Parse(s.Substring(4)); } else if (s.StartsWith("width=") && s.Length > 6) { width = int.Parse(s.Substring(6)); } else if (s.StartsWith("height=") && s.Length > 7) { height = int.Parse(s.Substring(7)); } else if (s.StartsWith("gnl=") && s.Length > 4) { gnl = int.Parse(s.Substring(4)); } else if (s.StartsWith("gnh=") && s.Length > 4) { gnh = int.Parse(s.Substring(4)); } else if (s.StartsWith("gll=") && s.Length > 4) { gll = char.Parse(s.Substring(4)); } else if (s.StartsWith("glh=") && s.Length > 4) { glh = char.Parse(s.Substring(4)); } } Dictionary states = new Dictionary(); states.Add("az", "arizona"); states.Add("ca", "california"); states.Add("co", "colorado"); states.Add("ct", "connecticut"); states.Add("de", "delaware"); states.Add("hi", "hawaii"); states.Add("id", "idaho"); states.Add("md", "maryland"); states.Add("me", "maine"); states.Add("mt", "montana"); states.Add("nd", "north dakota"); states.Add("nh", "new hampshire"); states.Add("nj", "new jersey"); states.Add("nm", "new mexico"); states.Add("nv", "nevada"); states.Add("ny", "new york"); states.Add("or", "oregon"); states.Add("pa", "pennsylvania"); states.Add("ri", "rhode island"); states.Add("sd", "south dakota"); states.Add("tx", "texas"); states.Add("ut", "utah"); states.Add("vt", "vermont"); states.Add("wa", "washington"); states.Add("wy", "wyoming"); if (states.ContainsKey(stateCode)) stateName = states[stateCode]; else stateName = "unknown"; System.Console.WriteLine("DIRECTORY : \"" + rootDir + "\""); System.Console.WriteLine("STATE : \"" + stateCode + " - " + stateName + "\""); System.Console.WriteLine("LATITUDE : " + startLat); System.Console.WriteLine("LONGITUDE : " + startLon); System.Console.WriteLine("Blocks : " + width + " x " + height); System.Console.WriteLine("Files : " + gll + " - " + glh + ", " + gnl + " - " + gnh); System.Console.WriteLine(""); if (!states.ContainsKey(stateCode)) { System.Console.WriteLine("Unknown state - terminating\n"); return(1); } string localDir = rootDir + "/" + stateCode + " - " + stateName; if (!Directory.Exists(localDir)) { System.Console.WriteLine("localDir \"" + localDir + "\" does not exist\n"); return(1); } for (int lat = startLat; lat < startLat + height; lat++) { for (int lon = startLon; lon < startLon + width; lon++) { // a-h, 1-8 for (char gridLet = gll; gridLet <= glh; gridLet++) { for (int gridNum = gnl; gridNum <= gnh; gridNum += 1) { if (getUSGS24kFile(localDir, stateCode, lat, lon, gridLet, gridNum) != 0) { //return(1); } } } } } return(0); } // private static int getUSGS24kFile(string localDir, string stateCode, int lat, int lon, char gridLet, int gridNum) { string tifFile, remoteFile, localFile; string latlon, gridRef; if (lon >= 100) latlon = "" + lat + "" + lon; else latlon = "" + lat + "0" + lon; gridRef = "" + gridLet + "" + gridNum; tifFile = "o" + latlon + "" + gridRef + ".tif"; localFile = localDir + "/" + tifFile; remoteFile = "http://www.archive.org/download/usgs_drg_" + stateCode + "_" + latlon + "_" + gridRef + "/" + tifFile; if (File.Exists (localFile)) { Console.WriteLine ("File " + localFile + " exists"); } else { Console.WriteLine ("Downloading file " + localFile); Console.WriteLine ("Remote: " + remoteFile); WebClient wc = new WebClient (); wc.Proxy = null; try { wc.DownloadFile(remoteFile, localFile); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.NameResolutionFailure) { Console.WriteLine("** - Name Resolution Failure"); File.Delete(localFile); } else if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse response = (HttpWebResponse) ex.Response; Console.WriteLine(" ** - Protocol Error - " + (int) response.StatusCode + ": " + response.StatusDescription); } else { Console.WriteLine(ex); if (File.Exists (localFile)) File.Delete(localFile); return(1); } } try { FileInfo fi = new FileInfo (localFile); if (fi.Exists && fi.Length < 99999) { Console.WriteLine ("Deleting " + localFile); File.Delete(localFile); } } catch (Exception ex) { Console.WriteLine(ex); File.Delete(localFile); } } return(0); } } } /****************************************************************************** * ( ( ( * * ) ) ) * * ( ( ( -- End of module -- * * '. ___ .' * * ' (> <) ' * *************ooO*(_)*Ooo******************************************************/