コード例 #1
0
        private void B_AddData_Click(object sender, EventArgs e)
        {
            if (CB_LonFld.Text == "" || CB_LatFld.Text == "")
            {
                MessageBox.Show("All fields should be set!", "Alarm");
                return;
            }

            SaveFileDialog saveDlg = new SaveFileDialog();
            string         shpFile;

            saveDlg.Filter = "shp file (*.shp)|*.shp";
            if (saveDlg.ShowDialog() == DialogResult.OK)
            {
                shpFile = saveDlg.FileName;
                if (System.IO.File.Exists(shpFile))
                {
                    string lPathNoExt = System.IO.Path.GetDirectoryName(shpFile) + @"\" + System.IO.Path.GetFileNameWithoutExtension(shpFile);
                    System.IO.File.Delete(lPathNoExt + ".shp");
                    System.IO.File.Delete(lPathNoExt + ".shx");
                    System.IO.File.Delete(lPathNoExt + ".dbf");
                    System.IO.File.Delete(lPathNoExt + ".prj");
                }

                //New layer
                VectorLayer aLayer = new VectorLayer(ShapeTypes.Point);
                aLayer.LayerDrawType = LayerDrawType.Map;
                aLayer.LayerName     = System.IO.Path.GetFileName(shpFile);
                aLayer.FileName      = shpFile;
                aLayer.LegendScheme  = LegendManage.CreateSingleSymbolLegendScheme(ShapeTypes.Point, Color.Black, 5);
                aLayer.Visible       = true;

                int lonIdx, latIdx;
                lonIdx = CB_LonFld.SelectedIndex;
                latIdx = CB_LatFld.SelectedIndex;
                double lon, lat;

                StreamReader sr = new StreamReader(m_Infile, System.Text.Encoding.UTF8);
                string[]     dataArray;
                string       aLine = sr.ReadLine(); //First line - title
                //Get field list
                List <string> fieldList = new List <string>();
                dataArray = aLine.Split(',');
                if (dataArray.Length < 3)
                {
                    MessageBox.Show("The data should have at least three fields!", "Error");
                    return;
                }
                fieldList = new List <string>(dataArray.Length);
                fieldList.AddRange(dataArray);

                //Judge field type
                List <string> varList = new List <string>();
                aLine     = sr.ReadLine(); //First data line
                dataArray = aLine.Split(',');
                for (int i = 3; i < dataArray.Length; i++)
                {
                    if (MeteoInfoC.Global.MIMath.IsNumeric(dataArray[i]))
                    {
                        varList.Add(fieldList[i]);
                    }
                }


                //Add fields
                for (int i = 0; i < fieldList.Count; i++)
                {
                    DataColumn aDC = new DataColumn();
                    aDC.ColumnName = fieldList[i];
                    if (varList.Contains(fieldList[i]))
                    {
                        aDC.DataType = typeof(double);
                    }
                    else
                    {
                        aDC.DataType = typeof(string);
                    }
                    aLayer.EditAddField(aDC);
                }

                //Read data
                //aLine = sr.ReadLine();
                while (aLine != null)
                {
                    dataArray = aLine.Split(',');
                    if (dataArray.Length < 2)
                    {
                        aLine = sr.ReadLine();
                        continue;
                    }

                    MeteoInfoC.PointD aPoint = new MeteoInfoC.PointD();
                    lon      = double.Parse(dataArray[lonIdx]);
                    lat      = double.Parse(dataArray[latIdx]);
                    aPoint.X = lon;
                    aPoint.Y = lat;

                    //Add shape
                    PointShape aPS = new PointShape();
                    aPS.Point = aPoint;
                    int shapeNum = aLayer.ShapeNum;
                    if (aLayer.EditInsertShape(aPS, shapeNum))
                    {
                        //Edit record value
                        for (int j = 0; j < fieldList.Count; j++)
                        {
                            if (varList.Contains(fieldList[j]))
                            {
                                aLayer.EditCellValue(fieldList[j], shapeNum, double.Parse(dataArray[j]));
                            }
                            else
                            {
                                aLayer.EditCellValue(fieldList[j], shapeNum, dataArray[j]);
                            }
                        }
                    }

                    aLine = sr.ReadLine();
                }

                //Save shape file
                ShapeFileManage.SaveShapeFile(shpFile, aLayer);

                //Add layer
                frmMain.CurrentWin.MapDocument.ActiveMapFrame.AddLayer(aLayer);
                frmMain.CurrentWin.MapDocument.ActiveMapFrame.MapView.PaintLayers();
            }
        }
