Пример #1
0
        private static Feature[] GetFeatures(string fname)
        {
            if (fname == null)
            {
                return(null);
            }
            IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(fname) as IVectorFeatureDataReader;

            if (dr == null)
            {
                return(null);
            }
            try
            {
                Feature[] fets = dr.FetchFeatures();
                if (fets == null || fets.Length == 0)
                {
                    return(null);
                }
                List <Feature> features = new List <Feature>();
                foreach (Feature fet in fets)
                {
                    if (fet.Geometry is ShapePolygon)
                    {
                        features.Add(fet);
                    }
                }
                return(features != null?features.ToArray() : null);
            }
            finally
            {
                dr.Dispose();
            }
        }
Пример #2
0
        public Feature[] LoadFeatures()
        {
            if (_urls == null)
            {
                return(null);
            }
            List <Feature>           fets = new List <Feature>();
            IVectorFeatureDataReader dr   = null;

            Feature[] temp = null;
            foreach (string url in _urls)
            {
                dr = VectorDataReaderFactory.GetUniversalDataReader(url) as IVectorFeatureDataReader;
                if (dr == null)
                {
                    continue;
                }
                try
                {
                    temp = dr.FetchFeatures();
                    if (temp == null || temp.Length == 0)
                    {
                        continue;
                    }
                    fets.AddRange(temp);
                }
                finally
                {
                    dr.Dispose();
                }
            }
            return(fets.Count == 0 ? null : fets.ToArray());
        }
Пример #3
0
        public float[] GetEvelope(string filename, string area)
        {
            List <Feature>           fets = new List <Feature>();
            IVectorFeatureDataReader dr   = null;
            int index = 0;

            if (area == "中国区")
            {
                url   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "数据引用\\基础矢量\\行政区划\\面\\中国边界.shp");
                index = 0;
            }
            else
            {
                url   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "数据引用\\基础矢量\\行政区划\\面\\省级行政区域_面.shp");
                index = GetFeatureIndx(area);
            }
            dr = VectorDataReaderFactory.GetUniversalDataReader(url) as IVectorFeatureDataReader;
            Feature[] temp = null;
            temp = dr.FetchFeatures();
            fets.AddRange(temp);
            fets.ToArray();
            Feature[] features = null;
            features       = fets.ToArray();
            _blockName     = area;
            _vectorFeature = features[index];//_selectFeature
            //string fieldValue = _vectorFeature.GetFieldValue(fieldIndex);
            Envelope evp1 = new Envelope();

            evp1 = GetMaskEnvelopeHDF(_vectorFeature.Geometry as ShapePolygon);
            float[] envelope = new float[] { Convert.ToSingle(evp1.MinX), Convert.ToSingle(evp1.MaxX), Convert.ToSingle(evp1.MinY), Convert.ToSingle(evp1.MaxY) };
            return(envelope);
        }
Пример #4
0
 private Feature[] ReadFeatures(string shpFileName)
 {
     using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFileName) as IVectorFeatureDataReader)
     {
         return(dr.FetchFeatures());
     }
 }
Пример #5
0
 public string[] GetDisplayFieldNames()
 {
     if (_displayFieldName == null || _shpFileName == null || !File.Exists(_shpFileName))
     {
         return(null);
     }
     using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(_shpFileName) as IVectorFeatureDataReader)
     {
         Feature[] fets = dr.FetchFeatures();
         if (fets == null || fets.Length == 0)
         {
             return(null);
         }
         if (fets[0].FieldNames == null || !fets[0].FieldNames.Contains(_displayFieldName))
         {
             return(null);
         }
         List <string> names = new List <string>();
         foreach (Feature f in fets)
         {
             names.Add(f.GetFieldValue(_displayFieldName));
         }
         return(names.Count > 0 ? names.ToArray() : null);
     }
 }
