How do I get a Spatial Reference from a Feature Dataset Path?

5400
5
Jump to solution
04-13-2015 05:31 PM
RichardFairhurst
MVP Honored Contributor

I am trying to create a custom geoprocessing custom tool and have set the output parameter to create a new feature class.  If the user chooses an output to a feature dataset path that exists, but contains no actual feature classes or other data, how can I discover the spatial reference of the feature dataset?  I need the output spatial reference to determine whether or not the dataset will contain feature classes with a Projected Coordinate System (PCS) or Geographic Coordinate System (GCS) and generate an error if the dataset output will use a GCS.  I do not want to create a feature class temporarily just to determine the feature dataset's spatial reference.

From the IGPValue of the output I can cast to the IName interface and find out if the output will be stored in a feature dataset by attempting to cast IName to an IFeatureDatasetName, but I am not sure how to get from that interface to the spatial reference of the feature dataset.  Even if I could navigate to the IFeatureDataset interface I don't see a method that would let me extract the spatial reference of the feature dataset.  Any ideas on what set of interfaces gets me from the path string of an existing feature dataset to the spatial reference of that feature dataset?

It seems like the spatial reference should be one of the main properties of a feature dataset, since one of the primary characteristic of a feature dataset is that nothing can be stored within it that does not match the feature dataset's spatial reference.

I prefer to use VB.Net, but any .Net code that shows the interface path I need to use is fine.

Reply
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

There's a snippet that get the spatial reference from a dataset

    '''<summary>Get the spatial reference information of a dataset that is supplied.</summary>

    ''' 

    '''<param name="dataset">An IDataset, this could be a table, feature class, feature dataset, workspace etc.</param>

    '''  

    '''<returns>An ISpatialReference interface if successful, nothing otherwise.</returns>

    '''  

    '''<remarks></remarks>

    Public Function GetSpatialReferenceFromDataset(ByVal dataset As ESRI.ArcGIS.Geodatabase.IDataset) As ESRI.ArcGIS.Geometry.ISpatialReference3

        'If the dataset supports IGeoDataset

        If TypeOf dataset Is ESRI.ArcGIS.Geodatabase.IGeoDataset Then

            'then grab the spatial reference information and return it.

            Dim geoDataset As ESRI.ArcGIS.Geodatabase.IGeoDataset = CType(dataset, ESRI.ArcGIS.Geodatabase.IGeoDataset)

            Return geoDataset.SpatialReference

        Else

            Return Nothing 'otherwise return nothing

        End If

    End Function

View solution in original post

5 Replies
JavierArtero
New Contributor III

Try following these steps:

- Obtain the IFeatureClass corresponding to the feature class you are working with, and get the access to its SHAPE attribute

Dim featureClass as IFeatureClass = (whatever)

Dim shapeIndex as Integer = featureClass.FindField(featureClass.ShapeFieldName)

DIm shapeField as IField = featureClass.Fields.Field(shapeIndex)

- Get the spatial reference from the field's Geometry Definition

Dim spatialRef as ISpatialReference = shapeField.GeometryDef.SpatialReference

... and that's it.

Don't know if there's another way to deal with this, but at least this method works

KenBuja
MVP Honored Contributor

There's a snippet that get the spatial reference from a dataset

    '''<summary>Get the spatial reference information of a dataset that is supplied.</summary>

    ''' 

    '''<param name="dataset">An IDataset, this could be a table, feature class, feature dataset, workspace etc.</param>

    '''  

    '''<returns>An ISpatialReference interface if successful, nothing otherwise.</returns>

    '''  

    '''<remarks></remarks>

    Public Function GetSpatialReferenceFromDataset(ByVal dataset As ESRI.ArcGIS.Geodatabase.IDataset) As ESRI.ArcGIS.Geometry.ISpatialReference3

        'If the dataset supports IGeoDataset

        If TypeOf dataset Is ESRI.ArcGIS.Geodatabase.IGeoDataset Then

            'then grab the spatial reference information and return it.

            Dim geoDataset As ESRI.ArcGIS.Geodatabase.IGeoDataset = CType(dataset, ESRI.ArcGIS.Geodatabase.IGeoDataset)

            Return geoDataset.SpatialReference

        Else

            Return Nothing 'otherwise return nothing

        End If

    End Function

View solution in original post

JavierArtero
New Contributor III

Uh... that's a better and more elegant solution indeed, Ken.

Reply
0 Kudos
RichardFairhurst
MVP Honored Contributor

Ken:

Thanks for pointing that out.  I never looked at the snippets and all help searches on IFeatureDataset never led me to IGeoDataset to get to the Spatial Reference.

Can an IFeatureDatasetName be cast to an IFeatureDataset, IDataset or IGeoDataset directly?  Is there a better way to take the text path to get one of those interfaces?  It seems so convoluted with no obvious bridge between the two interfaces.

I appreciate the other suggestion, but the point of this post is that no feature class exists to pass to any method.  I intend to create one, but until the tool completes no feature class would exist in the feature dataset.  Anyway, the IGeodataset interface is will do what I need if I can convert a workspace path to that interface.

Reply
0 Kudos
RichardFairhurst
MVP Honored Contributor

After looking more closely at the IName interface I see that it has an Open method that can instantiate the workspace or dataset, even when no feature class is included in the IName.  The IName help says:

"A Name object is a persistable software object that identifies and locates a geodatabase object such as a dataset or a workspace or a map object such as a layer.

A Name object supports an Open method that allows the client to get an instance of the actual object (for example, the dataset or workspace) given the name object. A name object thus acts as a moniker that supports binding to the named object.

The geodatabase supports methods on workspaces that hand out name objects that can be used by browsing clients to display the names of datasets in the workspace and to instantiate any specific dataset."

I suppose I would have to extract just the IFeatureDatasetName portion of the full tool output and cast that to IName prior to using the Open method, since otherwise the full output Name in the IGPValue would include a feature class that does not yet exist.  So I guess I can go from IGPValue to IName to IFeatureDatasetName, back to IName, then use the IName Open method to create an instance of the IDataset and then get an IGeodataset (or possibly open the IGeodataset directly using IName) and finally get the Spatial Reference.

Reply
0 Kudos