コード例 #2
0
        private void SaveWMPFile()
        {
            SaveFileDialog SFDlg = new SaveFileDialog();

            SFDlg.Filter = "wmp File (*.wmp)|*.wmp";
            if (SFDlg.ShowDialog() == DialogResult.OK)
            {
                //ProgressBar
                toolStripProgressBar1.Visible = true;
                this.Cursor = Cursors.WaitCursor;

                string       aFile       = SFDlg.FileName;
                StreamWriter sw          = new StreamWriter(aFile);
                List <int>   selIndexes  = _currentLayer.GetSelectedShapeIndexes();
                bool         hasSelShape = _currentLayer.HasSelectedShapes();
                int          shpNum      = _currentLayer.ShapeNum;
                if (hasSelShape)
                {
                    shpNum = selIndexes.Count;
                }
                int i;
                switch (_currentLayer.ShapeType)
                {
                case ShapeTypes.Point:
                    sw.WriteLine("Point");
                    sw.WriteLine(shpNum.ToString());
                    PointShape aPS = new PointShape();
                    if (hasSelShape)
                    {
                        for (i = 0; i < _currentLayer.ShapeNum; i++)
                        {
                            aPS = (PointShape)_currentLayer.ShapeList[i];
                            if (aPS.Selected)
                            {
                                sw.WriteLine(aPS.Point.X.ToString() + "," + aPS.Point.Y.ToString());
                            }
                            this.toolStripProgressBar1.Value = (i + 1) * 100 / _currentLayer.ShapeNum;
                        }
                    }
                    else
                    {
                        for (i = 0; i < _currentLayer.ShapeNum; i++)
                        {
                            aPS = (PointShape)_currentLayer.ShapeList[i];
                            sw.WriteLine(aPS.Point.X.ToString() + "," + aPS.Point.Y.ToString());
                            this.toolStripProgressBar1.Value = (i + 1) * 100 / _currentLayer.ShapeNum;
                        }
                    }
                    break;

                case ShapeTypes.Polyline:
                    sw.WriteLine("Polyline");
                    int           shapeNum = 0;
                    PolylineShape aPLS     = new PolylineShape();
                    if (hasSelShape)
                    {
                        for (i = 0; i < _currentLayer.ShapeNum; i++)
                        {
                            aPLS = (PolylineShape)_currentLayer.ShapeList[i];
                            if (aPLS.Selected)
                            {
                                shapeNum += aPLS.PartNum;
                            }
                        }
                    }
                    for (i = 0; i < _currentLayer.ShapeNum; i++)
                    {
                        aPLS      = (PolylineShape)_currentLayer.ShapeList[i];
                        shapeNum += aPLS.PartNum;
                    }
                    sw.WriteLine(shpNum.ToString());

                    for (i = 0; i < _currentLayer.ShapeNum; i++)
                    {
                        aPLS = (PolylineShape)_currentLayer.ShapeList[i];
                        if (hasSelShape)
                        {
                            if (!aPLS.Selected)
                            {
                                continue;
                            }
                        }
                        MeteoInfoC.PointD[] Pointps;
                        for (int p = 0; p < aPLS.PartNum; p++)
                        {
                            if (p == aPLS.PartNum - 1)
                            {
                                Pointps = new MeteoInfoC.PointD[aPLS.PointNum - aPLS.parts[p]];
                                for (int pp = aPLS.parts[p]; pp < aPLS.PointNum; pp++)
                                {
                                    Pointps[pp - aPLS.parts[p]] = (MeteoInfoC.PointD)aPLS.Points[pp];
                                }
                            }
                            else
                            {
                                Pointps = new MeteoInfoC.PointD[aPLS.parts[p + 1] - aPLS.parts[p]];
                                for (int pp = aPLS.parts[p]; pp < aPLS.parts[p + 1]; pp++)
                                {
                                    Pointps[pp - aPLS.parts[p]] = (MeteoInfoC.PointD)aPLS.Points[pp];
                                }
                            }
                            sw.WriteLine(Pointps.Length.ToString());
                            foreach (MeteoInfoC.PointD aPoint in Pointps)
                            {
                                sw.WriteLine(aPoint.X.ToString() + "," + aPoint.Y.ToString());
                            }
                            shapeNum += 1;
                        }
                        Application.DoEvents();
                        this.toolStripProgressBar1.Value = (i + 1) * 100 / _currentLayer.ShapeNum;
                    }
                    break;

                case ShapeTypes.Polygon:
                    sw.WriteLine("Polygon");
                    shapeNum = 0;
                    PolygonShape aPGS = new PolygonShape();
                    for (i = 0; i < _currentLayer.ShapeNum; i++)
                    {
                        aPGS = (PolygonShape)_currentLayer.ShapeList[i];
                        if (hasSelShape)
                        {
                            if (!aPGS.Selected)
                            {
                                continue;
                            }
                        }
                        shapeNum += aPGS.PartNum;
                    }
                    sw.WriteLine(shapeNum.ToString());

                    for (i = 0; i < _currentLayer.ShapeNum; i++)
                    {
                        aPGS = (PolygonShape)_currentLayer.ShapeList[i];
                        if (hasSelShape)
                        {
                            if (!aPGS.Selected)
                            {
                                continue;
                            }
                        }

                        MeteoInfoC.PointD[] Pointps;
                        for (int p = 0; p < aPGS.PartNum; p++)
                        {
                            if (p == aPGS.PartNum - 1)
                            {
                                Pointps = new MeteoInfoC.PointD[aPGS.PointNum - aPGS.parts[p]];
                                for (int pp = aPGS.parts[p]; pp < aPGS.PointNum; pp++)
                                {
                                    Pointps[pp - aPGS.parts[p]] = (MeteoInfoC.PointD)aPGS.Points[pp];
                                }
                            }
                            else
                            {
                                Pointps = new MeteoInfoC.PointD[aPGS.parts[p + 1] - aPGS.parts[p]];
                                for (int pp = aPGS.parts[p]; pp < aPGS.parts[p + 1]; pp++)
                                {
                                    Pointps[pp - aPGS.parts[p]] = (MeteoInfoC.PointD)aPGS.Points[pp];
                                }
                            }
                            sw.WriteLine(Pointps.Length.ToString());
                            foreach (MeteoInfoC.PointD aPoint in Pointps)
                            {
                                sw.WriteLine(aPoint.X.ToString() + "," + aPoint.Y.ToString());
                            }
                            shapeNum += 1;
                        }
                        Application.DoEvents();
                        this.toolStripProgressBar1.Value = (i + 1) * 100 / _currentLayer.ShapeNum;
                    }
                    break;
                }

                sw.Close();

                //Progressbar
                this.toolStripProgressBar1.Value   = 0;
                this.toolStripProgressBar1.Visible = false;
                this.Cursor = Cursors.Default;
            }
        }