Пример #6
0
        /// <summary>
        /// 读取海冰控制点,获取海冰控制单信息
        /// 最多6列,每列最多显示如下行数22,22,14,9,5,5=77,总共最多显示77个点,这样组织显示
        /// </summary>
        /// <param name="shpFile"></param>
        private void ReadIceControlPointInfo(string shpFile)
        {
            _iceCptInfos.Clear();
            List <string> infos = new List <string>();

            using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFile) as IVectorFeatureDataReader)
            {
                if (dr == null)
                {
                    return;
                }
                if (!dr.Fields.Contains("NO") || !dr.Fields.Contains("TEMP"))
                {
                    return;
                }
                Feature[] features = dr.FetchFeatures();
                if (features == null || features.Length == 0)
                {
                    return;
                }
                _iceCptInfos.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}\t", "编号", "经度", "纬度", "温度"));
                foreach (Feature fet in features)
                {
                    string     iceNo   = fet.GetFieldValue("NO");
                    string     iceTemp = fet.GetFieldValue("TEMP");
                    ShapePoint sp      = fet.Geometry as ShapePoint;
                    infos.Add(string.Format("{0}\t{1}\t{2}\t{3}\t", iceNo, sp.X.ToString("f2"), sp.Y.ToString("f2"), iceTemp));
                }
            }
            int maxCol = 22;

            int[] eachColCount = new int[] { 22, 21, 13, 7, 4, 4 };
            int   colCount     = eachColCount.Length;
            int   infoCount    = infos.Count;

            for (int i = 0; i < maxCol && i < infoCount; i++)
            {
                int           colBeginIndex = i;
                StringBuilder curLine       = new StringBuilder(infos[i]);
                for (int j = 1; j < colCount; j++)
                {
                    colBeginIndex += eachColCount[j - 1];
                    if (colBeginIndex >= infoCount)
                    {
                        break;
                    }
                    if (i < eachColCount[j])
                    {
                        curLine.AppendFormat("  {0}", infos[colBeginIndex]);
                    }
                    else
                    {
                        curLine.AppendFormat("  {0}", " ".PadRight(infos[colBeginIndex].Length));
                    }
                }
                _iceCptInfos.AppendLine(curLine.ToString());
            }
        }
Пример #7
0
        public IFileExtractResult ClipSNWResult(string filename, string area)
        {
            inputFileName = filename;
            using (IRasterDataProvider raster = GeoDataDriver.Open(filename) as IRasterDataProvider)
            {
                _resolutionX      = raster.ResolutionX;
                _resolutionY      = raster.ResolutionY;
                _fileSize         = new Size(raster.Width, raster.Height);
                _fileEnvelope     = raster.CoordEnvelope;
                _activeSpatialRef = raster.SpatialRef;
                _activeCoordType  = raster.CoordType;
                raster.Dispose();
            }
            _outsizeRegion = new CoordEnvelope(_fileEnvelope.MinX, _fileEnvelope.MaxX, _fileEnvelope.MinY, _fileEnvelope.MaxY);
            OutDir         = Path.GetDirectoryName(filename);

            string                   hdrfile = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + ".hdr");
            List <Feature>           fets    = new List <Feature>();
            IVectorFeatureDataReader dr      = null;

            if (area == "中国区")
            {
                url = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "数据引用\\基础矢量\\行政区划\\面\\中国边界.shp");
            }
            else
            {
                url = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "数据引用\\基础矢量\\行政区划\\面\\省级行政区域_面.shp");
            }
            dr = VectorDataReaderFactory.GetUniversalDataReader(url) as IVectorFeatureDataReader;
            Feature[] temp = null;
            temp = dr.FetchFeatures();
            fets.AddRange(temp);
            Feature[] features = null;
            features   = fets.ToArray();
            _blockName = area;
            int index = GetFeatureIndx(area);

            _vectorFeature = features[index];//_selectFeature
            //string fieldValue = _vectorFeature.GetFieldValue(fieldIndex);
            evp = GetMaskEnvelope(_vectorFeature.Geometry as ShapePolygon);
            BlockDefWithAOI        outEnvelope;
            Size                   size;
            List <BlockDefWithAOI> blockList = new List <BlockDefWithAOI>();

            GetArgs(out outEnvelope, out size, out OutDir, out filename);
            envelopes = new BlockDefWithAOI[] { outEnvelope };
            aoiIndex  = GetFeatureAOIIndex();
            blockList.AddRange(envelopes);
            blockList[0].AOIIndexes = aoiIndex;
            string[] put = RasterClipT(inputFileName, blockList.ToArray(), OutDir, "Cut");
            //put[0] = OutDir + put[0];
            string             getpath = Path.GetDirectoryName(put[0]);
            IFileExtractResult res     = new FileExtractResult("MWS", put[0], true);

            res.SetDispaly(false);
            return(res);
        }
Пример #8
0
 public PixelFeatures Export(Envelope geoEnvelope, Size rasterSize, string shpFile,
                             string fieldName, Func <Feature, double, bool> filter)
 {
     using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFile) as IVectorFeatureDataReader)
     {
         Feature[] fets = dr.FetchFeatures();
         if (fets == null || !(fets[0].Geometry is ShapePoint))
         {
             return(null);
         }
         ShapePoint pt;
         int        row = 0, col = 0;
         double     spanY = geoEnvelope.Height / rasterSize.Height;
         double     spanX = geoEnvelope.Width / rasterSize.Width;
         Dictionary <int, double> pixelFets = new Dictionary <int, double>();
         double fetValue;
         foreach (Feature fet in fets)
         {
             pt  = fet.Geometry as ShapePoint;
             row = (int)((geoEnvelope.MaxY - pt.Y) / spanY);
             col = (int)((pt.X - geoEnvelope.MinX) / spanX);
             if (row < 0 || row >= rasterSize.Height)
             {
                 continue;
             }
             if (col < 0 || col >= rasterSize.Width)
             {
                 continue;
             }
             fetValue = ToDouble(fet.GetFieldValue(fieldName));
             int key = row * rasterSize.Width + col;
             if (filter != null)
             {
                 if (filter(fet, fetValue))
                 {
                     if (!pixelFets.ContainsKey(key))
                     {
                         pixelFets.Add(key, fetValue);
                     }
                 }
             }
             else
             {
                 pixelFets.Add(key, fetValue);
             }
         }
         if (pixelFets.Count == 0)
         {
             return(null);
         }
         PixelFeatures ret = new PixelFeatures();
         ret.FeatureValues = pixelFets.Values.ToArray();
         ret.RasterIndexes = pixelFets.Keys.ToArray();
         return(ret);
     }
 }
Пример #9
0
        public ShapePolygon[] GetGeometry(List <string> retNames, string nameField)
        {
            string fname = FindVector();

            if (fname == null)
            {
                return(null);
            }
            IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(fname) as IVectorFeatureDataReader;

            if (dr == null)
            {
                return(null);
            }
            try
            {
                Feature[] fets;
                if (_where != null)
                {
                    fets = dr.FetchFeatures((f) => { return(_where(f)); });
                }
                else
                {
                    Feature f = dr.FetchFirstFeature();
                    if (f != null)
                    {
                        fets = new Feature[] { f }
                    }
                    ;
                    else
                    {
                        return(null);
                    }
                }
                if (fets == null || fets.Length == 0)
                {
                    return(null);
                }
                List <ShapePolygon> geometrys = new List <ShapePolygon>();
                foreach (Feature fet in fets)
                {
                    geometrys.Add(fet.Geometry as ShapePolygon);
                    if (retNames != null)
                    {
                        retNames.Add(fet.GetFieldValue(nameField) ?? string.Empty);
                    }
                }
                return(geometrys.ToArray());
            }
            finally
            {
                dr.Dispose();
            }
        }
Пример #10
0
        private void button1_Click(object sender, EventArgs e)
        {
            Color c = Color.Transparent;

            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "ESRI Shape Files(*.shp)|*.shp";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(dlg.FileName) as IVectorFeatureDataReader;
                    //Feature fet = dr.FetchFirstFeature();
                    //fet = dr.FetchFeature((f) => { return f.GetFieldValue("CNTRY_NAME") == "China"; });
                    v = dr.FetchFeatures();
                }
            }
        }
