【20090625-02】给GE下载的JPG卫片加配准JGW文件(转)

原帖:http://blog.csdn.net/duganghu/archive/2009/05/25/4215070.aspx

 

 

 

jpg地图的空间数据文件-jgw文件格式


1、X-Scale(一个像元的大小)
2、旋转项
3、旋转项
4、负的Y-Scale(一个像元的大小)
5、转换项,即左上角X坐标
6、转换项,即左上角Y坐标

 卫片为256*256大小,文件名以“qrst”串四分

view plaincopy to clipboardprint?
private void button10_Click(object sender, EventArgs e)  
{              
    if (Directory.Exists(textBox1.Text))  
    {  
        DirectoryInfo di = new DirectoryInfo(textBox1.Text);  
        foreach (FileInfo fi in  di.GetFiles("*.jpg"))  
        {  
            string str = fi.Name.Substring(0, fi.Name.IndexOf("."));  
            double[] coor = GetCoordinatesFromAddress(str);  
            double[] line = new double[6];  
            line[0] = (coor[2] - coor[0]) / 256;  
            line[1] = 0;  
            line[2] = 0;  
            line[3] = (-1) * (coor[1] - coor[3]) / 256;  
            line[4] = coor[0];  
            line[5] = coor[1];  
            try 
            {  
                FileStream fs = new FileStream(di.FullName + "\\"+str+".jgw", FileMode.Create, FileAccess.Write);  
                StreamWriter sw = new StreamWriter(fs);  
                for (int i = 0; i < 6; i++)  
                {  
                    sw.WriteLine(line[i].ToString("0.0000000000"));//小数点后保留10位  
 
                }  
                sw.Close();  
                fs.Close();  
            }  
            catch(Exception ex)  
            {  
                MessageBox.Show(ex.ToString());  
            }  
          
        }  
        MessageBox.Show("All Done!");  
    }  
}  
protected double[] GetCoordinatesFromAddress(string str)  
{  
    // get normalized coordinate first  
    double x = 0.0;//X,Y是以地图的左上角为原点的坐标  
    double y = 0.0;  
    double scale = 1.0;  
    str = str.Substring(1, str.Length - 1); // skip the first character  
    while (str.Length > 0)  
    {  
        scale *= 0.5;  
        //var c = str.charAt(0);    // remove first character  
        char c = str[0];  
        if (c == 'r' || c == 's')  
        {  
            x += scale;  
        }  
        if (c == 't' || c == 's')  
        {  
            y += scale;  
        }  
 
        str = str.Substring(1, str.Length - 1);  
    }  
 
    double[] ret = new double[9];  
    //qrst地图的0度经纬度是在十字中心,经度出现负数是西经,正数是东经  
    double long_left = (x - 0.5) * 360;  
    double lat_top = NormalToMercator(y);  
    double long_right = (x + scale - 0.5) * 360;  
    double lat_bot = NormalToMercator(y + scale);  
 
    double long_cent = (x + scale * 0.5 - 0.5) * 360;  
    double lat_cent = NormalToMercator(y + scale * 0.5);  
 
    ret[0] = Convert.ToDouble(long_left.ToString("F8"));  
    ret[1] = Convert.ToDouble(lat_top.ToString("F8"));  
    ret[2] = Convert.ToDouble(long_right.ToString("F8"));  
    ret[3] = Convert.ToDouble(lat_bot.ToString("F8"));  
    ret[4] = Convert.ToDouble(long_cent.ToString("F8"));  
    ret[5] = Convert.ToDouble(lat_cent.ToString("F8"));  
    ret[6] = x;  
    ret[7] = y;  
    ret[8] = scale;  
    return ret;  
}  
protected double NormalToMercator(double y)  
{  
    y -= 0.5;  
    y *= 2 * Math.PI;  
    y = Math.Exp(y * 2);  
    y = (y - 1) / (y + 1);  
    y = Math.Asin(y);  
    y = y * -180 / Math.PI;  
    return y;  

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/duganghu/archive/2009/05/25/4215070.aspx

 

posted @ 2009-06-25 19:22  WillWayer  阅读(513)  评论(0编辑  收藏  举报