Spatial Reference for Feature Dataset

373
8
Jump to solution
06-02-2012 06:28 AM
TauhidulIslam
New Contributor
Hello Everyone,
I am new in  development with C#.
Can anyone please give me any suggestion how can I create spatial reference using spatial Reference Dialogbox using C# code?

Thanks in Advance.

Tauhid
0 Kudos
1 Solution

Accepted Solutions
sapnas
by
Occasional Contributor III
Also, There is a flaw in your logic. You are explicitly setting theValue to 1 and hence your if(theValue != 1) is always false in other words the button click will not launch spatial reference dialog.

View solution in original post

0 Kudos
8 Replies
KenBuja
MVP Honored Contributor
This is the code I use for an Add-in which uses a button to set the spatial reference, checking if the user selected a projected or geographic projections.


private ESRI.ArcGIS.Geometry.ISpatialReference3 pSR;

private void cmdSetReference_Click(System.Object sender, System.EventArgs e) {
     ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 pSRDialog = new ESRI.ArcGIS.CatalogUI.SpatialReferenceDialog();
    pSR = pSRDialog.DoModalCreate(false, false, false, My.ArcMap.Application.hWnd);
    if ((pSR != null)) {
         if (!pSR is ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem2 & !pSR is ESRI.ArcGIS.Geometry.IProjectedCoordinateSystem5) {
             System.Windows.Forms.MessageBox.Show("Please select a projected or geographic coordinate system.   ", "Unknown Coordinate System", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
         }
     }
}
0 Kudos
TauhidulIslam
New Contributor
Dear KenBuja,
Thanks for your reply but it won't help me. I am stuc kwith my code. my code is give below:


private void cmdSub_Click(object sender, EventArgs e)
        {
            string strdatabasePath = "C:\\" + @txtProject.Text + "\\Survey\\Data_Table\\" + @txtDatabase.Text + @txtSub.Text;

            if (!Directory.Exists(strdatabasePath) == false)
            {
                MessageBox.Show("Destination Directory was not Found, Please Create the Directory First");
            }

            IWorkspaceFactory pWSF = new FileGDBWorkspaceFactoryClass();

            Boolean blsWorkspace = pWSF.IsWorkspace("C:\\" + @txtProject.Text + "\\Survey\\GeoDatabase\\" + @txtDatabase.Text + "\\" + @txtDatabase.Text + ".gdb");
            if (blsWorkspace == true)
            {
                IWorkspace pWorkspace = pWSF.OpenFromFile("C:\\" + @txtProject.Text + "\\Survey\\GeoDatabase\\" + @txtDatabase.Text + "\\" + @txtDatabase.Text + ".gdb", 0);
                IFeatureWorkspace pfeatureWorkspace = (IFeatureWorkspace)pWorkspace;
                IEnumDatasetName pEnumDatasetName = pWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
                IDatasetName pDatasetName = pEnumDatasetName.Next();
                while (pDatasetName != null)
                {
                    pEnumDatasetName = (IEnumDatasetName)pDatasetName;


                    string theDatasetName;
                    int theValue;
                    Boolean f = groupBox2.Focused;
                    int theVal = Convert.ToInt32(f);

                    while (pDatasetName == null)
                    {
                        theDatasetName = pDatasetName.Name;
                        if (theDatasetName == txtSub.Text)
                        {
                            theValue = 1;

                            pDatasetName = pEnumDatasetName.Next();

                            if (theValue != 1)
                            {
                                ISpatialReferenceDialog pSpaRefDlog = new SpatialReferenceDialogClass();
                                ISpatialReference m_SpaRef = pSpaRefDlog.DoModalCreate(true, true, true, theVal);
                             
                            }
                            MessageBox.Show("FeatureDataset Created");
                           
                        }
                        else
                        {
                            MessageBox.Show("No Spatial Reference Found, Feature Dataset was Not Created");
                        }
                    }
                }
            }
        }

there is no error shown but the button is not working when i click the button. please help me to solve the problem. can you please give me any suggestion what should I do.

Thank in Advance
Tauhid
0 Kudos
KenBuja
MVP Honored Contributor
The first thing to do is put some error handling code in your subroutine to see where it is failing.
0 Kudos
sapnas
by
Occasional Contributor III
The logic hightlighted in red will alter spatial reference of feature dataset.


if (theValue != 1)
{
ISpatialReferenceDialog pSpaRefDlog = new SpatialReferenceDialogClass();
ISpatialReference m_SpaRef = pSpaRefDlog.DoModalCreate(true, true, true, theVal);
IName nameObj = pDatasetName as IName;
IDataset dataset = nameObj.Open() as IDataset;
IGeoDataset pGeoDataset = dataset as IGeoDataset;
IGeoDatasetSchemaEdit pGDsEdit = pGeoDataset as IGeoDatasetSchemaEdit;
pGDsEdit.AlterSpatialReference( m_SpaRef);
                            
}
0 Kudos
TauhidulIslam
New Contributor
hello sshetty,
this won't work either. can you give me any other suggestion please?

Thanks,
Tauhid
GIS Specialist
NRECA International Ltd
0 Kudos
sapnas
by
Occasional Contributor III
Also, There is a flaw in your logic. You are explicitly setting theValue to 1 and hence your if(theValue != 1) is always false in other words the button click will not launch spatial reference dialog.

View solution in original post

0 Kudos
TauhidulIslam
New Contributor
sshety,
thanks for your help. can you please give me any suggestion that how can I use this if statement for open the spatial reference dialog box?

Again thanks for pointing out the problem.
0 Kudos
sapnas
by
Occasional Contributor III
If you want to test it then comment out the if statement and that should launch the spatial reference dialog box. Regarding the usability of If statement , you will have to analyse the code and modify it according to your requirement.
0 Kudos