Ejemplo n.º 1
0
        /// <summary>
        /// This will return the correct shapefile type by reading the fileName.
        /// </summary>
        /// <param name="fileName">A string specifying the file with the extension .shp to open.</param>
        /// <param name="progressHandler">receives progress messages and overrides the ProgressHandler on the DataManager.DefaultDataManager</param>
        /// <returns>A correct shapefile object which is exclusively for reading the .shp data</returns>
        public static new Shapefile OpenFile(string fileName, IProgressHandler progressHandler)
        {
            var ext = Path.GetExtension(fileName);

            if (ext != null)
            {
                ext = ext.ToLower();
            }
            if (ext != ".shp" && ext != ".shx" && ext != ".dbf")
            {
                throw new ArgumentException(String.Format("The file extension {0} is not supported by Shapefile data provider.", ext));
            }
            string name = Path.ChangeExtension(fileName, ".shp");
            var    head = new ShapefileHeader();

            head.Open(name);
            switch (head.ShapeType)
            {
            case ShapeType.MultiPatch:
                throw new NotImplementedException("This shape type is not yet supported.");

            case ShapeType.MultiPoint:
            case ShapeType.MultiPointM:
            case ShapeType.MultiPointZ:
                var mpsf = new MultiPointShapefile();
                mpsf.Open(name, progressHandler);
                return(mpsf);

            case ShapeType.NullShape:
                throw new NotImplementedException("This shape type is not yet supported.");

            case ShapeType.Point:
            case ShapeType.PointM:
            case ShapeType.PointZ:
                var psf = new PointShapefile();
                psf.Open(name, progressHandler);
                return(psf);

            case ShapeType.Polygon:
            case ShapeType.PolygonM:
            case ShapeType.PolygonZ:
                var pgsf = new PolygonShapefile();
                pgsf.Open(name, progressHandler);
                return(pgsf);

            case ShapeType.PolyLine:
            case ShapeType.PolyLineM:
            case ShapeType.PolyLineZ:
                var lsf = new LineShapefile();
                lsf.Open(name, progressHandler);
                return(lsf);
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This will return the correct shapefile type by reading the fileName.
        /// </summary>
        /// <param name="fileName">A string specifying the file with the extension .shp to open.</param>
        /// <param name="progressHandler">receives progress messages and overrides the ProgressHandler on the DataManager.DefaultDataManager</param>
        /// <exception cref="NotImplementedException">Throws a NotImplementedException if the ShapeType of the loaded file is NullShape or MultiPatch.</exception>
        /// <returns>A correct shapefile object which is exclusively for reading the .shp data</returns>
        public static new Shapefile OpenFile(string fileName, IProgressHandler progressHandler)
        {
            var ext = Path.GetExtension(fileName)?.ToLower();

            if (ext != ".shp" && ext != ".shx" && ext != ".dbf")
            {
                throw new ArgumentException(string.Format(DataStrings.FileExtension0NotSupportedByShapefileDataProvider, ext));
            }

            string name = Path.ChangeExtension(fileName, ".shp");
            var    head = new ShapefileHeader();

            head.Open(name);
            switch (head.ShapeType)
            {
            case ShapeType.MultiPatch:
            case ShapeType.NullShape:
                throw new NotImplementedException(DataStrings.ShapeTypeNotYetSupported);

            case ShapeType.MultiPoint:
            case ShapeType.MultiPointM:
            case ShapeType.MultiPointZ:
                var mpsf = new MultiPointShapefile();
                mpsf.Open(name, progressHandler);
                return(mpsf);

            case ShapeType.Point:
            case ShapeType.PointM:
            case ShapeType.PointZ:
                var psf = new PointShapefile();
                psf.Open(name, progressHandler);
                return(psf);

            case ShapeType.Polygon:
            case ShapeType.PolygonM:
            case ShapeType.PolygonZ:
                var pgsf = new PolygonShapefile();
                pgsf.Open(name, progressHandler);
                return(pgsf);

            case ShapeType.PolyLine:
            case ShapeType.PolyLineM:
            case ShapeType.PolyLineZ:
                var lsf = new LineShapefile();
                lsf.Open(name, progressHandler);
                return(lsf);
            }

            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This will return the correct shapefile type by reading the fileName.
        /// </summary>
        /// <param name="fileName">A string specifying the file with the extension .shp to open.</param>
        /// <param name="progressHandler">receives progress messages and overrides the ProgressHandler on the DataManager.DefaultDataManager</param>
        /// <returns>A correct shapefile object which is exclusively for reading the .shp data</returns>
        public static new Shapefile OpenFile(string fileName, IProgressHandler progressHandler)
        {
            var ext = Path.GetExtension(fileName);
            if (ext != null) ext = ext.ToLower();
            if (ext != ".shp" && ext != ".shx" && ext != ".dbf")
                throw new ArgumentException(String.Format("The file extension {0} is not supported by Shapefile data provider.", ext));
            string name = Path.ChangeExtension(fileName, ".shp");
            var head = new ShapefileHeader();
            head.Open(name);
            switch (head.ShapeType)
            {
                case ShapeType.MultiPatch:
                    throw new NotImplementedException("This shape type is not yet supported.");

                case ShapeType.MultiPoint:
                case ShapeType.MultiPointM:
                case ShapeType.MultiPointZ:
                    var mpsf = new MultiPointShapefile();
                    mpsf.Open(name, progressHandler);
                    return mpsf;

                case ShapeType.NullShape:
                    throw new NotImplementedException("This shape type is not yet supported.");

                case ShapeType.Point:
                case ShapeType.PointM:
                case ShapeType.PointZ:
                    var psf = new PointShapefile();
                    psf.Open(name, progressHandler);
                    return psf;

                case ShapeType.Polygon:
                case ShapeType.PolygonM:
                case ShapeType.PolygonZ:
                    var pgsf = new PolygonShapefile();
                    pgsf.Open(name, progressHandler);
                    return pgsf;

                case ShapeType.PolyLine:
                case ShapeType.PolyLineM:
                case ShapeType.PolyLineZ:
                    var lsf = new LineShapefile();
                    lsf.Open(name, progressHandler);
                    return lsf;
            }

            return null;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This will return the correct shapefile type by reading the fileName.
        /// </summary>
        /// <param name="fileName">A string specifying the file with the extension .shp to open.</param>
        /// <param name="progressHandler">receives progress messages and overrides the ProgressHandler on the DataManager.DefaultDataManager</param>
        /// <returns>A correct shapefile object which is exclusively for reading the .shp data</returns>
        public static new Shapefile OpenFile(string fileName, IProgressHandler progressHandler)
        {
            string ext = Path.GetExtension(fileName).ToLower();

            if (ext != ".shp" && ext != ".shx" && ext != ".dbf")
            {
                throw new ArgumentException(String.Format("The file extension {0} is not supported by Shapefile data provider.", ext));
            }
            string          name = Path.ChangeExtension(fileName, ".shp");
            ShapefileHeader head = new ShapefileHeader();

            head.Open(name);
            PointShapefile      psf;
            LineShapefile       lsf;
            PolygonShapefile    pgsf;
            MultiPointShapefile mpsf;

            switch (head.ShapeType)
            {
            case ShapeType.MultiPatch:
                throw new NotImplementedException("This shape type is not yet supported.");

            case ShapeType.MultiPoint:
            case ShapeType.MultiPointM:
            case ShapeType.MultiPointZ:
                mpsf = new MultiPointShapefile();
                mpsf.Open(name, progressHandler);
                return(mpsf);

            case ShapeType.NullShape:
                throw new NotImplementedException("This shape type is not yet supported.");

            case ShapeType.Point:
            case ShapeType.PointM:
            case ShapeType.PointZ:

                // Instantiate a new object to handle the point shapefile
                psf = new PointShapefile();

                // Open the geometric components of the data (but not the dbf components)
                psf.Open(name, progressHandler);

                return(psf);

            case ShapeType.Polygon:
            case ShapeType.PolygonM:
            case ShapeType.PolygonZ:
                pgsf = new PolygonShapefile();
                pgsf.Open(name, progressHandler);
                return(pgsf);

            case ShapeType.PolyLine:
            case ShapeType.PolyLineM:
            case ShapeType.PolyLineZ:
                lsf = new LineShapefile();
                lsf.Open(name, progressHandler);
                return(lsf);
            }

            return(null);
        }
Ejemplo n.º 5
0
        /// <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);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// This will return the correct shapefile type by reading the fileName.
        /// </summary>
        /// <param name="fileName">A string specifying the file with the extension .shp to open.</param>
        /// <param name="progressHandler">receives progress messages and overrides the ProgressHandler on the DataManager.DefaultDataManager</param>
        /// <returns>A correct shapefile object which is exclusively for reading the .shp data</returns>
        public static new Shapefile OpenFile(string fileName, IProgressHandler progressHandler)
        {
            string ext = Path.GetExtension(fileName).ToLower();
            if (ext != ".shp" && ext != ".shx" && ext != ".dbf")
                throw new ArgumentException(String.Format("The file extension {0} is not supported by Shapefile data provider.", ext));
            string name = Path.ChangeExtension(fileName, ".shp");
            ShapefileHeader head = new ShapefileHeader();
            head.Open(name);
            PointShapefile psf;
            LineShapefile lsf;
            PolygonShapefile pgsf;
            MultiPointShapefile mpsf;
            switch (head.ShapeType)
            {
                case ShapeType.MultiPatch:
                    throw new NotImplementedException("This shape type is not yet supported.");

                case ShapeType.MultiPoint:
                case ShapeType.MultiPointM:
                case ShapeType.MultiPointZ:
                    mpsf = new MultiPointShapefile();
                    mpsf.Open(name, progressHandler);
                    return mpsf;

                case ShapeType.NullShape:
                    throw new NotImplementedException("This shape type is not yet supported.");

                case ShapeType.Point:
                case ShapeType.PointM:
                case ShapeType.PointZ:

                    // Instantiate a new object to handle the point shapefile
                    psf = new PointShapefile();

                    // Open the geometric components of the data (but not the dbf components)
                    psf.Open(name, progressHandler);

                    return psf;

                case ShapeType.Polygon:
                case ShapeType.PolygonM:
                case ShapeType.PolygonZ:
                    pgsf = new PolygonShapefile();
                    pgsf.Open(name, progressHandler);
                    return pgsf;

                case ShapeType.PolyLine:
                case ShapeType.PolyLineM:
                case ShapeType.PolyLineZ:
                    lsf = new LineShapefile();
                    lsf.Open(name, progressHandler);
                    return lsf;
            }

            return null;
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        /// <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);
            }
        }