public bool DownloadAndImportShapefile(string desiredUrl, string destFilepath, string niceName, string tablename)
{
_log.DebugFormat("Downloading shapefile of {0} for {1}", niceName, this.State);
if (!FileDownloader.GetFileByURL(desiredUrl, destFilepath, ref this._cancelled, WorkOffline))
{
_log.ErrorFormat("Shapefile download failed: Could not download {0}", niceName);
return false;
}
if (!FileLocator.ExpandZipFile(destFilepath, this.ShapePath))
{
_log.ErrorFormat("Shapefile import failed: Could not decompress {0}", niceName);
return false;
}
_log.DebugFormat("State {0} decompressed successfully", niceName);
var client = DbClient;
using (var conn = client.GetConnection())
{
if (DataClient.HasTable(conn, client, tablename))
{
DbClient.GetCommand(string.Format("DROP TABLE IF EXISTS \"{0}\";", tablename), conn).ExecuteNonQuery();
}
{
_log.DebugFormat("{0} table not found, importing...", tablename);
var filenames = FileUtilities.FindFileNameInZipLike(destFilepath, "*.shp");
if ((filenames == null) || (filenames.Count == 0))
{
_log.ErrorFormat("Compressed file {0} didn't contain a shapefile!", destFilepath);
return false;
}
foreach (string filename in filenames)
{
string fullShapefilename = Path.Combine(this.ShapePath, filename);
ShapefileHelper.MakeCensusProjFile(fullShapefilename);
ShapefileHelper.ImportShapefile(conn, this.DbClient,
fullShapefilename, tablename, 4269);
//TODO: multiple shape files in one zip?
break;
}
}
}
return true;
}