📄 clscreatestatfeaturelayer.cs.svn-base
字号:
using System;
using System.Collections.Generic;
using System.Text;
using dist.hb.EnvQueryStat.Pub;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using System.Data;
using System.Data.OleDb;
using dist.hb.EnvQueryStat.Pub;
namespace dist.hb.EnvQueryStat.Stat
{
/// <summary>
/// 根据统计结果表创建统计的地图图层
/// </summary>
class clsCreateStatFeatureLayer
{
#region "类的初始化"
public clsStatEnv StatEnv;
public clsCreateStatTable StatTable;
public clsCreateStatFeatureLayer()
{
StatEnv = null;
StatTable = null;
}
public IFeatureClass CreateLayer()
{
if (StatTable == null || StatEnv == null) return null;
//以下开始开始创建图层
IWorkspace ipWork;// = m_pEstiParam->m_pWorkspace;
//以下开始开始创建图层
//打开SHP工作区
string strDataDir;
strDataDir = System.Windows.Forms.Application.StartupPath + "\\" + BaseDataDef.EVN_TEMP_ACCESS;
IWorkspaceFactory ipWorkFact = new AccessWorkspaceFactoryClass();
ipWork = ipWorkFact.OpenFromFile(strDataDir, 0);
if (ipWork == null) return null;
IFeatureWorkspace ipFeatWork = ipWork as IFeatureWorkspace;
//如果存在则删除它
IFeatureWorkspaceManage pWManage = ipWork as IFeatureWorkspaceManage;
IDatasetName pDatasetName = new FeatureClassNameClass();
pDatasetName.Name = StatEnv.StatInstName;
IFeatureClass pOldFClass;
try
{
pOldFClass = ipFeatWork.OpenFeatureClass(StatEnv.StatInstName);
}
catch (Exception)
{
pOldFClass = null;
}
if (pOldFClass != null)
{
IDataset dataset = pOldFClass as IDataset;
IDataset mydataset = pOldFClass.FeatureDataset;//mydataset == null ???
dataset.Delete();
}
// if(hr != S_OK) CPubToolsMdl::ReportArcGisEngineError(hr);
//创建基本字段
IGeometryDefEdit pGeometryDefEdit;
IGeometryDef pGeometryDef = new GeometryDefClass();
pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
pGeometryDefEdit.HasM_2 = false;
pGeometryDefEdit.HasZ_2 = false;
pGeometryDefEdit.GridCount_2 = 1;
pGeometryDefEdit.set_GridSize(0, 0);
//从当前图层上获取空间参考
ISpatialReference ipSRef = StatEnv.MapControl.SpatialReference;///new UnknownCoordinateSystemClass() ;//比较死!
pGeometryDefEdit.SpatialReference_2 = ipSRef;
IFields pFields;
IFieldsEdit pFieldsEdit;
IObjectClassDescription pFeatClsDesc = new FeatureClassDescription();
pFields = pFeatClsDesc.RequiredFields;
pFieldsEdit = pFields as IFieldsEdit;
IField pField;
IFieldEdit pFieldEdit;
//////////////////////////////////////////////////////////////////////////
long lFieldCount;
lFieldCount = pFields.FieldCount;
for (int j = 0; j < lFieldCount; j++)
{
pField = pFields.get_Field(j);
esriFieldType type;
type = pField.Type;
if (type == esriFieldType.esriFieldTypeGeometry)
{
pField = pFieldsEdit.get_Field(j);
pFieldEdit = pField as IFieldEdit;
pFieldEdit.GeometryDef_2 = pGeometryDefEdit;
break;
}
}
//增加统计项目字段
AddAttrField(pFieldsEdit);
//建立shp
UID pUID = new UIDClass();
pUID.Value = "esriGeoDatabase.Feature";
IFeatureClass pShapeClass;//m_strLayerName
IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;
pShapeClass = ipFeatWork.CreateFeatureClass(StatEnv.StatInstName, pFields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "SHAPE", "");
return pShapeClass;
}
public void FillStatTable(IFeatureClass pFClass)
{
if (pFClass == null || StatTable == null || StatTable.workTable == null) return;
try
{
IWorkspace pWorkspace;
IDataset pDataset;
pDataset = pFClass as IDataset;
pWorkspace = pDataset.Workspace;
IWorkspaceEdit pWorkEdit = pWorkspace as IWorkspaceEdit;
if (pWorkEdit != null)
{
pWorkEdit.StartEditOperation();
pWorkEdit.StartEditing(false);
}
ExportTable(pFClass);
if (pWorkEdit != null)
{
pWorkEdit.StopEditing(true);
pWorkEdit.StopEditOperation();
}
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
#endregion
#region "辅助函数"
private void AddAttrField(IFieldsEdit pFieldsEdit)
{
if (pFieldsEdit == null || StatTable == null) return;
foreach (DataColumn var in StatTable.workTable.Columns)
{
string strFieldName = var.ColumnName;
string strFieldAlias = var.Caption + "_" + var.ColumnName;
esriFieldType FieldType;
int FieldLen;
int nScale;
if (var.DataType.ToString().ToLower().Contains("int"))
{
FieldType = esriFieldType.esriFieldTypeSmallInteger;
FieldLen = 4;
nScale = 0;
}
else if (var.DataType.ToString().ToLower().Contains("double"))
{
FieldType = esriFieldType.esriFieldTypeDouble;
FieldLen = 10;
nScale = 3;
}
else
{
FieldType = esriFieldType.esriFieldTypeString;
FieldLen = 80;
nScale = 0;
}
AddAttField(pFieldsEdit, strFieldName, strFieldAlias, FieldType, FieldLen, nScale);
}
}
private void AddAttField(IFieldsEdit pFieldsEditor, string strFieldName,
string strFieldAlias, esriFieldType FieldType, int FieldLen, int nScale)
{
if (pFieldsEditor == null) return;
IField ipField = new Field();
IFieldEdit ipFieldEdit = ipField as IFieldEdit;
int len = FieldLen;
ipFieldEdit.Name_2 = strFieldName;
ipFieldEdit.AliasName_2 = strFieldAlias;
ipFieldEdit.Type_2 = FieldType;
ipFieldEdit.Precision_2 = len;
//ipFieldEdit->put_Length(len);
if (nScale > 0) ipFieldEdit.Scale_2 = nScale;
pFieldsEditor.AddField(ipField);
}
private void ExportTable(IFeatureClass pNewFClass)
{
if (pNewFClass == null || StatTable == null || StatTable.workTable == null) return;
IFeatureCursor FCursor = GetGroupCursor();
if (FCursor == null) return;
while (true)
{
IFeature Feat;
Feat = FCursor.NextFeature();
if (Feat == null) break;
IFeature pNewFeat;
pNewFeat = pNewFClass.CreateFeature();
CopyFeature(Feat, pNewFeat);
pNewFeat.Store();
}
////复制几何体
//IGeometry pGeo;
//pNewFeat.Shape = pGeo;
//pNewFeat.set_Value();
}
private IFeatureCursor GetGroupCursor()
{
if (StatEnv == null || StatTable == null) return null;
LayerInfoArray LayerInfoList = new LayerInfoArray();
LayerBaseInfo LayerInfo = LayerInfoList.GetLayerInfo(StatEnv.MainGroupItem);
if (LayerInfo == null) return null;
IFeatureLayer FLayer = clsGISPub.FindFeatureLayer(StatEnv.MapControl.Map, LayerInfo.LayerName);//分组图层
if (FLayer == null) return null;
string code = "";
foreach (myItemInfo var in StatTable.MainGroupList)
{
if (code == "") code = var.ItemCode ;
else code = code + "," + var.ItemCode ;
}
string SQL = LayerInfo.codeField + " in (" + code + ")";
IFeatureCursor FCursor = clsGISPub.SearchFeature(FLayer.FeatureClass, SQL);
return FCursor;
}
private void CopyFeature(IFeature OldFeat, IFeature NewFeat)
{
if (OldFeat == null || NewFeat == null) return;
NewFeat.Shape = OldFeat.ShapeCopy;
LayerInfoArray LayerInfoList = new LayerInfoArray();
LayerBaseInfo LayerInfo = LayerInfoList.GetLayerInfo(StatEnv.MainGroupItem);
if (LayerInfo == null) return;
int iFieldIndex;
iFieldIndex = OldFeat.Fields.FindField(LayerInfo.codeField);
string code;
code = OldFeat.get_Value(iFieldIndex).ToString();
CopyeAtt(NewFeat, code);
}
private DataRow[] GetRow(string code)
{
if (StatTable == null || StatEnv == null) return null;
clsProcMainGroupField procField = new clsProcMainGroupField();
procField.StatEnv = StatEnv;
//dr[procField.GetMainFieldCode()] = GroupCode;
//dr[procField.GetMainFieldName()] = GroupName;
string sql = procField.GetMainFieldCode() + "='" + code + "'";
return StatTable.workTable.Select(sql);
}
void CopyeAtt(IFeature NewFeat, string code)
{
DataRow[] drs = GetRow(code);
CopyeAtt(NewFeat, drs);
}
private void CopyeAtt(IFeature feat, DataRow[] drs)
{
if (feat == null || drs == null || drs.Length == 0 ) return;
foreach (DataColumn var in StatTable.workTable.Columns)
{
string ColumnName = var.ColumnName;
int iFieldIndex;
iFieldIndex = feat.Fields.FindField(ColumnName);
if(iFieldIndex < 0) continue;
object obj = drs[0][ColumnName];
if (obj.ToString() == "")
{
IField field = feat.Fields.get_Field(iFieldIndex);
if (field.Type == esriFieldType.esriFieldTypeDouble
|| field.Type == esriFieldType.esriFieldTypeInteger || field.Type == esriFieldType.esriFieldTypeSingle
|| field.Type == esriFieldType.esriFieldTypeSmallInteger)
obj = 0;
}
feat.set_Value(iFieldIndex, obj);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -