You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
928 lines
38 KiB
928 lines
38 KiB
using GIS.HPU.ZYZ.SHP.DBF; |
|
using OSGeo.GDAL; |
|
using OSGeo.OGR; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
|
|
namespace Kingo.Plugin.ProofManager.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 = dbfRecord[fidIndex]; |
|
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(); |
|
drNewRow[fidName] = bsm; |
|
tableRst.Rows.Add(drNewRow); |
|
|
|
|
|
} |
|
dbfFile.Close(); |
|
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; |
|
} |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 创建table |
|
/// </summary> |
|
/// <param name="oDefn"></param> |
|
/// <returns></returns> |
|
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; |
|
} |
|
|
|
/// <summary> |
|
/// 创建新行 |
|
/// </summary> |
|
/// <param name="feature"></param> |
|
/// <returns></returns> |
|
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, LayerEntity entity, out List<string> listfield, string keyField = "") |
|
{ |
|
try |
|
{ |
|
listfield = new List<string>(); |
|
// 注册所有的驱动 |
|
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<string>(); |
|
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<string>(); |
|
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; |
|
} |
|
|
|
/// <summary> |
|
/// Shape转WKT |
|
/// </summary> |
|
/// <param name="obejctID">主键</param> |
|
/// <param name="layer">图形</param> |
|
/// <returns>WKT</returns> |
|
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; |
|
} |
|
|
|
public GeometryEntity GetWKTByProjet(string ringsPolygon) |
|
{ |
|
string wktstring = ""; |
|
GeometryEntity result = new GeometryEntity(); |
|
OSGeo.OGR.Geometry geometryPoint = GetPolygon(ringsPolygon); |
|
OSGeo.OGR.Geometry geometry = GetPolygonProject(ringsPolygon); |
|
if (geometryPoint != null) |
|
{ |
|
OSGeo.OGR.Geometry g = geometryPoint.Centroid(); |
|
ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); |
|
ESRI.ArcGIS.Geometry.ISpatialReferenceFactory pSpatialReferenceFactory = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironment(); |
|
ESRI.ArcGIS.Geometry.ISpatialReference pFromSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(4326); |
|
ESRI.ArcGIS.Geometry.ISpatialReference pToSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(4528); |
|
point.X = Math.Round(g.GetX(0), 3); |
|
point.Y = Math.Round(g.GetY(0), 3); |
|
point.SpatialReference = pFromSpatialReference; |
|
point.Project(pToSpatialReference); |
|
result.x = point.X; |
|
result.y = point.Y; |
|
} |
|
if (geometry != null) |
|
{ |
|
geometry.ExportToWkt(out wktstring); |
|
result.wktStr = wktstring; |
|
} |
|
return result; |
|
} |
|
|
|
/// <summary> |
|
/// 获取坐标点 |
|
/// </summary> |
|
/// <param name="pointsStr"></param> |
|
/// <returns></returns> |
|
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; |
|
} |
|
|
|
} |
|
|
|
private OSGeo.OGR.Geometry GetPolygonProject(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); |
|
ESRI.ArcGIS.Geometry.IPoint pointProject = new ESRI.ArcGIS.Geometry.PointClass(); |
|
ESRI.ArcGIS.Geometry.ISpatialReferenceFactory pSpatialReferenceFactory = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironment(); |
|
ESRI.ArcGIS.Geometry.ISpatialReference pFromSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(4326); |
|
ESRI.ArcGIS.Geometry.ISpatialReference pToSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(4528); |
|
pointProject.SpatialReference = pFromSpatialReference; |
|
foreach (var ring in p.rings) |
|
{ |
|
OSGeo.OGR.Geometry geometryTmp = new Geometry(wkbGeometryType.wkbLinearRing); |
|
foreach (var point in ring) |
|
{ |
|
pointProject.X = point[0]; |
|
pointProject.Y = point[1]; |
|
pointProject.Project(pToSpatialReference); |
|
geometryTmp.AddPoint_2D(pointProject.X, pointProject.Y); |
|
//.Add(new ESRI.ArcGIS.Client.Geometry.MapPoint(point[0], point[1])); |
|
} |
|
geometryTmp.CloseRings(); |
|
geometry.AddGeometry(geometryTmp); |
|
} |
|
return geometry; |
|
} |
|
catch (Exception ex) |
|
{ |
|
return null; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 获取图层所有字段名称 |
|
/// </summary> |
|
/// <param name="shapePath"></param> |
|
/// <param name="layerName"></param> |
|
/// <returns></returns> |
|
public List<string> 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<string> layerFieldNames = new List<string>(); |
|
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(); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 打开图层 |
|
/// </summary> |
|
/// <param name="shapePath"></param> |
|
/// <param name="layerName"></param> |
|
/// <returns></returns> |
|
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; |
|
} |
|
|
|
} |
|
|
|
//public double GetDistance( point,OSGeo.OGR.Geometry polyon) |
|
//{ |
|
|
|
// return point.Distance(polyon); |
|
//} |
|
|
|
//private string Geometry2Json(OSGeo.OGR.Geometry pGeo) |
|
//{ |
|
// double x, y; |
|
// StringBuilder sb = new StringBuilder("{"); |
|
// //sb.Append(@"""geometries""" + ":{"); |
|
// switch (pGeo.) |
|
// { |
|
// #region Point2Json |
|
// case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint: |
|
// pPoint = pGeo as ESRI.ArcGIS.Geometry.IPoint; |
|
// pPoint.QueryCoords(out x, out y); |
|
// //string json = @"{""x"":" + x + @",""y"":" + y + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"; |
|
// string json = @"{""x"":" + x + @",""y"":" + y; |
|
// sb.Append(@"""point"":" + json); |
|
// break; |
|
// #endregion |
|
// #region Polyline2Json |
|
// case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline: |
|
// pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection; |
|
// IPolyline pPolyline = pGeo as IPolyline; |
|
// IGeometryCollection pGeoetryCollection = pPolyline as IGeometryCollection; |
|
// if (pGeoetryCollection.GeometryCount >= 1) |
|
// { |
|
// sb.Append(@"""paths"":["); |
|
// for (int i = 0; i < pGeoetryCollection.GeometryCount; i++) |
|
// { |
|
// //paths可能有多个path,而每一个path是多个点,用两个for循环 |
|
// if (pGeoetryCollection.get_Geometry(i) is IPath) |
|
// { |
|
// sb.Append("["); |
|
// pPoints = pGeoetryCollection.get_Geometry(i) as IPointCollection; |
|
|
|
// for (int j = 0; j < pPoints.PointCount; j++) |
|
// { |
|
// pPoint = pPoints.get_Point(j); |
|
// pPoint.QueryCoords(out x, out y); |
|
// sb.Append("[" + x + "," + y + "],"); |
|
// } |
|
// sb.Remove(sb.Length - 1, 1); |
|
// sb.Append("]"); |
|
// } |
|
// } |
|
// //sb.Append("]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"); |
|
// sb.Replace(',', ']', sb.Length - 1, 1); |
|
// //sb.Append("]"); |
|
// } |
|
// break; |
|
// #endregion |
|
// #region Polygon2Json |
|
// case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon: |
|
// pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection; |
|
// IPolygon pPolygon = pGeo as IPolygon; |
|
// //外环和内环?面的构造比较复杂,在AO中我们可以获取内环和外环,如果注意过在客户端API中的Rings数组,似乎看不出,所以这里我没采用外环和内环的构造方式,但是代码在后面附上。 |
|
// IGeometryCollection pGeoetryCollection1 = pPolygon as IGeometryCollection; |
|
// if (pGeoetryCollection1.GeometryCount >= 1) |
|
// { |
|
// sb.Append(@"""rings"":["); |
|
// for (int i = 0; i < pGeoetryCollection1.GeometryCount; i++) |
|
// { |
|
// if (pGeoetryCollection1.get_Geometry(i) is IRing) |
|
// { |
|
// sb.Append("["); |
|
// pPoints = pGeoetryCollection1.get_Geometry(i) as IPointCollection; |
|
// for (int j = 0; j < pPoints.PointCount; j++) |
|
// { |
|
// pPoint = pPoints.get_Point(j); |
|
// pPoint.QueryCoords(out x, out y); |
|
// sb.Append("[" + x + "," + y + "],"); |
|
// } |
|
// sb.Remove(sb.Length - 1, 1); |
|
// sb.Append("],"); |
|
// } |
|
// } |
|
// //sb.Append("]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"); |
|
// sb.Replace(',', ']', sb.Length - 1, 1); |
|
// } |
|
// break; |
|
// #endregion |
|
// } |
|
// //sb.Append("}"); |
|
// //添加Geometry |
|
// sb.Append("}"); |
|
// return sb.ToString(); |
|
//} |
|
|
|
////下面是获取面的内环和外环的坐标 |
|
//private void PolygonToString(IPolygon4 polygon) |
|
//{ |
|
// IGeometryBag exteriorRingGeometryBag = polygon.ExteriorRingBag; |
|
// IGeometryCollection exteriorRingGeometryCollection = exteriorRingGeometryBag as IGeometryCollection; |
|
// Trace.WriteLine("polygon.ExteriorRingCount = " + exteriorRingGeometryCollection.GeometryCount); |
|
// for (int i = 0; i < exteriorRingGeometryCollection.GeometryCount; i++) |
|
// { |
|
// Trace.WriteLine("polygon.ExteriorRing[" + i + "]"); |
|
// IGeometry exteriorRingGeometry = exteriorRingGeometryCollection.get_Geometry(i); |
|
// IPointCollection exteriorRingPointCollection = exteriorRingGeometry as IPointCollection; |
|
// for (int j = 0; j < exteriorRingPointCollection.PointCount; j++) |
|
// { |
|
// Trace.WriteLine("Point[" + j + "] = " + PointToString(exteriorRingPointCollection.get_Point(j))); |
|
// } |
|
// IGeometryBag interiorRingGeometryBag = polygon.get_InteriorRingBag(exteriorRingGeometry as IRing); |
|
// IGeometryCollection interiorRingGeometryCollection = interiorRingGeometryBag as IGeometryCollection; |
|
// Trace.WriteLine("polygon.InteriorRingCount[exteriorRing" + i + "] = " + interiorRingGeometryCollection.GeometryCount); |
|
// for (int k = 0; k < interiorRingGeometryCollection.GeometryCount; k++) |
|
// { |
|
// Trace.WriteLine("polygon.InteriorRing[" + k + "]"); |
|
// IGeometry interiorRingGeometry = interiorRingGeometryCollection.get_Geometry(k); |
|
// IPointCollection interiorRingPointCollection = interiorRingGeometry as IPointCollection; |
|
// for (int m = 0; m < interiorRingPointCollection.PointCount; m++) |
|
// { |
|
// Trace.WriteLine("Point[" + m + "] = " + PointToString(interiorRingPointCollection.get_Point(m))); |
|
// } |
|
// } |
|
// } |
|
//} |
|
|
|
//private string PointToString(IPoint point) |
|
//{ |
|
// return (point.X + ", " + point.Y + ", " + point.Z); |
|
//} |
|
|
|
} |
|
}
|
|
|