转一篇文章,以便在后续文章中讲解——开源gis项目Mitab在C#中的使用方法

Yush

我一直是用MapX写的写MapInfo公司Tab格式的文件,但是由于涉及到大量的图元绘制,MapX的绘制速度是受到限制的,其绘制的速度并不快,于是探索是不是有比他更好的组件。前段时间曾经用过ShapeLib来绘制shp文件,其绘制的速度还是相当可以的,有兴趣的话可以搜搜,其提供的开源包中有实例程序,怎么来用。

而我今天要介绍的这个Mitab开源包中,并没有提供可以直接运行能看到效果的实例Demo,于是我开始了解如果在dot net程序中使用他。

Mitab开源项目是采用C++编写的库,其编译了mitab.dll文件,我们用他的功能也主要从这个dll中调用功能。但不是单纯的添加引用或者添加一个这个dll为控件能解决问题的,要用好这个dll,不得不继续分析他的开源包,在MiApi.cs文件中,有很多的[DllImport("mitab.dll")]语句,于是我明白了如何应用,下面来介绍他的应用步骤。

第一步:新建一个C#桌面应用程序;

第二步:添加开源库中的MiApi.csMiWrapper.cs两个类到程序中。

第三步:拷贝mitab.dll到程序的.exe的目录中,因为[DllImport("mitab.dll")]就是要查找系统运行的目录中是否存在这个dll文件,所以一定要拷贝。

第四步:写生成Tab文件的函数,调用MiApi.cs中函数。具体如何应用这些函数,请参考开源包中的mitab_vb_test.vb这个文件。

第五步:运行程序,生成Tab,搞定。

