2

I would like to create a SpatialReference with ArcObjects, but I won't know whether it is Projected or Geographic, I'll only have the SRID. Is it fine practice to use 'ISpatialReferenceFactory.CreateGeographicCoordinateSystem' with a 'try', see if it fails, and if so, use 'CreateProjectedCoordinateSystem', and if that fails, give a message that it won't work? I have a feeling that I shouldn't use Exceptions in this way, but I'm not aware of an alternative.

I am aware of these answers, but they don't quite answer the question:

Arcobjects: Is my spatial reference Geographic or Projected?

How to programmatically determine whether a spatial reference factory code is valid?

Edit: This is after the fact, but I have become aware of this answer

4
  • 1
    You cannot determine the coordinate system properties until you instantiate it. Those two questions do appear to solve your issue. What more do you need?
    – Vince
    Commented Aug 28, 2015 at 15:06
  • So I am correct in thinking the only way to do this is to try one of the two methods (Geographic or Projected), see if it fails, then try the other? This is using Try and Catch for program flow though, which seems inappropriate. There's no other way?
    – TSJ
    Commented Aug 28, 2015 at 15:08
  • That's what EAFP is about. Alternatively, you can let the user provide a projected code or a geographic code, and trap the error if they've given a bad one.
    – Vince
    Commented Aug 28, 2015 at 15:11
  • Yep, that's the way to do it. To check if a WKID is valid, the software has to create the object anyway then check its type.
    – mkennedy
    Commented Aug 28, 2015 at 15:56

2 Answers 2

2

Further to what Vince has indicated.. create a spatial reference with ISpatialReferenceFactory3 (CreateSpatialReference is the easiest) and then find out what it is. You have the SRID/EPSG code so create a spatial reference object and then test its type (ISpatialReference is implemented by both geographic and projected coordinate systems and also unknown coordinate systems but hopefully you won't encounter one of them).

ISpatialReferenceFactory3 pSRfact = new SpatialReferenceEnvironmentClass();
ISpatialReference pSR = pSRfact.CreateSpatialReference(Code);
if (pSR is IProjectedCoordinateSystem)
{ 
    // do your projected stuff
}
else if (pSR is IGeographicCoordinateSystem)
{
    // do your geographic stuff
}

Or if you don't like is use the as operator (returns null if the type can't be cast)

ISpatialReferenceFactory3 pSRfact = new SpatialReferenceEnvironmentClass();
ISpatialReference pSR = pSRfact.CreateSpatialReference(Code);
IProjectedCoordinateSystem pProjSR = pSR as IProjectedCoordinateSystem;
IGeographicCoordinateSystem pGeoSR = pSR as IGeographicCoordinateSystem;
if (pProjSR != null)
{ 
    // do your projected stuff
}
else if (pGeoSR != null)
{
    // do your geographic stuff
}

of course if the code isn't supported neither will be created, make sure there's a way in your code to avoid junk SRID/EPSG codes safely.

2
  • Thank you! I was sure there must be a way, but I hadn't looked in ISpatialReferenceFatory2, only in the original. Yes, 'CreateSpatialReference' is what I needed.
    – TSJ
    Commented Sep 1, 2015 at 22:03
  • You're welcome. It pays to look at the newer interfaces, in order for a new interface to be created there's bugs fixed and/or new functionality added. Be mindful with the co-classes, for example ITopologicalOperator5 is only implemented for PolyLine objects (not point, polygon or multipoint) but ITopologicalOperator6 is implemented for all simple geometry types. Commented Sep 1, 2015 at 22:26
1

So this seems to be the correct way to check if a WKID is Projected or Geographic...

edit: This is what I had done, but @Michael Miles Stimson found the proper method to use instead of relying on try/catch.

public ISpatialReference getSpatialReference_byCode(int srid)
        {
            ISpatialReference spatialReference;
            ISpatialReferenceFactory sr_factory = new SpatialReferenceEnvironmentClass();
            try
            {
                spatialReference = sr_factory.CreateGeographicCoordinateSystem(srid);
            }
            catch
            {
                try
                {
                    spatialReference = sr_factory.CreateProjectedCoordinateSystem(srid);
                }
                catch
                {
                    throw new NotImplementedException("This is an unkown Spatial Reference");
                }
            }

            return spatialReference;
        }
3
  • 1
    Where does spatialReference get set to something other than null?
    – Vince
    Commented Aug 28, 2015 at 16:56
  • Does C# permit untyped catch? I only use Java.
    – Vince
    Commented Aug 28, 2015 at 17:05
  • So far it has worked for me. I should work harder to understand the COM Errors so that I can understand the types...
    – TSJ
    Commented Aug 28, 2015 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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