Exemplo n.º 1
0
        public static string WriteB3dm(string path, B3dm b3dm)
        {
            var header_length = 28;

            b3dm.B3dmHeader.ByteLength = b3dm.GlbData.Length + header_length + b3dm.FeatureTableJson.Length + b3dm.BatchTableJson.Length + b3dm.BatchTableBinary.Length + b3dm.FeatureTableBinary.Length;
            b3dm.B3dmHeader.FeatureTableJsonByteLength   = b3dm.FeatureTableJson.Length;
            b3dm.B3dmHeader.BatchTableJsonByteLength     = b3dm.BatchTableJson.Length;
            b3dm.B3dmHeader.FeatureTableBinaryByteLength = b3dm.FeatureTableBinary.Length;
            b3dm.B3dmHeader.BatchTableBinaryByteLength   = b3dm.BatchTableBinary.Length;

            var fileStream   = File.Open(path, FileMode.Create);
            var binaryWriter = new BinaryWriter(fileStream);

            binaryWriter.Write(b3dm.B3dmHeader.AsBinary());
            binaryWriter.Write(Encoding.UTF8.GetBytes(b3dm.FeatureTableJson));
            if (b3dm.FeatureTableBinary != null)
            {
                binaryWriter.Write(b3dm.FeatureTableBinary);
            }
            binaryWriter.Write(Encoding.UTF8.GetBytes(b3dm.BatchTableJson));
            if (b3dm.BatchTableBinary != null)
            {
                binaryWriter.Write(b3dm.BatchTableBinary);
            }
            binaryWriter.Write(b3dm.GlbData);
            binaryWriter.Flush();
            binaryWriter.Close();
            return(fileStream.Name);
        }
