public static bool ImportShapefile(DbConnection conn, IDataClient client, string filename, string tableName, int srid)
{
try
{
if (!File.Exists(filename))
{
_log.ErrorFormat("ImportShapefile Failed!: File does not exist {0}", filename);
return false;
}
if (DataClient.HasTable(conn, client, tableName))
{
client.GetCommand(string.Format("DROP TABLE \"{0}\"", tableName)).ExecuteNonQuery();
}
//trim off the '.shp' from the end
filename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename));
string sql = string.Format("CREATE VIRTUAL TABLE " + tableName + " USING VirtualShape('{0}', CP1252, {1});", filename, srid);
client.GetCommand(sql, conn).ExecuteNonQuery();
_log.DebugFormat("Imported Shapefile {0} into table {1}",
Path.GetFileNameWithoutExtension(filename),
tableName);
return true;
}
catch (Exception ex)
{
_log.Error("ImportShapefile failed: Error while loading shapefile ", ex);
}
return false;
}