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

IsForbiddenShapefileName() public static method

Checks to make sure a given table name doesn't conflict with any of the census shapefiles
public static IsForbiddenShapefileName ( string name ) : bool
name string
return bool
        public static bool IsForbiddenShapefileName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return true;
            }
            var levels = (new List<string> {
                Settings.ShapeFileBlockGroupFilename,
                Settings.ShapeFileTractFilename,
                Settings.ShapeFileCountySubdivisionsFilename,
                Settings.ShapeFileVotingFilename,
                Settings.ShapeFileThreeDigitZipsFilename,
                Settings.ShapeFileFiveDigitZipsFilename,
                Settings.ShapeFileCountiesFilename
                }).Where(s => !string.IsNullOrEmpty(s));

            StringBuilder newRegex = new StringBuilder(512);
            foreach (string level in levels)
            {
                string temp = level.Replace(Settings.FipsPlaceholder, "\\d{2}").Replace("_shp.zip", "");
                newRegex.Append("^").Append(temp).Append("$").Append("|");
            }

            var regex = new Regex(newRegex.ToString(0, newRegex.Length - 1));
            return regex.IsMatch(name.ToLower().Replace(".shp", ""));
        }

Usage Example

        /// <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);
        }