Ejemplo n.º 1
0
        private static byte[] BuildHeader(ulong count, ulong featuresSize, IList <LayerMeta> layers, PackedHilbertRTree index)
        {
            // TODO: size might not be enough, need to be adaptive
            var builder = new FlatBufferBuilder(1024);

            // TODO: can be different per layer...
            var          columns       = layers.First().Columns;
            VectorOffset?columnsOffset = null;

            if (columns != null)
            {
                var columnsArray = columns
                                   .Select(c => Column.CreateColumn(builder, builder.CreateString(c.Name), c.Type))
                                   .ToArray();
                columnsOffset = Column.CreateSortedVectorOfColumn(builder, columnsArray);
            }

            var layerOffsets = layers
                               .Select(l =>
            {
                Layer.StartLayer(builder);
                if (columnsOffset.HasValue)
                {
                    Layer.AddColumns(builder, columnsOffset.Value);
                }
                Layer.AddGeometryType(builder, l.GeometryType);
                var layerOffset = Layer.EndLayer(builder);
                return(layerOffset);
            }).ToArray();

            var layersOffset = Header.CreateLayersVector(builder, layerOffsets);

            Header.StartHeader(builder);
            Header.AddLayers(builder, layersOffset);
            if (index != null)
            {
                Header.AddIndexNodesCount(builder, index.NumNodes);
            }
            Header.AddFeaturesCount(builder, count);
            Header.AddFeaturesSize(builder, featuresSize);
            var offset = Header.EndHeader(builder);

            builder.FinishSizePrefixed(offset.Value);

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