Beispiel #1
0
        protected bool build(BuildLayer layer)
        {
            string work_dir_name = project.getAbsoluteWorkingDirectory();

            if (string.IsNullOrEmpty(work_dir_name))
            {
                work_dir_name = "work_" + project.getName();
            }

            string work_dir = PathUtils.combinePaths(
                project.getBaseURI(),
                work_dir_name);

            if (osgDB.makeDirectory(work_dir))
            {
                Registry.instance().setWorkDirectory(work_dir);
            }

            //osgGIS.notice() << "Building layer \"" << layer.getName() << "\"." << std.endl;

            // first create and initialize a Session that will share data across the build.
            Session session = new Session();

            // add shared scripts to the session:
            foreach (Script i in project.getScripts())
            {
                session.addScript(i);
            }

            // add shared resources to the session:
            foreach (Resource i in project.getResources())
            {
                session.getResources().addResource(i);
            }

            // now establish the source data record form this layer and open a feature layer
            // that connects to that source.
            Source source = layer.getSource(); // default source.. may be overriden in slices

            //if ( !source )
            //{
            //    //TODO: log error
            //    osgGIS.notify( osg.WARN )
            //        << "No source specified for layer \"" << layer.getName() << "\"." << std.endl;
            //    return false;
            //}



            // recursively build any sources that need building.
            if (source && !build(source, session.get()))
            {
                // osgGIS.warn()
                //    << "Unable to build source \"" << source.getName() << "\" or one of its dependencies."
                //     << std.endl;
                return(false);
            }

            FeatureLayer feature_layer;

            if (source != null)
            {
                feature_layer = Registry.instance().createFeatureLayer(source.getAbsoluteURI());

                if (feature_layer == null)
                {
                    //TODO: log error
                    //osgGIS.warn()
                    //    << "Cannot access source \"" << source.getName()
                    //    << "\" for layer \"" << layer.getName() << "\"." << std.endl;
                    return(false);
                }
            }

            // The reference terrain:
            osg.Node         terrain_node;
            SpatialReference terrain_srs;
            GeoExtent        terrain_extent;

            Terrain terrain = layer.getTerrain();

            if (!getTerrainData(terrain, terrain_node, terrain_srs, terrain_extent))
            {
                return(false);
            }

            // output file:
            string output_file = layer.getAbsoluteTargetPath();

            osgDB.makeDirectoryForFile(output_file);
            if (!osgDB.fileExists(osgDB.getFilePath(output_file)))
            {
                //osgGIS.warn()
                //    << "Unable to establish target location for layer \""
                //    << layer.getName() << "\" at \"" << output_file << "\"."
                //    << std.endl;
                return(false);
            }

            // whether to include textures in IVE files:
            bool inline_ive_textures = layer.getProperties().getBoolValue("inline_textures", false);

            // TODO: deprecate this as we move towards the ResourcePackager...
            osgDB.ReaderWriter.Options options;
            if (inline_ive_textures)
            {
                options = new osgDB.ReaderWriter.Options("noWriteExternalReferenceFiles useOriginalExternalReferences");
            }
            else
            {
                options = new osgDB.ReaderWriter.Options("noTexturesInIVEFile noWriteExternalReferenceFiles useOriginalExternalReferences");
            }

            osgDB.Registry.instance().setOptions(options);


            osgDB.Archive archive;
            string        archive_file = output_file;

            if (osgDB.getLowerCaseFileExtension(output_file) == "osga")
            {
                archive     = osgDB.openArchive(output_file, osgDB.Archive.CREATE, 4096);
                output_file = "out.ive";

                // since there's no way to set the master file name...fake it out
                osg.Group dummy = new osg.Group();
                archive.writeNode(dummy, output_file);
            }

            // intialize a task manager if necessary:
            TaskManager manager =
                num_threads > 1? new TaskManager(num_threads) :
                num_threads < 1? new TaskManager() :
                null;



            // prep the map layer definition:
            MapLayer map_layer = new MapLayer();

            // a resource packager if necessary will copy ext-ref files to the output location:
            ResourcePackager packager = new ResourcePackager();

            packager.setArchive(archive.get());
            packager.setOutputLocation(osgDB.getFilePath(output_file));

            if (layer.getProperties().getBoolValue("localize_resources", false))
            {
                packager.setMaxTextureSize(layer.getProperties().getIntValue("max_texture_size", 0));
                packager.setCompressTextures(layer.getProperties().getBoolValue("compress_textures", false));
                packager.setInlineTextures(layer.getProperties().getBoolValue("inline_textures", false));
            }

            if (!addSlicesToMapLayer(layer.getSlices(), layer.getEnvProperties(), map_layer, packager, 0, session, source))
            {
                //osgGIS.warn() << "Failed to add all slices to layer " << layer.getName() << std.endl;
                return(false);
            }

            // calculate the grid cell size:
            double col_size = layer.getProperties().getDoubleValue("col_size", -1.0);
            double row_size = layer.getProperties().getDoubleValue("row_size", -1.0);

            if (col_size <= 0.0 || row_size <= 0.0)
            {
                int num_cols = Math.Max(1, layer.getProperties().getIntValue("num_cols", 1));
                int num_rows = Math.Max(1, layer.getProperties().getIntValue("num_rows", 1));
                col_size = map_layer.getAreaOfInterest().getWidth() / (double)num_cols;
                row_size = map_layer.getAreaOfInterest().getHeight() / (double)num_rows;
            }
            map_layer.setCellWidth(col_size);
            map_layer.setCellHeight(row_size);
            map_layer.setEncodeCellRadius(layer.getProperties().getBoolValue("encode_cell_radius", true));


            MapLayerCompiler compiler;

            // figure out which compiler to use:
            if (layer.getType() == BuildLayer.LayerType.TYPE_QUADTREE)
            {
                compiler = new QuadTreeMapLayerCompiler(map_layer, session);
            }
            else if (layer.getType() == BuildLayer.LayerType.TYPE_GRIDDED)
            {
                compiler = new GriddedMapLayerCompiler(map_layer.get(), session.);
            }
            else if (layer.getType() == BuildLayer.LayerType.TYPE_SIMPLE)
            {
                compiler = new SimpleMapLayerCompiler(map_layer, session);
            }

            if (compiler.get())
            {
                compiler.setAbsoluteOutputURI(output_file);
                compiler.setPaged(layer.getProperties().getBoolValue("paged", true));
                compiler.setTerrain(terrain_node.get(), terrain_srs, terrain_extent);
                compiler.setArchive(archive.get(), archive_file);
                compiler.setResourcePackager(packager.get());

                // build the layer and write the root file to output:
                osg.Group result = compiler.compile(manager.get());

                if (result != null)
                {
                    packager.packageNode(result.get(), output_file);
                }
            }

            if (archive != null)
            {
                archive.close();
            }

            //osgGIS.notice() << "Done building layer \"" << layer.getName() << "\"." << std.endl;

            return(true);
        }
Beispiel #2
0
        protected bool build(BuildLayer layer)
        {
            string work_dir_name = project.getAbsoluteWorkingDirectory();
            if ( string.IsNullOrEmpty(work_dir_name) )
            work_dir_name = "work_" + project.getName();

             string work_dir = PathUtils.combinePaths(
            project.getBaseURI(),
            work_dir_name );

            if ( osgDB.makeDirectory( work_dir ) )
            {
            Registry.instance().setWorkDirectory( work_dir );
            }

            //osgGIS.notice() << "Building layer \"" << layer.getName() << "\"." << std.endl;

            // first create and initialize a Session that will share data across the build.
             Session  session = new Session();

            // add shared scripts to the session:
            foreach( Script  i in project.getScripts())
            session.addScript( i );

            // add shared resources to the session:
            foreach( Resource i in project.getResources() )
            session.getResources().addResource( i );

            // now establish the source data record form this layer and open a feature layer
            // that connects to that source.
            Source  source = layer.getSource(); // default source.. may be overriden in slices
            //if ( !source )
            //{
            //    //TODO: log error
            //    osgGIS.notify( osg.WARN )
            //        << "No source specified for layer \"" << layer.getName() << "\"." << std.endl;
            //    return false;
            //}

            // recursively build any sources that need building.
            if ( source && !build( source, session.get() ) )
            {
               // osgGIS.warn()
               //    << "Unable to build source \"" << source.getName() << "\" or one of its dependencies."
               //     << std.endl;
            return false;
            }

             FeatureLayer  feature_layer;

            if ( source != null)
            {
            feature_layer = Registry.instance().createFeatureLayer( source.getAbsoluteURI() );

            if ( feature_layer == null )
            {
            //TODO: log error
            //osgGIS.warn()
            //    << "Cannot access source \"" << source.getName()
            //    << "\" for layer \"" << layer.getName() << "\"." << std.endl;
            return false;
            }
            }

            // The reference terrain:
             osg.Node         terrain_node;
             SpatialReference  terrain_srs;
            GeoExtent                      terrain_extent;

            Terrain  terrain = layer.getTerrain();
            if ( !getTerrainData( terrain, terrain_node, terrain_srs, terrain_extent ) )
            return false;

            // output file:
             string output_file = layer.getAbsoluteTargetPath();
            osgDB.makeDirectoryForFile( output_file );
            if ( !osgDB.fileExists( osgDB.getFilePath( output_file ) ) )
            {
            //osgGIS.warn()
            //    << "Unable to establish target location for layer \""
            //    << layer.getName() << "\" at \"" << output_file << "\"."
            //    << std.endl;
            return false;
            }

            // whether to include textures in IVE files:
            bool inline_ive_textures = layer.getProperties().getBoolValue( "inline_textures", false );

            // TODO: deprecate this as we move towards the ResourcePackager...
            osgDB.ReaderWriter.Options  options;
            if ( inline_ive_textures )
            {
            options = new osgDB.ReaderWriter.Options( "noWriteExternalReferenceFiles useOriginalExternalReferences" );
            }
            else
            {
            options = new osgDB.ReaderWriter.Options( "noTexturesInIVEFile noWriteExternalReferenceFiles useOriginalExternalReferences" );
            }

            osgDB.Registry.instance().setOptions( options );

            osgDB.Archive archive;
            string archive_file = output_file;

            if ( osgDB.getLowerCaseFileExtension( output_file ) == "osga" )
            {
            archive = osgDB.openArchive( output_file, osgDB.Archive.CREATE, 4096 );
            output_file = "out.ive";

            // since there's no way to set the master file name...fake it out
            osg.Group dummy = new osg.Group();
            archive.writeNode( dummy , output_file );
            }

            // intialize a task manager if necessary:
            TaskManager manager =
            num_threads > 1? new TaskManager( num_threads ) :
            num_threads < 1? new TaskManager() :
            null;

            // prep the map layer definition:
             MapLayer  map_layer = new MapLayer();

            // a resource packager if necessary will copy ext-ref files to the output location:
            ResourcePackager     packager = new ResourcePackager();
            packager.setArchive( archive.get() );
            packager.setOutputLocation( osgDB.getFilePath( output_file ) );

            if ( layer.getProperties().getBoolValue( "localize_resources", false ) )
            {
            packager.setMaxTextureSize( layer.getProperties().getIntValue( "max_texture_size", 0 ) );
            packager.setCompressTextures( layer.getProperties().getBoolValue( "compress_textures", false ) );
            packager.setInlineTextures( layer.getProperties().getBoolValue( "inline_textures", false ) );
            }

            if ( !addSlicesToMapLayer( layer.getSlices(), layer.getEnvProperties(), map_layer, packager, 0, session, source ) )
            {
            //osgGIS.warn() << "Failed to add all slices to layer " << layer.getName() << std.endl;
            return false;
            }

            // calculate the grid cell size:
            double col_size = layer.getProperties().getDoubleValue( "col_size", -1.0 );
            double row_size = layer.getProperties().getDoubleValue( "row_size", -1.0 );
            if ( col_size <= 0.0 || row_size <= 0.0 )
            {
            int num_cols = Math.Max( 1, layer.getProperties().getIntValue( "num_cols", 1 ) );
            int num_rows = Math.Max(1, layer.getProperties().getIntValue("num_rows", 1));
            col_size = map_layer.getAreaOfInterest().getWidth() / (double)num_cols;
            row_size = map_layer.getAreaOfInterest().getHeight() / (double)num_rows;
            }
            map_layer.setCellWidth( col_size );
            map_layer.setCellHeight( row_size );
            map_layer.setEncodeCellRadius( layer.getProperties().getBoolValue( "encode_cell_radius", true ) );

            MapLayerCompiler compiler;

            // figure out which compiler to use:
            if ( layer.getType() == BuildLayer.LayerType.TYPE_QUADTREE )
            {
            compiler = new QuadTreeMapLayerCompiler( map_layer, session );
            }
            else if (layer.getType() == BuildLayer.LayerType.TYPE_GRIDDED)
            {
            compiler = new GriddedMapLayerCompiler( map_layer.get(), session. );
            }
            else if (layer.getType() == BuildLayer.LayerType.TYPE_SIMPLE)
            {
            compiler = new SimpleMapLayerCompiler( map_layer, session );
            }

            if ( compiler.get() )
            {
            compiler.setAbsoluteOutputURI( output_file );
            compiler.setPaged( layer.getProperties().getBoolValue( "paged", true ) );
            compiler.setTerrain( terrain_node.get(), terrain_srs, terrain_extent );
            compiler.setArchive( archive.get(), archive_file );
            compiler.setResourcePackager( packager.get() );

            // build the layer and write the root file to output:
               osg.Group result = compiler.compile( manager.get() );

            if ( result != null )
            {
            packager.packageNode( result.get(), output_file );
            }
            }

            if ( archive != null )
            {
            archive.close();
            }

            //osgGIS.notice() << "Done building layer \"" << layer.getName() << "\"." << std.endl;

            return true;
        }