예제 #1
0
 public static Offset <Feature> CreateFeature(FlatBufferBuilder builder,
                                              ulong fid = 0,
                                              VectorOffset ring_countsOffset  = default(VectorOffset),
                                              VectorOffset ring_lengthsOffset = default(VectorOffset),
                                              VectorOffset lengthsOffset      = default(VectorOffset),
                                              VectorOffset coordsOffset       = default(VectorOffset),
                                              VectorOffset propertiesOffset   = default(VectorOffset))
 {
     builder.StartObject(6);
     Feature.AddFid(builder, fid);
     Feature.AddProperties(builder, propertiesOffset);
     Feature.AddCoords(builder, coordsOffset);
     Feature.AddLengths(builder, lengthsOffset);
     Feature.AddRingLengths(builder, ring_lengthsOffset);
     Feature.AddRingCounts(builder, ring_countsOffset);
     return(Feature.EndFeature(builder));
 }
예제 #2
0
        public static byte[] ToByteBuffer(IFeature feature, GeometryType geometryType, byte dimensions, IList <ColumnMeta> columns)
        {
            var builder      = new FlatBufferBuilder(4096);
            var go           = GeometryConversions.BuildGeometry(builder, feature.Geometry, geometryType, dimensions);
            var memoryStream = new MemoryStream();

            if (feature.Attributes != null && feature.Attributes.Count > 0 && columns != null)
            {
                var writer = new BinaryWriter(memoryStream, Encoding.UTF8);
                for (ushort i = 0; i < columns.Count(); i++)
                {
                    var column = columns[i];
                    var type   = column.Type;
                    var name   = column.Name;
                    if (!feature.Attributes.Exists(name))
                    {
                        continue;
                    }
                    var value = feature.Attributes[name];
                    if (value is null)
                    {
                        continue;
                    }
                    writer.Write(i);
                    switch (type)
                    {
                    case ColumnType.Bool:
                        writer.Write((bool)value);
                        break;

                    case ColumnType.Int:
                        writer.Write((int)value);
                        break;

                    case ColumnType.Long:
                        writer.Write((long)value);
                        break;

                    case ColumnType.Double:
                        writer.Write((double)value);
                        break;

                    case ColumnType.String:
                        var bytes = Encoding.UTF8.GetBytes((string)value);
                        writer.Write(bytes.Length);
                        writer.Write(bytes);
                        break;

                    default:
                        throw new ApplicationException("Unknown type " + value.GetType().FullName);
                    }
                }
            }

            VectorOffset?propertiesOffset = null;

            if (memoryStream.Position > 0)
            {
                propertiesOffset = Feature.CreatePropertiesVector(builder, memoryStream.ToArray());
            }

            Feature.StartFeature(builder);
            Feature.AddCoords(builder, go.coordsOffset.Value);
            if (go.lengthsOffset.HasValue)
            {
                Feature.AddLengths(builder, go.lengthsOffset.Value);
            }
            if (go.ringLengthsOffset.HasValue)
            {
                Feature.AddRingLengths(builder, go.ringLengthsOffset.Value);
            }
            if (go.ringCountsOffset.HasValue)
            {
                Feature.AddRingCounts(builder, go.ringCountsOffset.Value);
            }
            if (propertiesOffset.HasValue)
            {
                Feature.AddProperties(builder, propertiesOffset.Value);
            }
            var featureOffset = Feature.EndFeature(builder);

            builder.FinishSizePrefixed(featureOffset.Value);

            return(builder.DataBuffer.ToSizedArray());
        }