コード例 #3
0
        private void SavGrADSMaskoutFile()
        {
            SaveFileDialog SFDlg = new SaveFileDialog();

            SFDlg.Filter = "GrADS File (*.ctl)|*.ctl";
            if (SFDlg.ShowDialog() == DialogResult.OK)
            {
                string aFile = SFDlg.FileName;
                int    i;
                bool   hasSelShape = _currentLayer.HasSelectedShapes();

                //Get grid set
                PolygonShape aPGS    = new PolygonShape();
                Extent       aExtent = new Extent();
                int          n       = 0;
                for (i = 0; i < _currentLayer.ShapeNum; i++)
                {
                    aPGS = (PolygonShape)_currentLayer.ShapeList[i];
                    if (hasSelShape)
                    {
                        if (!aPGS.Selected)
                        {
                            continue;
                        }
                    }
                    if (n == 0)
                    {
                        aExtent = aPGS.Extent;
                    }
                    else
                    {
                        aExtent = MIMath.GetLagerExtent(aExtent, aPGS.Extent);
                    }
                    n += 1;
                }

                GridDataSetting aGDP = new GridDataSetting();
                aGDP.DataExtent.minX = Math.Floor(aExtent.minX);
                aGDP.DataExtent.maxX = Math.Ceiling(aExtent.maxX);
                aGDP.DataExtent.minY = Math.Floor(aExtent.minY);
                aGDP.DataExtent.maxY = Math.Ceiling(aExtent.maxY);
                aGDP.XNum            = 20;
                aGDP.YNum            = 20;

                frmGridSet aFrmGS = new frmGridSet();
                aFrmGS.SetParameters(aGDP);
                if (aFrmGS.ShowDialog() == DialogResult.OK)
                {
                    aFrmGS.GetParameters(ref aGDP);

                    //Show progressbar
                    this.toolStripProgressBar1.Visible = true;
                    this.toolStripProgressBar1.Value   = 0;
                    this.Cursor = Cursors.WaitCursor;
                    Application.DoEvents();

                    //Get grid data
                    double[,] gridData = new double[aGDP.YNum, aGDP.XNum];
                    int j, p;
                    MeteoInfoC.PointD aPoint = new MeteoInfoC.PointD();
                    double            xSize, ySize;
                    xSize = (aGDP.DataExtent.maxX - aGDP.DataExtent.minX) / (aGDP.XNum - 1);
                    ySize = (aGDP.DataExtent.maxY - aGDP.DataExtent.minY) / (aGDP.YNum - 1);
                    bool isIn = false;
                    for (i = 0; i < aGDP.YNum; i++)
                    {
                        aPoint.Y = aGDP.DataExtent.minY + i * ySize;
                        for (j = 0; j < aGDP.XNum; j++)
                        {
                            aPoint.X = aGDP.DataExtent.minX + j * xSize;
                            isIn     = false;
                            for (p = 0; p < _currentLayer.ShapeNum; p++)
                            {
                                aPGS = (PolygonShape)_currentLayer.ShapeList[p];
                                if (hasSelShape)
                                {
                                    if (!aPGS.Selected)
                                    {
                                        continue;
                                    }
                                }
                                if (MIMath.PointInPolygon(aPGS, aPoint))
                                {
                                    isIn = true;
                                    break;
                                }
                            }
                            if (isIn)
                            {
                                gridData[i, j] = 1;
                            }
                            else
                            {
                                gridData[i, j] = -1;
                            }
                        }
                        this.toolStripProgressBar1.Value = (i + 1) * 100 / aGDP.YNum;
                        Application.DoEvents();
                    }

                    //Get GrADS data info
                    string        dFile     = Path.ChangeExtension(aFile, ".dat");
                    GrADSDataInfo aDataInfo = new GrADSDataInfo();
                    aDataInfo.TITLE       = "Mask data";
                    aDataInfo.DSET        = dFile;
                    aDataInfo.DTYPE       = "GRIDDED";
                    aDataInfo.XDEF.Type   = "LINEAR";
                    aDataInfo.XDEF.XNum   = aGDP.XNum;
                    aDataInfo.XDEF.XMin   = (Single)aGDP.DataExtent.minX;
                    aDataInfo.XDEF.XDelt  = (Single)(xSize);
                    aDataInfo.YDEF.Type   = "LINEAR";
                    aDataInfo.YDEF.YNum   = aGDP.YNum;
                    aDataInfo.YDEF.YMin   = (Single)aGDP.DataExtent.minY;
                    aDataInfo.YDEF.YDelt  = (Single)(ySize);
                    aDataInfo.ZDEF.Type   = "LINEAR";
                    aDataInfo.ZDEF.ZNum   = 1;
                    aDataInfo.ZDEF.SLevel = 1;
                    aDataInfo.ZDEF.ZDelt  = 1;
                    aDataInfo.TDEF.Type   = "LINEAR";
                    aDataInfo.TDEF.TNum   = 1;
                    aDataInfo.TDEF.STime  = DateTime.Now;
                    aDataInfo.TDEF.TDelt  = "1mo";
                    Variable aVar = new Variable();
                    aVar.Name = "mask";
                    //aVar.LevelNum = 0;
                    aVar.Units       = "99";
                    aVar.Description = "background mask data";
                    aDataInfo.VARDEF.AddVar(aVar);

                    //Save files
                    aDataInfo.WriteGrADSCTLFile();
                    aDataInfo.WriteGrADSData_Grid(dFile, gridData);

                    //Hide progressbar
                    this.toolStripProgressBar1.Visible = false;
                    this.Cursor = Cursors.Default;
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Write Sufer BLN map file
        /// </summary>
        /// <param name="aFile">output file</param>
        /// <param name="shapes">shape list</param>
        public static void WriteMapFile_BLN(string aFile, List <Shape.Shape> shapes)
        {
            StreamWriter sw     = new StreamWriter(aFile);
            int          shpNum = shapes.Count;
            int          i;

            switch (shapes[0].ShapeType)
            {
            case ShapeTypes.Polyline:
                int           shapeNum = 0;
                PolylineShape aPLS     = new PolylineShape();
                for (i = 0; i < shpNum; i++)
                {
                    aPLS = (PolylineShape)shapes[i];
                    MeteoInfoC.PointD[] Pointps;
                    for (int p = 0; p < aPLS.PartNum; p++)
                    {
                        if (p == aPLS.PartNum - 1)
                        {
                            Pointps = new MeteoInfoC.PointD[aPLS.PointNum - aPLS.parts[p]];
                            for (int pp = aPLS.parts[p]; pp < aPLS.PointNum; pp++)
                            {
                                Pointps[pp - aPLS.parts[p]] = (MeteoInfoC.PointD)aPLS.Points[pp];
                            }
                        }
                        else
                        {
                            Pointps = new MeteoInfoC.PointD[aPLS.parts[p + 1] - aPLS.parts[p]];
                            for (int pp = aPLS.parts[p]; pp < aPLS.parts[p + 1]; pp++)
                            {
                                Pointps[pp - aPLS.parts[p]] = (MeteoInfoC.PointD)aPLS.Points[pp];
                            }
                        }
                        sw.WriteLine(Pointps.Length.ToString() + ",1");
                        foreach (MeteoInfoC.PointD aPoint in Pointps)
                        {
                            sw.WriteLine(aPoint.X.ToString() + "," + aPoint.Y.ToString());
                        }
                        shapeNum += 1;
                    }
                }
                break;

            case ShapeTypes.Polygon:
                shapeNum = 0;
                PolygonShape aPGS = new PolygonShape();
                for (i = 0; i < shpNum; i++)
                {
                    aPGS = (PolygonShape)shapes[i];
                    MeteoInfoC.PointD[] Pointps;
                    for (int p = 0; p < aPGS.PartNum; p++)
                    {
                        if (p == aPGS.PartNum - 1)
                        {
                            Pointps = new MeteoInfoC.PointD[aPGS.PointNum - aPGS.parts[p]];
                            for (int pp = aPGS.parts[p]; pp < aPGS.PointNum; pp++)
                            {
                                Pointps[pp - aPGS.parts[p]] = (MeteoInfoC.PointD)aPGS.Points[pp];
                            }
                        }
                        else
                        {
                            Pointps = new MeteoInfoC.PointD[aPGS.parts[p + 1] - aPGS.parts[p]];
                            for (int pp = aPGS.parts[p]; pp < aPGS.parts[p + 1]; pp++)
                            {
                                Pointps[pp - aPGS.parts[p]] = (MeteoInfoC.PointD)aPGS.Points[pp];
                            }
                        }
                        sw.WriteLine(Pointps.Length.ToString() + ",1");
                        foreach (MeteoInfoC.PointD aPoint in Pointps)
                        {
                            sw.WriteLine(aPoint.X.ToString() + "," + aPoint.Y.ToString());
                        }
                        shapeNum += 1;
                    }
                }
                break;
            }

            sw.Close();
        }