Ejemplo n.º 1
0
        public bool MoveNext()
        {
            if (++currentIndex >= indicies.Count)
            {
                return(false);
            }
            else
            {
                // Set current box to next item in collection.
                SpatialData data = new SpatialData();
                lock (EGIS.ShapeFileLib.ShapeFile.Sync)
                {
                    data.Id       = indicies[currentIndex].ToString(System.Globalization.CultureInfo.InvariantCulture);
                    data.Geometry = shapeFile.GetShapeDataD(indicies[currentIndex]).ToList();
                    if (shapeFile.ShapeType == ShapeType.PolyLineM)
                    {
                        data.Measures = shapeFile.GetShapeMDataD(indicies[currentIndex]).ToList();
                    }
                    else
                    {
                        data.Measures = null;
                    }
                    data.Attributes = new List <KeyValuePair <string, string> >();
                    string[] fieldNames = shapeFile.GetAttributeFieldNames();
                    string[] values     = shapeFile.GetAttributeFieldValues(indicies[currentIndex]);

                    for (int n = 0; n < values.Length; ++n)
                    {
                        data.Attributes.Add(new KeyValuePair <string, string>(fieldNames[n], values[n].Trim()));
                    }
                }
                this.current = data;
            }
            return(true);
        }
Ejemplo 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_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();
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private void BuildColorList(RenderSettings defaultSettings, string serialNbr, ShapeFile shpfile)
        {
            int fieldIndex = defaultSettings.DbfReader.IndexOfFieldName(serialNbr);

            if (fieldIndex >= 0)
            {
                colorList     = new List <System.Drawing.Color>();
                metersListGIS = new Dictionary <string, int>(40000);

                int numRecords = defaultSettings.DbfReader.DbfRecordHeader.RecordCount;
                for (int n = 0; n < numRecords; ++n)
                {
                    colorList.Add(Color.Red);
                    metersListGIS.Add(shpfile.GetAttributeFieldValues(n)[fieldIndex].Trim(), n);
                }
            }
            else
            {
                //MessageBox.Show("Couldn't find Field Name");
            }
        }
Ejemplo n.º 6
0
        private VectorTileLayer ProcessPointTile(ShapeFile shapeFile, int tileX, int tileY, int zoom, OutputTileFeatureDelegate outputTileFeature)
        {
            int        tileSize   = TileSize;
            RectangleD tileBounds = TileUtil.GetTileLatLonBounds(tileX, tileY, zoom, tileSize);

            //create a buffer around the tileBounds
            tileBounds.Inflate(tileBounds.Width * 0.05, tileBounds.Height * 0.05);

            int simplificationFactor = Math.Min(10, Math.Max(1, SimplificationPixelThreshold));

            System.Drawing.Point tilePixelOffset = new System.Drawing.Point((tileX * tileSize), (tileY * tileSize));

            List <int> indicies = new List <int>();

            shapeFile.GetShapeIndiciesIntersectingRect(indicies, tileBounds);
            GeometryAlgorithms.ClipBounds clipBounds = new GeometryAlgorithms.ClipBounds()
            {
                XMin = -20,
                YMin = -20,
                XMax = tileSize + 20,
                YMax = tileSize + 20
            };


            VectorTileLayer tileLayer = new VectorTileLayer();

            tileLayer.Extent  = (uint)tileSize;
            tileLayer.Version = 2;
            tileLayer.Name    = !string.IsNullOrEmpty(shapeFile.Name) ? shapeFile.Name : System.IO.Path.GetFileNameWithoutExtension(shapeFile.FilePath);

            if (indicies.Count > 0)
            {
                foreach (int index in indicies)
                {
                    if (outputTileFeature != null && !outputTileFeature(shapeFile, index, zoom, tileX, tileY))
                    {
                        continue;
                    }

                    VectorTileFeature feature = new VectorTileFeature()
                    {
                        Id           = index.ToString(System.Globalization.CultureInfo.InvariantCulture),
                        Geometry     = new List <List <Coordinate> >(),
                        Attributes   = new List <AttributeKeyValue>(),
                        GeometryType = Tile.GeomType.Point
                    };

                    //output the pixel coordinates
                    List <Coordinate> coordinates = new List <Coordinate>();
                    //get the point data
                    var recordPoints = shapeFile.GetShapeDataD(index);
                    foreach (PointD[] points in recordPoints)
                    {
                        for (int n = 0; n < points.Length; ++n)
                        {
                            Int64 x, y;
                            TileUtil.LLToPixel(points[n], zoom, out x, out y, tileSize);
                            coordinates.Add(new Coordinate((int)(x - tilePixelOffset.X), (int)(y - tilePixelOffset.Y)));
                        }
                    }
                    if (coordinates.Count > 0)
                    {
                        feature.Geometry.Add(coordinates);
                    }

                    //add the record attributes
                    string[] fieldNames = shapeFile.GetAttributeFieldNames();
                    string[] values     = shapeFile.GetAttributeFieldValues(index);
                    for (int n = 0; n < values.Length; ++n)
                    {
                        feature.Attributes.Add(new AttributeKeyValue(fieldNames[n], values[n].Trim()));
                    }

                    if (feature.Geometry.Count > 0)
                    {
                        tileLayer.VectorTileFeatures.Add(feature);
                    }
                }
            }

            return(tileLayer);
        }
Ejemplo n.º 7
0
        private VectorTileLayer ProcessPolygonTile(ShapeFile shapeFile, int tileX, int tileY, int zoom, OutputTileFeatureDelegate outputTileFeature)
        {
            int        tileSize   = TileSize;
            RectangleD tileBounds = TileUtil.GetTileLatLonBounds(tileX, tileY, zoom, tileSize);

            //create a buffer around the tileBounds
            tileBounds.Inflate(tileBounds.Width * 0.05, tileBounds.Height * 0.05);

            int simplificationFactor = Math.Min(10, Math.Max(1, SimplificationPixelThreshold));

            System.Drawing.Point tilePixelOffset = new System.Drawing.Point((tileX * tileSize), (tileY * tileSize));

            List <int> indicies = new List <int>();

            shapeFile.GetShapeIndiciesIntersectingRect(indicies, tileBounds);
            GeometryAlgorithms.ClipBounds clipBounds = new GeometryAlgorithms.ClipBounds()
            {
                XMin = -20,
                YMin = -20,
                XMax = tileSize + 20,
                YMax = tileSize + 20
            };

            List <System.Drawing.Point> clippedPolygon = new List <System.Drawing.Point>();


            VectorTileLayer tileLayer = new VectorTileLayer();

            tileLayer.Extent  = (uint)tileSize;
            tileLayer.Version = 2;
            tileLayer.Name    = !string.IsNullOrEmpty(shapeFile.Name) ? shapeFile.Name : System.IO.Path.GetFileNameWithoutExtension(shapeFile.FilePath);

            if (indicies.Count > 0)
            {
                foreach (int index in indicies)
                {
                    if (outputTileFeature != null && !outputTileFeature(shapeFile, index, zoom, tileX, tileY))
                    {
                        continue;
                    }

                    VectorTileFeature feature = new VectorTileFeature()
                    {
                        Id           = index.ToString(System.Globalization.CultureInfo.InvariantCulture),
                        Geometry     = new List <List <Coordinate> >(),
                        Attributes   = new List <AttributeKeyValue>(),
                        GeometryType = Tile.GeomType.Polygon
                    };

                    //get the point data
                    var recordPoints = shapeFile.GetShapeDataD(index);
                    int partIndex    = 0;
                    foreach (PointD[] points in recordPoints)
                    {
                        //convert to pixel coordinates;
                        if (pixelPoints.Length < points.Length)
                        {
                            pixelPoints           = new System.Drawing.Point[points.Length + 10];
                            simplifiedPixelPoints = new System.Drawing.Point[points.Length + 10];
                        }
                        int pointCount = 0;
                        for (int n = 0; n < points.Length; ++n)
                        {
                            Int64 x, y;
                            TileUtil.LLToPixel(points[n], zoom, out x, out y, tileSize);

                            pixelPoints[pointCount].X   = (int)(x - tilePixelOffset.X);
                            pixelPoints[pointCount++].Y = (int)(y - tilePixelOffset.Y);
                        }
                        ////check for duplicates points at end after they have been converted to pixel coordinates
                        ////polygons need at least 3 points so don't reduce less than this
                        //while(pointCount > 3 && (pixelPoints[pointCount-1] == pixelPoints[pointCount - 2]))
                        //{
                        //    --pointCount;
                        //}

                        int outputCount = 0;
                        SimplifyPointData(pixelPoints, null, pointCount, simplificationFactor, simplifiedPixelPoints, null, ref pointsBuffer, ref outputCount);
                        //simplifiedPixelPoints[outputCount++] = pixelPoints[pointCount-1];

                        if (outputCount > 1)
                        {
                            GeometryAlgorithms.PolygonClip(simplifiedPixelPoints, outputCount, clipBounds, clippedPolygon);

                            if (clippedPolygon.Count > 0)
                            {
                                //output the clipped polygon
                                List <Coordinate> lineString = new List <Coordinate>();
                                feature.Geometry.Add(lineString);
                                for (int i = clippedPolygon.Count - 1; i >= 0; --i)
                                {
                                    lineString.Add(new Coordinate(clippedPolygon[i].X, clippedPolygon[i].Y));
                                }
                            }
                        }
                        ++partIndex;
                    }

                    //add the record attributes
                    string[] fieldNames = shapeFile.GetAttributeFieldNames();
                    string[] values     = shapeFile.GetAttributeFieldValues(index);
                    for (int n = 0; n < values.Length; ++n)
                    {
                        feature.Attributes.Add(new AttributeKeyValue(fieldNames[n], values[n].Trim()));
                    }

                    if (feature.Geometry.Count > 0)
                    {
                        tileLayer.VectorTileFeatures.Add(feature);
                    }
                }
            }

            return(tileLayer);
        }
Ejemplo n.º 8
0
        private VectorTileLayer ProcessLineStringTile(ShapeFile shapeFile, int tileX, int tileY, int zoom, OutputTileFeatureDelegate outputTileFeature)
        {
            int        tileSize   = TileSize;
            RectangleD tileBounds = TileUtil.GetTileLatLonBounds(tileX, tileY, zoom, tileSize);

            //create a buffer around the tileBounds
            tileBounds.Inflate(tileBounds.Width * 0.05, tileBounds.Height * 0.05);

            int simplificationFactor = Math.Min(10, Math.Max(1, SimplificationPixelThreshold));

            System.Drawing.Point tilePixelOffset = new System.Drawing.Point((tileX * tileSize), (tileY * tileSize));

            List <int> indicies = new List <int>();

            shapeFile.GetShapeIndiciesIntersectingRect(indicies, tileBounds);
            GeometryAlgorithms.ClipBounds clipBounds = new GeometryAlgorithms.ClipBounds()
            {
                XMin = -20,
                YMin = -20,
                XMax = tileSize + 20,
                YMax = tileSize + 20
            };
            bool outputMeasureValues = this.OutputMeasureValues && (shapeFile.ShapeType == ShapeType.PolyLineM || shapeFile.ShapeType == ShapeType.PolyLineZ);

            System.Drawing.Point[] pixelPoints           = new System.Drawing.Point[1024];
            System.Drawing.Point[] simplifiedPixelPoints = new System.Drawing.Point[1024];
            double[] simplifiedMeasures = new double[1024];

            PointD[] pointsBuffer = new PointD[1024];

            VectorTileLayer tileLayer = new VectorTileLayer();

            tileLayer.Extent  = (uint)tileSize;
            tileLayer.Version = 2;
            tileLayer.Name    = !string.IsNullOrEmpty(shapeFile.Name) ? shapeFile.Name : System.IO.Path.GetFileNameWithoutExtension(shapeFile.FilePath);

            if (indicies.Count > 0)
            {
                foreach (int index in indicies)
                {
                    if (outputTileFeature != null && !outputTileFeature(shapeFile, index, zoom, tileX, tileY))
                    {
                        continue;
                    }

                    VectorTileFeature feature = new VectorTileFeature()
                    {
                        Id         = index.ToString(System.Globalization.CultureInfo.InvariantCulture),
                        Geometry   = new List <List <Coordinate> >(),
                        Attributes = new List <AttributeKeyValue>()
                    };

                    //get the point data
                    var recordPoints = shapeFile.GetShapeDataD(index);
                    System.Collections.ObjectModel.ReadOnlyCollection <double[]> recordMeasures = null;

                    if (outputMeasureValues)
                    {
                        recordMeasures = shapeFile.GetShapeMDataD(index);
                    }

                    List <double> outputMeasures = new List <double>();
                    int           partIndex      = 0;
                    foreach (PointD[] points in recordPoints)
                    {
                        double[] measures = recordMeasures != null ? recordMeasures[partIndex] : null;
                        //convert to pixel coordinates;
                        if (pixelPoints.Length < points.Length)
                        {
                            pixelPoints           = new System.Drawing.Point[points.Length + 10];
                            simplifiedPixelPoints = new System.Drawing.Point[points.Length + 10];
                            simplifiedMeasures    = new double[points.Length + 10];
                        }

                        for (int n = 0; n < points.Length; ++n)
                        {
                            Int64 x, y;
                            TileUtil.LLToPixel(points[n], zoom, out x, out y, tileSize);
                            pixelPoints[n].X = (int)(x - tilePixelOffset.X);
                            pixelPoints[n].Y = (int)(y - tilePixelOffset.Y);
                        }

                        int outputCount = 0;
                        SimplifyPointData(pixelPoints, measures, points.Length, simplificationFactor, simplifiedPixelPoints, simplifiedMeasures, ref pointsBuffer, ref outputCount);

                        //output count may be zero for short records at low zoom levels as
                        //the pixel coordinates wil be a single point after simplification
                        if (outputCount > 0)
                        {
                            List <int> clippedPoints = new List <int>();
                            List <int> parts         = new List <int>();

                            if (outputMeasureValues)
                            {
                                List <double> clippedMeasures = new List <double>();
                                GeometryAlgorithms.PolyLineClip(simplifiedPixelPoints, outputCount, clipBounds, clippedPoints, parts, simplifiedMeasures, clippedMeasures);
                                outputMeasures.AddRange(clippedMeasures);
                            }
                            else
                            {
                                GeometryAlgorithms.PolyLineClip(simplifiedPixelPoints, outputCount, clipBounds, clippedPoints, parts);
                            }
                            if (parts.Count > 0)
                            {
                                //output the clipped polyline
                                for (int n = 0; n < parts.Count; ++n)
                                {
                                    int index1 = parts[n];
                                    int index2 = n < parts.Count - 1 ? parts[n + 1] : clippedPoints.Count;

                                    List <Coordinate> lineString = new List <Coordinate>();
                                    feature.GeometryType = Tile.GeomType.LineString;
                                    feature.Geometry.Add(lineString);
                                    //clipped points store separate x/y pairs so there will be two values per measure
                                    for (int i = index1; i < index2; i += 2)
                                    {
                                        lineString.Add(new Coordinate(clippedPoints[i], clippedPoints[i + 1]));
                                    }
                                }
                            }
                        }
                        ++partIndex;
                    }

                    //add the record attributes
                    string[] fieldNames = shapeFile.GetAttributeFieldNames();
                    string[] values     = shapeFile.GetAttributeFieldValues(index);
                    for (int n = 0; n < values.Length; ++n)
                    {
                        feature.Attributes.Add(new AttributeKeyValue(fieldNames[n], values[n].Trim()));
                    }
                    if (outputMeasureValues)
                    {
                        string s = Newtonsoft.Json.JsonConvert.SerializeObject(outputMeasures, new DoubleFormatConverter(4));
                        feature.Attributes.Add(new AttributeKeyValue(this.MeasuresAttributeName, s));
                    }

                    if (feature.Geometry.Count > 0)
                    {
                        tileLayer.VectorTileFeatures.Add(feature);
                    }
                }
            }
            return(tileLayer);
        }
Ejemplo n.º 9
0
        public static TigerLookupAllData LookupAll(double latitude, double longitude)
        {
            var result = new TigerLookupAllData();

            lock (ShapeFile.Sync)
            {
                var countyIndex = CountyShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (countyIndex < 0)
                {
                    return(result);
                }
                var countyValues = CountyShapeFile.GetAttributeFieldValues(countyIndex);
                result.Fips      = countyValues[0].Trim();
                result.StateCode = StateCache.StateCodeFromLdsStateCode(result.Fips);
                result.County    = countyValues[1].Trim();

                var congressIndex =
                    CongressShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (congressIndex >= 0)
                {
                    var congressValues = CongressShapeFile.GetAttributeFieldValues(congressIndex);
                    result.Congress = Translate(TableTypeCongressionalDistrict, result.StateCode,
                                                congressValues[1].Trim());
                }

                var upperIndex = UpperShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (upperIndex >= 0)
                {
                    var upperValues = UpperShapeFile.GetAttributeFieldValues(upperIndex);
                    result.Upper = Translate(TableTypeStateSenate, result.StateCode,
                                             upperValues[1].Trim());
                }

                var lowerIndex = LowerShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (lowerIndex >= 0)
                {
                    var lowerValues = LowerShapeFile.GetAttributeFieldValues(lowerIndex);
                    result.Lower = Translate(TableTypeStateHouse, result.StateCode,
                                             lowerValues[1].Trim());
                }

                var districtIndex =
                    DistrictShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (districtIndex >= 0)
                {
                    var districtValues = DistrictShapeFile.GetAttributeFieldValues(districtIndex);
                    result.District         = districtValues[2].Trim();
                    result.DistrictName     = districtValues[5].Trim();
                    result.DistrictLongName = districtValues[6].Trim();
                }

                var placeIndex =
                    PlaceShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (placeIndex >= 0)
                {
                    var placeValues = PlaceShapeFile.GetAttributeFieldValues(placeIndex);
                    result.Place         = placeValues[1].Trim();
                    result.PlaceName     = placeValues[4].Trim();
                    result.PlaceLongName = placeValues[5].Trim();
                }

                var elementaryIndex =
                    ElementaryShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (elementaryIndex >= 0)
                {
                    var elementaryValues = ElementaryShapeFile.GetAttributeFieldValues(elementaryIndex);
                    result.Elementary     = elementaryValues[1].Trim();
                    result.ElementaryName = elementaryValues[3].Trim();
                }

                var secondaryIndex =
                    SecondaryShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (secondaryIndex >= 0)
                {
                    var secondaryValues = SecondaryShapeFile.GetAttributeFieldValues(secondaryIndex);
                    result.Secondary     = secondaryValues[1].Trim();
                    result.SecondaryName = secondaryValues[3].Trim();
                }

                var unifiedIndex = UnifiedShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (unifiedIndex >= 0)
                {
                    var unifiedValues = UnifiedShapeFile.GetAttributeFieldValues(unifiedIndex);
                    result.Unified     = unifiedValues[1].Trim();
                    result.UnifiedName = unifiedValues[3].Trim();
                }

                var cityCouncilIndex = CityCouncilShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (cityCouncilIndex >= 0)
                {
                    var cityCouncilValues = CityCouncilShapeFile.GetAttributeFieldValues(cityCouncilIndex);
                    result.CityCouncil         = cityCouncilValues[1].Trim();
                    result.CityCouncilDistrict = cityCouncilValues[3].Trim();
                }

                var countySupervisorsIndex = CountySupervisorsShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0);
                if (countySupervisorsIndex >= 0)
                {
                    var countySupervisorsValues = CountySupervisorsShapeFile.GetAttributeFieldValues(countySupervisorsIndex);
                    result.CountySupervisors         = countySupervisorsValues[2].Trim();
                    result.CountySupervisorsDistrict = countySupervisorsValues[3].Trim();
                }
            }
            return(result);
        }
Ejemplo n.º 10
0
        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}"
                }
            }