欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

谷歌瓦片地图纠偏

程序员文章站 2022-05-13 23:45:54
对谷歌瓦片地图进行纠偏,有两种方法:一是对拼接大图进行纠偏,然后重新切片;二是直接对瓦片图进行纠偏。这里我用的是第二种方法,即直接对瓦片地图进行纠偏。 App.config配置:

    对谷歌瓦片地图进行纠偏,有两种方法:一是对拼接大图进行纠偏,然后重新切片;二是直接对瓦片图进行纠偏。这里我用的是第二种方法,即直接对瓦片地图进行纠偏。

app.config配置:

谷歌瓦片地图纠偏
<appsettings>
  <add key="inputpath" value="d:\_临时文件\gismap\1818940751"/>
  <add key="outputpath" value="d:\_临时文件\gismapoutput\1818940751"/>
  <add key="deltapixcelx" value="1031"/>
  <add key="deltapixcely" value="421"/>
  <add key="frommapzoom" value="1"/>
  <add key="tomapzoom" value="18"/>
</appsettings>
view code

对瓦片图进行纠偏处理的算法代码:

谷歌瓦片地图纠偏
using system;
using system.collections.generic;
using system.componentmodel;
using system.configuration;
using system.data;
using system.drawing;
using system.io;
using system.linq;
using system.text;
using system.text.regularexpressions;
using system.threading;
using system.threading.tasks;
using system.windows.forms;
using utils;

namespace tileprocess
{
    public partial class form1 : form
    {
        private int _count = 0;
        private int _deltapixcelx;
        private int _deltapixcely;
        private string _inputpath;
        private string _outputpath;
        private int _frommapzoom;
        private int _tomapzoom;

        private datetime _starttime;
        private int _lastcount;

        public form1()
        {
            initializecomponent();

            _deltapixcelx = convert.toint32(configurationmanager.appsettings["deltapixcelx"]);
            _deltapixcely = convert.toint32(configurationmanager.appsettings["deltapixcely"]);
            _inputpath = configurationmanager.appsettings["inputpath"];
            _outputpath = configurationmanager.appsettings["outputpath"];
            _frommapzoom = convert.toint32(configurationmanager.appsettings["frommapzoom"]);
            _tomapzoom = convert.toint32(configurationmanager.appsettings["tomapzoom"]);
        }

        private void btntileprocess_click(object sender, eventargs e)
        {
            this.btntileprocess.enabled = false;

            task.factory.startnew(() =>
            {
                logutil.log("开始处理");
                process();
            });

            thread thread = new thread(new threadstart(() =>
            {
                int sleepinterval = 1000;
                while (true)
                {
                    thread.sleep(sleepinterval);
                    this.begininvoke(new action(() =>
                    {
                        double totalseconds = datetime.now.subtract(_starttime).totalseconds;
                        int avg = (int)(_count / totalseconds);
                        lblmsg.text = string.format("已处理 {0} 张瓦片图", _count);
                        if (_count - _lastcount > 0)
                        {
                            lblspeed.text = string.format("当前速度:{0} 张/每秒,平均速度:{1} 张/每秒", (_count - _lastcount) * 1000.0 / sleepinterval, avg);
                        }
                        _lastcount = _count;
                    }));
                }
            }));
            thread.isbackground = true;
            thread.start();
        }

        /// <summary>
        /// 瓦片纠偏处理
        /// </summary>
        private void process()
        {
            _starttime = datetime.now;
            regex regex = new regex(@"\\(\d+)\\(\d+).png", regexoptions.ignorecase);
            for (int i = _frommapzoom; i <= _tomapzoom; i++)
            {
                int deltapixcelx = (int)math.round(_deltapixcelx / math.round(math.pow(2, 18 - i)));
                int deltapixcely = (int)math.round(_deltapixcely / math.round(math.pow(2, 18 - i)));

                string[] filearr = directory.getfiles(_inputpath + "\\" + i, "*.*", searchoption.alldirectories);
                foreach (string file in filearr)
                {
                    threaddata data = new threaddata();
                    data.file = file;
                    data.i = i;
                    data.deltapixcelx = deltapixcelx;
                    data.deltapixcely = deltapixcely;

                    threadutil.run((obj) =>
                    {
                        threaddata d = obj as threaddata;

                        match match = regex.match(d.file);
                        if (match.success)
                        {
                            int x = convert.toint32(match.groups[1].value);
                            int y = convert.toint32(match.groups[2].value);

                            string pathtarget = string.format(string.format(@"{0}\{1}\{2}\{3}.png", _outputpath, d.i, x, y));
                            if (!file.exists(pathtarget))
                            {
                                if (!directory.exists(path.getdirectoryname(pathtarget)))
                                {
                                    directory.createdirectory(path.getdirectoryname(pathtarget));
                                }
                                bitmap bmpnew = new bitmap(256, 256, system.drawing.imaging.pixelformat.format32bppargb);
                                graphics graph = graphics.fromimage(bmpnew);

                                int deltax = data.deltapixcelx / 256;
                                int deltay = data.deltapixcely / 256;

                                //临时变量定义
                                string pathsource = null;
                                filestream fs = null;
                                byte[] barr = null;
                                memorystream ms = null;
                                bitmap bmpsource = null;

                                //起始
                                pathsource = string.format(@"{0}\{1}\{2}\{3}.png", _inputpath, d.i, x + deltax, y + deltay);
                                if (file.exists(pathsource))
                                {
                                    fs = new filestream(pathsource, filemode.open, fileaccess.read);
                                    barr = new byte[fs.length];
                                    int readcount = fs.read(barr, 0, barr.length);
                                    ms = new memorystream(barr, 0, readcount);
                                    bmpsource = new bitmap(ms);
                                    graph.drawimage(bmpsource, 0, 0, new rectanglef(data.deltapixcelx % 256, data.deltapixcely % 256, 256 - data.deltapixcelx % 256, 256 - data.deltapixcely % 256), graphicsunit.pixel);
                                    graph.flush();

                                    fs.close();
                                    fs = null;
                                    ms.close();
                                    ms = null;
                                    bmpsource.dispose();
                                    bmpsource = null;
                                }

                                //右
                                pathsource = string.format(@"{0}\{1}\{2}\{3}.png", _inputpath, d.i, x + deltax + 1, y + deltay);
                                if (file.exists(pathsource))
                                {
                                    fs = new filestream(pathsource, filemode.open, fileaccess.read);
                                    barr = new byte[fs.length];
                                    int readcount = fs.read(barr, 0, barr.length);
                                    ms = new memorystream(barr, 0, readcount);
                                    bmpsource = new bitmap(ms);
                                    graph.drawimage(bmpsource, 256 - data.deltapixcelx % 256, 0, new rectanglef(0, data.deltapixcely % 256, data.deltapixcelx % 256, 256 - data.deltapixcely % 256), graphicsunit.pixel);
                                    graph.flush();

                                    fs.close();
                                    fs = null;
                                    ms.close();
                                    ms = null;
                                    bmpsource.dispose();
                                    bmpsource = null;
                                }

                                //下
                                pathsource = string.format(@"{0}\{1}\{2}\{3}.png", _inputpath, d.i, x + deltax, y + deltay + 1);
                                if (file.exists(pathsource))
                                {
                                    fs = new filestream(pathsource, filemode.open, fileaccess.read);
                                    barr = new byte[fs.length];
                                    int readcount = fs.read(barr, 0, barr.length);
                                    ms = new memorystream(barr, 0, readcount);
                                    bmpsource = new bitmap(ms);
                                    graph.drawimage(bmpsource, 0, 256 - data.deltapixcely % 256, new rectanglef(data.deltapixcelx % 256, 0, 256 - data.deltapixcelx % 256, data.deltapixcely % 256), graphicsunit.pixel);
                                    graph.flush();

                                    fs.close();
                                    fs = null;
                                    ms.close();
                                    ms = null;
                                    bmpsource.dispose();
                                    bmpsource = null;
                                }

                                //右下
                                pathsource = string.format(@"{0}\{1}\{2}\{3}.png", _inputpath, d.i, x + deltax + 1, y + deltay + 1);
                                if (file.exists(pathsource))
                                {
                                    fs = new filestream(pathsource, filemode.open, fileaccess.read);
                                    barr = new byte[fs.length];
                                    int readcount = fs.read(barr, 0, barr.length);
                                    ms = new memorystream(barr, 0, readcount);
                                    bmpsource = new bitmap(ms);
                                    graph.drawimage(bmpsource, 256 - data.deltapixcelx % 256, 256 - data.deltapixcely % 256, new rectanglef(0, 0, data.deltapixcelx % 256, data.deltapixcely % 256), graphicsunit.pixel);
                                    graph.flush();

                                    fs.close();
                                    fs = null;
                                    ms.close();
                                    ms = null;
                                    bmpsource.dispose();
                                    bmpsource = null;
                                }

                                bmpnew.save(pathtarget);
                                //bmpnew.save("d:\\_临时文件\\1234.png"); //测试用

                                bmpnew.dispose();
                                bmpnew = null;
                                graph.dispose();
                                graph = null;

                                _count++;
                            } //end if (!file.exists(pathtarget))
                        } //end if (match.success)
                    }, data, (ex) =>
                    {
                        this.begininvoke(new action(() =>
                        {
                            lblerrormsg.text = "出错:" + ex.message + "\r\n" + ex.stacktrace;
                            logutil.logerror(ex, "出错");
                        }));
                    }); //end threadutil.run
                } //end foreach (string file in filearr)
            } //end for (int i = _frommapzoom; i <= _tomapzoom; i++)
        }
    }
}
view code

处理效率:我自己电脑每秒处理大约350张瓦片图,1到18级瓦片共100多万张图片,大约需要处理50分钟。

瓦片图纠偏前后对比:

谷歌瓦片地图纠偏