GIS資料的查詢,插入,刪除,更新(ArcEngine)

jianzhanger發表於2011-04-14

1 查詢資料

1 )、利用FeaturCursor 進行空間查詢

// 利用構造的Envelope (矩形框),查詢和Envelope 相交的FeatureClass 的要素集

// 結果資料利用迴圈FeatureCursor.nextFeature 獲得

IEnvelope envelope = new EnvelopeClass();

envelope.PutCoords(508786, 681196, 513033, 684341);

// 空間查詢

ISpatialFilter spatialFilter = new SpatialFilterClass();

spatialFilter.Geometry = envelope;

String shpFld = featureClass.ShapeFieldName;

spatialFilter.GeometryField = shpFld;

spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;// 相交

IQueryFilter queryFilter = new QueryFilterClass();

queryFilter = (IQueryFilter)spatialFilter;

IFeatureCursor searchCursor = featureClass.Search(queryFilter, true);

IFeature feature = searchCursor.NextFeature();

int n = 0;

while (feature != null){

    n++;

    feature = baseCursor.NextFeature();

}

2 )、利用FeatureCursor 在選擇集中進行查詢

IFeatureSelection pFeatureSelectio = pFeatureLayer as IFeatureSelection; 

ICursor ppCursor;
pFeatureSelectio.SelectionSet.Search(null, false, out ppCursor);

IFeatureCursor pFeatureCursor = ppCursor as IFeatureCursor; 

IFeature pFeature = pFeatureCursor.NextFeature();

3 )、利用FeatureCursor 進行屬性查詢

IQueryFilter pQueryFilter = new QueryFilterClass();                           

pQueryFilter.WhereClause = "PROJECTCODE = '" + this.ProjectNumTextBox.Text.Trim() + "'";

IFeatureCursor pFeatureCursor = LineFeaClass.Search(pQueryFilter, false);                           

IFeature pFeatureIfExit = pFeatureCursor.NextFeature();

 

2   插入資料

1 )、利用 Insert Cursors featureBuffer

IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();

IFeatureCursor featureCursor = featureClass.Insert( true);

object featureOID;         

featureBuffer.set_Value(featureBuffer.Fields.FindField("InstBy"), "B Pierce");

for ( int ic = 0; ic < 99; ic++)

  {  

  featureBuffer.Shape = geometry;    

    featureOID = featureCursor.InsertFeature(featureBuffer);

}

featureBuffer.set_Value(featureBuffer.Fields.FindField("InstBy"), "K Johnston");

for ( int ic = 0; ic < 99; ic++)

{

      featureBuffer.Shape = geometry

      featureOID = featureCursor.InsertFeature(featureBuffer);

}

featureCursor.Flush();

2 )、直接進行資料插入(Feature.Store 方法)

 IFeature newLineFeature = LineFeaClass.CreateFeature();                           

newLineFeature.Shape = pFeature.Shape;
int ii = newLineFeature.Fields.FindField(AreaZonalName); 

 if (ii > 0)                           

{                               

newLineFeature.set_Value(ii, this.AnalysAreaComboBox.Text.Trim());
}

newLineFeature.Store();

3 資料刪除  delete 

IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Parcels");

IQueryFilter queryFilter = new QueryFilterClass();

queryFilter.WhereClause = "ZONING_S = 'R'";

IFeatureCursor updateCursor = featureClass.Update(queryFilter, false);

IFeature feature = updateCursor.NextFeature();

int m = 0;

while (feature != null)

{

    m++;

    updateCursor.DeleteFeature(feature);

    feature = updateCursor.NextFeature();

}

4   資料更新  update 

IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Parcels");

// 屬性查詢

IQueryFilter queryFilter = new QueryFilterClass();

queryFilter.WhereClause = "ZONING_S = 'U'";

// 利用FeatureCursor 進行資料更新

IFeatureCursor updateCursor = featureClass.Update(queryFilter, false);

int fieldindex = featureClass.FindField("ZONING_S");

IFeature feature = updateCursor.NextFeature();

int m = 0;

while (feature != null)

{

    m++;

    feature.set_Value(fieldindex, "X");

    updateCursor.UpdateFeature(feature);

    feature = updateCursor.NextFeature();

}

 

相關文章