Пример #11
0
        ProductColorTable _colorTable;                                                      //ISOT

        private void CreateLegendItems(string shpFile)
        {
            using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFile) as IVectorFeatureDataReader)
            {
                if (dr == null)
                {
                    return;
                }
                Feature[] features = dr.FetchFeatures();
                if (features == null || features.Length == 0)
                {
                    return;
                }
                string field = dr.Fields[0];//Contour
                GetFeatureValueType(features, field);
            }
        }
Пример #12
0
 public void ProcessVectorToRaster(string shpFileName, enumDataType dataType, double resolution, string rasterFileName)
 {
     if (string.IsNullOrEmpty(rasterFileName))
     {
         return;
     }
     using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFileName)
                                          as IVectorFeatureDataReader)
     {
         if (dr == null)
         {
             return;
         }
         CoordEnvelope       envelope = new CoordEnvelope(-180, 180, -90, 90);
         int                 height   = (int)Math.Ceiling((envelope.MaxY - envelope.MinY) / resolution);
         int                 width    = (int)Math.Ceiling((envelope.MaxX - envelope.MinX) / resolution);
         IRasterDataProvider dataPrd  = null;
         try
         {
             if (Path.GetExtension(rasterFileName).ToUpper() == ".DAT")
             {
                 IRasterDataDriver driver  = GeoDataDriver.GetDriverByName("MEM") as IRasterDataDriver;
                 string            mapInfo = envelope.ToMapInfoString(new Size(width, height));
                 dataPrd = driver.Create(rasterFileName, width, height, 1, dataType, mapInfo);
             }
             else
             {
                 return;
             }
             Feature[] features = dr.FetchFeatures();
             if (features == null || features.Length < 1)
             {
                 return;
             }
             ProcessVectorToRaster(features, dataPrd);
         }
         finally
         {
             if (dataPrd != null)
             {
                 dataPrd.Dispose();
             }
         }
     }
 }
Пример #13
0
 private void LoadFeatures(string shpFileName)
 {
     if (!File.Exists(shpFileName))
     {
         throw new FileNotFoundException(shpFileName);
     }
     //
     using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFileName) as IVectorFeatureDataReader)
     {
         Feature[] fets = dr.FetchFeatures();
         if (fets != null || fets.Length > 0)
         {
             _features.AddRange(fets);
         }
     }
     //
     TryLoadInterestRegions();
     _shpFileName = shpFileName;
 }
Пример #14
0
        public Feature[] GetFeatures()
        {
            string fname = FindVector();

            if (fname == null)
            {
                return(null);
            }
            IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(fname) as IVectorFeatureDataReader;

            if (dr == null)
            {
                return(null);
            }
            try
            {
                Feature[] fets;
                if (_where != null)
                {
                    fets = dr.FetchFeatures((f) => { return(_where(f)); });
                }
                else
                {
                    Feature f = dr.FetchFirstFeature();
                    if (f != null)
                    {
                        fets = new Feature[] { f }
                    }
                    ;
                    else
                    {
                        return(null);
                    }
                }
                return(fets.Length == 0 ? null : fets.ToArray());
            }
            finally
            {
                dr.Dispose();
            }
        }
Пример #15
0
 private void TryLoadInterestRegions()
 {
     if (_interestRegions == null || _interestRegions.Length == 0)
     {
         return;
     }
     foreach (string region in _interestRegions)
     {
         string fname = GetShpFileName(region);
         if (fname != null)
         {
             using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(fname) as IVectorFeatureDataReader)
             {
                 Feature[] fets = dr.FetchFeatures();
                 if (fets != null || fets.Length > 0)
                 {
                     _waterBodyFeatures.AddRange(fets);
                 }
             }
         }
     }
 }
