How can I save an IGeometry to disk as a Layer file?
ArcEngine 10, C#, VS2010
Edit 1: With Jason Scheirer 's suggestion I managed to convert a point into a shapefile - and I'll post the code here for the community.
string strFolder = @"C:\temp";
string strName = "NewShapeFile"; //don't include .shp extension
string strShapeFieldName = "Shape";
ShapefileWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0) as IFeatureWorkspace;
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = strShapeFieldName; //Name is read-only Name_2 is writeable -- don't ask me why
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeomDef = new GeometryDefClass();
IGeometryDefEdit pGeomDefEdit = pGeomDef as IGeometryDefEdit;
pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
pGeomDefEdit.SpatialReference_2 = CrowWingCoordinates;
pFieldEdit.GeometryDef_2 = pGeomDef;
pFieldsEdit.AddField(pField);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = 25;
pFieldEdit.Name_2 = "TextField";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
IFeatureClass pFeatClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, "");
IFeatureBuffer fb = pFeatClass.CreateFeatureBuffer();
IFeature feature = pFeatClass.CreateFeature();
IFeatureCursor featureCursor = pFeatClass.Insert(true);
double x = -94.105797;
double y = 46.559214;
IPoint p = new PointClass();
p.SpatialReference = gcs;
p.PutCoords(x, y);
p.Project(myCoordinates);
IGeometry geometry = p as IGeometry;
fb.Shape = p;
featureCursor.InsertFeature(fb);
featureCursor.Flush();
Answer
A layer file does not hold any geographic data -- all it contains is pointers to data sources and symbology. You will need to save your IGeometry
to disk as a Feature Class. If you then get the IName
for the feature class, you could open a layer file on disk, point to that feature class and save a copy back out.
Workflow:
- Create new feature class (shapefile)
- Write your geometry to it with an Insert Cursor
- Open layer file with predefined symbology
- Set its root
ILayer
's data source toIName
of the shapefile - Turn on relative paths in the layer file
- Save a copy out to the same directory as the shapefile
The superior alternative is to use a layer package with the Package Layer tool.
No comments:
Post a Comment