Select to view content in your preferred language

AutomationException: No spatial reference exists.

3388
3
10-28-2010 06:31 PM
KerriParker
Deactivated User
I have been given the task of creating a file geodatabase, as a way of exporting spatial data from our (Oracle) database into a format suitable for use in a client's ArcGIS system.

I have managed to create a feature workspace, and a feature dataset within this workspace.  My code fails while attempting to create a feature class, throwing "AutomationException: No spatial reference exists." on the "featureDataset.createFeatureClass" call marked in the code below.  I'm totally confused by this, as I have explicitly created a SpatialReference object and used it when creating the dataset.  Where am I going wrong?

package envirosys.gi;

import com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactory;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.geometry.*;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.*;
import envirosys.util.*;
import oracle.jbo.*;
import org.apache.log4j.Logger;

public class GeodatabaseTask extends Thread {

  private ApplicationModule am = null;
  private Logger logger = null;
  private String path = null;
  private String filename = null;
  
  private boolean isNew = false;

  public GeodatabaseTask(ApplicationModule am, Logger logger, String path, String filename) {
    this.am = am;
    this.logger = logger;
    this.path = path;
    this.filename = filename;
  }
  
  public void run() {
    try {
      this.logger.info("Starting Geodatabase export");
      
      EngineInitializer.initializeEngine();
      
      AoInitialize aoInitializer = new AoInitialize();
      if ( aoInitializer.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable ) {
        aoInitializer.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
      } else {
        aoInitializer.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      }
      
      IWorkspace workspace = this.getWorkspace();
      IFeatureWorkspace featureWorkspace = new IFeatureWorkspaceProxy(workspace);
      
      ISpatialReference spatialReference = new UnknownCoordinateSystem();
      spatialReference.setDomain(0, 21474.83645, 0, 21474.83645);
      spatialReference.setFalseOriginAndUnits(0, 0, 100000);
      spatialReference.setMDomain(0, 21474.83645);
      spatialReference.setZDomain(0, 21474.83645);
      spatialReference.setZFalseOriginAndUnits(0, 100000);
     
      StringBuffer sbSQL = new StringBuffer("SELECT spae_id, schema_name, table_name FROM spatial_extracts ORDER BY spae_id");
      ViewObject voSpatialExtracts = DBCommon.getViewObject(this.am, "voSpatialExtracts", sbSQL.toString(), true);
      while ( voSpatialExtracts.hasNext() ) {
        Row rowSpatialExtract = voSpatialExtracts.next();
        
        String spaeId = PageFields.getStringValue(rowSpatialExtract.getAttribute("SPAE_ID"));
        String schemaName = PageFields.getStringValue(rowSpatialExtract.getAttribute("SCHEMA_NAME"));
        String tableName = PageFields.getStringValue(rowSpatialExtract.getAttribute("TABLE_NAME"));

        IFeatureDataset featureDataset = null;
        if ( this.isNew ) {
          featureDataset = featureWorkspace.createFeatureDataset(tableName, spatialReference);
        } else {
          featureDataset = featureWorkspace.openFeatureDataset(tableName);
        }
 
        IFeatureClass featureClass = this.createFeatureClass(featureDataset, spaeId, tableName);
 
        this.createFeatures(featureClass, schemaName, tableName);
      }
      voSpatialExtracts.remove();
      voSpatialExtracts = null;
      
      aoInitializer.shutdown();
      
      this.logger.info("Completed Geodatabase export");
    } catch ( AutomationException ae ) {
      System.out.println("CODE [" + ae.getCode() + "], DESCRIPTION [" + ae.getDescription() + "]");
    } catch ( Exception e ) {
      Utilities.dumpError("GeodatabaseTask.run", this.logger, e);
    }
  }
  
  private IWorkspace getWorkspace() {
    IWorkspace workspace = null;

    try {
      IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
      
      if ( workspaceFactory.isWorkspace(this.path + this.filename + ".gdb") ) {
        workspace = workspaceFactory.openFromFile(this.path + this.filename + ".gdb", 0);
      } else {
        IWorkspaceName workspaceName = workspaceFactory.create(this.path, this.filename + ".gdb", null, 0);
        IName name = (IName)workspaceName;
        workspace = new IWorkspaceProxy(name.open());
        
        this.isNew = true;
      }
    } catch ( AutomationException ae ) {
      System.out.println("CODE [" + ae.getCode() + "], DESCRIPTION [" + ae.getDescription() + "]");
    } catch ( Exception e ) {
      Utilities.dumpError("GeodatabaseTask.getWorkspace", this.logger, e);
    } finally {
      return workspace;
    }
  }

  private IFeatureClass createFeatureClass(IFeatureDataset featureDataset, String spaeId, String table) {
    IFeatureClass featureClass = null;

    try {
      StringBuffer sbSQL = new StringBuffer("SELECT spae.method_alias, tesm.name FROM spatial_extract_methods spae, test_methods tesm WHERE spae.spae_id = " + spaeId + " AND tesm.tesm_id = spae.tesm_id");
      ViewObject voMethods = DBCommon.getViewObject(this.am, "voMethods", sbSQL.toString(), true);
      int fieldCount = new Long(voMethods.getEstimatedRowCount()).intValue() + 2;
      int fieldCtr = 0;

      IFields fields = new Fields();
      IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
      fieldsEdit.setFieldCount(fieldCount);

      IField fieldUserDefined = new Field();
      IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined;
      fieldEdit.setName("OBJECTID");
      fieldEdit.setAliasName("OBJECT ID");
      fieldEdit.setType(esriFieldType.esriFieldTypeOID);
      fieldsEdit.setFieldByRef(fieldCtr++, fieldUserDefined);

      IGeometryDef geometryDef = new GeometryDef();
      IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
      geometryDefEdit.setGeometryType(esriGeometryType.esriGeometryPoint);

      geometryDefEdit.setGridCount(1);
      geometryDefEdit.setGridSize(0, 0);
      geometryDefEdit.setHasM(false);
      geometryDefEdit.setHasZ(false);

      fieldUserDefined = new Field();
      fieldEdit = (IFieldEdit)fieldUserDefined;
      fieldEdit.setName("SHAPE");
      fieldEdit.setType(esriFieldType.esriFieldTypeGeometry);
      fieldEdit.setGeometryDefByRef(geometryDef);
      fieldEdit.setIsNullable(true);
      fieldEdit.setRequired(true);
      fieldsEdit.setFieldByRef(fieldCtr++, fieldUserDefined);

      while ( voMethods.hasNext() ) {
        Row rowMethod = voMethods.next();
        
        fieldUserDefined = new Field();
        fieldEdit = (IFieldEdit)fieldUserDefined;
        fieldEdit.setName(PageFields.getStringValue(rowMethod.getAttribute("NAME")));
        fieldEdit.setAliasName(PageFields.getStringValue(rowMethod.getAttribute("METHOD_ALIAS")));
        fieldEdit.setEditable(true);
        fieldEdit.setIsNullable(false);
        fieldEdit.setPrecision(18);
        fieldEdit.setScale(8);
        fieldEdit.setType(esriFieldType.esriFieldTypeDouble);
        fieldsEdit.setFieldByRef(fieldCtr++, fieldUserDefined);
      }
      voMethods.remove();
      voMethods = null;

      IFeatureClassDescription fcDesc = new FeatureClassDescription();
      IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;

      featureClass = featureDataset.createFeatureClass(table, fields, ocDesc.getInstanceCLSID(), ocDesc.getClassExtensionCLSID(), esriFeatureType.esriFTSimple, "SHAPE", ""); // fails on this line
    } catch ( AutomationException ae ) {
      System.out.println("CODE [" + ae.getCode() + "], DESCRIPTION [" + ae.getDescription() + "]");
    } catch ( Exception e ) {
      Utilities.dumpError("GeodatabaseTask.createFeatureClass", this.logger, e);
    } finally {
      return featureClass;
    }
  }
  
  private void createFeatures(IFeatureClass featureClass, String schema, String table) {
    
    try {
      if ( featureClass.getShapeType() != esriGeometryType.esriGeometryPoint) {
        return;
      }
      
      IFields fields = featureClass.getFields();
      
      StringBuffer sbSQL = new StringBuffer("SELECT * FROM " + schema + "." + table);
      ViewObject voPoints = DBCommon.getViewObject(this.am, "voPoints", sbSQL.toString(), true);
      while ( voPoints.hasNext() ) {
        Row rowPoint = voPoints.next();
        
        IPoint point = null;
        
        IFeature feature = featureClass.createFeature();
        feature.setShapeByRef(point);
        
        ISubtypes subtypes = (ISubtypes)featureClass;
        IRowSubtypes rowSubtypes = (IRowSubtypes)feature;
        if ( subtypes.isHasSubtype() ) {
          rowSubtypes.setSubtypeCode(3);
        }
        
        rowSubtypes.initDefaultValues();
        
        for ( int fieldCtr = 0; fieldCtr < fields.getFieldCount(); fieldCtr++ ) {
          IField field = fields.getField(fieldCtr);
          String alias = field.getAliasName();
          
          String value = PageFields.getStringValue(rowPoint.getAttribute(alias));
          if ( !value.equals("") ) {
            feature.setValue(fieldCtr, value);
          }
        }
        
        feature.store();
      }
      voPoints.remove();
      voPoints = null;
    } catch ( AutomationException ae ) {
      System.out.println("CODE [" + ae.getCode() + "], DESCRIPTION [" + ae.getDescription() + "]");
    } catch ( Exception e ) {
      Utilities.dumpError("GeodatabaseTask.createFeatures", this.logger, e);
    }
  }
}
0 Kudos
3 Replies
KerriParker
Deactivated User
From the API doco for the createFeatureClass method in the IFeatureDataset interface "requires
that the spatial reference specified for the feature class to be created match the spatial reference of the feature dataset. Note that the spatial reference for the feature class to be created is specified in the GeometryDef property of the Field object for the Shape field in the supplied Fields collection. The GeometryDef object must be fully set up with information on both the spatial reference (the projected or geographic coordinate system, vertical coordinate system, the  coordinate domains and the coordinate resolution values), and the spatial index for the shape Field before calling CreateFeatureClass."

My code - which was cobbled together from various sources - included a comment (removed from the snippet to reduce the number of characters) which contradicted the documentation.  Unfortunately, I haven't been able to track down the offending source.
0 Kudos
VienMr
by
New Contributor
From the API doco for the createFeatureClass method in the IFeatureDataset interface "requires
that the spatial reference specified for the feature class to be created match the spatial reference of the feature dataset. Note that the spatial reference for the feature class to be created is specified in the GeometryDef property of the Field object for the Shape field in the supplied Fields collection. The GeometryDef object must be fully set up with information on both the spatial reference (the projected or geographic coordinate system, vertical coordinate system, the  coordinate domains and the coordinate resolution values), and the spatial index for the shape Field before calling CreateFeatureClass."

My code - which was cobbled together from various sources - included a comment (removed from the snippet to reduce the number of characters) which contradicted the documentation.  Unfortunately, I haven't been able to track down the offending source.


You do not have to set the spatial reference, as it is inherited from the feature dataset.
Because the GeometryDef property is read only.
But I have same error with you!
Can you help me!
0 Kudos
Victoria__Tori__H_Hall
New Contributor

Was this problem ever solved?  I am having a similar problem,

0 Kudos