Пример #1
0
        /// <summary>初始数据源
        /// </summary>
        /// <param name="strFilename">文件名</param>
        /// <param name="oFileEncoding">文件编码类型</param>
        /// <param name="nLayerindex">层序号</param>
        /// <param name="bUpdata">是否可以修改</param>
        /// <returns></returns>
        public bool CreateDataset(string strFilename, Encoding oFileEncoding, int nLayerindex = 0, bool bUpdata = false)
        {
            int bXSize, bYSize;
            int w, h;

            w      = 100;
            h      = 100;
            bXSize = w;
            bYSize = 1;

            Gdal.AllRegister();
            OSGeo.GDAL.Driver drv = Gdal.GetDriverByName("GTiff");
            if (drv == null)
            {
                Console.WriteLine("Can't get driver.");
                System.Environment.Exit(-1);
            }
            Console.WriteLine("Using driver " + drv.LongName);

            string[] options = new string[] { "BLOCKXSIZE=" + bXSize, "BLOCKYSIZE=" + bYSize };
            Dataset  ds      = drv.Create(strFilename, w, h, 1, DataType.GDT_Byte, options);

            if (ds == null)
            {
                Console.WriteLine("Can't open " + strFilename);
                System.Environment.Exit(-1);
            }

            GCP[] GCPs = new GCP[] {
                new GCP(44.5, 27.5, 0, 0, 0, "info0", "id0"),
                new GCP(45.5, 27.5, 0, 100, 0, "info1", "id1"),
                new GCP(44.5, 26.5, 0, 0, 100, "info2", "id2"),
                new GCP(45.5, 26.5, 0, 100, 100, "info3", "id3")
            };
            ds.SetGCPs(GCPs, "");

            Band ba = ds.GetRasterBand(1);

            byte[] buffer = new byte[w * h];
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    buffer[i * w + j] = (byte)(i * 256 / w);
                }
            }
            ba.WriteRaster(0, 0, w, h, buffer, w, h, 0, 0);
            ba.FlushCache();
            ds.FlushCache();
            return(true);
        }
Пример #2
0
        /// <summary>
        /// 控制点转换
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="tragetFile"></param>
        /// <param name="multiPoint"></param>
        /// <param name="wkt"></param>
        /// <returns></returns>
        public bool ControlPointTransform(string srcFile, string traFile, string wkt, string[] multiPoint)
        {
            bool success = false;

            if (multiPoint.Length <= 0)
            {
                return(success);
            }
            else
            {
                try
                {
                    //"[[12,21,23,24],[45,56,21,2],{45,56,21,2},{45,56,21,2}]";
                    //if(multiPoint.Contains("},{"))
                    //{
                    //    multiPoint = multiPoint.Replace("},{","#");
                    //}
                    //multiPoint = multiPoint.TrimStart('{').TrimStart('[').TrimEnd('}').TrimEnd(']');
                    //string[] point = multiPoint.Split('#');

                    GCP[] pGCPS = new GCP[multiPoint.Length];
                    for (int i = 0; i < multiPoint.Length; i++)
                    {
                        string[] point = multiPoint[i].Split(',');
                        GCP      gcp   = new GCP(Double.Parse(point[0]), Double.Parse(point[1]), 0.0, Double.Parse(point[2]), Double.Parse(point[3]), "", i.ToString());
                        pGCPS[i] = gcp;
                    }

                    GCPWarpMethod warpMethod = GCPWarpMethod.Order1;

                    InfoNotify info = (i) => { System.Diagnostics.Debug.WriteLine(i); };
                    ErrNotify  err  = (i) => { System.Diagnostics.Debug.WriteLine(i); };
                    wkt     = GetWKTText(wkt);
                    success = GCPWarp.VectorWarp(srcFile, traFile, pGCPS, warpMethod, wkt, "UTF-8", info, err);
                }
                catch (Exception ex)
                {
                }
                return(success);
            }
        }
