Beispiel #1
0
        /// <summary>
        /// Reads the next shape
        /// </summary>
        /// <returns></returns>
        public IGeometry ReadNext(int RecordNumber)
        {
            IntPtr pShape = ShapeLib.SHPReadObject(_shapePointer, RecordNumber);

            ShapeLib.SHPObject shpObject = new ShapeLib.SHPObject();
            Marshal.PtrToStructure(pShape, shpObject);
            double[] x = new double[shpObject.nVertices];
            Marshal.Copy(shpObject.padfX, x, 0, x.Length);
            double[] y = new double[shpObject.nVertices];
            Marshal.Copy(shpObject.padfY, y, 0, y.Length);
            double[] z = new double[shpObject.nVertices];
            Marshal.Copy(shpObject.padfZ, z, 0, z.Length);

            int[] partstarts = null;
            if (shpObject.nParts > 0)
            {
                partstarts = new int[shpObject.nParts];
                Marshal.Copy(shpObject.paPartStart, partstarts, 0, partstarts.Length);
            }

            ShapeLib.SHPDestroyObject(pShape);

            IGeometry geom = null;

            switch (type)
            {
            case ShapeLib.ShapeType.NullShape:
                break;

            case ShapeLib.ShapeType.MultiPoint:
            case ShapeLib.ShapeType.MultiPointZ:
            case ShapeLib.ShapeType.MultiPointM:
            case ShapeLib.ShapeType.PointM:
            case ShapeLib.ShapeType.PointZ:
            case ShapeLib.ShapeType.Point:
                geom = new XYPoint(x[0], y[0]);
                break;

            case ShapeLib.ShapeType.PolyLineM:
            case ShapeLib.ShapeType.PolyLineZ:
            case ShapeLib.ShapeType.PolyLine:
                geom = new XYPolyline();
                for (int i = 0; i < x.Length; i++)
                {
                    ((XYPolyline)geom).Points.Add(new XYPoint(x[i], y[i]));
                }
                break;

            case ShapeLib.ShapeType.PolygonM:
            case ShapeLib.ShapeType.PolygonZ:
            case ShapeLib.ShapeType.Polygon:

                if (partstarts.Count() == 1)
                {
                    geom = new XYPolygon();

                    for (int i = 0; i < x.Length; i++)
                    {
                        ((XYPolygon)geom).Points.Add(new XYPoint(x[i], y[i]));
                    }
                    ((XYPolygon)geom).Points.Reverse();
                }
                else
                {
                    geom = new MultiPartPolygon();

                    //foreach (var partstart in partstarts.Reverse())
                    //{
                    //  var poly = new XYPolygon();
                    //  for (int i = end; i > partstart; i--)
                    //    poly.Points.Add(new XYPoint(x[i], y[i]));
                    //  end = partstart;
                    //  ((MultiPartPolygon)geom).Polygons.Add(poly);
                    //}
                    for (int j = 0; j < partstarts.Count(); j++)
                    {
                        int end;
                        if (j < partstarts.Count() - 1)
                        {
                            end = partstarts[j + 1];
                        }
                        else
                        {
                            end = x.Length;
                        }

                        var poly = new XYPolygon();
                        for (int i = partstarts[j]; i < end; i++)
                        {
                            poly.Points.Add(new XYPoint(x[i], y[i]));
                        }
                        poly.Points.Reverse();
                        ((MultiPartPolygon)geom).Polygons.Add(poly);
                    }
                }
                break;

            case ShapeLib.ShapeType.MultiPatch:
                break;

            default:
                break;
            }
            return(geom);
        }