Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
        {
        }
}
share|improve this question
1  
At what license level (Engine or Desktop?). –  Kirk Kuykendall Aug 22 '11 at 14:30
    
If your original question was answered, please mark the answer. –  kenbuja Aug 30 '11 at 13:59
add comment

3 Answers 3

up vote 3 down vote accepted

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

share|improve this answer
1  
Depends on licensing level, but I would try to use the built in dialog as much as I can. Projections are a complex matter that and if you need to build your own dialog, you will spend some valuable time. –  George Aug 22 '11 at 14:34
add comment
  1. Initialize an IGxDialog and set its ObjectFilter property to an IGxObjectFilter instantiated with the GxFilterSpatialReferences co-class.
  2. Show the dialog using DoModalOpen()
  3. Get the first IGxObject from the output IEnumGxObject (and check that it is not null).
  4. Cast the IGxObject to IGxPrjFile.
  5. Return its SpatialReference property.

See here for a C# example.

If you need more control over which types of spatial references can be selected, implement IGxObjectFilter in a new class and use it for the IGxDialog's ObjectFilter property instead (see here for a VB6 example).

share|improve this answer
add comment

Thanks. 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
        {
        }

    }
share|improve this answer
2  
I would suggest adding this code to your question and then asking a new question. –  blah238 Aug 24 '11 at 7:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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