Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options">options obtained when convert b3dm data</param>
        /// <returns></returns>
        public static SingleTileset Create(TilesetCreationOptions options)
        {
            if (options == null)
            {
                options = new TilesetCreationOptions();
            }

            var longitude   = options.Longitude;
            var latitude    = options.Latitude;
            var minHeight   = options.MinHeight;
            var maxHeight   = options.MaxHeight;
            var transHeight = options.TransHeight;
            var tileWidth   = options.TileWidth;
            var tileHeight  = options.TileHeight;
            var offsetX     = options.OffsetX;
            var offsetY     = options.OffsetY;
            var upAxis      = options.GltfUpAxis ?? "Y";
            // properties
            var geometricError = options.GeometricError;
            var transformArray = options.Transfrom;

            if (transformArray == null || transformArray.Length == 0)
            {
                transformArray = GisUtil.Wgs84Transform(
                    longitude, latitude, transHeight).ToArray();
            }
            var height = maxHeight - minHeight;

            if (!(options.UseRegion || options.UseBox || options.UseSphere))
            {
                options.UseRegion = true;
            }
            BoundingVolume boundingVolume = null;

            if (options.UseRegion)
            {
                var longitudeExtent = GisUtil.MetersToLongitude(tileWidth, latitude);
                var latitudeExtent  = GisUtil.MetersToLatituide(tileHeight);

                var west = longitude - longitudeExtent / 2 +
                           offsetX / tileWidth * longitudeExtent;
                var south = latitude - latitudeExtent / 2 -
                            offsetY / tileHeight * latitudeExtent;
                var east = longitude + longitudeExtent / 2 +
                           offsetX / tileWidth * longitudeExtent;
                var north = latitude + latitudeExtent / 2 -
                            offsetY / tileHeight * latitudeExtent;

                boundingVolume = new BoundingVolume
                {
                    Region = new double[] { west, south, east, north, minHeight, maxHeight }
                };
            }
            else if (options.UseBox)
            {
                boundingVolume = new BoundingVolume
                {
                    Box = new double[] {
                        offsetX, -offsetY, height / 2 + minHeight, // center
                        tileWidth / 2, 0, 0,                       // width
                        0, tileHeight / 2, 0,                      // depth
                        0, 0, height / 2                           // height
                    }
                };
            }
            else if (options.UseSphere)
            {
                boundingVolume = new BoundingVolume
                {
                    Sphere = new double[]
                    {
                        offsetX, -offsetY, height / 2 + minHeight,
                        Math.Sqrt(tileWidth * tileWidth / 4 + tileHeight * tileHeight / 4 + height * height / 4)
                    }
                };
            }

            return(new SingleTileset
            {
                Asset = new TilesetAsset {
                    GltfUpAxis = upAxis
                },
                //Properties
                GeometricError = geometricError,
                Root = new Tile
                {
                    Transform = transformArray,
                    BoundingVolume = boundingVolume,
                    Content = new TileContent
                    {
                        Url = options.TileName
                    },
                    OriginalX = options.OriginalX,
                    OriginalY = options.OriginalY,
                    OriginalZ = options.OriginalZ
                }
            });
        }
Exemplo n.º 2
0
        private static TilesetCreationOptions WriteB3dm(Converter converter,
                                                        string outputFile, Options options = null)
        {
            if (options == null)
            {
                options = new Options();
            }


            var glb            = converter.GetGlb();
            var batchTableJson = converter.GetBatchTable();
            var length         = batchTableJson.MaxPoint.Count;
            var boundary       = 8;

            if (options.FeatureTableJson == null || options.FeatureTableJson.Count == 0)
            {
                if (options.FeatureTableJson == null)
                {
                    options.FeatureTableJson = new List <byte>();
                }
                var featureTable = new FeatureTable()
                {
                    BatchLength = (uint)length
                };
                var featureTableJsonBuffer =
                    Converter.GetJsonBufferPadded(featureTable, boundary, 28);
                options.FeatureTableJson.AddRange(featureTableJsonBuffer);
            }
            List <byte> featureTableBinary;

            if (options != null && options.FeatureTableBinary != null)
            {
                featureTableBinary = options.FeatureTableBinary;
            }
            else
            {
                featureTableBinary = new List <byte>();
            }
            var batchTableJsonBuffer = Converter.GetJsonBufferPadded(batchTableJson, boundary);

            options.BatchTableJson = batchTableJsonBuffer.ToList();
            List <byte> batchTableBinary;

            if (options != null && options.BatchTableBinary != null)
            {
                batchTableBinary = options.BatchTableBinary;
            }
            else
            {
                batchTableBinary = new List <byte>();
            }

            var b3dm  = new B3dm(glb);
            var bytes = b3dm.Convert(options);

            File.WriteAllBytes(outputFile, bytes);

            var tileFullname   = Path.GetFileName(outputFile);
            var folder         = Path.GetDirectoryName(outputFile);
            var tilesetPath    = Path.Combine(folder, "tileset.json");
            var tilesetOptions = new TilesetCreationOptions();
            //var batchTableJson = json;
            var minMaxX = new MinMax();

            Converter.UpdateMinMax(
                batchTableJson.MaxPoint.Select(c => c[0]).Concat(
                    batchTableJson.MinPoint.Select(c => c[0])).ToArray(), minMaxX);
            var minMaxY = new MinMax();

            Converter.UpdateMinMax(
                batchTableJson.MaxPoint.Select(c => c[1]).Concat(
                    batchTableJson.MinPoint.Select(c => c[1])).ToArray(), minMaxY);
            var minMaxZ = new MinMax();

            Converter.UpdateMinMax(
                batchTableJson.MaxPoint.Select(c => c[2]).Concat(
                    batchTableJson.MinPoint.Select(c => c[2])).ToArray(), minMaxZ);
            var width  = minMaxX.Max - minMaxX.Min;
            var height = minMaxZ.Max - minMaxZ.Min;

            width  = Math.Ceiling(width);
            height = Math.Ceiling(height);

            var offsetX = width / 2 + minMaxX.Min;
            var offsetY = height / 2 + minMaxZ.Min;

            tilesetOptions.TileName    = tileFullname;
            tilesetOptions.TileWidth   = width;
            tilesetOptions.TileHeight  = height;
            tilesetOptions.OriginalX   = minMaxX;
            tilesetOptions.OriginalY   = minMaxY;
            tilesetOptions.OriginalZ   = minMaxZ;
            tilesetOptions.TransHeight = -minMaxY.Min;
            tilesetOptions.MinHeight   = minMaxY.Min + tilesetOptions.TransHeight;
            tilesetOptions.MaxHeight   = minMaxY.Max + tilesetOptions.TransHeight;
            tilesetOptions.OffsetX     = offsetX;
            tilesetOptions.OffsetY     = offsetY;

            return(tilesetOptions);
        }