ArcEngine二次开发日常开发工具类

一、加载shp

public IFeatureLayer LoadShapefile(string shpFullPath)
{
    string pathName = System.IO.Path.GetDirectoryName(shpFullPath);
    string fileName = System.IO.Path.GetFileNameWithoutExtension(shpFullPath);
    IWorkspaceFactory pWorkspaceFactory;
    IFeatureWorkspace pFeatureWorkspace;
    IFeatureLayer pFeatureLayer;

    pWorkspaceFactory = new ShapefileWorkspaceFactory();
    pFeatureLayer = new FeatureLayerClass();
    pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(pathName, 0) as IFeatureWorkspace;
    pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
    pFeatureLayer.Name = fileName;
    return pFeatureLayer;
}

二、加载栅格 

public IRasterLayer LoadRasterLayer(string rasterFullPath)
{
    IRasterLayer pRasterLayer = new RasterLayerClass();
    pRasterLayer.CreateFromFilePath(rasterFullPath);
    return pRasterLayer;
}

三、修改字段值

public bool ResetFiledValue(IFeatureLayer featureLayer)
{
     IFeature pFeature;
     IFeatureClass pFeactureClass;
     IQueryFilter pQueryFilter;
     IFeatureCursor pFeatureCursor;

     try
     {
         pFeactureClass = featureLayer.FeatureClass;
         pQueryFilter = new QueryFilterClass();
         pQueryFilter.WhereClause = "";
         pFeatureCursor = pFeactureClass.Update(pQueryFilter, false);
         pFeature = pFeatureCursor.NextFeature();
         while (pFeature != null)
         {
             
             pFeature.set_Value(pFeature.Fields.FindField("SFJZ"), "Y");
             pFeatureCursor.UpdateFeature(pFeature);
             pFeature = pFeatureCursor.NextFeature();
         }
         System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
         return true;
     }
     catch (Exception)
     {
         throw;
     }
}

四、符号填充

public void PolygonSymol(IFeatureLayer pFeaturelayer)
{
    IGeoFeatureLayer pGeoFeatLyr = pFeaturelayer as IGeoFeatureLayer;
    //设置面填充符号
    ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
    pLineSymbol.Width = 2;//定义线的宽度
    IRgbColor pRgbColor = GetRgbColor(255, 0, 0, 1);
    pLineSymbol.Color = pRgbColor;//定义线的颜色
    ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
    pSimpleFillSymbol.Outline = pLineSymbol;
    pSimpleFillSymbol.Color = GetRgbColor(255, 255, 255, 0);

    ILineSymbol pLineSymbol2 = new SimpleLineSymbolClass();
    pLineSymbol2.Width = 2;//定义线宽
    IRgbColor pRgbColor2 = GetRgbColor(0, 0, 0, 1);
    pLineSymbol2.Color = pRgbColor2;//定义线的颜色
    ISimpleFillSymbol pSimpleFillSymbol2 = new SimpleFillSymbolClass();
    pSimpleFillSymbol2.Outline = pLineSymbol2;
    pSimpleFillSymbol2.Color = GetRgbColor(255, 255, 255, 0);
    //更改符号样式
    IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass();
    pUniqueValueRenderer.FieldCount = 1;
    pUniqueValueRenderer.set_Field(0, "SFJZ");
    pUniqueValueRenderer.AddValue("Y", "已举证", (ISymbol)pSimpleFillSymbol);
    pUniqueValueRenderer.AddValue("N", "未举证", (ISymbol)pSimpleFillSymbol2);
    pGeoFeatLyr.Renderer = pUniqueValueRenderer as IFeatureRenderer;
}

private IRgbColor GetRgbColor(int intR, int intG, int intB, byte trans)
{
    IRgbColor pRgbColor = null;
    if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
    {
        return pRgbColor;
    }
    pRgbColor = new RgbColorClass();
    pRgbColor.Transparency = trans;
    pRgbColor.Red = intR;
    pRgbColor.Green = intG;
    pRgbColor.Blue = intB;
    return pRgbColor;
}

五、缩放至要素

public void ZoinFeatures(IFeatureLayer pFeatureLayer, IActiveView pActiveview, string tbybh)
{
    if (pFeatureLayer == null)return;
    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    IQueryFilter pQueryFilter = new QueryFilterClass();
    pQueryFilter.WhereClause = "\"TBYBH\" = '" + tbybh + "'";
    IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
    pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
    if (pFeatureSelection.SelectionSet.Count == 0)return;
    IEnumGeometryBind tEnumGeometryBind = new EnumFeatureGeometryClass();
    tEnumGeometryBind.BindGeometrySource(null, pFeatureSelection.SelectionSet);
    IEnumGeometry tEnumGeometry = (IEnumGeometry)tEnumGeometryBind;
    IGeometryFactory tGeometryFactory = new GeometryEnvironmentClass();
    IGeometry tGeometry = tGeometryFactory.CreateGeometryFromEnumerator(tEnumGeometry);
    IEnvelope pEnvelope = tGeometry.Envelope;
    pEnvelope.XMax += 20;
    pEnvelope.XMin -= 20;
    pEnvelope.YMax += 20;
    pEnvelope.YMin -= 20;
    pActiveview.Extent = pEnvelope;
    
    //pActiveview.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, pFeatureSelection, tGeometry.Envelope);
    pActiveview.Refresh();
}

 

你可能感兴趣的:(Esri,ArcEngine,ArcEngine)