Exemplo n.º 1
0
        private static void Main()
        {
            const string dir           = @"D:\Users\CurtNew\Dropbox\Documents\vote\tiger";
            var          shapeFile     = new ShapeFile(Path.Combine(dir, @"2016\tl_2016_us_cousub\tl_2016_us_cousub.shp"));
            var          enumerator    = shapeFile.GetShapeFileEnumerator();
            var          districtInfos = new List <DistrictInfo>();

            while (enumerator.MoveNext())
            {
                var fieldValues = shapeFile.GetAttributeFieldValues(enumerator.CurrentShapeIndex);
                var stateFips   = fieldValues[0].Trim();
                var stateCode   = StateCache.StateCodeFromLdsStateCode(stateFips);
                var countyFips  = fieldValues[1].Trim();
                districtInfos.Add(new DistrictInfo
                {
                    StateFips        = stateFips,
                    CountyFips       = countyFips,
                    DistrictFips     = fieldValues[2].Trim(),
                    DistrictName     = fieldValues[5].Trim(),
                    DistrictLongName = fieldValues[6].Trim(),
                    StateCode        = stateCode,
                    StateName        = StateCache.GetStateName(stateCode),
                    CountyName       = CountyCache.GetCountyName(stateCode, countyFips)
                });
            }
            var textWriter = new StreamWriter(Path.Combine(dir, @"districts.csv"));
            var csvWriter  = new SimpleCsvWriter();

            csvWriter.AddField("State FIPS");
            csvWriter.AddField("County FIPS");
            csvWriter.AddField("District FIPS");
            csvWriter.AddField("State Code");
            csvWriter.AddField("State Name");
            csvWriter.AddField("County Name");
            csvWriter.AddField("District Name");
            csvWriter.AddField("District Long Name");
            csvWriter.Write(textWriter);
            foreach (
                var district in
                districtInfos.OrderBy(d => d.StateName)
                .ThenBy(d => d.CountyName)
                .ThenBy(d => d.DistrictLongName))
            {
                csvWriter.AddField(district.StateFips);
                csvWriter.AddField(district.CountyFips);
                csvWriter.AddField(district.DistrictFips);
                csvWriter.AddField(district.StateCode);
                csvWriter.AddField(district.StateName);
                csvWriter.AddField(district.CountyName);
                csvWriter.AddField(district.DistrictName);
                csvWriter.AddField(district.DistrictLongName);
                csvWriter.Write(textWriter);
            }
            textWriter.Close();
        }
Exemplo n.º 2
0
        private static void Main()
        {
            const string dir        = @"D:\Users\CurtNew\Dropbox\Documents\vote\tiger";
            var          shapeFile  = new ShapeFile(Path.Combine(dir, @"2016\tl_2016_us_place\tl_2016_us_place.shp"));
            var          enumerator = shapeFile.GetShapeFileEnumerator();
            var          placeInfos = new List <PlaceInfo>();

            while (enumerator.MoveNext())
            {
                var fieldValues = shapeFile.GetAttributeFieldValues(enumerator.CurrentShapeIndex);
                var stateFips   = fieldValues[0].Trim();
                var stateCode   = StateCache.StateCodeFromLdsStateCode(stateFips);
                placeInfos.Add(new PlaceInfo
                {
                    StateFips     = stateFips,
                    PlaceFips     = fieldValues[1].Trim(),
                    PlaceName     = fieldValues[4].Trim(),
                    PlaceLongName = fieldValues[5].Trim(),
                    StateCode     = stateCode,
                    StateName     = StateCache.GetStateName(stateCode)
                });
            }
            var textWriter = new StreamWriter(Path.Combine(dir, @"places.csv"));
            var csvWriter  = new SimpleCsvWriter();

            csvWriter.AddField("State FIPS");
            csvWriter.AddField("Place FIPS");
            csvWriter.AddField("State Code");
            csvWriter.AddField("State Name");
            csvWriter.AddField("Place Name");
            csvWriter.AddField("Place Long Name");
            csvWriter.Write(textWriter);
            foreach (
                var district in
                placeInfos.OrderBy(d => d.StateName)
                .ThenBy(d => d.PlaceLongName))
            {
                csvWriter.AddField(district.StateFips);
                csvWriter.AddField(district.PlaceFips);
                csvWriter.AddField(district.StateCode);
                csvWriter.AddField(district.StateName);
                csvWriter.AddField(district.PlaceName);
                csvWriter.AddField(district.PlaceLongName);
                csvWriter.Write(textWriter);
            }
            textWriter.Close();
        }
Exemplo n.º 3
0
        private static void Analyze(string stateFips, string tigerFolderPath)
        {
            var countyShapeFile = new ShapeFile(Path.Combine(tigerFolderPath,
                                                             @"tl_2016_us_county\tl_2016_us_county.shp"));
            var unifiedShapeFile = new ShapeFile(Path.Combine(tigerFolderPath,
                                                              @"tl_2016_us_unsd\tl_2016_us_unsd.shp"));
            var unifiedEnumerator = unifiedShapeFile.GetShapeFileEnumerator();

            while (unifiedEnumerator.MoveNext())
            {
                var unifiedFieldValues =
                    unifiedShapeFile.GetAttributeFieldValues(unifiedEnumerator.CurrentShapeIndex);
                var st = unifiedFieldValues[0].Trim();
                if (stateFips == null || stateFips == st)
                {
                    var unifiedBounds    = unifiedShapeFile.GetShapeBoundsD(unifiedEnumerator.CurrentShapeIndex);
                    var unifiedData      = unifiedShapeFile.GetShapeDataD(unifiedEnumerator.CurrentShapeIndex);
                    var countyEnumerator = countyShapeFile.GetShapeFileEnumerator(unifiedBounds);
                    var inCounties       = new List <string>();
                    while (countyEnumerator.MoveNext())
                    {
                        var countyFieldValues = countyShapeFile.GetAttributeFieldValues(countyEnumerator.CurrentShapeIndex);
                        var inCounty          = false;
                        var countyData        =
                            countyShapeFile.GetShapeDataD(countyEnumerator.CurrentShapeIndex);
                        foreach (var c in countyData)
                        {
                            foreach (var p in unifiedData)
                            {
                                if (GeometryAlgorithms.PolygonPolygonIntersect(c, c.Length,
                                                                               p, p.Length))
                                {
                                    inCounty = true;
                                }
                            }
                        }
                        if (inCounty)
                        {
                            inCounties.Add(countyFieldValues[1]); // fips
                        }
                    }
                }
            }
        }
        private void AuditSchoolDistricts(ShapeFile shapefile, string tigerType, string description)
        {
            if (shapefile == null)
            {
                throw new Exception("Could not find the shapefile");
            }
            var enumerator = shapefile.GetShapeFileEnumerator();
            var list       = new List <string[]>();

            while (enumerator.MoveNext())
            {
                list.Add(shapefile.GetAttributeFieldValues(enumerator.CurrentShapeIndex));
            }
            // reproject into nicely-named structure and eliminate "F" (ficticious entry)
            var shapefileData = list
                                .Select(i => new
            {
                StateCode = StateCache.StateCodeFromLdsStateCode(i[0].Trim()),
                Id        = i[1].Trim(),
                Name      = i[3].Trim(),
                FuncStat  = i[9].Trim()
            }).Where(i => i.FuncStat != "F").ToList();

            var tigerSchoolsData        = TigerSchools.GetDataByTigerType(tigerType);
            var tigerPlacesCountiesData = TigerPlacesCounties.GetDataByTigerType(tigerType);

            // District is in the shapefile, but is not in TigerSchools.
            var notInDatabase = shapefileData
                                .Where(s => !tigerSchoolsData.Any(d => d.StateCode == s.StateCode &&
                                                                  d.TigerCode == s.Id)).ToList();

            // District is in TigerSchools, but not in the shapefile.
            var notInShapefile = tigerSchoolsData
                                 .Where(d => !shapefileData.Any(s => s.StateCode == d.StateCode &&
                                                                s.Id == d.TigerCode)).ToList();

            // Check TigerSchools in TigerPlacesCounties
            var notInTigerPlacesCounties = tigerSchoolsData.Where(s =>
                                                                  !tigerPlacesCountiesData.Any(c =>
                                                                                               c.StateCode == s.StateCode && c.TigerCode == s.TigerCode)).ToList();

            // Check TigerPlacesCounties in TigerSchools
            var notInTigerSchools = tigerPlacesCountiesData.Where(c =>
                                                                  !tigerSchoolsData.Any(s =>
                                                                                        s.StateCode == c.StateCode && s.TigerCode == c.TigerCode)).ToList();

            var results = new HtmlDiv().AddTo(ResultsPlaceHolder, "results");

            new HtmlP {
                InnerText = $"Audit of {description} School District ShapeFile"
            }.AddTo(results, "head");
            new HtmlP {
                InnerText = $"There are {shapefileData.Count} entries in the shapefile."
            }
            .AddTo(results);
            new HtmlP
            {
                InnerText =
                    $"There are {tigerSchoolsData.Count} district entries and " +
                    $"{tigerPlacesCountiesData.Count} county entries in the database."
            }.AddTo(results);

            new HtmlP
            {
                InnerText =
                    $"{notInDatabase.Count} entries are in the shapefile, but not in the database."
            }.AddTo(results);
            foreach (var i in notInDatabase)
            {
                new HtmlP {
                    InnerText = $"{i.StateCode}:{i.Id} {i.Name}"
                }
            }