0

I'm trying to create a Feature class. It shows the error

X Y domain on the spatial reference system is not set or in valid

Here is my code attempt:

      IWorkspaceFactory wsf = new  FileGDBWorkspaceFactory();
      IWorkspace ws = wsf.OpenFromFile(@"D:\ARC OBJECTS\Newgdb.gdb", 
                       ArcMap.Application.hWnd);
      IFeatureWorkspace fws = ws as IFeatureWorkspace;
      IFields flds = new FieldsClass();
      IFieldsEdit fldsedit = flds as IFieldsEdit;
      IField fld = new FieldClass();
      IFieldEdit fldedit = fld as IFieldEdit;
      fldedit.AliasName_2 = "Shape";
      fldedit.IsNullable_2 = false;
      fldedit.Name_2 = "Shape";
      fldedit.Type_2 = esriFieldType.esriFieldTypeGeometry;
      IGeometryDef gdef = new GeometryDefClass();
      IGeometryDefEdit gdedit = gdef as IGeometryDefEdit;
      gdedit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
      gdedit.SpatialReference_2 = new UnknownCoordinateSystemClass();
      fldedit.GeometryDef_2 = gdedit;
      fldsedit.AddField(fld);
      IFeatureClass feacls= fws.CreateFeatureClass("Error", flds, null,
                          null,esriFeatureType.esriFTSimple, "shape", "");

Where did I go wrong?

3
  • A coordinate system is only a fraction of a coordinate reference, and there really isn't any reason to not define either correctly. If you don't understand how a CoordinateReference shapes the contents of a feature class, you are likely to be corrupting the data so thoroughly that any further ArcObjects coding with that data is pointless.
    – Vince
    Commented Mar 6, 2018 at 10:56
  • How to define a spatial reference: desktop.arcgis.com/en/arcobjects/latest/net/… and go to ArcObjects namespaces, Geometry.
    – mkennedy
    Commented Mar 6, 2018 at 19:48

1 Answer 1

1

With a EPSG as parameter I work with this:

IGeometryDefEdit geometryDefEdit = new GeometryDefClass();
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISpatialReference spatialReference = 
spatialReferenceFactory.CreateProjectedCoordinateSystem(EPSG); //or CreateGeographicCoordinateSystem
geometryDefEdit.SpatialReference_2 = spatialReference;

Then, when I define the field "Shape" I use:

IFieldsEdit fields = new FieldsClass();    
field = new FieldClass();
field.Name_2 = "Shape";
field.Type_2 = esriFieldType.esriFieldTypeGeometry;
field.GeometryDef_2 = geometryDefEdit as IGeometryDef;
fields.AddField(field);

Finally you have to add all the other fields...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.