[OpenLayers-Users] How to access style properties of features?

Andreas Hocevar ahocevar at opengeo.org
Mon Dec 21 09:35:53 EST 2009


Hi,

Peter.Sykora at ooe.gv.at wrote:
> I use a styleMap that contains a user style, so I thought that this style should be accessible.
>   

Ah, I see. This is not the case. The styleMap does not write style 
properties on the feature.

> The strange thing for me is that with my custom functions e.g.: getChartURL or getSizeH I am able to set and influence the styling prameters but afterwards I´m not able to access their values.  
>   

This is expected behavior.

> var template = {
> 	fillOpacity: "${getOpacity}",
> 	externalGraphic: "${getChartURL}",
> 	graphicWidth: "${getSizeW}",
> 	graphicHeight: "${getSizeH}",
> 	strokeWidth: 0,
> 	graphicXOffset: "${getXOffset}",
> 	graphicYOffset: "${getYOffset}"
> };
> style = new OpenLayers.Style(template, {context: context});
> styleMap = new OpenLayers.StyleMap({'default': style, 'select': {fillOpacity: 0.7}});
> 			
> With this style template I define the functions that calculate the styling parameters. E.g. the getSizeH function looks something like this:
>
> getSizeH: function(feature) {
> 	setDefaultValues(feature);
> 	var Sum = getSum(feature);
> 	var size = calcSize(feature,Sum);
> 	// Size for pin icons
> 	if (Sum==1) {
> 		if (feature.attributes.hover==1) {size=48} else {size=32}
> 	}
> 	return size;
> }
>
> This function works fine and sets the graphicHeight to the needed value. Somehow internally in OpenLayers this information must be stored, otherwise the features couldn´t be drawn correctly, but how can I access this information? I thought this should be in my user style 

This information is stored in the DOM of the renderer and not meant to 
be accessed.

> Maybe my problem is that the features themselfs never get a style assigned in my code.

Exactly.

>  They seem to get their styling information from the layers styleMap. But again the information about each individual symbols size in the map must be stored somewhere, but where??
>   

It is calculated from the styleMap when drawing the layer. You can 
access these properties like this. Let's say "layer" is your layer, and 
"feature" a feature you are interested in:

var symbolizer = layer.styleMap.createSymbolizer(feature, 
feature.renderIntent);

Now if you want to know your graphicWidth, you can access

symbolizer.graphicWidth

Note that createSymbolizer also takes the current scale into account for 
calculating the symbolizer. And don't expect this technique to help if 
you have a graphicHeight and want to know the graphicWidth. Some 
renderers do not need the other value, and some calculate it before 
rendering the feature, based on the size of the externalGraphic image.

Does this help?

Regards,
Andreas.

-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.




More information about the Users mailing list