Beispiel #1
0
        public _prop_SnapLtrans getAllData(double lat, double lon, double radius)
        {
            _prop_SnapLtrans     data_gis = null;
            PolylineDistanceInfo polylineDistanceInfo;
            PointD pt = new PointD();

            pt.X = lon;
            pt.Y = lat;
            if (!(sf.ShapeType == ShapeType.PolyLine || sf.ShapeType == ShapeType.PolyLineM))
            {
                return(null);
            }
            int index;

            EGIS.ShapeFileLib.ConversionFunctions.RefEllipse = 23;
            index = sf.GetClosestShape(pt, radius, out polylineDistanceInfo);
            if (index >= 0)
            {
                Console.Out.WriteLine("LineSegmentSide:" + polylineDistanceInfo.LineSegmentSide);
                if (sf.ShapeType == ShapeType.PolyLine)
                {
                    data_gis = new _prop_SnapLtrans();
                    double   total = 0;
                    PointD[] data  = sf.GetShapeDataD(index)[0];
                    for (int i = 0; i < data.Length - 1; i++)
                    {
                        total = total + ConversionFunctions.DistanceBetweenLatLongPoints(INDEX_WGS_84, data[i].Y, data[i].X, data[i + 1].Y, data[i + 1].X);
                    }

                    double distance_to_end = 0;
                    for (int i = polylineDistanceInfo.PointIndex; i < data.Length - 1; i++)
                    {
                        distance_to_end = distance_to_end + ConversionFunctions.DistanceBetweenLatLongPoints(INDEX_WGS_84, data[i].Y, data[i].X, data[i + 1].Y, data[i + 1].X);
                    }

                    double distance_snap     = ConversionFunctions.DistanceBetweenLatLongPoints(INDEX_WGS_84, lat, lon, polylineDistanceInfo.PolylinePoint.Y, polylineDistanceInfo.PolylinePoint.X);
                    string side_of_road      = GetLineSegmentSideDescription(polylineDistanceInfo.LineSegmentSide);
                    double distance_to_start = total - distance_to_end;
                    data_gis.lattitude_on_line = polylineDistanceInfo.PolylinePoint.Y;
                    data_gis.lontitude_on_line = polylineDistanceInfo.PolylinePoint.X;
                    data_gis.side_of_line      = side_of_road;
                    data_gis.snap_distance     = distance_snap;
                    data_gis.index_shape       = index;
                    data_gis.tVal = polylineDistanceInfo.TVal;
                    data_gis.calculate_length_line  = total;
                    data_gis.distance_snap_to_start = distance_to_start;
                    data_gis.distance_snap_to_end   = distance_to_end;
                    data_gis.data_db = this.dbr.GetFields(index);
                    data_gis.dt      = DbfToTable(dbr.GetFieldNames(), data_gis.data_db);
                    if (data.Length > 0)
                    {
                        data_gis.lat_start = data[0].Y;
                        data_gis.lon_start = data[0].X;
                        data_gis.lat_end   = data[data.Length - 1].Y;
                        data_gis.lon_end   = data[data.Length - 1].X;
                    }
                }
            }
            return(data_gis);
        }
Beispiel #2
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);
        }
 /// <summary>
 /// Constructs a ShapeFile using a path to a .shp shape file
 /// </summary>
 /// <param name="shapeFilePath">The path to the ".shp" shape file</param>
 private Shape(string shapeFilePath)
 {
     using (var file = new ShapeFile(shapeFilePath))
     {
         RecordCount = file.RecordCount;
         ShapeData = new IEnumerable<PointXYZ[]>[RecordCount];
         for (int i = 0; i < RecordCount; ++i)
         {
             var shapeData = file.GetShapeDataD(i);
             ShapeData[i] = shapeData.Select(x => x.Select(p => new PointXYZ() { X = p.X, Y = p.Y, Z = 0 }).ToArray());
         }
     }
     string dbfFilePath = Path.ChangeExtension(shapeFilePath, "dbf");
     Database = new DbfReader(dbfFilePath);
 }
Beispiel #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
                        }
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Constructs a ShapeFile using a path to a .shp shape file
        /// </summary>
        /// <param name="shapeFilePath">The path to the ".shp" shape file</param>
        private Shape(string shapeFilePath)
        {
            using (var file = new ShapeFile(shapeFilePath))
            {
                RecordCount = file.RecordCount;
                ShapeData   = new IEnumerable <PointXYZ[]> [RecordCount];
                for (int i = 0; i < RecordCount; ++i)
                {
                    var shapeData = file.GetShapeDataD(i);
                    ShapeData[i] = shapeData.Select(x => x.Select(p => new PointXYZ()
                    {
                        X = p.X, Y = p.Y, Z = 0
                    }).ToArray());
                }
            }
            string dbfFilePath = Path.ChangeExtension(shapeFilePath, "dbf");

            Database = new DbfReader(dbfFilePath);
        }
Beispiel #6
0
        private string GetGeoJson3(HttpContext context)
        {
            string    shapeFilePath = context.Server.MapPath("/demos/demo2_files/j5505_roads.shp");
            ShapeFile sf            = new ShapeFile(shapeFilePath);
            int       recordIndex   = rnd.Next(sf.RecordCount);

            PointD[] pts = sf.GetShapeDataD(recordIndex)[0];

            FeatureCollection featureCollection = new FeatureCollection();
            Feature           feature           = new Feature();

            feature.geometry = new LineString(pts);
            StyleOptions featureStyleOptions = new StyleOptions();

            featureStyleOptions.strokeWeight  = 3;
            featureStyleOptions.strokeColor   = "red";
            featureStyleOptions.strokeOpacity = 0.5f;
            feature.properties = new { id = "0", styleOptions = featureStyleOptions };
            featureCollection.features.Add(feature);

            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();

            return(javaScriptSerializer.Serialize(featureCollection));
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }
Beispiel #9
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);
        }