背景:
最近,用c#做二次开发要实现一个功能,即通过鼠标点击创建一个以当前单机点为圆心的缓冲区。要创建缓冲区进行缓冲区分析,则必须要有相应的图层。因此我首先需要通过鼠标点创建一个点状图层添加到图层中去。在网上找到一篇博文《AE 创建shp图层》:http://www.cnblogs.com/jinqier/archive/2013/05/28/3104245.html,这篇文章给出了可以直接使用的代码。可以在此基础上修改,特转载方便自己和他人使用。
原文没有说要引用什么,这可能会对部分阅读者造成不便,这里我附上我项目中的引用,方便大家进行快速开发:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using UNovice.Forms;
using UNovice.Properties;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesFile;
原文:
AE 创建shp图层
C#+AE中创建点状Shp文件
今天将点写入一个新的SHP文件中,自己先做了一个简单试验,一切正常。代码如下
private void CreateShpFromPoint()
{
ISpatialReference pSpatialReference = this.axMapData.ActiveView.FocusMap.SpatialReference;
string strShapeFolder="C:/";
string strShapeFile = "test.shp";
string shapeFileFullName = strShapeFolder + strShapeFile;
IWorkspaceFactory pWorkspaceFactory= new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWorkspaceFactory.OpenFromFile(strShapeFolder, 0);
IFeatureClass pFeatureClass;
if (File.Exists(shapeFileFullName))
{
pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
IDataset pDataset = (IDataset)pFeatureClass;
pDataset.Delete();
}
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
IField pField = new FieldClass();
IFieldEdit pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = "SHAPE";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDefEdit pGeoDef = new GeometryDefClass();
IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
pGeoDefEdit.SpatialReference_2 = pSpatialReference; //new UnknownCoordinateSystemClass();
pFieldEdit.GeometryDef_2 = pGeoDef;
pFieldsEdit.AddField(pField);
pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = "ID";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = "Pixels";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFieldsEdit.AddField(pField);
pFeatureClass = pFeatureWorkspace.CreateFeatureClass(strShapeFile, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
IPoint pPoint = new PointClass();
pPoint.X = 113.0;
pPoint.Y = 23.0;
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pPoint;
pFeature.set_Value(pFeature.Fields.FindField("ID"), "D-1");
pFeature.set_Value(pFeature.Fields.FindField("Pixels"), 1);
pFeature.Store();
IFeatureLayer pFeaturelayer = new FeatureLayerClass();
pFeaturelayer.FeatureClass = pFeatureClass;
pFeaturelayer.Name = "layer";
this.axMapData.AddLayer(pFeaturelayer);
}