using System; using System.Collections.Generic; using System.Linq; using System.Text; using OSGeo.OGR; using OSGeo.GDAL; using System.Runtime.InteropServices; using System.Threading.Tasks; using GIS.HPU.ZYZ.SHP.DBF; using System.IO; using Kingo.Plugin.MakeTaskPackage.Entity; namespace Kingo.Plugin.MakeTaskPackage.Common { public class GdalShapeOperate { private static GdalShapeOperate _instance = null; public static GdalShapeOperate Instance { get { if (_instance == null) _instance = new GdalShapeOperate(); return _instance; } } public GdalShapeOperate() { //// 注册所有的驱动 //Ogr.RegisterAll(); //// 为了支持中文路径,请添加下面这句代码 //Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); //// 为了使属性表字段支持中文,请添加下面这句 //Gdal.SetConfigOption("SHAPE_ENCODING", "NO"); } public Layer CreateShp(string strVectorFile, string layerName, OSGeo.OSR.SpatialReference pSRS) { try { string strDriverName = "Memory"; int count = Ogr.GetDriverCount(); OSGeo.OGR.Driver oDriver = Ogr.GetDriverByName(strDriverName); if (oDriver == null) { return null; } // 创建数据源 DataSource oDS = oDriver.CreateDataSource(strVectorFile + layerName, null); if (oDS == null) { return null; } Layer oLayer = oDS.CreateLayer(layerName, pSRS, wkbGeometryType.wkbPolygon, null); if (oLayer == null) { return null; } else return oLayer; } catch (Exception ex) { return null; } } System.Data.DataTable tableRst = null; public System.Data.DataTable ShapeToDataTable(string shapePath, string layerName = null) { try { ////注册所有的驱动 //Ogr.RegisterAll(); //// 为了支持中文路径,请添加下面这句代码 //Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); //// 为了使属性表字段支持中文,请添加下面这句 //Gdal.SetConfigOption("SHAPE_ENCODING", "NO"); string dbfPathS = Path.GetDirectoryName(shapePath) + "\\" + Path.GetFileNameWithoutExtension(shapePath) + ".dbf"; string dbfPath = AppDomain.CurrentDomain.BaseDirectory + Guid.NewGuid().ToString() + ".dbf"; File.Copy(dbfPathS, dbfPath, true); DbfFile dbfFile = new DbfFile(Encoding.GetEncoding(936));//编码 dbfFile.Open(dbfPath, FileMode.Open); //打开数据 DataSource ds = Ogr.Open(shapePath, 0); if (ds == null || ds.GetLayerCount() <= 0) return null; Layer layer = null; if (string.IsNullOrWhiteSpace(layerName)) { layer = ds.GetLayerByIndex(0); } else { layer = ds.GetLayerByName(layerName); } if (layer == null) return null; OSGeo.OGR.Feature feature = null; //tableRst = GetDataTable(layer.GetLayerDefn()); string fidName = layer.GetFIDColumn(); if (string.IsNullOrWhiteSpace(fidName)) fidName = "OBJECTID"; tableRst = GetDataTable(dbfFile.Header); for (int i = 0; i < dbfFile.Header.RecordCount; i++) { DbfRecord dbfRecord = dbfFile.Read(i); fidName = "FID"; int fidIndex = dbfFile.Header.FindColumn(fidName); if (fidIndex < 0) { fidName = "OBJECTID"; fidIndex = dbfFile.Header.FindColumn(fidName); } if (fidIndex < 0) { fidName = "BSM"; fidIndex = dbfFile.Header.FindColumn(fidName); } string bsm = string.Empty; if (fidIndex > -1) { bsm = dbfRecord[fidIndex].Trim(); layer.ResetReading(); layer.SetAttributeFilter(string.Format("{1}='{0}'", bsm, fidName)); } feature = layer.GetNextFeature(); if (feature == null) continue; System.Data.DataRow drNewRow = NewDataRow(dbfRecord, dbfFile.Header, tableRst); OSGeo.OGR.Geometry geometry = feature.GetGeometryRef(); if (geometry == null) continue; drNewRow["SHAPE"] = geometry.Clone(); if (fidIndex > -1) { drNewRow[fidName] = bsm; } tableRst.Rows.Add(drNewRow); } dbfFile.Close(); return tableRst; } catch (Exception ex) { throw ex; } } public System.Data.DataTable GDBToDataTable(string shapePath, string layerName) { try { //// 注册所有的驱动 //Ogr.RegisterAll(); //// 为了支持中文路径,请添加下面这句代码 //Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); //// 为了使属性表字段支持中文,请添加下面这句 //Gdal.SetConfigOption("SHAPE_ENCODING", "NO"); //打开数据 DataSource ds = null; if (shapePath.ToLower().EndsWith(".gdb") || shapePath.ToLower().EndsWith(".mdb")) { ds = Ogr.Open(shapePath, 0); } else { ds = Ogr.Open(shapePath.Substring(0, shapePath.LastIndexOf("\\")), 0); } if (ds == null || ds.GetLayerCount() <= 0) return null; Layer layer = null; layer = ds.GetLayerByName(layerName); if (layer == null) return null; tableRst = ShapeToDataTable(layer); return tableRst; } catch (Exception ex) { throw ex; } } private System.Data.DataRow NewDataRow(DbfRecord record, DbfHeader header, System.Data.DataTable tableRst) { if (tableRst == null) return null; System.Data.DataRow newRow = tableRst.NewRow(); for (int iField = 0; iField < header.ColumnCount; iField++) { string name = header[iField].Name.ToUpper(); if (name == "SHAPE" || name == "OBJECTID" || name == "FID") continue; if (name == "SHAPE_AREA") name = "SHAPEAREA"; else if (name == "SHAPE_LENGTH") { name = "SHAPELENGTH"; } newRow[name] = record[iField].Trim(); } return newRow; } private System.Data.DataTable GetDataTable(DbfHeader header) { System.Data.DataTable dtNew = new System.Data.DataTable(); for (int i = 0; i < header.ColumnCount; i++) { string name = header[i].Name.ToUpper(); if (name.ToUpper() == "SHAPE") continue; //if (name.ToUpper() == "BSM") //{ // dtNew.Columns.Add("BSM", typeof(Int32)); // continue; //} string colName = name.ToUpper(); if (colName == "SHAPE_AREA") colName = "SHAPEAREA"; else if (colName == "SHAPE_LENGTH") { colName = "SHAPELENGTH"; } switch (header[i].ColumnType) { case DbfColumn.DbfColumnType.Character: dtNew.Columns.Add(colName, typeof(string)); break; case DbfColumn.DbfColumnType.Number: dtNew.Columns.Add(colName, typeof(double)); break; case DbfColumn.DbfColumnType.Boolean: dtNew.Columns.Add(colName, typeof(bool)); break; case DbfColumn.DbfColumnType.Date: dtNew.Columns.Add(colName, typeof(DateTime)); break; case DbfColumn.DbfColumnType.Memo: dtNew.Columns.Add(colName, typeof(string)); break; case DbfColumn.DbfColumnType.Binary: dtNew.Columns.Add(colName, typeof(byte[])); break; case DbfColumn.DbfColumnType.Integer: dtNew.Columns.Add(colName, typeof(Int32)); break; default: dtNew.Columns.Add(colName, typeof(string)); break; } } dtNew.Columns.Add("SHAPE", typeof(object)); return dtNew; } public System.Data.DataTable ShapeToDataTable(Layer layer) { //// 注册所有的驱动 //Ogr.RegisterAll(); //// 为了支持中文路径,请添加下面这句代码 //Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); //// 为了使属性表字段支持中文,请添加下面这句 //Gdal.SetConfigOption("SHAPE_ENCODING", "NO"); if (layer == null) return null; OSGeo.OGR.Feature feature = null; tableRst = GetDataTable(layer.GetLayerDefn()); wkbGeometryType layerGeometryType = layer.GetGeomType(); while ((feature = layer.GetNextFeature()) != null) { System.Data.DataRow drNewRow = NewDataRow(feature); OSGeo.OGR.Geometry geometry = feature.GetGeometryRef(); if (geometry == null) continue; drNewRow["SHAPE"] = geometry.Clone(); tableRst.Rows.Add(drNewRow); } return tableRst; } public bool CheckIsExistFeature(string sourcePath, string layerName) { DataSource ds = null; try { //打开数据 ds = Ogr.Open(sourcePath, 0); if (ds == null || ds.GetLayerCount() <= 0) return false; Layer layer = null; if (string.IsNullOrWhiteSpace(layerName)) { layer = ds.GetLayerByIndex(0); } else { layer = ds.GetLayerByName(layerName); } if (layer == null) return false; //return layer.GetFeatureCount(0)>0; return true; } catch { return false; } finally { if (ds != null) { ds.Dispose(); ds = null; } } } /// /// 创建table /// /// /// private System.Data.DataTable GetDataTable(FeatureDefn oDefn) { System.Data.DataTable dtNew = new System.Data.DataTable(); int iFieldCount = oDefn.GetFieldCount(); for (int i = 0; i < iFieldCount; i++) { FieldDefn oField = oDefn.GetFieldDefn(i); FieldType type = oField.GetFieldType(); string name = oField.GetNameRef(); if (name.ToUpper() == "SHAPE" || name.ToUpper() == "OBJECTID" || name.ToUpper() == "FID") continue; if (name.ToUpper() == "SHAPE") continue; string colName = name.ToUpper(); if (colName == "SHAPE_AREA") colName = "SHAPEAREA"; else if (colName == "SHAPE_LENGTH") { colName = "SHAPELENGTH"; } switch (type) { case FieldType.OFTInteger: dtNew.Columns.Add(colName, typeof(Int32)); break; case FieldType.OFTReal: dtNew.Columns.Add(colName, typeof(double)); break; case FieldType.OFTString: dtNew.Columns.Add(colName, typeof(string)); break; default: dtNew.Columns.Add(colName, typeof(string)); break; } //Console.WriteLine("{0}:{1} ({2}.{3})", oField.GetNameRef(), // oField.GetFieldTypeName(oField.GetFieldType()), // oField.GetWidth(), oField.GetPrecision()); } dtNew.Columns.Add("SHAPE", typeof(object)); dtNew.Columns.Add("FID", typeof(double)); return dtNew; } /// /// 创建新行 /// /// /// private System.Data.DataRow NewDataRow(Feature feature) { if (tableRst == null) return null; System.Data.DataRow newRow = tableRst.NewRow(); FeatureDefn oDefn = feature.GetDefnRef(); for (int iField = 0; iField < feature.GetFieldCount(); iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); string name = oFieldDefn.GetName().ToUpper(); if (name == "SHAPE" || name == "OBJECTID" || name == "FID") continue; if (name == "SHAPE_AREA") name = "SHAPEAREA"; else if (name == "SHAPE_LENGTH") { name = "SHAPELENGTH"; } switch (type) { //case FieldType.OFTBinary: // break; //case FieldType.OFTDate: // //feature.GetFieldAsDateTime(i, // break; //case FieldType.OFTDateTime: // break; //case FieldType.OFTIntegerList: // break; //case FieldType.OFTRealList: // break; //case FieldType.OFTStringList: // break; //case FieldType.OFTTime: // break; //case FieldType.OFTWideString: // break; //case FieldType.OFTWideStringList: // break; case FieldType.OFTInteger: newRow[name] = feature.GetFieldAsInteger(iField); break; case FieldType.OFTReal: newRow[name] = feature.GetFieldAsDouble(iField); break; case FieldType.OFTString: newRow[name] = feature.GetFieldAsString(iField); break; default: newRow[name] = feature.GetFieldAsString(iField); break; } //switch (type) //{ // case caseFieldType.OFTString: // Console.WriteLine("{0}\t",feature.GetFieldAsString(iField)); // break; //caseFieldType.OFTReal: // Console.WriteLine("{0}\t",feature.GetFieldAsDouble(iField)); // break; //caseFieldType.OFTInteger: // Console.WriteLine("{0}\t",feature.GetFieldAsInteger(iField)); // break; //default: // Console.WriteLine("{0}\t",feature.GetFieldAsString(iField)); // break; //} } return newRow; } private string GetPoints(OSGeo.OGR.Geometry geometry) { StringBuilder sb = new StringBuilder("{"); switch (geometry.GetGeometryType()) { case wkbGeometryType.wkbGeometryCollection: break; case wkbGeometryType.wkbGeometryCollection25D: break; case wkbGeometryType.wkbLineString: break; case wkbGeometryType.wkbLineString25D: break; case wkbGeometryType.wkbLinearRing: break; case wkbGeometryType.wkbMultiLineString: break; case wkbGeometryType.wkbMultiLineString25D: break; case wkbGeometryType.wkbMultiPoint: break; case wkbGeometryType.wkbMultiPoint25D: break; case wkbGeometryType.wkbMultiPolygon25D: break; case wkbGeometryType.wkbNone: break; case wkbGeometryType.wkbPoint: string json = @"{""x"":" + geometry.GetX(0) + @",""y"":" + geometry.GetY(0); sb.Append(@"""point"":" + json); break; case wkbGeometryType.wkbPoint25D: break; case wkbGeometryType.wkbMultiPolygon: case wkbGeometryType.wkbPolygon: int geometryCount = geometry.GetGeometryCount(); if (geometryCount > 0) { sb.Append(@"""rings"":["); for (int i = 0; i < geometryCount; i++) { Geometry ge = geometry.GetGeometryRef(i); int subGeoCount = ge.GetGeometryCount(); if (subGeoCount > 0) { for (int j = 0; j < subGeoCount; j++) { sb.Append(GetSingleGeometry(ge.GetGeometryRef(j))); } } else { sb.Append(GetSingleGeometry(ge)); } //sb.Append("["); //Geometry ge = geometry.GetGeometryRef(i); //int count = ge.GetPointCount(); //for (int j = 0; j < count; j++) //{ // sb.Append("[" + ge.GetX(j) + "," + ge.GetY(j) + "],"); //} //sb.Remove(sb.Length - 1, 1); //sb.Append("],"); } sb.Replace(',', ']', sb.Length - 1, 1); } break; case wkbGeometryType.wkbPolygon25D: break; case wkbGeometryType.wkbUnknown: break; default: break; } sb.Append("}"); return sb.ToString(); } private string GetSingleGeometry(Geometry geo) { StringBuilder sb = new StringBuilder(); sb.Append("["); //Geometry ge = geo.GetGeometryRef(i); int count = geo.GetPointCount(); for (int j = 0; j < count; j++) { sb.Append("[" + geo.GetX(j) + "," + geo.GetY(j) + "],"); } sb.Remove(sb.Length - 1, 1); sb.Append("],"); return sb.ToString(); } public string CheckedDataValid(string sourcePath, Entity.LayerEntity entity, out List listfield, string keyField = "") { try { listfield = new List(); // 注册所有的驱动 Ogr.RegisterAll(); // 为了支持中文路径,请添加下面这句代码 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 为了使属性表字段支持中文,请添加下面这句 Gdal.SetConfigOption("SHAPE_ENCODING", "NO"); //打开数据 DataSource ds = Ogr.Open(sourcePath, 0); if (ds == null || ds.GetLayerCount() <= 0) return "无效数据,请检查数据"; Layer layer = ds.GetLayerByIndex(0); if (layer == null) return "无效数据,请检查数据"; if (entity != null) { listfield.Clear(); var task = Task.Factory.StartNew(() => { var listconfigfields = new List(); foreach (var item in entity.Fields) { listconfigfields.Add(item.Name.ToUpper()); } return listconfigfields; }); OSGeo.OGR.Feature feature = null; FeatureDefn feacture = layer.GetLayerDefn(); wkbGeometryType layerGeometryType = layer.GetGeomType(); int iFieldCount = feacture.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = feacture.GetFieldDefn(iAttr); if (oField != null) { listfield.Add(oField.GetName().ToUpper()); } } Task.WaitAll(task); if (listfield.Count != entity.Fields.Count || task.Result.Intersect(listfield).Count() != task.Result.Count) { return "数据模板不一致"; } } if (layer.GetGeomType() != wkbGeometryType.wkbMultiPolygon && layer.GetGeomType() != wkbGeometryType.wkbPolygon) { return "无效数据,请选择面数据"; } else if (layer.GetFeatureCount(0) <= 0) { return "选择数据源不存在数据"; } else if (!string.IsNullOrWhiteSpace(keyField)) { if (layer.FindFieldIndex(keyField, 1) < 0) { return "无效数据,不存在字段" + keyField + ",请参照模板"; } } return null; } catch (Exception ex) { listfield = new List(); return null; } } public string CheckedGeometry(Layer layer) { OSGeo.OGR.Feature feature = null; string rstMsg = null; while ((feature = layer.GetNextFeature()) != null) { OSGeo.OGR.Geometry geometry = feature.GetGeometryRef(); if (!geometry.IsValid()) { rstMsg = "选择数据中有错误数据,请先修复"; } else if (!geometry.IsSimple()) { rstMsg = "选择数据中有错误数据,请先修复"; } else if (geometry.IsEmpty()) { rstMsg = "选择数据中有错误数据,请先修复"; } } return rstMsg; } /// /// Shape转WKT /// /// 主键 /// 图形 /// WKT public GeometryEntity GetWKT(string ringsPolygon) { string wktstring = ""; GeometryEntity result = new GeometryEntity(); OSGeo.OGR.Geometry geometry = GetPolygon(ringsPolygon); if (geometry != null) { OSGeo.OGR.Geometry g = geometry.Centroid(); result.x = Math.Round(g.GetX(0), 3); result.y = Math.Round(g.GetY(0), 3); geometry.ExportToWkt(out wktstring); result.wktStr = wktstring; } return result; } /// /// 获取坐标点 /// /// /// private OSGeo.OGR.Geometry GetPolygon(string pointsStr) { try { if (string.IsNullOrEmpty(pointsStr)) return null; JsonRings p = JsonRings.FromJson(pointsStr); //ESRI.ArcGIS.Client.Geometry.Polygon polygon = new ESRI.ArcGIS.Client.Geometry.Polygon(); OSGeo.OGR.Geometry geometry = new Geometry(wkbGeometryType.wkbPolygon); foreach (var ring in p.rings) { OSGeo.OGR.Geometry geometryTmp = new Geometry(wkbGeometryType.wkbLinearRing); foreach (var point in ring) { geometryTmp.AddPoint_2D(point[0], point[1]); //.Add(new ESRI.ArcGIS.Client.Geometry.MapPoint(point[0], point[1])); } geometryTmp.CloseRings(); geometry.AddGeometry(geometryTmp); } return geometry; } catch (Exception ex) { return null; } } /// /// 获取图层所有字段名称 /// /// /// /// public List GetLayerFieldsName(string shapePath, string layerName = null) { Layer layer = null; try { layer = OpenLayer(shapePath, layerName); if (layer == null) { KGIS.Framework.Utils.LogAPI.Debug("无法打开图层:" + layerName); return null; } List layerFieldNames = new List(); FeatureDefn oDefn = layer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int i = 0; i < iFieldCount; i++) { FieldDefn oField = oDefn.GetFieldDefn(i); layerFieldNames.Add(oField.GetName()); } return layerFieldNames; } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug("GetLayerFieldsName方法异常:" + ex); throw ex; } finally { if (layer != null) layer.Dispose(); } } /// /// 打开图层 /// /// /// /// public Layer OpenLayer(string shapePath, string layerName = null) { Layer layer = null; try { DataSource ds = Ogr.Open(shapePath, 0); if (ds == null || ds.GetLayerCount() <= 0) { KGIS.Framework.Utils.LogAPI.Debug("ds为空"); return null; } if (string.IsNullOrWhiteSpace(layerName)) { layer = ds.GetLayerByIndex(0); } else { layer = ds.GetLayerByName(layerName); } return layer; } catch (Exception ex) { KGIS.Framework.Utils.LogAPI.Debug("OpenLayer方法异常" + ex); throw ex; } } } }