Пример #16
0
        private IExtractResult GenerateExtractResult(string gfrFile, IRasterDataProvider dataPrd)
        {
            IPixelIndexMapper result   = PixelIndexMapperFactory.CreatePixelIndexMapper("FIR", dataPrd.Width, dataPrd.Height, dataPrd.CoordEnvelope, dataPrd.SpatialRef);
            Dataset           _dataset = Gdal.Open(gfrFile, Access.GA_ReadOnly);

            if (_dataset.RasterCount == 0)
            {
                return(result);
            }
            else
            {
                CoordEnvelope envelope = dataPrd.CoordEnvelope.Clone();
                double        maxX     = envelope.MaxX;
                double        minX     = envelope.MinX;
                double        maxY     = envelope.MaxY;
                double        minY     = envelope.MinY;
                using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(gfrFile) as IVectorFeatureDataReader)
                {
                    Feature[] features = dr.FetchFeatures();
                    for (int i = 0; i < features.Length; i++)
                    {
                        double x, y;
                        if (double.TryParse(features[i].FieldValues[4], out x) && double.TryParse(features[i].FieldValues[3], out y))
                        {
                            if (IsInRange(minX, maxX, x) && IsInRange(minY, maxY, y))
                            {
                                int index = GetIndex(x, y);
                                if (index >= result.Count)
                                {
                                    break;
                                }
                                result.Put(index);
                            }
                        }
                    }
                }
                return(result);
            }
        }
Пример #17
0
 public int[,] GetPointPosition(string shpFileName)
 {
     using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFileName)
                                          as IVectorFeatureDataReader)
     {
         if (dr == null)
         {
             return(null);
         }
         Feature[] features = dr.FetchFeatures();
         if (features == null || features.Length < 1)
         {
             return(null);
         }
         int[,] index = new int[2, features.Length];
         int i = 0;
         foreach (Feature fea in features)
         {
             ShapePoint pt  = fea.Geometry as ShapePoint;
             int        col = (int)Math.Ceiling((pt.X + 180) / _resolution) - 1;
             int        row = (int)Math.Ceiling((90 - pt.Y) / _resolution) - 1;
             if (col < 0)
             {
                 col = 0;
             }
             if (row < 0)
             {
                 row = 0;
             }
             index[0, i] = row;
             index[1, i] = col;
             i++;
         }
         return(index);
     }
 }
Пример #18
0
        public void ProcessVectorToRaster(string shpFileName, string shpPrimaryField, enumDataType dataType, double resolution, string rasterFileName)
        {
            //创建目标文件
            if (string.IsNullOrEmpty(rasterFileName))
            {
                return;
            }
            if (string.IsNullOrEmpty(shpFileName) || !File.Exists(shpFileName) ||
                Path.GetExtension(shpFileName).ToUpper() != ".SHP")
            {
                return;
            }
            using (IVectorFeatureDataReader dr = VectorDataReaderFactory.GetUniversalDataReader(shpFileName)
                                                 as IVectorFeatureDataReader)
            {
                if (dr == null)
                {
                    return;
                }
                Envelope env = dr.Envelope;
                if (env == null)
                {
                    return;
                }
                CoordEnvelope       envelope = new CoordEnvelope(env.MinX, env.MaxX, env.MinY, env.MaxY);
                int                 height   = (int)Math.Ceiling((envelope.MaxY - envelope.MinY) / resolution);
                int                 width    = (int)Math.Ceiling((envelope.MaxX - envelope.MinX) / resolution);
                IRasterDataProvider dataPrd  = null;
                try
                {
                    string extension = Path.GetExtension(rasterFileName).ToUpper();
                    switch (extension)
                    {
                    case ".LDF":
                    {
                        IRasterDataDriver driver  = GeoDataDriver.GetDriverByName("LDF") as IRasterDataDriver;
                        string            mapInfo = envelope.ToMapInfoString(new Size(width, height));
                        dataPrd = driver.Create(rasterFileName, width, height, 1, dataType, mapInfo);
                        break;
                    }

                    case ".DAT":
                    {
                        IRasterDataDriver driver  = GeoDataDriver.GetDriverByName("MEM") as IRasterDataDriver;
                        string            mapInfo = envelope.ToMapInfoString(new Size(width, height));
                        dataPrd = driver.Create(rasterFileName, width, height, 1, dataType, mapInfo);
                        break;
                    }

                    default:
                        return;
                    }
                    Feature[] features = dr.FetchFeatures();
                    if (features == null || features.Length < 1)
                    {
                        return;
                    }
                    ProcessVectorToRaster(features, shpPrimaryField, dataPrd);
                }
                finally
                {
                    if (dataPrd != null)
                    {
                        dataPrd.Dispose();
                    }
                }
            }
        }