http://www.cnblogs.com/bpcbcm/archive/2009/05/16/bpc.html
以配电网gis为例,将采集好的电网数据gps数据导入到sde数据库, 以杆塔线路为例,杆塔数据绘制好之后自动联线,同时保持网络关系。
/// <summary>
/// 获取文件所有要添加的杆塔的所有的线路名称
/// /// </summary>
///
public void Getlinenames()
{
//gpsEdit.CurrentLayer=
IFeatureWorkspace pFeatWS;
IWorkspaceFactory pWorkspaceFactory;
pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
if (Fileworkspacestring == null) return;
pFeatWS = pWorkspaceFactory.OpenFromFile(Fileworkspacestring, 0) as IFeatureWorkspace;
foreach (String file in listnames)
{
// Create a PictureBox.
if (file == "Export_Output.shp")
{
try
{
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.SubFields = "xlmc,zlmc";
IFeatureClass pFeatureClass = pFeatWS.OpenFeatureClass(file);
ISelectionSet selectionSet = pFeatureClass.Select(null, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
IEnumIDs enumIDs = selectionSet.IDs;
int fieldIndex = pFeatureClass.FindField("xlmc");
int fieldIndex1 = pFeatureClass.FindField("zlmc");
IFeature feature;
int iD = enumIDs.Next();
xlname xl = new xlname();
int isfirst = 0;
while (iD != -1) //-1 is reutned after the last valid ID has been reached
{
if (isfirst == 0)
{
feature = pFeatureClass.GetFeature(iD);
xl.xlmc = feature.get_Value(fieldIndex).ToString();
xl.zlmc = feature.get_Value(fieldIndex1).ToString();
xlnames.Add(xl);
isfirst = 1;
}
else
{
feature = pFeatureClass.GetFeature(iD);
xl.xlmc = feature.get_Value(fieldIndex).ToString();
xl.zlmc = feature.get_Value(fieldIndex1).ToString();
bool iscz = true;
foreach (xlname xlmc in xlnames)
{
if ((xlmc.xlmc == xl.xlmc) && (xlmc.zlmc == xl.zlmc))
{
iscz = false;//是否存在
}
}
if (iscz == true) //不存在就添加
{
xlnames.Add(xl);
}
}
iD = enumIDs.Next();
}
int dd = xlnames.Count;
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
}
}
/// <summary>
/// 绘制点状设备
/// </summary>
/// <param name="iflayer"></param>
/// <param name="pPoint"></param>
private void Drawpoint(ILayer iflayer,IPoint pPoint)
{
try
{
if (iflayer == null) return;
if (!(iflayer is IGeoFeatureLayer)) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)iflayer;
IFeatureClass mfeatureClass = pFeatureLayer.FeatureClass;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return;
// 开始编辑,并设置Undo/Redo 为可用
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
if (!pWorkspaceEdit.IsBeingEdited())
{
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.EnableUndoRedo();
IFeature myfeature;
IFeatureClassWrite fr = mfeatureClass as IFeatureClassWrite;//绘制杆塔
pWorkspaceEdit.StartEditOperation();
myfeature = mfeatureClass.CreateFeature();
myfeature.Shape = pPoint;
myfeature.Store();
fr.WriteFeature(myfeature);
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing (true );
m_pMap.ClearSelection();
IActiveView pActiveView = (IActiveView)m_pMap;
pActiveView.Refresh();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 画线路
/// </summary>
public void Drawline(ILayer iflayer, IPointCollection pointCollection)
{
try
{
if (iflayer == null) return;
if (!(iflayer is IGeoFeatureLayer)) return;
IFeatureLayer pFeatureLayer = (IFeatureLayer)iflayer;
IFeatureClass mfeatureClass = pFeatureLayer.FeatureClass;
IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
if (pDataset == null) return;
// 开始编辑,并设置Undo/Redo 为可用
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
if (!pWorkspaceEdit.IsBeingEdited())
{
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.EnableUndoRedo();
IFeature myfeature;
IFeatureClassWrite fr = mfeatureClass as IFeatureClassWrite;//绘制杆塔
pWorkspaceEdit.StartEditOperation();
myfeature = mfeatureClass.CreateFeature();
pWorkspaceEdit.StartEditOperation();
myfeature = mfeatureClass.CreateFeature();
//IGeometry gGeometry = (IGeometry)pointCollection ;
IPolyline pPolyline = pointCollection as IPolyline;
myfeature.Shape = pPolyline;
myfeature.Store();
fr.WriteFeature(myfeature);
IComplexEdgeFeature Ccomplexedte = myfeature as IComplexEdgeFeature;//建立连通性
Ccomplexedte.ConnectAtIntermediateVertices();
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
m_pMap.ClearSelection();
IActiveView pActiveView = (IActiveView)m_pMap;
pActiveView.Refresh();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
/// <summary>
/// 绘制gps数据,杆塔,线路
/// </summary>
public void Draw()
{
IFeatureWorkspace pFeatWS;
IWorkspaceFactory pWorkspaceFactory;
pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
if (Fileworkspacestring == null) return;
pFeatWS = pWorkspaceFactory.OpenFromFile(Fileworkspacestring, 0) as IFeatureWorkspace;
foreach (String file in listnames)
{
if (file == "Export_Output.shp")//布绘杆塔
{
foreach (xlname xlmc in xlnames)
{
IFeature featurepoint;
int idline = GetLayerid(m_pMap, "GIS.lines");//获取线图层
ILayer ilayerline = m_pMap.get_Layer(idline);
int idpoint = GetLayerid(m_pMap, "gt");//获取点图层
ILayer iflayer = m_pMap.get_Layer(idpoint);
IPolyline mypolylin = new PolylineClass();
IPointCollection pointCollection = mypolylin as IPointCollection;
object missingVal = System.Reflection.Missing.Value;
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "xlmc='"+xlmc .xlmc+"'"+" and zlmc='" + xlmc.zlmc+"'";
IFeatureClass pFeatureClass = pFeatWS.OpenFeatureClass(file);
//IQueryFilterDefinition queryFilterDefinition = (IQueryFilterDefinition)queryFilter;
//queryFilterDefinition.PostfixClause = "ORDER BY gh";
IFields fField = pFeatureClass.Fields;
for (int fieldindex = 0; fieldindex < fField.FieldCount;fieldindex++ )
{
string dd = fField.get_Field(fieldindex).Name;
string ddd = pFeatureClass.ShapeFieldName;
}
ISelectionSet selectionSet = pFeatureClass.Select(queryFilter, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
int fieldIndexgh = pFeatureClass.FindField("gh");
int fieldIndexzjgh = pFeatureClass.FindField("zjgh");
int fieldindexxlmc = pFeatureClass.FindField("xlmc");
int fieldindexzlmc = pFeatureClass.FindField("zlmc");
int fieldindextjxl = pFeatureClass.FindField("tjxl");
int fieldindextjgh = pFeatureClass.FindField("tjgh");
IEnumIDs enumIDs = selectionSet.IDs;
for (int j = 1; j <= selectionSet.Count;j++ )
{
bool istjgh = false;
enumIDs.Reset();
int iD = enumIDs.Next();
while (iD != -1) //-1 is reutned after the last valid ID has been reached
{
featurepoint = pFeatureClass.GetFeature(iD);
IGeometry geometrygt = featurepoint.Shape;//获取shp文件中的对象
IPoint pointgt = geometrygt as IPoint;
gtzb gtxx = new gtzb();
gtxx.gh = featurepoint.get_Value(fieldIndexgh).ToString();
gtxx.xlmc = featurepoint.get_Value(fieldindexxlmc).ToString();
gtxx.zlmc = featurepoint.get_Value(fieldindexzlmc).ToString();
gtxx.zjgh = featurepoint.get_Value(fieldIndexzjgh).ToString();
if (gtxx.gh == j.ToString() && gtxx.zjgh.ToString().Trim() =="")
{
Drawpoint(iflayer, pointgt); //将shp文件中的对象绘制到数据库
pointCollection.AddPoint(pointgt, ref missingVal, ref missingVal);
}
else if (gtxx.gh == j.ToString() && gtxx.zjgh.ToString().Trim() != "")
{
istjgh = true;
}
iD = enumIDs.Next();
}
if (istjgh)
{
IQueryFilter queryFiltersub = new QueryFilterClass();
queryFiltersub.WhereClause = "xlmc='" + xlmc.xlmc + "'" + " and zlmc='" + xlmc.zlmc + "'and zjgh='" + j + "'";
ISelectionSet selectionSetsub = pFeatureClass.Select(queryFiltersub, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
for (int subj = 1; subj <= selectionSetsub.Count; subj++)
{
IEnumIDs enumsubIDs = selectionSetsub.IDs;
enumsubIDs.Reset();
int iDsub = enumsubIDs.Next();
while (iDsub != -1) //-1 is reutned after the last valid ID has been reached
{
featurepoint = pFeatureClass.GetFeature(iDsub);
IGeometry geometrygt = featurepoint.Shape;
IPoint pointgt = geometrygt as IPoint;
gtzb gtxx = new gtzb();
gtxx.longitude = pointgt.X;
gtxx.latitude = pointgt.Y;
gtxx.elevation = pointgt.Z;
gtxx.gh = featurepoint.get_Value(fieldIndexgh).ToString();
gtxx.zjgh = featurepoint.get_Value(fieldIndexzjgh).ToString();
gtxx.xlmc = featurepoint.get_Value(fieldindexxlmc).ToString();
gtxx.zlmc = featurepoint.get_Value(fieldindexzlmc).ToString();
if (gtxx.zjgh == subj.ToString())
{
Drawpoint(iflayer, pointgt);
pointCollection.AddPoint(pointgt, ref missingVal, ref missingVal);
}
iDsub = enumsubIDs.Next();
}
}
}
}
//绘制线路
Drawline(ilayerline, pointCollection);
}
}
}