ArcGIS Server SOC创建shape

本文介绍如何使用C#在ArcGIS Server环境中程序化地创建Shapefile。文章提供了详细的代码示例,包括如何设置工作空间、定义字段及几何类型等关键步骤,并针对常见错误给出了调试建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# - Create Shapefile Programmatically PDF Print E-mail

Written by Steve Gourley,

When I first starting looking into creating a shapefile programmatically, I thought it would be a piece of cake since there is so much reference code out there from VBA GIS users. ArcGIS Server is not a VBA editor within ArcMap though and there were a few hurdles to jump over to make this work. Below is the code, I hope it makes your day. It would have made mine to find this somewhere.

Notes: If you get this error code System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040258 or System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040351

try creating your objects with the IServerContext.CreateObject method.

if you get this error code System.Runtime.InteropServices.COMException: Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

Check that your CLSID string is correct. Good luck finding a list of these - it's mostly an educated guessing game.





[color=blue]public static void CreateShapefile()

{

string shapeName = "test2";

string savePath = string.Format(@"C:\arcgisserver\arcgisoutput\{0}\", shapeName);

string shapeFieldName = "Shape";



ESRI.ArcGIS.Server.IServerContext sC = null;

ESRI.ArcGIS.Geodatabase.IWorkspace wP = null;



try

{

ConnectToWRI(out wP, out sC);

}

catch (Exception)

{

return;

}



if (Directory.Exists(savePath))

{

Directory.Delete(savePath, true);

}



Directory.CreateDirectory(savePath);



ShapefileWorkspaceFactory workspaceFactory = (ShapefileWorkspaceFactory)sC.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory.1");

IFeatureWorkspace fws = workspaceFactory.OpenFromFile(savePath, 0) as IFeatureWorkspace;



IFields fieldCollection = (IFields)sC.CreateObject("esriGeoDatabase.Fields");

IFieldsEdit editableFieldCollection = (IFieldsEdit)fieldCollection;



IField field = (IField)sC.CreateObject("esriGeoDatabase.Field");

IFieldEdit editableField = (IFieldEdit)field;



editableField.Name_2 = shapeFieldName;

editableField.Type_2 = esriFieldType.esriFieldTypeGeometry;



SpatialReferenceEnvironment spatialEnviro = (SpatialReferenceEnvironment)sC.CreateObject("esriGeometry.SpatialReferenceEnvironment");

IProjectedCoordinateSystem coordSystem = spatialEnviro.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_12N);



IGeometryDef geometryDefinition = (IGeometryDef)sC.CreateObject("esriGeoDatabase.GeometryDef");

IGeometryDefEdit editableGeomDefinition = (IGeometryDefEdit)geometryDefinition;

editableGeomDefinition.GeometryType_2 = esriGeometryType.esriGeometryPolygon;

editableGeomDefinition.SpatialReference_2 = coordSystem;



editableField.GeometryDef_2 = geometryDefinition;

editableFieldCollection.AddField(field);



IField field2 = (IField)sC.CreateObject("esriGeoDatabase.Field");

IFieldEdit editableField2 = (IFieldEdit)field2;



editableField2.Length_2 = 25;

editableField2.Name_2 = "Name";

editableField2.Type_2 = esriFieldType.esriFieldTypeString;



editableFieldCollection.AddField(field2);



try

{

ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatClass = fws.CreateFeatureClass(shapeName, fieldCollection, null, null, esriFeatureType.esriFTSimple, shapeFieldName, "") as ESRI.ArcGIS.Geodatabase.IFeatureClass;

}

catch (Exception)

{

sC.ReleaseContext();

}



sC.ReleaseContext();

}[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值