GDAL图层根据字段融合

1、根据所选字段值域分别提取出新的图层

        private Layer GetLayerByZSQYDM(DataSource sourceDS, string zsqydm, ref string strMSg)
        {
            Layer layer = sourceDS.GetLayerByName(_LayerName);

            if (layer == null)
            {
                strMSg = "图层打开失败";
                return null;
            }

            string strFilter = $"{_ZSQYFieldName}='{zsqydm}'";

            if (layer.SetAttributeFilter(strFilter) != 0)
            {
                strMSg = $"提取 {strFilter} 的图斑失败 ";
                return null;
            }
            layer.ResetReading();
            return layer;
        }

2、根据原图层信息创建新的图层

       private Layer CreateNewLayer(Layer sourceLayer, ref string strMsg)
       {
            OSGeo.OGR.Driver dv = Ogr.GetDriverByName("ESRI Shapefile");
            DataSource destDS = dv.CreateDataSource(Path.Combine(_TempDir, $"{_LayerName}_Dissolve.shp"), null);
            List<string> fileldsList = new List<string> { _ZSQYFieldName };
            SpatialReference srcSpatial = sourceLayer.GetSpatialRef();
            FeatureDefn featureDefn = sourceLayer.GetLayerDefn();
            Layer dstLayer = ds.CreateLayer($"{_LayerName}", srcSpatial, featureDefn.GetGeomType(), null);

            if (dstLayer == null)
            {
                strMsg = "创建图层失败";
                return null;
            }

            foreach (string field in fileldsList)
            {
                int index = featureDefn.GetFieldIndex(field);
                FieldDefn fieldDefn = featureDefn.GetFieldDefn(index);
                dstLayer.CreateField(fieldDefn, 1);
                fieldDefn.Dispose();
            }
            featureDefn.Dispose();
            return dstLayer;
       }

3、图层融合

        private bool Dissolve(Layer input, Layer dissolve, string strValue, ref string strMsg)
        {
            FeatureDefn defn = null;
            Geometry multi = null;
            Feature inputFea = null;
            Geometry geo = null;
            Geometry geo1 = null;
            Geometry union = null;

            try
            {
                multi = new Geometry(wkbGeometryType.wkbMultiPolygon);
                while ((inputFea = input.GetNextFeature()) != null)
                {
                    geo = inputFea.GetGeometryRef();
                    int count = geo.GetGeometryCount();
                    for (int i = 0; i < count; i++)
                    {
                        geo1 = geo.GetGeometryRef(i);
                        geo1.CloseRings();
                        geo1.ExportToWkt(out string wkt);
                        Geometry ge = Geometry.CreateFromWkt(wkt);                        
                        multi.AddGeometryDirectly(ge);
                        ge.Dispose();
                    }
                }

                union = multi.UnionCascaded();
                defn = dissolve.GetLayerDefn();
                Feature fea = new Feature(defn);
                fea.SetField(_ZSQYFieldName, strValue);
                fea.SetGeometry(union);
                dissolve.CreateFeature(fea);
                fea.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                strMsg = ex.Message + ex.StackTrace;
                return false;
            }
            finally
            {
                union?.Dispose();
                geo?.Dispose();
                geo1?.Dispose();
                inputFea?.Dispose();
                multi?.Dispose();
                defn?.Dispose();
            }
        }

 

4、

posted @ 2022-08-15 14:26  白草红叶黄花  阅读(225)  评论(0编辑  收藏  举报