//2添加要素图层
IWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
IFeatureWorkspace= IWorkspaceFactory .OpenFromFile(gdbPath, 0) as IFeatureWorkspace;
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = IFeatureWorkspace .OpenFeatureClass(路径);
pFeatureLayer.Name = "图层名"
axMapControlxy.Map.AddLayer(pFeatureLayer as ILayer);
//3导出图层lyr
//3导出图层lyr
//1.获取新图层文件的输出路径 - 您可以使用SaveFileDialog或任何其他形式来获取新图层文件的输出文件名。图层文件必须具有.lyr扩展名。看下面的代码示例:
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Layer File|*.lyr|All Files|*.*";
saveFileDialog.Title = "Create Layer File";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory,
layer.Name + ".lyr");
//Show the dialog box.
DialogResult dr = saveFileDialog.ShowDialog();
//2创建一个LayerFile类的新实例 - 使用LayerFile类来创建一个图层文件的新实例。看下面的代码示例:
//Create a new LayerFile instance.
ILayerFile layerFile = new LayerFileClass();
//Create a new layer file.
layerFile.New(saveFileDialog.FileName);
//3将地图中的图层与新创建的图层文件绑定在一起 - 创建新的图层文件后,您需要将其与要保存到磁盘的地图中的当前图层绑定。看下面的代码示例:
//Bind the layer file with the layer from the map.
layerFile.ReplaceContents(layer);
//4保存图层文件 - 要完成操作,请保存图层文件。看下面的代码示例:
layerFile.Save();
//4唯一值渲染器
private void DefineUniqueValueRenderer(IGeoFeatureLayer pGFL, string fiedname)
{
IRandomColorRamp pRCRamp = new RandomColorRampClass();
pRCRamp.Size = 66;
bool ok = true;
pRCRamp.CreateRamp(out ok);//!!!!!!!!!!!!!!!!!!!!!!
IUniqueValueRenderer pUVRender = new UniqueValueRendererClass();
pUVRender.FieldCount = 1;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
pUVRender.set_Field(0, "NAME");//这个0不是字段NAME的index!!!!!
IFeatureCursor pFCursor = pGFL.Search(null, false);
IFeature pF = pFCursor.NextFeature();
int index = pFCursor.FindField("NAME");
IEnumColors pEC = pRCRamp.Colors;
while (pF != null)
{
ISimpleFillSymbol pSFS = new SimpleFillSymbolClass();
IColor c = pEC.Next();
pSFS.Color = c;
string value=pF.get_Value(index).ToString();
pUVRender.AddValue(value,value, pSFS as ISymbol);//第一个参数是字段的值!
pF=pFCursor.NextFeature();
}
pGFL.Renderer = pUVRender as IFeatureRenderer;
pActiveView.Refresh();
}
//5高质量输出JPG
private void 输出ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog pSaveFileDialog = new SaveFileDialog();
pSaveFileDialog.FileName = "";
pSaveFileDialog.Filter = "JPG图片(*.jpg)|*.jpg";
if (pSaveFileDialog.ShowDialog() == DialogResult.OK)
{
IExport export = new ExportJPEGClass();
export.ExportFileName = pSaveFileDialog.FileName;
int screenResolution = 96;
System.Int32 outputResolution = 300;
export.Resolution = outputResolution;
tagRECT exportRECT; //这是一个结构
exportRECT.left = 0;
exportRECT.top = 0;
exportRECT.right = axPageLayoutControl1.ActiveView.ExportFrame.right * (outputResolution / screenResolution);
exportRECT.bottom = axPageLayoutControl1.ActiveView.ExportFrame.bottom * (outputResolution / screenResolution);
//设置PixelBounds信封以匹配exportRECT
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(0, 0, exportRECT.right, exportRECT.bottom);
export.PixelBounds = envelope;
int hDC = export.StartExporting();
axPageLayoutControl1.ActiveView.Output(hDC, (int) export.Resolution, ref exportRECT, null, null); //需要显式强制转换和'ref'关键字
export.FinishExporting();
export.Cleanup();
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "输出图片!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//6分段渲染
private void render图层()
{
axMapControl1.AddShapeFile(@"H:\桌面\已处理数据\中间数据可删\", "最终值.shp");
IFeatureLayer pFL = axMapControl1.get_Layer(0) as IFeatureLayer;
ITable pTable = pFL.FeatureClass as ITable;
double[] classes = { 10, 20, 36, 47, 100 };
IClassBreaksRenderer pClassBreakRenderer = new ClassBreaksRenderer();
pClassBreakRenderer.BreakCount = 5;//类数
pClassBreakRenderer.Field = "最终值";
pClassBreakRenderer.SortClassesAscending = true;//按顺序排列
IRgbColor tranparent = new RgbColor() as IRgbColor;
tranparent.Transparency = 0;
ISimpleLineSymbol pSLS = new SimpleLineSymbolClass();
pSLS.Color = tranparent;
for (int i = 0; i < classes.Length; i++)
{
ISimpleFillSymbol pSymbolFillSymbol = new SimpleFillSymbolClass();
pSymbolFillSymbol.Outline = pSLS;
ISymbol pSymbol = pSymbolFillSymbol as ISymbol;
pClassBreakRenderer.set_Symbol(i, pSymbol);
pClassBreakRenderer.set_Break(i, classes[i]);
}
IGeoFeatureLayer pGeoFeatureLayer = pFL as IGeoFeatureLayer;
pGeoFeatureLayer.Renderer = pClassBreakRenderer as IFeatureRenderer;
axMapControl1.Map.ClearLayers();
axMapControl1.AddLayer(pGeoFeatureLayer as ILayer);
}
// 解锁并打开shp文件
public IFeatureLayer OpenShapefile(string filePath)
{
//todo:先判断是否存在
IWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
IWorkspaceFactoryLockControl pWFLC = pWF as IWorkspaceFactoryLockControl;
if (pWFLC.SchemaLockingEnabled)
{
pWFLC.DisableSchemaLocking();
}
IWorkspace pWS=pWF.OpenFromFile(System.IO.Path.GetDirectoryName(filePath),0);
IFeatureWorkspace pFWS=pWS as IFeatureWorkspace;
//获得要素类
IFeatureClass pFC=pFWS.OpenFeatureClass(System.IO.Path.GetFileName(filePath));
//获得图层
IFeatureLayer pFL=new FeatureLayer();
pFL.FeatureClass=pFC;
pFL.Name=pFC.AliasName;
return pFL;
}
IName DataSet.Fullname返回和数据集的(名称对象IName),其Open()可以获取内存中的和这个名称对象相关的object,再用as转换到具体对象