예제 #1
0
        private void runFunc()
        {
            Ogr.RegisterAll();
            Gdal.SetConfigOption("SHAPE_ENCODING", "CP936");

            string strInfo = string.Empty;
            //打开数据
            DataSource ds = Ogr.Open(mFileName, 0);

            if (ds == null)
            {
                strInfo += string.Format("打开文件【{0}】失败!\n", mFileName);
                return;
            }
            strInfo += string.Format("打开文件【{0}】成功!\n", mFileName);

            CGetShp mdbToShp = new CGetShp(mFileName, mOutPath);
            // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个
            int iLayerCount = ds.GetLayerCount();

            strInfo += string.Format("个人地理数据库共包含{0}个图层!\n", iLayerCount);
            for (int i = 0; i < iLayerCount; i++)
            {
                // 获取第一个图层
                Layer oLayer = ds.GetLayerByIndex(i);

                if (oLayer == null)
                {
                    strInfo += string.Format("获取第{0}个图层失败!\n", i);
                    return;
                }
                strInfo += string.Format("第{0}个图层名称:{1}\n", i, oLayer.GetName());
                //若当前图层为向量数据
                if (oLayer.GetName().Contains("LINE"))
                {
                    //调用方法获取向量信息,并传入想应的点图层名以方便查询
                    mdbToShp.readLine(ref oLayer, oLayer.GetName().Replace("LINE", "POINT"));
                    //break;
                }
                else
                {
                    mdbToShp.readPoint(ref oLayer);
                    //break;
                    //mdbToShp.test(ref oLayer);
                }

                SetTextMessage(100 * (i + 1) / iLayerCount);
            }
        }
예제 #2
0
파일: GetShp.cs 프로젝트: CarlosGray/GetShp
        public void readLine(ref Layer oLayer, string pointTableName)
        {
            // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空
            oLayer.ResetReading();

            //打开数据
            DataSource newDs = Ogr.Open(mSrcFile, 0);

            if (newDs == null)
            {
                Console.WriteLine(string.Format("打开文件【{0}】失败!\n", mSrcFile));
                return;
            }

            //根据图层名称获取相应的图层
            Layer matchLayer = newDs.GetLayerByName(pointTableName);

            Driver     oDriver  = null;
            DataSource oDS      = null;
            Layer      newLayer = null;

            initShp(ref oLayer, ref oDriver, ref oDS, ref newLayer, LAYER_TYPE.LINE);

            // 获取图层中的属性表表头并输出
            string      strInfo     = "属性表结构信息:\n";
            FeatureDefn oDefn       = oLayer.GetLayerDefn();
            int         iFieldCount = oDefn.GetFieldCount();

            for (int iAttr = 0; iAttr < iFieldCount; iAttr++)
            {
                FieldDefn oField = oDefn.GetFieldDefn(iAttr);
                FieldType type   = oField.GetFieldType();

                //为新图层创建属性
                FieldDefn newField = new FieldDefn(oField.GetNameRef(), type);
                if (type == FieldType.OFTString)
                {
                    newField.SetWidth(oField.GetWidth());
                }
                newLayer.CreateField(newField, 1);

                strInfo += string.Format("{0}:{1} ({2}.{3})\n", oField.GetNameRef(),
                                         oField.GetFieldTypeName(oField.GetFieldType()),
                                         oField.GetWidth(), oField.GetPrecision());
            }
            FeatureDefn newDefn = newLayer.GetLayerDefn();

            // 输出图层中的要素个数
            strInfo += string.Format("要素个数 = {0}\n", oLayer.GetFeatureCount(0));
            Feature oFeature = null;
            // 下面开始遍历图层中的要素
            double S_X = 0.0;
            double S_Y = 0.0;
            double E_X = 0.0;
            double E_Y = 0.0;

            while ((oFeature = oLayer.GetNextFeature()) != null)
            {
                strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID());

                //为新图层创建要素
                Feature oFeatureLineString = new Feature(newDefn);

                string sql = string.Empty;
                // 获取要素中的属性表内容
                for (int iField = 0; iField < iFieldCount; iField++)
                {
                    FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField);
                    string    name       = oFieldDefn.GetNameRef();
                    FieldType type       = oFieldDefn.GetFieldType();
                    switch (type)
                    {
                    case FieldType.OFTString:
                        IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField);
                        string val   = Marshal.PtrToStringAnsi(pchar);
                        oFeatureLineString.SetField(iField, val);
                        switch (name)
                        {
                        case "S_Point":
                            sql = oFeature.GetFieldAsString(iField);
                            getCoordinate(ref matchLayer, ref sql, ref S_X, ref S_Y);
                            break;

                        case "E_Point":
                            sql = oFeature.GetFieldAsString(iField);
                            getCoordinate(ref matchLayer, ref sql, ref E_X, ref E_Y);
                            break;

                        default:
                            break;
                        }
                        break;

                    case FieldType.OFTReal:
                        oFeatureLineString.SetField(name, oFeature.GetFieldAsDouble(iField));
                        break;

                    case FieldType.OFTInteger:
                        oFeatureLineString.SetField(iField, oFeature.GetFieldAsInteger(iField));
                        break;

                    default:
                        oFeatureLineString.SetField(iField, oFeature.GetFieldAsString(iField));
                        break;
                    }
                }
                Geometry oGeometry = Geometry.CreateFromWkt(string.Format("LINESTRING({0} {1},{2} {3})", S_X, S_Y, E_X, E_Y));
                oFeatureLineString.SetGeometryDirectly(oGeometry);
                newLayer.CreateFeature(oFeatureLineString);
                oGeometry.Dispose();
            }
            strInfo += "\n数据集关闭!";

            oDS.Dispose();
        }