Beispiel #1
0
        public void Write(GeoRefData geodata)
        {
            double[]           Xs   = null;
            double[]           Ys   = null;
            ShapeLib.ShapeType type = ShapeLib.ShapeType.NullShape;

            if (geodata.Geometry is IXYPoint)
            {
                IXYPoint p = (IXYPoint)geodata.Geometry;
                type = ShapeLib.ShapeType.Point;
                Xs   = new double[] { p.X };
                Ys   = new double[] { p.Y };
            }
            else if (geodata.Geometry.GetType().Equals(typeof(XYPolyline)))
            {
                XYPolyline p = (XYPolyline)geodata.Geometry;
                type = ShapeLib.ShapeType.PolyLine;
                int npoints = p.Points.Count;

                Xs = new double[npoints];
                Ys = new double[npoints];

                for (int i = 0; i < npoints; i++)
                {
                    Xs[i] = p.Points[i].X;
                    Ys[i] = p.Points[i].Y;
                }
            }
            else if (geodata.Geometry.GetType().Equals(typeof(XYLine)))
            {
                XYLine p = (XYLine)geodata.Geometry;
                type = ShapeLib.ShapeType.PolyLine;
                int npoints = 2;

                Xs = new double[] { p.P1.X, p.P2.X };
                Ys = new double[] { p.P1.Y, p.P2.Y };;
            }

            else if (geodata.Geometry.GetType().Equals(typeof(XYPolygon)))
            {
                XYPolygon p = (XYPolygon)geodata.Geometry;
                type = ShapeLib.ShapeType.Polygon;
                int npoints = p.Points.Count;

                Xs = new double[npoints];
                Ys = new double[npoints];
                p.Points.Reverse();

                for (int i = 0; i < npoints; i++)
                {
                    Xs[i] = p.Points[i].X;
                    Ys[i] = p.Points[i].Y;
                }
                p.Points.Reverse();
            }
            else if (geodata.Geometry.GetType().Equals(typeof(MultiPartPolygon)))
            {
                MultiPartPolygon p = (MultiPartPolygon)geodata.Geometry;
                type = ShapeLib.ShapeType.Polygon;
                int npoints = p.Polygons.Sum(pol => pol.Points.Count);

                foreach (var poly in p.Polygons)
                {
                    poly.Points.Reverse();
                }

                Xs = new double[npoints];
                Ys = new double[npoints];
                int i = 0;
                foreach (var point in p.Polygons.SelectMany(pol => pol.Points))
                {
                    Xs[i] = point.X;
                    Ys[i] = point.Y;
                    i++;
                }
                foreach (var poly in p.Polygons)
                {
                    poly.Points.Reverse();
                }
            }

            if (_shapePointer == IntPtr.Zero)
            {
                _shapePointer = ShapeLib.SHPCreate(_fileName, type);
            }

            if (_shapePointer == IntPtr.Zero)
            {
                throw new Exception("Could not create: " + Path.GetFullPath(_fileName) + "\nMake sure directory exists.");
            }


            IntPtr obj;

            if (geodata.Geometry.GetType().Equals(typeof(MultiPartPolygon)))
            {
                MultiPartPolygon p = (MultiPartPolygon)geodata.Geometry;

                int[] partstarts = new int[p.Polygons.Count];
                partstarts[0] = 0;
                ShapeLib.PartType[] partype = new ShapeLib.PartType[p.Polygons.Count];
                for (int i = 0; i < partype.Count(); i++)
                {
                    partype[i] = ShapeLib.PartType.Ring;
                }

                for (int i = 1; i < partstarts.Count(); i++)
                {
                    partstarts[i] = partstarts[i - 1] + p.Polygons[i - 1].Points.Count;
                }


                obj = ShapeLib.SHPCreateObject(type, -1, partstarts.Count(), partstarts, partype, Xs.Count(), Xs, Ys, null, null);
            }
            else
            {
                obj = ShapeLib.SHPCreateSimpleObject(type, Xs.Count(), Xs, Ys, null);
            }
            ShapeLib.SHPWriteObject(_shapePointer, -1, obj);
            ShapeLib.SHPDestroyObject(obj);
            _dbf.WriteData(geodata.Data);

            NoOfEntries++;
        }