Пример #3
0
        //creates an ENVI header file with the same layout as ENVI outputs when calibrating a file
        public static void createENVIHeader(string headerFilename, string datasetFilename, Dataset ds, BandInfo[] bandInfos, DataType outputDataType, double scaleFactor)
        {
            TextWriter tw = new StreamWriter(headerFilename);

            tw.WriteLine("ENVI");
            tw.WriteLine("description = {WorldView Calibration Result, units = MicroWatts/(square centimeter * steradian * nanometer) [" + DateTime.Now.ToString("ddd MMM d yyyy HH:mm:ss") + "]}");
            tw.WriteLine("samples = " + ds.RasterXSize);
            tw.WriteLine("lines = " + ds.RasterYSize);
            tw.WriteLine("bands = " + ds.RasterCount);
            tw.WriteLine("header offset = 0");
            tw.WriteLine("file type = ENVI Standard");
            if (outputDataType == DataType.GDT_Byte)
            {
                tw.WriteLine("data type = 1");
            }
            else if (outputDataType == DataType.GDT_UInt16)
            {
                tw.WriteLine("data type = 12");
            }
            else if (outputDataType == DataType.GDT_Float32)
            {
                tw.WriteLine("data type = 4");
            }
            tw.WriteLine("interleave = bsq");
            tw.WriteLine("byte order = 0");

            //coordinate information can be in either of these two places, so just pick the longer one (the other is usually empty)
            string coordinateSystemString1    = ds.GetGCPProjection();
            string coordinateSystemString2    = ds.GetProjection();
            string coordinateSystemStringBest = (coordinateSystemString1.Length > coordinateSystemString2.Length) ? coordinateSystemString1 : coordinateSystemString2;

            tw.WriteLine("coordinate system string = {" + coordinateSystemStringBest + "}");

            //write default RGB bands if present
            int rIndex = -1, gIndex = -1, bIndex = -1;

            for (int b = 0; b < bandInfos.Length; b++)
            {
                if (bandInfos[b].bandCode.Equals("R"))
                {
                    rIndex = b;
                }
                if (bandInfos[b].bandCode.Equals("G"))
                {
                    gIndex = b;
                }
                if (bandInfos[b].bandCode.Equals("B"))
                {
                    bIndex = b;
                }
            }
            if (rIndex > -1 && gIndex > -1 && bIndex > -1)
            {
                tw.WriteLine("default bands = {" + (rIndex + 1) + "," + (gIndex + 1) + "," + (bIndex + 1) + "}");
            }

            tw.WriteLine("wavelength units = Nanometers");

            tw.WriteLine("geo points = {");
            GCP[] gcps = ds.GetGCPs();
            if (gcps == null)              //GDAL couldn't read corner coordinates, so try to parse them ourselves (GDAL compatibility issue with WorldView3 NITFs?)
            {
                string cscrna = ds.GetMetadataItem("CSCRNA", "TRE");
                if (cscrna != null)
                {
                    gcps = new GCP[4];
                    int cscidx = 1;
                    for (int i = 0; i < 4; i++)
                    {
                        double lat = double.Parse(cscrna.Substring(cscidx, 9)); cscidx += 9;
                        double lon = double.Parse(cscrna.Substring(cscidx, 10)); cscidx += 10;

                        int pixelx, pixely;
                        if (i == 0)
                        {
                            pixelx = 1; pixely = 1;
                        }
                        else if (i == 1)
                        {
                            pixelx = ds.RasterXSize + 1; pixely = 1;
                        }
                        else if (i == 2)
                        {
                            pixelx = ds.RasterXSize + 1; pixely = ds.RasterYSize + 1;
                        }
                        else
                        {
                            pixelx = 1; pixely = ds.RasterYSize + 1;
                        }

                        cscidx += 8;
                        gcps[i] = new GCP(lon, lat, 0, pixelx, pixely, "", "");
                    }
                }
            }
            if (gcps != null)              //write GCP info if available
            //swap 3rd and 4th GCPs, because NITF stores in UL,UR,LR,LL order, but envi wants UL,UR,LL,LR order
            {
                GCP GCPtmp = gcps[2];
                gcps[2] = gcps[3];
                gcps[3] = GCPtmp;
                //write GCPs
                for (int i = 0; i < gcps.Length; i++)
                {
                    tw.WriteLine(" " + gcps[i].GCPPixel + ", " + gcps[i].GCPLine + ", " + gcps[i].GCPY + ", " + gcps[i].GCPX + ((i == gcps.Length - 1) ? "}" : ""));
                }
            }

            tw.WriteLine("pseudo projection info = {Geographic Lat/Lon, WGS-84, units=Degrees}");

            tw.WriteLine("band names = {");
            for (int i = 0; i < bandInfos.Length; i++)
            {
                tw.WriteLine(" WV Calibrated Radiance: Band " + (i + 1) + " (" + bandInfos[i].bandCode + ")" + ((i == bandInfos.Length - 1) ? "}" : ", "));
            }

            //parse wavelengths and write to header
            double[] wavelengths = new double[bandInfos.Length];
            string   cssfaa      = ds.GetMetadataItem("CSSFAA", "TRE");

            if (cssfaa != null)              //parse wavelength data if available
            {
                int iPos = 1;
                for (int i = 0; i < wavelengths.Length; i++)
                {
                    iPos++;
                    wavelengths[i] = double.Parse(cssfaa.Substring(iPos, 6)); iPos += 6;
                    iPos          += 99;
                }
                tw.WriteLine("wavelength = {");
                tw.Write(" ");
                for (int i = 0; i < wavelengths.Length; i++)
                {
                    tw.Write(wavelengths[i] + ((i == wavelengths.Length - 1) ? "}\n" : ", "));
                }
            }

            //data gains
            tw.Write("data gain values = {\n ");
            for (int i = 0; i < bandInfos.Length; i++)
            {
                tw.Write(bandInfos[i].gain + ((i == bandInfos.Length - 1) ? "}\n" : ", "));
            }

            //data offset values
            tw.Write("data offset values = {\n ");
            for (int i = 0; i < bandInfos.Length; i++)
            {
                tw.Write((i == bandInfos.Length - 1) ? "0}\n" : "0, ");
            }

            //rpc coefficients
            List <double> rpcCoef = new List <double>();
            string        rpc2    = ds.GetMetadataItem("RPC00B", "TRE");

            if (rpc2 != null)              //parse RPC data if available
            {
                int charPos = 1 + 7 + 7;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 6))); charPos += 6;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 5))); charPos += 5;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 8))); charPos += 8;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 9))); charPos += 9;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 5))); charPos += 5;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 6))); charPos += 6;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 5))); charPos += 5;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 8))); charPos += 8;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 9))); charPos += 9;
                rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 5))); charPos += 5;
                for (int i = 0; i < 80; i++)
                {
                    rpcCoef.Add(double.Parse(rpc2.Substring(charPos, 12))); charPos += 12;
                }
                rpcCoef.Add(0);
                rpcCoef.Add(0);
                rpcCoef.Add(1);

                tw.WriteLine("rpc info = {");
                tw.Write(" ");
                for (int i = 0; i < rpcCoef.Count; i++)
                {
                    tw.Write(rpcCoef[i].ToString(" 0.00000000e+000;-0.00000000e+000"));

                    if (i != rpcCoef.Count - 1)
                    {
                        if (i % 4 == 3)
                        {
                            tw.Write(",\n ");
                        }
                        else
                        {
                            tw.Write(", ");
                        }
                    }
                    else
                    {
                        tw.Write("}\n");
                    }
                }
            }

            //write scale factor for all values if specified
            if (scaleFactor != 1)
            {
                tw.WriteLine("radiance_scale_factor = " + scaleFactor);
            }

            tw.Close();
        }
Пример #4
0
    public static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            usage();
        }

        // Using early initialization of System.Console
        Console.WriteLine("Writing sample: " + args[0]);

        int bXSize, bYSize;
        int w, h;

        w = 100;
        h = 100;

        if (args.Length > 1)
        {
            w = int.Parse(args[1]);
        }

        if (args.Length > 2)
        {
            h = int.Parse(args[2]);
        }

        bXSize = w;
        bYSize = 1;

        //try
        {
            /* -------------------------------------------------------------------- */
            /*      Register driver(s).                                             */
            /* -------------------------------------------------------------------- */
            Gdal.AllRegister();

            /* -------------------------------------------------------------------- */
            /*      Get driver                                                      */
            /* -------------------------------------------------------------------- */
            Driver drv = Gdal.GetDriverByName("GTiff");

            if (drv == null)
            {
                Console.WriteLine("Can't get driver.");
                System.Environment.Exit(-1);
            }

            Console.WriteLine("Using driver " + drv.LongName);

            /* -------------------------------------------------------------------- */
            /*      Open dataset.                                                   */
            /* -------------------------------------------------------------------- */
            string[] options = new string [] { "BLOCKXSIZE=" + bXSize, "BLOCKYSIZE=" + bYSize };
            Dataset  ds      = drv.Create(args[0], w, h, 1, DataType.GDT_Byte, options);

            if (ds == null)
            {
                Console.WriteLine("Can't open " + args[0]);
                System.Environment.Exit(-1);
            }

            /* -------------------------------------------------------------------- */
            /*      Setting corner GCPs.                                            */
            /* -------------------------------------------------------------------- */
            GCP[] GCPs = new GCP[] {
                new GCP(44.5, 27.5, 0, 0, 0, "info0", "id0"),
                new GCP(45.5, 27.5, 0, 100, 0, "info1", "id1"),
                new GCP(44.5, 26.5, 0, 0, 100, "info2", "id2"),
                new GCP(45.5, 26.5, 0, 100, 100, "info3", "id3")
            };
            ds.SetGCPs(GCPs, "");

            Band ba = ds.GetRasterBand(1);

            byte [] buffer = new byte [w * h];

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    buffer[i * w + j] = (byte)(i * 256 / w);
                }
            }

            ba.WriteRaster(0, 0, w, h, buffer, w, h, 0, 0);

            ba.FlushCache();
            ds.FlushCache();
        }

        /*catch (Exception e)
         * {
         *  Console.WriteLine("Application error: " + e.Message);
         * }*/
    }
Пример #5
0
 private static extern SIZE16 GetCharacterPlacement(HDC hdc, string lpString, int nCount, int nMaxExtent, ref GCP_RESULTS lpResults, GCP dwFlags);
Пример #6
0
 //===========================================================
 //		GetCharacterPlacement
 //===========================================================
 //
 //	何に使うのか良く分からない関数。後で詳しく MSDN を見る。
 //
 public SIZE16 GetCharacterPlacement(string text, GCP flags, int maxExtent, ref GCP_RESULTS results)
 {
     return(GetCharacterPlacement(this.hdc, text, text.Length, maxExtent, ref results, flags));
 }