Azavea.NijPredictivePolicing.ACSAlchemistLibrary.FileFormats.ShapefileHelper.ImportShapefile C# (CSharp) Method

ImportShapefile() public static method

Imports the provided shapefile into a sqlite database using the VirtualShape extension
public static ImportShapefile ( DbConnection conn, IDataClient client, string filename, string tableName, int srid ) : bool
conn System.Data.Common.DbConnection
client IDataClient
filename string
tableName string
srid int
return bool
        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;
        }

Usage Example

        /// <summary>
        /// Helper function for testing the shapefile importer
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static bool OpenShapefile(string filename, string tableName)
        {
            string databaseFileName = Path.Combine(Path.GetDirectoryName(filename), "shape.dat");
            var    client           = new SqliteDataClient(databaseFileName);

            using (DbConnection conn = client.GetConnection())
            {
                return(ShapefileHelper.ImportShapefile(conn, client, filename, tableName, -1));
            }
        }
All Usage Examples Of Azavea.NijPredictivePolicing.ACSAlchemistLibrary.FileFormats.ShapefileHelper::ImportShapefile