附我写的一个demo实例代码。

      public void testWritefile()
        {
            WriteFile("D://11//testtab.tab", "tab");
        }

        private void WriteFile(string pszDest, string pszMifOrTab)
        {
            long dataset;

            long feature;
            double[] x = new double[100];
            double[] y = new double[100];
            long field_index;
            string sTmp;
            long i1;
            long i2;

            dataset = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_create(pszDest, pszMifOrTab, "CoordSys Earth Projection 1, 0", 90, 0, 180, -180);

            if (dataset == 0)
            {
                sTmp = "";
                sTmp = EBop.MapObjects.MapInfo.MiApi.mitab_c_getlasterrormsg();
                MessageBox.Show("Failed to create " + pszMifOrTab + " file: " + sTmp);
                return;
            }

            /* -------------------------------------------------------------------- */
            /* Add a text, float and integer field. */
            /* -------------------------------------------------------------------- */
            field_index = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_add_field((System.IntPtr)dataset, "TestInt", 2, 8, 0, 0, 0);
            field_index = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_add_field((System.IntPtr)dataset, "TestFloat", 5, 12, 2, 0, 0);
            field_index = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_add_field((System.IntPtr)dataset, "TestString", 1, 10, 0, 0, 0);



            /* -------------------------------------------------------------------- */
            /* Write a point. */
            /* -------------------------------------------------------------------- */
            feature = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_create_feature((System.IntPtr)dataset, 1);

            x[0] = 98;
            y[0] = 50;

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_points((System.IntPtr)feature, 0, 1, ref x[0], ref y[0]);
            i1 = 256;
            i2 = 255;
            i1 = i1 * i2;
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_symbol((System.IntPtr)feature, 41, 15, (int)i1);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_field((System.IntPtr)feature, 0, "100");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_field((System.IntPtr)feature, 1, "100.5");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_field((System.IntPtr)feature, 2, "12345678901234567890");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_write_feature((System.IntPtr)dataset, (System.IntPtr)feature);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_destroy_feature((System.IntPtr)feature);


            /* -------------------------------------------------------------------- */
            /* Write a MultiPoint. */
            /* -------------------------------------------------------------------- */
            feature = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_create_feature((System.IntPtr)dataset, 10);

            x[0] = 90;
            y[0] = 51;
            x[1] = 90.5;
            y[1] = 51.5;
            x[2] = 91;
            y[2] = 52;

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_points((System.IntPtr)feature, 0, 3, ref x[0], ref y[0]);
            i1 = 256;
            i2 = 255;
            i1 = i1 * i2;
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_symbol((System.IntPtr)feature, 41, 15, (int)i1);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_field((System.IntPtr)feature, 0, "100");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_field((System.IntPtr)feature, 1, "100.5");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_field((System.IntPtr)feature, 2, "12345678901234567890");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_write_feature((System.IntPtr)dataset, (System.IntPtr)feature);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_destroy_feature((System.IntPtr)feature);

            /* -------------------------------------------------------------------- */
            /* Write a line. */
            /* -------------------------------------------------------------------- */
            feature = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_create_feature((System.IntPtr)dataset, 5);

            x[0] = 100;
            y[0] = 49;
            x[1] = 101;
            y[1] = 48;

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_points((System.IntPtr)feature, 0, 2, ref x[0], ref y[0]);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_pen((System.IntPtr)feature, 1, 2, 255);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_write_feature((System.IntPtr)dataset, (System.IntPtr)feature);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_destroy_feature((System.IntPtr)feature);


            /* -------------------------------------------------------------------- */
            /* Write text. */
            /* -------------------------------------------------------------------- */
            feature = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_create_feature((System.IntPtr)dataset, 4);

            x[0] = 101;
            y[0] = 51;

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_points((System.IntPtr)feature, 0, 1, ref x[0], ref y[0]);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_text((System.IntPtr)feature, "My text");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_font((System.IntPtr)feature, "Arial");
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_text_display((System.IntPtr)feature, 45, 1, 7, 255 * 65536, 0, -1, -1, -1);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_write_feature((System.IntPtr)dataset, (System.IntPtr)feature);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_destroy_feature((System.IntPtr)feature);


            /* -------------------------------------------------------------------- */
            /* Write region (polygon). */
            /* -------------------------------------------------------------------- */
            feature = (long)EBop.MapObjects.MapInfo.MiApi.mitab_c_create_feature((System.IntPtr)dataset, 7);

            x[0] = 101;
            y[0] = 51;
            x[1] = 100;
            y[1] = 51;
            x[2] = 100;
            y[2] = 50;
            x[3] = 101;
            y[3] = 50;
            x[4] = 101;
            y[4] = 51;

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_points((System.IntPtr)feature, 0, 5, ref x[0], ref y[0]);

            x[0] = 100.5;
            y[0] = 50.5;
            x[1] = 100.5;
            y[1] = 50.7;
            x[2] = 100.7;
            y[2] = 50.7;
            x[3] = 100.7;
            y[3] = 50.5;
            x[4] = 100.5;
            y[4] = 50.5;

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_points((System.IntPtr)feature, 1, 5, ref x[0], ref y[0]);

            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_brush((System.IntPtr)feature, 255, 0, 2, 0);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_set_pen((System.IntPtr)feature, 1, 2, 65535);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_write_feature((System.IntPtr)dataset, (System.IntPtr)feature);
            EBop.MapObjects.MapInfo.MiApi.mitab_c_destroy_feature((System.IntPtr)feature);
            /* -------------------------------------------------------------------- */
            /* Cleanup */
            /* -------------------------------------------------------------------- */
            EBop.MapObjects.MapInfo.MiApi.mitab_c_close((System.IntPtr)dataset);

            if (EBop.MapObjects.MapInfo.MiApi.mitab_c_getlasterrorno() != 0)
            {
                sTmp = EBop.MapObjects.MapInfo.MiApi.mitab_c_getlasterrormsg();
                //i1 = mitab_c_getlasterrormsg_vb(sTmp, 255);
                MessageBox.Show("Last Error: " + sTmp);
            }
        }


你可能感兴趣的:(c,C#,Integer,dll,dataset)