Beispiel #1
0
        public static FipsSubCountyEntry GetSubCounty(string subCountyCode)
        {
            if (!string.IsNullOrEmpty(subCountyCode))
            {
                if (subCountyCode.ToUpper() == "N")
                {
                    return(NotApplicableSubCounty());
                }

                return(FipsSubCodes.FirstOrDefault(s => s.SubCountyCode == subCountyCode)
                       ?? UnknownSubCounty(subCountyCode));
            }

            return(UnknownSubCounty("Unspecified"));
        }
Beispiel #2
0
        static void LoadCache()
        {
            try
            {
                var fipsData = System.IO.File.ReadAllLines(FipsSubCountyResource);

                var fipsLines = fipsData.Select(l => l.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));

                FipsCodes = fipsLines.Select(l => new FipsCountyEntry()
                {
                    StateName  = l[0],
                    StateCode  = l[1],
                    CountyCode = l[2],
                    CountyName = l[3],
                    StatusCode = l[4]
                })
                            .ToList();

                var fipsSubData = System.IO.File.ReadAllLines(FipsCountyResource);

                foreach (var line in fipsSubData)
                {
                    if (line.Trim().Length == 0)
                    {
                        continue;
                    }

                    var cols = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                    if (cols.Length != 7 && cols.Length != 8)
                    {
                        Debugger.Break();
                    }

                    if (cols.Length == 8)
                    {
                        FipsSubCodes.Add(new FipsSubCountyEntry()
                        {
                            StateName        = cols[0],
                            StateCode        = cols[1],
                            CountyCode       = cols[2],
                            CountyName       = cols[3],
                            SubCountyCode    = cols[4],
                            SubCountyName    = $"{cols[5]}, {cols[6]}",
                            FunctionalStatus = cols[7]
                        });
                    }
                    else if (cols.Length == 7)
                    {
                        FipsSubCodes.Add(new FipsSubCountyEntry()
                        {
                            StateName        = cols[0],
                            StateCode        = cols[1],
                            CountyCode       = cols[2],
                            CountyName       = cols[3],
                            SubCountyCode    = cols[4],
                            SubCountyName    = cols[5],
                            FunctionalStatus = cols[6]
                        });
                    }
                    else
                    {
                        Debugger.Break();
                    }
                }

                foreach (var code in FipsCodes)
                {
                    var thisState = FIPS.States.FirstOrDefault(s => s.Name == code.StateName);

                    if (thisState == null)
                    {
                        thisState = new FipsState()
                        {
                            Name = code.StateName, Code = code.StateCode
                        };
                        FIPS.States.Add(thisState);
                    }

                    thisState.Counties.Add(new FipsCounty()
                    {
                        Name = code.CountyName, Code = code.CountyCode, StatusCode = code.StatusCode, State = thisState
                    });
                }

                foreach (var code in FipsSubCodes)
                {
                    var thisState = FIPS.States.FirstOrDefault(s => s.Name == code.StateName);

                    var thisCounty = thisState?.Counties.FirstOrDefault(c => c.Code == code.CountyCode);

                    thisCounty?.SubCounties.Add(new FipsSubCounty()
                    {
                        County           = thisCounty,
                        Name             = code.SubCountyName,
                        Code             = code.SubCountyCode,
                        FunctionalStatus = code.FunctionalStatus
                    });
                }

                Debug.WriteLine("(Loaded FIPS data)\n");
            }
            catch (Exception ex)
            {
                throw new Exception($"Unable to read FIPS files to populate Cache: {ex.Message}", ex);
            }
        }