/// <summary>
/// Imports a shapefile into the database. Do not use this to load census shapefiles.
/// </summary>
/// <param name="filename">The path of the file to import</param>
/// <param name="DbClient">The database to load the file into</param>
/// <returns>True on success, False on failure</returns>
public static bool LoadShapefile(string filename, string tableName, IDataClient DbClient, out ICoordinateSystem CRS)
{
CRS = null;
if ((string.IsNullOrEmpty(filename)) || (!File.Exists(filename)))
{
_log.ErrorFormat("LoadShapefile failed: filename was empty or file does not exist {0}", filename);
return(false);
}
try
{
string fileWithoutExt = Path.GetFileNameWithoutExtension(filename);
string prjFileName = Path.Combine(Path.GetDirectoryName(filename), fileWithoutExt) + ".prj";
if (ShapefileHelper.IsForbiddenShapefileName(fileWithoutExt))
{
_log.ErrorFormat("LoadShapefile failed: {0} conflicts with a reserved census shapefile name, please rename", fileWithoutExt);
return(false);
}
if (File.Exists(prjFileName))
{
CRS = Utilities.GetCoordinateSystemByWKTFile(prjFileName);
}
else
{
_log.ErrorFormat("LoadShapefile failed: shapefile {0} is missing a .prj file.", filename);
return(false);
}
using (DbConnection conn = DbClient.GetConnection())
{
if (!ShapefileHelper.ImportShapefile(conn, DbClient, filename, tableName, (int)CRS.AuthorityCode))
{
_log.Error("LoadShapefile failed: unable to import shapefile.");
return(false);
}
}
_log.Debug("Shapefile imported successfully...");
return(true);
}
catch (Exception ex)
{
_log.Error("Error while importing shapefile", ex);
}
return(false);
}