Skip to content

Text layer does not obey setMaxVisibleRange #2940

Description

@gwaldron

Discussed in #2938

Originally posted by bclay1297 March 18, 2026
When I add a shapefile to the scene using the code below the lines disapear based one the value passed to setMaxVisibleRange but the text layer does not disappear

int  VectorDataLayer::AddShapefileLayer2(std::string nodeName,
	std::string filename, std::string labelFieldName, std::string priority,
	float lineWidth, unsigned int lineColor)
{
	int status = -1;

	bool useDraping = false;
	bool useClamping = true;

	osgEarth::OGRFeatureSource* features = new osgEarth::OGRFeatureSource();

	if (features != NULL)
	{
		features->setName(nodeName);

		features->setURL(filename);

		osgEarth::Style style;

		osgEarth::LineSymbol* ls = style.getOrCreateSymbol<osgEarth::LineSymbol>();
		ls->stroke().mutable_value().color() = osgEarth::Color(lineColor);
		ls->stroke().mutable_value().width() = osgEarth::Distance(lineWidth, osgEarth::Units::PIXELS);
		ls->tessellationSize() = osgEarth::Distance(100, osgEarth::Units::KILOMETERS);

		if (useDraping)
		{
			osgEarth::AltitudeSymbol* alt = style.getOrCreate<osgEarth::AltitudeSymbol>();
			alt->clamping() = alt->CLAMP_TO_TERRAIN;
			alt->technique() = alt->TECHNIQUE_DRAPE;
		}
		else if (useClamping)
		{
			osgEarth::AltitudeSymbol* alt = style.getOrCreate<osgEarth::AltitudeSymbol>();
			alt->clamping() = alt->CLAMP_TO_TERRAIN;
			alt->technique() = alt->TECHNIQUE_GPU;

			ls->tessellationSize() = osgEarth::Distance(100, osgEarth::Units::KILOMETERS);

			osgEarth::RenderSymbol* render = style.getOrCreate<osgEarth::RenderSymbol>();
			render->depthOffset().mutable_value().automatic() = true;
		}

		auto* layer = new osgEarth::TiledFeatureModelLayer();
		layer->setFeatureSource(features);

		osgEarth::StyleSheet* styleSheet = new osgEarth::StyleSheet();

		styleSheet->addStyle(style);

		layer->setStyleSheet(styleSheet);

		layer->setMinVisibleRange(mMinLabelDisplayLevel);
		layer->setMaxVisibleRange(mMaxLabelDisplayLevel);

		mMapNode->getMap()->addLayer(layer);

		if (labelFieldName.length() > 0)
		{
			// set up symbology for drawing labels. We're pulling the label
			// text from the name attribute, and its draw priority from the
			// population attribute.
			osgEarth::Style labelStyle;

			osgEarth::TextSymbol* text = labelStyle.getOrCreateSymbol<osgEarth::TextSymbol>();
			text->content() = osgEarth::StringExpression(labelFieldName);
			text->priority() = atof(priority.c_str());
			text->size() = 16.0f;
			text->alignment() = osgEarth::TextSymbol::ALIGN_CENTER_CENTER;
			text->fill().mutable_value().color() = osgEarth::Color::White;
			text->halo().mutable_value().color() = osgEarth::Color::DarkGray;

			osgEarth::StyleSheet* sheet = new osgEarth::StyleSheet();
			sheet->addStyle(labelStyle);

			// and configure a model layer:
			auto* fml = new osgEarth::TiledFeatureModelLayer();
			fml->setName(nodeName + "_Labels");
			fml->setFeatureSource(features);
			fml->setStyleSheet(sheet);

			fml->setMinVisibleRange(mMinLabelDisplayLevel);
			fml->setMaxVisibleRange(mMaxLabelDisplayLevel);

			mMapNode->getMap()->addLayer(fml);
		}

		osgEarth::LayerVector layers;
		mMapNode->getMap()->getLayers(layers);
		for (osgEarth::LayerVector::const_iterator i = layers.begin(); i != layers.end(); ++i)
		{
			osgEarth::Layer* layer = i->get();
			if (layer->getOpenAutomatically() && !layer->isOpen())
			{
				OE_WARN << layer->getName() << " : " << layer->getStatus().toString() << std::endl;
			}
		}

	}

	return(status);
}

'

Metadata

Metadata

Assignees

No one assigned

    Labels

    workaroundBroken, but a reasonable workaround exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions