GDAL:Ogr读写FileGDB文件(C#)


首先需要FileGDBAPI.dll支持(ogr_FileGDB.dll只支持读,不支持写),存放处理可参见https://blog.csdn.net/xzhh19921019/article/details/53419065。上代码样例(用于将一个文件进行坐标转换生成新的文件。shapefile读写类同于fileGDB,差别在于只有一个layer,无需循环,这里略去具体代码。mdb还没有找到好的方法——不想用AE):

private void ConversionToGeodeticWithFourParametersOfLgo() {
            string errMsg = "";
            foreach (string strFileName in lstFileName) {
                //try {
                DataSource originDataSource = Ogr.Open(strFileName, 0);//0 is for reading,1 is for update
                if (originDataSource == null) {
                    errMsg += "不能打开:" + strFileName + "\r\n";
                    continue;
                }
                Layer layer0 = originDataSource.GetLayerByIndex(0);//if it is a shapefile ,then it only has 1 layer.
                if (layer0 == null) {
                    errMsg += "获取文件:" + strFileName + "的第0个图层失败!" + "\r\n";
                    continue;
                }
                SpatialReference originSpatialReference = layer0.GetSpatialRef();
                string strTemp = originSpatialReference.GetAttrValue("SPHEROID", 0);
                if (strTemp == null) {
                    errMsg += strFileName + ":请注意,该文件无空间参考系,请选用参考系设置”工具将界面设定的“源坐标系”参数写入文件!" + "\r\n";
                    continue;
                }
                SpatialReference targetSpatialReference = originSpatialReference.CloneGeogCS();
                string suffix = Path.GetExtension(strFileName);
                string targetFileName;
                //originDataSource.Dispose();
                if (originSpatialReference.IsProjected() == 1) {//projected to geodetic
                    originSpatialReference.Dispose();
                    double e1Square = 2 * 1 / targetEllipsoid.fReciprocal - 1 / targetEllipsoid.fReciprocal * 1 / targetEllipsoid.fReciprocal;// first eccentricity square
                    double b = targetEllipsoid.a * (1 - 1 / targetEllipsoid.fReciprocal);
                    double e2Square = Math.Pow(targetEllipsoid.a / b, 2) - 1;// second eccentricity square
                    double[] gaussCoefficient = LidfSurveyFunctions.GaussCoefficient(1 / targetEllipsoid.fReciprocal);
                    if (suffix.Equals(".shp")) {                        
                       //略去
                    } else if (suffix.Equals(".gdb")) {
                        targetFileName = Path.Combine(targetDirectory, Path.GetFileName(strFileName));
                        if (Directory.Exists(targetFileName)) {
                            FileHelper.DeleteFolder(targetFileName);
                        }
                        if (!Directory.Exists(targetDirectory)) {
                            Directory.CreateDirectory(targetDirectory);
                        }
                        OSGeo.OGR.Driver driver = Ogr.GetDriverByName("FileGDB");
                        if (driver == null) {
                            errMsg += "不能获取:" + strFileName + "的驱动器,请检查文件!" + "\r\n";
                            continue;
                        }
                        DataSource targetDataSource = driver.CreateDataSource(targetFileName, null);
                        int layerCount = originDataSource.GetLayerCount();
                        for (int iLayer = 0; iLayer < layerCount; iLayer++) {
                            Layer targetLayer = targetDataSource.CreateLayer(originDataSource.GetLayerByIndex(iLayer).GetName(), targetSpatialReference, originDataSource.GetLayerByIndex(iLayer).GetGeomType(), null);
                            Feature feature;
                            Layer originLayer = originDataSource.GetLayerByIndex(iLayer);
                            originLayer.ResetReading();
                            //processing attribute table
                            FeatureDefn featureDefn = originLayer.GetLayerDefn();
                            int DefnCount = featureDefn.GetFieldCount();
                            for (int j = 0; j < DefnCount; j++) {
                                FieldDefn fieldDefn = featureDefn.GetFieldDefn(j);
                                targetLayer.CreateField(fieldDefn, 1);
                            }
                            //processing feathers
                            var GeomType = originLayer.GetGeomType().ToString();
                            if (GeomType.Contains("wkbPoint")) {
                                while ((feature = originLayer.GetNextFeature()) != null) {
                                    Geometry point = feature.GetGeometryRef();
                                    if (point != null) {
                                        double[] xy = LidfSurveyFunctions.Lgo2DConversion(point.GetY(0), point.GetX(0), fourParameters);
                                        double[] BL = LidfSurveyFunctions.GaussNegativeComputation(xy[0], xy[1], targetEllipsoid.a, 1 / targetEllipsoid.fReciprocal, targetProjectionSetting.CentralMeridian, e1Square, e2Square, gaussCoefficient, targetProjectionSetting.XAdditiveConstant, targetProjectionSetting.YAdditiveConstant); //B=BL[0],L=BL[1]
                                        BL[0] = LidfSurveyFunctions.RadianToDegree(BL[0]);
                                        BL[1] = LidfSurveyFunctions.RadianToDegree(BL[1]);
                                        point.SetPoint(0, BL[1], BL[0], point.GetZ(0));//L is front
                                    }
                                    targetLayer.CreateFeature(feature);
                                }
                            } else if (GeomType.Contains("wkbLineString")) {
                                while ((feature = originLayer.GetNextFeature()) != null) {
                                    Geometry line = feature.GetGeometryRef();
                                    if (line != null) {
                                        int count = line.GetPointCount();
                                        for (int i = 0; i < count; i++) {
                                            double[] xy = LidfSurveyFunctions.Lgo2DConversion(line.GetY(i), line.GetX(i), fourParameters);
                                            double[] BL = LidfSurveyFunctions.GaussNegativeComputation(xy[0], xy[1], targetEllipsoid.a, 1 / targetEllipsoid.fReciprocal, targetProjectionSetting.CentralMeridian, e1Square, e2Square, gaussCoefficient, targetProjectionSetting.XAdditiveConstant, targetProjectionSetting.YAdditiveConstant); //B=BL[0],L=BL[1]
                                            BL[0] = LidfSurveyFunctions.RadianToDegree(BL[0]);
                                            BL[1] = LidfSurveyFunctions.RadianToDegree(BL[1]);
                                            line.SetPoint(i, BL[1], BL[0], line.GetZ(i)); //L is front 
                                        }
                                    }
                                    targetLayer.CreateFeature(feature);
                                }
                            } else if (GeomType.Contains("wkbPolygon")) {
                                while ((feature = originLayer.GetNextFeature()) != null) {
                                    Geometry polygon = feature.GetGeometryRef();
                                    if (polygon != null) {
                                        int ringCount = polygon.GetGeometryCount();
                                        for (int i = 0; i < ringCount; i++) {
                                            Geometry ring = polygon.GetGeometryRef(i);
                                            int pointCount = ring.GetPointCount();
                                            for (int j = 0; j < pointCount; j++) {
                                                double[] xy = LidfSurveyFunctions.Lgo2DConversion(ring.GetY(j), ring.GetX(j), fourParameters);
                                                double[] BL = LidfSurveyFunctions.GaussNegativeComputation(xy[0], xy[1], targetEllipsoid.a, 1 / targetEllipsoid.fReciprocal, targetProjectionSetting.CentralMeridian, e1Square, e2Square, gaussCoefficient, targetProjectionSetting.XAdditiveConstant, targetProjectionSetting.YAdditiveConstant); //B=BL[0],L=BL[1]
                                                BL[0] = LidfSurveyFunctions.RadianToDegree(BL[0]);
                                                BL[1] = LidfSurveyFunctions.RadianToDegree(BL[1]);
                                                ring.SetPoint(j, BL[1], BL[0], ring.GetZ(j)); //L is front 
                                            }
                                        }
                                    }
                                    targetLayer.CreateFeature(feature);
                                }
                            } else if (GeomType.Contains("wkbMultiPoint")) {
                                while ((feature = originLayer.GetNextFeature()) != null) {
                                    Geometry multiPoint = feature.GetGeometryRef();
                                    if (multiPoint != null) {
                                        int pointCount = multiPoint.GetGeometryCount();
                                        for (int i = 0; i < pointCount; i++) {
                                            double[] xy = LidfSurveyFunctions.Lgo2DConversion(multiPoint.GetY(i), multiPoint.GetX(i), fourParameters);
                                            double[] BL = LidfSurveyFunctions.GaussNegativeComputation(xy[0], xy[1], targetEllipsoid.a, 1 / targetEllipsoid.fReciprocal, targetProjectionSetting.CentralMeridian, e1Square, e2Square, gaussCoefficient, targetProjectionSetting.XAdditiveConstant, targetProjectionSetting.YAdditiveConstant); //B=BL[0],L=BL[1]
                                            BL[0] = LidfSurveyFunctions.RadianToDegree(BL[0]);
                                            BL[1] = LidfSurveyFunctions.RadianToDegree(BL[1]);
                                            multiPoint.SetPoint(i, BL[1], BL[0], multiPoint.GetZ(i)); //L is front 
                                        }
                                        //feature.SetGeometry(multiPoint);
                                    }
                                    targetLayer.CreateFeature(feature);
                                }
                            } else if (GeomType.Contains("wkbMultiLineString")) {
                                while ((feature = originLayer.GetNextFeature()) != null) {
                                    Geometry multiLine = feature.GetGeometryRef();
                                    if (multiLine != null) {
                                        int lineCount = multiLine.GetGeometryCount();
                                        for (int i = 0; i < lineCount; i++) {
                                            Geometry line = multiLine.GetGeometryRef(i);
                                            int pointCount = line.GetPointCount();
                                            for (int j = 0; j < pointCount; j++) {
                                                double[] xy = LidfSurveyFunctions.Lgo2DConversion(line.GetY(j), line.GetX(j), fourParameters);
                                                double[] BL = LidfSurveyFunctions.GaussNegativeComputation(xy[0], xy[1], targetEllipsoid.a, 1 / targetEllipsoid.fReciprocal, targetProjectionSetting.CentralMeridian, e1Square, e2Square, gaussCoefficient, targetProjectionSetting.XAdditiveConstant, targetProjectionSetting.YAdditiveConstant); //B=BL[0],L=BL[1]
                                                BL[0] = LidfSurveyFunctions.RadianToDegree(BL[0]);
                                                BL[1] = LidfSurveyFunctions.RadianToDegree(BL[1]);
                                                line.SetPoint(j, BL[1], BL[0], line.GetZ(j)); //L is front 
                                            }
                                        }
                                    }
                                    targetLayer.CreateFeature(feature);
                                }
                            } else if (GeomType.Contains("wkbMultiPolygon")) {
                                while ((feature = originLayer.GetNextFeature()) != null) {
                                    Geometry multiPolygon = feature.GetGeometryRef();
                                    if (multiPolygon != null) {
                                        int polygonCount = multiPolygon.GetGeometryCount();
                                        for (int i = 0; i < polygonCount; i++) {
                                            Geometry polygon = multiPolygon.GetGeometryRef(i);
                                            int ringCount = polygon.GetGeometryCount();
                                            for (int j = 0; j < ringCount; j++) {
                                                Geometry ring = polygon.GetGeometryRef(j);
                                                int pointCount = ring.GetPointCount();
                                                for (int k = 0; k < pointCount; k++) {
                                                    double[] xy = LidfSurveyFunctions.Lgo2DConversion(ring.GetY(k), ring.GetX(k), fourParameters);
                                                    double[] BL = LidfSurveyFunctions.GaussNegativeComputation(xy[0], xy[1], targetEllipsoid.a, 1 / targetEllipsoid.fReciprocal, targetProjectionSetting.CentralMeridian, e1Square, e2Square, gaussCoefficient, targetProjectionSetting.XAdditiveConstant, targetProjectionSetting.YAdditiveConstant); //B=BL[0],L=BL[1]
                                                    BL[0] = LidfSurveyFunctions.RadianToDegree(BL[0]);
                                                    BL[1] = LidfSurveyFunctions.RadianToDegree(BL[1]);
                                                    ring.SetPoint(k, BL[1], BL[0], ring.GetZ(k)); //L is front 
                                                }
                                            }
                                        }
                                    }
                                    targetLayer.CreateFeature(feature);
                                }
                            }
                            /*
                            switch (originLayer.GetGeomType()) {
                                case wkbGeometryType.wkbPoint:
                                case wkbGeometryType.wkbPoint25D:
                                case wkbGeometryType.wkbPointM:
                                case wkbGeometryType.wkbPointZM:
                                   
                                    break;
                                case wkbGeometryType.wkbLineString:
                                case wkbGeometryType.wkbLineString25D:
                                case wkbGeometryType.wkbLineStringM:
                                case wkbGeometryType.wkbLineStringZM:
                                   
                                    break;
                                case wkbGeometryType.wkbPolygon:
                                case wkbGeometryType.wkbPolygon25D:
                                case wkbGeometryType.wkbPolygonM:
                                case wkbGeometryType.wkbPolygonZM:
                                   
                                    break;
                                case wkbGeometryType.wkbMultiPoint:
                                case wkbGeometryType.wkbMultiPoint25D:
                                case wkbGeometryType.wkbMultiPointM:
                                case wkbGeometryType.wkbMultiPointZM:
                                    
                                    break;
                                case wkbGeometryType.wkbMultiLineString:
                                case wkbGeometryType.wkbMultiLineString25D:
                                case wkbGeometryType.wkbMultiLineStringM:
                                case wkbGeometryType.wkbMultiLineStringZM:
                                    
                                    break;
                                case wkbGeometryType.wkbMultiPolygon:
                                case wkbGeometryType.wkbMultiPolygon25D:
                                case wkbGeometryType.wkbMultiPolygonM:
                                case wkbGeometryType.wkbMultiPolygonZM:
                                    
                                    break;
                            }
                            */
                        }
                        targetDataSource.Dispose();
                        originDataSource.Dispose();
                    } else {//*.mdb
                        //继续
                    }
                }

                //} catch (Exception e) {
                //errMsg += strFileName + ":未完成转换工作!" + e;
                //}
            }
            if (errMsg != "") {
                MessageBox.Show(errMsg, "提示!", MessageBoxButtons.OK);
            }
        }

转载自:https://blog.csdn.net/nny886/article/details/88031436

You may also like...