Tuesday, March 30, 2010

Getting the Primary Geometry Field Name from a GRecordset

As a GeoMedia programmer, it is necessary to be able to determine the primary geometry field name of a feature's GRecordset at runtime. The database field in the record set  that stores the feature geometry in a GeoMedia warehouse is typically named as Geometry1 but it is not always the case. Sometimes it can be simply Geometry or GDO_GEOMETRY or any name the programmers encoded into the application commands.

 If you are doing any programming using GeoMedia, then it is possible that at some point you would need to determine the primary geometry field name by code. You can use the ExtendedPropertySet class to directly read the primary geometry field name of record sets generated through the originating pipe object. Failing that, you can loop through all the fields of the record set to find the fields of type gdbSpatial or gdbGraphic. An example C# method is shown below:


        public static string GetGeometryFieldName(PClient.GRecordset rs)
        {
            string geomFieldName = string.Empty;
            PClient.ExtendedPropertySet exPropSet;
            PClient.GField field = null;
 
            exPropSet = (PClient.ExtendedPropertySet)rs.GetExtension("ExtendedPropertySet");
            geomFieldName = (string)exPropSet.GetValue("PrimaryGeometryFieldName");
 
            if (geomFieldName.Length == 0)
            {
                foreach (PClient.GField fld in rs.GFields)
                {
                    if (fld.Type == GDO.GConstants.gdbSpatial | fld.Type == GDO.GConstants.gdbGraphic)
                    {
                        geomFieldName = fld.Name;
                        break;
                    }
                }
            }
            if (exPropSet != null) Marshal.FinalReleaseComObject(exPropSet);
            if (field != null) Marshal.FinalReleaseComObject(field);
            return geomFieldName;
 
        }

No comments: