Ejemplo n.º 1
0
        /// <summary>
        /// 根据文件流创建shapefile
        /// </summary>
        /// <param name="shpFileStream"></param>
        /// <param name="shxFileStream"></param>
        /// <param name="dbfFileStream"></param>
        /// <param name="prjFileStream"></param>
        /// <returns></returns>
        public static FeatureSet CreateFeatureSet
            (Stream shpFileStream, Stream shxFileStream,
            Stream dbfFileStream, Stream prjFileStream = null)
        {
            using var dbfReader = new DbfReader(dbfFileStream);
            var shxIndexs = new ShxReader().ReadShx(shxFileStream);
            var recordNum = shxIndexs.Count;

            using var shpReader = new ShpReader(shpFileStream, shxIndexs);

            var fs = CreateFeatureSet(shpReader, dbfReader, recordNum);

            if (prjFileStream != null)
            {
                string prjWkt = "";
                using (var sr = new StreamReader(prjFileStream, dbfReader.Encoding))
                {
                    prjWkt = sr.ReadToEnd();
                }

                var proj = Crs.CreateFromWkt(prjWkt);
                fs.Crs = proj;
            }
            return(fs);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从shapefile生成FeatureSet
        /// </summary>
        /// <param name="shpFilePath"></param>
        /// <returns></returns>
        public static FeatureSet CreateFeatureSet(string shpFilePath)
        {
            if (!ShpUtil.VerificationShp(shpFilePath, out var subFiles))
            {
                throw new IOException("该shapefile文件不存在或者主文件缺失!");
            }
            //else
            using var dbfReader = new DbfReader(subFiles.Item2);
            var shxIndexs = new ShxReader().ReadShx(subFiles.Item1);
            var recordNum = shxIndexs.Count;

            using var shpReader = new ShpReader(shpFilePath, shxIndexs);

            var fs = CreateFeatureSet(shpReader, dbfReader, recordNum);

            if (File.Exists(subFiles.Item3))
            {
                string prjWkt = "";
                using (var sr = new StreamReader(subFiles.Item3, Encoding.UTF8))
                {
                    prjWkt = sr.ReadToEnd();
                }

                var proj = Crs.CreateFromWkt(prjWkt);
                fs.Crs = proj;
            }

            return(fs);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据ShpReader和DbfReader创建FeatureSet
        /// </summary>
        /// <param name="shpReader"></param>
        /// <param name="dbfReader"></param>
        /// <param name="recordNum"></param>
        /// <returns></returns>
        private static FeatureSet CreateFeatureSet(ShpReader shpReader, DbfReader dbfReader, int recordNum)
        {
            GeometryType geometryType = (GeometryType)shpReader.ShpHeader.ShapeType;
            var          featureType  = ShpUtil
                                        .GeometryType2FeatureType(geometryType);
            FeatureSet   fs       = new FeatureSet(featureType);
            IFeatureList features = new FeatureList(fs);

            for (int i = 0; i < recordNum; i++)
            {
                var spatialBytes = shpReader.GetNextRecord();
                dbfReader.GetNextRow();
                IGeometry geometry;
                switch (geometryType)
                {
                case GeometryType.Point:
                    geometry = BytesToGeometry.CreatePoint(spatialBytes);
                    break;

                case GeometryType.MultiPoint:
                    geometry = BytesToGeometry.CreateMultipoint(spatialBytes);
                    break;

                case GeometryType.PolyLine:
                    geometry = BytesToGeometry.CreatePolyLine(spatialBytes);
                    break;

                case GeometryType.Polygon:
                    geometry = BytesToGeometry.CreatePolygon(spatialBytes);
                    break;

                default:
                    geometry = null;
                    break;
                }

                IFeature feature = new Feature(geometry);
                features.Add(feature);
            }

            fs.Features.Set(features);
            fs.AttrTable = dbfReader.DbfTable;
            var header = shpReader.ShpHeader;

            //fs.Envelope = new Envelope(header.XMin, header.YMin,
            //    header.XMax, header.YMax,
            //    header.ZMin, header.ZMax);

            return(fs);
        }