Exemplo n.º 2
0
        static void Pack(PackOptions o)
        {
            Console.WriteLine($"Action: Pack");
            Console.WriteLine($"Input: {o.Input}");
            var f         = File.ReadAllBytes(o.Input);
            var batchFile = Path.GetFileNameWithoutExtension(o.Input) + ".batch";
            var b3dm      = new B3dm.Tile.B3dm(f);

            if (File.Exists(batchFile))
            {
                Console.WriteLine($"Input batch file: {batchFile}");
                var batchTableJson = File.ReadAllLines(batchFile);
                b3dm.FeatureTableJson = batchTableJson[0];
                b3dm.BatchTableJson   = batchTableJson[1];
            }

            var b3dmfile = (o.Output == string.Empty ? Path.GetFileNameWithoutExtension(o.Input) + ".b3dm" : o.Output);

            if (File.Exists(b3dmfile) && !o.Force)
            {
                Console.WriteLine($"File {b3dmfile} already exists. Specify -f or --force to overwrite existing files.");
            }
            else
            {
                B3dmWriter.WriteB3dm(b3dmfile, b3dm);
                Console.WriteLine("B3dm created " + b3dmfile);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Sample conversion from glb to b3dm.");
            var inputfile   = @"testfixtures/building.glb";
            var buildingGlb = File.ReadAllBytes(inputfile);
            var b3dm        = new B3dm.Tile.B3dm(buildingGlb);

            B3dmWriter.WriteB3dm($"building.b3dm", b3dm);
            Console.WriteLine($"File building.b3dm is written...");
            Console.WriteLine($"Press any key to continue...");
            Console.ReadKey();
        }
Exemplo n.º 4
0
        private static void WriteTiles(NpgsqlConnection conn, string geometryTable, string geometryColumn, string idcolumn, double[] translation, Node node, string outputPath, string colorColumn = "", string attributesColumn = "")
        {
            if (node.Features.Count > 0)
            {
                counter++;
                var subset     = (from f in node.Features select(f.Id)).ToArray();
                var geometries = BoundingBoxRepository.GetGeometrySubset(conn, geometryTable, geometryColumn, idcolumn, translation, subset, colorColumn, attributesColumn);

                var triangleCollection = GetTriangles(geometries);

                var bytes        = GlbCreator.GetGlb(triangleCollection);
                var b3dm         = new B3dm.Tile.B3dm(bytes);
                var featureTable = new FeatureTable();
                featureTable.BATCH_LENGTH = geometries.Count;
                b3dm.FeatureTableJson     = JsonConvert.SerializeObject(featureTable);

                if (attributesColumn != string.Empty)
                {
                    var batchtable    = new BatchTable();
                    var allattributes = new List <object>();
                    foreach (var geom in geometries)
                    {
                        // only take the first now....
                        allattributes.Add(geom.Attributes[0]);
                    }

                    var item = new BatchTableItem();
                    item.Name   = attributesColumn;
                    item.Values = allattributes.ToArray();
                    batchtable.BatchTableItems.Add(item);
                    var json = JsonConvert.SerializeObject(batchtable, new BatchTableJsonConverter(typeof(BatchTable)));
                    b3dm.BatchTableJson = json;
                }

                B3dmWriter.WriteB3dm($"{outputPath}/tiles/{node.Id}.b3dm", b3dm);
            }
            // and write children too
            foreach (var subnode in node.Children)
            {
                var perc = Math.Round(((double)counter / Counter.Instance.Count) * 100, 2);
                Console.Write($"\rProgress: tile {counter} - {perc.ToString("F")}%");
                WriteTiles(conn, geometryTable, geometryColumn, idcolumn, translation, subnode, outputPath, colorColumn, attributesColumn);
            }
        }
Exemplo n.º 5
0
        public static B3dm ReadB3dm(BinaryReader reader)
        {
            var b3dmHeader        = new B3dmHeader(reader);
            var featureTableJson  = Encoding.UTF8.GetString(reader.ReadBytes(b3dmHeader.FeatureTableJsonByteLength));
            var featureTableBytes = reader.ReadBytes(b3dmHeader.FeatureTableBinaryByteLength);
            var batchTableJson    = Encoding.UTF8.GetString(reader.ReadBytes(b3dmHeader.BatchTableJsonByteLength));
            var batchTableBytes   = reader.ReadBytes(b3dmHeader.BatchTableBinaryByteLength);

            var glbLength = b3dmHeader.ByteLength - b3dmHeader.Length;
            var glbBuffer = reader.ReadBytes(glbLength);

            var b3dm = new B3dm {
                B3dmHeader         = b3dmHeader,
                GlbData            = glbBuffer,
                FeatureTableJson   = featureTableJson,
                FeatureTableBinary = featureTableBytes,
                BatchTableJson     = batchTableJson,
                BatchTableBinary   = batchTableBytes
            };

            return(b3dm);
        }
Exemplo n.º 6
0
        public static B3dm ReadB3dm(Stream stream)
        {
            using (var reader = new BinaryReader(stream)) {
                var b3dmHeader        = new B3dmHeader(reader);
                var featureTableJson  = Encoding.UTF8.GetString(reader.ReadBytes(b3dmHeader.FeatureTableJsonByteLength));
                var featureTableBytes = reader.ReadBytes(b3dmHeader.FeatureTableBinaryByteLength);
                var batchTableJson    = Encoding.UTF8.GetString(reader.ReadBytes(b3dmHeader.BatchTableJsonByteLength));
                var batchTableBytes   = reader.ReadBytes(b3dmHeader.BatchTableBinaryByteLength);

                var glbLength = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
                var glbBuffer = reader.ReadBytes(glbLength);

                var b3dm = new B3dm {
                    B3dmHeader         = b3dmHeader,
                    GlbData            = glbBuffer,
                    FeatureTableJson   = featureTableJson,
                    FeatureTableBinary = featureTableBytes,
                    BatchTableJson     = batchTableJson,
                    BatchTableBinary   = batchTableBytes
                };
                return(b3dm);
            }
        }
Exemplo n.º 7
0
        private static void WriteTiles(NpgsqlConnection conn, string geometryTable, string geometryColumn, double[] translation, Node node, string outputPath, string colorColumn = "")
        {
            if (node.Features.Count > 0)
            {
                counter++;
                var subset     = (from f in node.Features select(f.Id)).ToArray();
                var geometries = BoundingBoxRepository.GetGeometrySubset(conn, geometryTable, geometryColumn, translation, subset, colorColumn);

                var triangleCollection = Triangulator.GetTriangles(geometries);

                var bytes        = GlbCreator.GetGlb(triangleCollection);
                var b3dm         = new B3dm.Tile.B3dm(bytes);
                var featureTable = new FeatureTable();
                featureTable.BATCH_LENGTH = geometries.Count;
                b3dm.FeatureTableJson     = JsonConvert.SerializeObject(featureTable);

                var batchTable = new BatchTable();

                var r       = new Random();
                var heights = new List <float>();
                for (var i = 0; i < geometries.Count; i++)
                {
                    heights.Add(r.Next(100));
                }
                batchTable.Height   = heights.ToArray();
                b3dm.BatchTableJson = JsonConvert.SerializeObject(batchTable);
                B3dmWriter.WriteB3dm($"{outputPath}/tiles/{node.Id}.b3dm", b3dm);
            }
            // and write children too
            foreach (var subnode in node.Children)
            {
                var perc = Math.Round(((double)counter / Counter.Instance.Count) * 100, 2);
                Console.Write($"\rProgress: tile {counter} - {perc.ToString("F")}%");
                WriteTiles(conn, geometryTable, geometryColumn, translation, subnode, outputPath, colorColumn);
            }
        }
Exemplo n.º 8
0
 public void WriteB3dm()
 {
     var buildingGlb = File.ReadAllBytes(@"1.glb");
     var b3dm        = new B3dm.Tile.B3dm(buildingGlb);
     var res         = b3dm.ToBytes();
 }