Example #1
0
        public static ObjectClassMsg ToObjectClassMsg([NotNull] ITable table, int classHandle,
                                                      bool includeFields = false,
                                                      string aliasName   = null)
        {
            esriGeometryType  geometryType = esriGeometryType.esriGeometryNull;
            ISpatialReference spatialRef   = null;

            if (table is IFeatureClass fc)
            {
                geometryType = fc.ShapeType;
                spatialRef   = DatasetUtils.GetSpatialReference(fc);
            }

            IWorkspace workspace = ((IDataset)table).Workspace;

            ObjectClassMsg result =
                new ObjectClassMsg()
            {
                Name             = DatasetUtils.GetName(table),
                ClassHandle      = classHandle,
                SpatialReference = ProtobufGeometryUtils.ToSpatialReferenceMsg(
                    spatialRef, SpatialReferenceMsg.FormatOneofCase.SpatialReferenceEsriXml),
                GeometryType    = (int)geometryType,
                WorkspaceHandle = workspace?.GetHashCode() ?? -1
            };

            if (aliasName == null)
            {
                aliasName = DatasetUtils.GetAliasName((IObjectClass)table);
            }

            CallbackUtils.DoWithNonNull(aliasName, s => result.Alias = s);

            if (includeFields)
            {
                List <FieldMsg> fieldMessages = new List <FieldMsg>();

                for (int i = 0; i < table.Fields.FieldCount; i++)
                {
                    IField field = table.Fields.Field[i];

                    fieldMessages.Add(ToFieldMsg(field));
                }

                result.Fields.AddRange(fieldMessages);
            }

            return(result);
        }
Example #2
0
        public static GdbObjectMsg ToGdbObjectMsg(
            [NotNull] IObject featureOrObject,
            [CanBeNull] IGeometry geometry,
            int objectClassHandle,
            SpatialReferenceMsg.FormatOneofCase spatialRefFormat =
            SpatialReferenceMsg.FormatOneofCase.SpatialReferenceWkid)
        {
            var result = new GdbObjectMsg();

            result.ClassHandle = objectClassHandle;

            result.ObjectId = featureOrObject.OID;

            result.Shape =
                ProtobufGeometryUtils.ToShapeMsg(geometry, ShapeMsg.FormatOneofCase.EsriShape,
                                                 spatialRefFormat);

            return(result);
        }
Example #3
0
        public static GdbObjectMsg ToGdbObjectMsg([NotNull] IObject featureOrObject,
                                                  bool includeSpatialRef  = false,
                                                  bool includeFieldValues = false)
        {
            var result = new GdbObjectMsg();

            result.ClassHandle = featureOrObject.Class.ObjectClassID;

            result.ObjectId = featureOrObject.OID;

            if (featureOrObject is IFeature feature)
            {
                IGeometry featureShape = GdbObjectUtils.GetFeatureShape(feature);

                result.Shape = ProtobufGeometryUtils.ToShapeMsg(
                    featureShape, ShapeMsg.FormatOneofCase.EsriShape,
                    includeSpatialRef
                                                ? SpatialReferenceMsg.FormatOneofCase.SpatialReferenceEsriXml
                                                : SpatialReferenceMsg.FormatOneofCase.SpatialReferenceWkid);
            }

            if (includeFieldValues)
            {
                for (int i = 0; i < featureOrObject.Fields.FieldCount; i++)
                {
                    IField field = featureOrObject.Fields.Field[i];

                    object valueObject = featureOrObject.Value[i];

                    var attributeValue = new AttributeValue();

                    result.Values.Add(attributeValue);

                    if (valueObject == DBNull.Value || valueObject == null)
                    {
                        attributeValue.DbNull = true;
                    }
                    else
                    {
                        switch (field.Type)
                        {
                        case esriFieldType.esriFieldTypeSmallInteger:
                            attributeValue.ShortIntValue = (int)valueObject;
                            break;

                        case esriFieldType.esriFieldTypeInteger:
                            attributeValue.LongIntValue = (int)valueObject;
                            break;

                        case esriFieldType.esriFieldTypeSingle:
                            attributeValue.ShortIntValue = (int)valueObject;
                            break;

                        case esriFieldType.esriFieldTypeDouble:
                            attributeValue.DoubleValue = (double)valueObject;
                            break;

                        case esriFieldType.esriFieldTypeString:
                            attributeValue.StringValue = (string)valueObject;
                            break;

                        case esriFieldType.esriFieldTypeDate:
                            attributeValue.DateTimeTicksValue = ((DateTime)valueObject).Ticks;
                            break;

                        case esriFieldType.esriFieldTypeOID:
                            attributeValue.ShortIntValue = (int)valueObject;
                            break;

                        case esriFieldType.esriFieldTypeGeometry:
                            // Leave empty, it is sent through Shape property
                            break;

                        case esriFieldType.esriFieldTypeBlob:
                            // TODO: Test and make this work
                            attributeValue.BlobValue =
                                ByteString.CopyFrom((byte[])valueObject);
                            break;

                        case esriFieldType.esriFieldTypeRaster:
                            // Not supported, ignore
                            break;

                        case esriFieldType.esriFieldTypeGUID:
                            byte[] asBytes = new Guid((string)valueObject).ToByteArray();
                            attributeValue.UuidValue =
                                new UUID {
                                Value = ByteString.CopyFrom(asBytes)
                            };
                            break;

                        case esriFieldType.esriFieldTypeGlobalID:
                            asBytes = new Guid((string)valueObject).ToByteArray();
                            attributeValue.UuidValue =
                                new UUID {
                                Value = ByteString.CopyFrom(asBytes)
                            };
                            break;

                        case esriFieldType.esriFieldTypeXML:
                            // Not supported, ignore
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
            }

            return(result);
        }