In ArcObjects I am loading Bing Maps as a base layer. It's coordinate system is esriSRProjCS3Type.esriSRProjCS_WGS1984WebMercatorMajorAuxSphere
. On top of the base layer, I am trying to load points, lines and polygons via SqlGeometry
objects where geometric coordinate system is WGS1984
or esriSRGeoCSType.esriSRGeoCS_WGS1984
.
I am converting SqlGeometry
to ESRI's IGeometry
objects and set spatial reference to esriSRGeoCSType.esriSRGeoCS_WGS1984
specifically:
public static IGeometry Convert(SqlGeometry sqlGeometry) {
int count;
IGeometry esriGeometry;
var factory = (IGeometryFactory3) new GeometryEnvironment();
factory.CreateGeometryFromWkbVariant(sqlGeometry.STAsBinary().Value, out esriGeometry, out count);
esriGeometry.SpatialReference = spatialReferenceForGeoCS_WGS1984;
return esriGeometry;
}
And I am following ESRI's documentation to create a feature class and assign spatial reference to it (doc):
public IFeatureClass CreateFeatureClass(String featureClassName,
IFeatureWorkspace featureWorkspace,
ISpatialReference spatialReference)
{
// Instantiate a feature class description to get the required fields.
IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;
IFields fields = ocDescription.RequiredFields;
// Find the shape field in the required fields and modify its GeometryDef to
// use point geometry and to set the spatial reference.
int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);
IField field = fields.get_Field(shapeFieldIndex);
IGeometryDef geometryDef = field.GeometryDef;
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
geometryDefEdit.SpatialReference_2 = spatialReferenceForGeoCS_WGS1984;
// Create the feature class.
IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(featureClassName, fields,
ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple,
fcDescription.ShapeFieldName, "");
return featureClass;
}
Still when I start editing session, I am getting "Spatial reference does not match data frame" error. Is there another artifact that needs spatial reference?
Best Answer
The spatial references don't match because you are mixing projected and unprojected (geographic) coordinate systems. Projected coordinate systems are typically in meters whereas geographic will tend to be lat/long.
SqlGeomeetry
returns data in an unprojected (geographic) format. Therefore,FeatureClass
andIGeometry
need to be projected onto base layer withesriSRProjCS3Type.esriSRProjCS_WGS1984WebMercatorMajorAuxSphere
system, which is used by base layer here.So
Convert
method readsSqlGeomerty
in a unprojected form. Before returning it needs to projectIGeometry
by callingProject
method.CreateFeatureClass
needs to useesriSRProjCS3Type.esriSRProjCS_WGS1984WebMercatorMajorAuxSphere
system rather thanesriSRGeoCSType.esriSRGeoCS_WGS1984
.