[GIS] How to let the user select a spatial reference using ArcObjects

arcobjectscoordinate system

I need to build a tool where the user selects a coordinate system for a given CAD file. What is the easiest way to do this with ArcObjects?

Obviously, I could populate a list with the hundreds of projection constants (i.e. esrSRProjCSType), but what user is going to want to scroll through such a huge list? Must be a better way… any tips?

Update:

Thanks everybody. I went with the SpatialReferenceDialog approach below. Now, anybody know how to use NewGeoTransformationDialog to apply datum trans?

private void btn_getCs_Click(object sender, EventArgs e)
    {

        string StatusMsg = "";
        bool Status = true;
        SpatialReferenceDialog oSpatialReferenceDialog = null;
        ISpatialReference oSpatialReference = null;
        ISpatialReferenceFactory2 oSpatialReferenceFactory2 = null;
        IGeographicCoordinateSystem oGCS = null;

        try
        {
            oSpatialReferenceFactory2 = (SpatialReferenceEnvironmentClass)new SpatialReferenceEnvironmentClass();
            oGCS = oSpatialReferenceFactory2.CreateGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_WGS1984.GetHashCode());
            oSpatialReferenceDialog = new SpatialReferenceDialogClass();
            oSpatialReference = oSpatialReferenceDialog.DoModalEdit(oGCS, false, false, false, false, true, this.Handle.ToInt32());

            txtBx_srDwg.Text = oSpatialReference.Name;
        }
        catch (System.Runtime.InteropServices.COMException CE)
        {
            StatusMsg = CE.ErrorCode.ToString() + ": " + CE.Message;
            Status = false;
        }
        catch (Exception E)
        {
            StatusMsg = E.ToString();
            Status = false;
        }
        finally
        {
        }
}

Best Answer

You can use the built in SpatialReferenceDialog that allows the user to select the spatial reference. Here's an example built in VB.NET that I use in one of my applications. pSR is set as ESRI.ArcGIS.Geometry.ISpatialReference and SelectSR is a Boolean that I use to check whether the user selected a valid spatial reference. Check for the latest version of each of the interfaces.

Private Sub cmdSetReference_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetReference.Click

    Dim pSRDialog As ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 = New ESRI.ArcGIS.CatalogUI.SpatialReferenceDialog

    pSR = pSRDialog.DoModalCreate(False, False, False, 0)
    If Not pSR Is Nothing Then
        If Not TypeOf pSR Is ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem2 And Not TypeOf pSR Is ESRI.ArcGIS.Geometry.IProjectedCoordinateSystem5 Then
            System.Windows.Forms.MessageBox.Show("Please select a projected or geographic coordinate system.   ", "Unknown Coordinate System", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
            SelectSR = False
        Else
            SelectSR = True
        End If
    Else
        SelectSR = False
    End If
End Sub
Related Question