Exemple #1
0
        /// <summary>
        /// Updates the file with an additional feature.
        /// </summary>
        /// <param name="feature">Feature that gets added.</param>
        public void Add(IFeature feature)
        {
            if (feature.FeatureType != FeatureType)
            {
                throw new FeatureTypeMismatchException();
            }

            string dir = Path.GetDirectoryName(Filename);

            if (dir != null && !Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            // We must add the dbf entry before changing the shx because if we already have one deleted record, the AttributeTable thinks we have none
            AttributeTable dbf = GetAttributeTable(Filename);

            dbf.AddRow(feature.DataRow);

            int numFeatures = 0;
            var header      = new ShapefileHeader();

            if (File.Exists(Filename))
            {
                header.Open(Filename);
                UpdateHeader(header, feature.Geometry);
                numFeatures = (header.ShxLength - 50) / 4;
            }
            else
            {
                header.Xmin = feature.Geometry.EnvelopeInternal.MinX;
                header.Xmax = feature.Geometry.EnvelopeInternal.MaxX;
                header.Ymin = feature.Geometry.EnvelopeInternal.MinY;
                header.Ymax = feature.Geometry.EnvelopeInternal.MaxY;
                if (double.IsNaN(feature.Geometry.Coordinates[0].M))
                {
                    header.ShapeType = ShapeType;
                }
                else
                {
                    if (double.IsNaN(feature.Geometry.Coordinates[0].Z))
                    {
                        header.ShapeType = ShapeTypeM;
                    }
                    else
                    {
                        header.Zmin      = feature.Geometry.MinZ();
                        header.Zmax      = feature.Geometry.MaxZ();
                        header.ShapeType = ShapeTypeZ;
                    }

                    header.Mmin = feature.Geometry.MinM();
                    header.Mmax = feature.Geometry.MaxM();
                }

                header.ShxLength = 4 + 50;
                header.SaveAs(Filename);
            }

            AppendGeometry(header, feature.Geometry, numFeatures);

            Quadtree?.Insert(feature.Geometry.EnvelopeInternal, numFeatures - 1);
        }
        /// <inheritdocs/>
        public void AddRange(IEnumerable <IFeature> features)
        {
            // Make sure the Output Directory exists
            string dir = Path.GetDirectoryName(Filename);

            if (dir != null && !Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            // Get the Attribute Table
            AttributeTable dbf = GetAttributeTable(Filename);

            // Open the Header if the Filename exists
            bool filenameExists = File.Exists(Filename);
            var  header         = new ShapefileHeader();

            if (filenameExists)
            {
                header.Open(Filename);
            }

            foreach (var feature in features)
            {
                if (feature.FeatureType != FeatureType)
                {
                    throw new FeatureTypeMismatchException();
                }

                // We must add the dbf entry before changing the shx because if we already have one deleted record, the AttributeTable thinks we have none
                dbf.AddRow(feature.DataRow);

                int numFeatures = 0;
                if (filenameExists)
                {
                    UpdateHeader(header, feature.Geometry, true);
                    numFeatures = (header.ShxLength - 50) / 4;
                }
                else
                {
                    header.Xmin = feature.Geometry.EnvelopeInternal.MinX;
                    header.Xmax = feature.Geometry.EnvelopeInternal.MaxX;
                    header.Ymin = feature.Geometry.EnvelopeInternal.MinY;
                    header.Ymax = feature.Geometry.EnvelopeInternal.MaxY;
                    if (double.IsNaN(feature.Geometry.Coordinates[0].M))
                    {
                        header.ShapeType = ShapeType;
                    }
                    else
                    {
                        if (double.IsNaN(feature.Geometry.Coordinates[0].Z))
                        {
                            header.ShapeType = ShapeTypeM;
                        }
                        else
                        {
                            header.Zmin      = feature.Geometry.EnvelopeInternal.Minimum.Z;
                            header.Zmax      = feature.Geometry.EnvelopeInternal.Maximum.Z;
                            header.ShapeType = ShapeTypeZ;
                        }
                        header.Mmin = feature.Geometry.EnvelopeInternal.Minimum.M;
                        header.Mmax = feature.Geometry.EnvelopeInternal.Maximum.M;
                    }
                    header.ShxLength = 4 + 50;
                    header.SaveAs(Filename);
                    filenameExists = true;
                }

                AppendBasicGeometry(header, feature.Geometry, numFeatures);

                feature.RecordNumber = numFeatures;

                if (null != Quadtree)
                {
                    Quadtree.Insert(feature.Geometry.EnvelopeInternal, numFeatures - 1);
                }
            }
        }
        /// <inheritdocs/>
        public void AddRange(IEnumerable<IFeature> features)
        {
            // Make sure the Output Directory exists
            string dir = Path.GetDirectoryName(Filename);
            if (dir != null && !Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            // Get the Attribute Table
            AttributeTable dbf = GetAttributeTable(Filename);

            // Open the Header if the Filename exists
            bool filenameExists = File.Exists(Filename);
            var header = new ShapefileHeader();
            if (filenameExists)
            {
                header.Open(Filename);
            }

            foreach (var feature in features)
            {
                if (feature.FeatureType != FeatureType)
                {
                    throw new FeatureTypeMismatchException();
                }

                // We must add the dbf entry before changing the shx because if we already have one deleted record, the AttributeTable thinks we have none
                dbf.AddRow(feature.DataRow);

                int numFeatures = 0;
                if (filenameExists)
                {
                    UpdateHeader(header, feature, true);
                    numFeatures = (header.ShxLength - 50) / 4;
                }
                else
                {
                    header.Xmin = feature.Envelope.Minimum.X;
                    header.Xmax = feature.Envelope.Maximum.X;
                    header.Ymin = feature.Envelope.Minimum.Y;
                    header.Ymax = feature.Envelope.Maximum.Y;
                    if (double.IsNaN(feature.Coordinates[0].M))
                    {
                        header.ShapeType = ShapeType;
                    }
                    else
                    {
                        if (double.IsNaN(feature.Coordinates[0].Z))
                        {
                            header.ShapeType = ShapeTypeM;
                        }
                        else
                        {
                            header.Zmin = feature.Envelope.Minimum.Z;
                            header.Zmax = feature.Envelope.Maximum.Z;
                            header.ShapeType = ShapeTypeZ;
                        }
                        header.Mmin = feature.Envelope.Minimum.M;
                        header.Mmax = feature.Envelope.Maximum.M;
                    }
                    header.ShxLength = 4 + 50;
                    header.SaveAs(Filename);
                    filenameExists = true;
                }

                AppendBasicGeometry(header, feature, numFeatures);

                feature.RecordNumber = numFeatures;

                if (null != Quadtree)
                    Quadtree.Insert(feature.Envelope, numFeatures - 1);
            }
        }