现在的位置: 首页 > 综合 > 正文

geoserver动态修改地图样式sld

2013年03月27日 ⁄ 综合 ⁄ 共 4114字 ⁄ 字号 评论关闭

 

首先说一下自己的思想:

        我们用Udig提前写好一个样,大家都知道样式文件是xml文件,那我们在action中修改样式,在前台加载新添加的样式,这样不就可以动态修改样式了吗!

        action代码:(修改图层中栅格图的样式加一个颜色)

Action代码如下:

try {
	SAXReader saxReader = new SAXReader();
	Document document = saxReader.read(getClass().getClassLoader().getResourceAsStream("GISStat/sld-tasmania.xml"));
	List list = document.selectNodes("//sld:Rule/sld:PolygonSymbolizer/sld:Fill/sld:CssParameter" );
	Iterator iter=list.iterator();
	Integer i=0;
	while (iter.hasNext()) {
		Element element = (Element) iter.next();
		if(element.attributeValue("name").equals("fill")){
			element.setText(Rg[i][1].toString());
			i++;
		}
	}	
	XMLWriter output = new XMLWriter(new FileWriter(new File(
	Thread.currentThread().getContextClassLoader().getResource("sld.xml").getPath())));
	output.write(document);
	output.close();
} catch (Exception e) {
	// TODO: handle exception
	System.out.print(e.toString());
}

代码说明:sld-tasmania.xml是原来的样式,我们修改这个xml文件,生成一个新的sld.xml文件,我们在前台调用这个新生成sld样式文件,实现动态修改地图的样式:

 

sld-tasmania.xml样式表代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<sld:StyledLayerDescriptor version="1.0.0"
    xmlns:sld="http://www.opengis.net/sld"
    xmlns:ogc="http://www.opengis.net/ogc"
    xmlns:gml="http://www.opengis.net/gml"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
  <sld:NamedLayer>
    <sld:Name>zhengzhou:xianjie</sld:Name>
    <sld:UserStyle>
      <sld:Name>xianjie</sld:Name>
      <sld:Title>Default Styler</sld:Title>
      <sld:IsDefault>1</sld:IsDefault>
      <sld:FeatureTypeStyle>
        <sld:Name>group 0</sld:Name>
        <sld:FeatureTypeName>Feature</sld:FeatureTypeName>
        <sld:SemanticTypeIdentifier>generic:geometry</sld:SemanticTypeIdentifier>
        <sld:SemanticTypeIdentifier>colorbrewer:unique:ylgn</sld:SemanticTypeIdentifier>
		  <sld:Rule>
            <sld:Name>rule01</sld:Name>
            <sld:Title>410100</sld:Title>
            <ogc:Filter>
                <ogc:PropertyIsEqualTo>
                    <ogc:PropertyName>ADMINCODE</ogc:PropertyName>
                    <ogc:Literal>410100</ogc:Literal>
                </ogc:PropertyIsEqualTo>
            </ogc:Filter>
            <sld:PolygonSymbolizer>
                <sld:Fill>
                    <sld:CssParameter name="fill">#FFFFE5</sld:CssParameter>
                    <sld:CssParameter name="fill-opacity">0.5</sld:CssParameter>
                </sld:Fill>
                <sld:Stroke/>
            </sld:PolygonSymbolizer>
            <sld:TextSymbolizer>
                <sld:Label>
                    <ogc:PropertyName>NAME</ogc:PropertyName>
                </sld:Label>
                <sld:Font>
                    <sld:CssParameter name="font-family">宋体</sld:CssParameter>
                    <sld:CssParameter name="font-size">12.0</sld:CssParameter>
                    <sld:CssParameter name="font-style">normal</sld:CssParameter>
                    <sld:CssParameter name="font-weight">bold</sld:CssParameter>
                </sld:Font>
                <sld:LabelPlacement>
                    <sld:PointPlacement>
                        <sld:AnchorPoint>
                            <sld:AnchorPointX>
                                <ogc:Literal>0.5</ogc:Literal>
                            </sld:AnchorPointX>
                            <sld:AnchorPointY>
                                <ogc:Literal>0.5</ogc:Literal>
                            </sld:AnchorPointY>
                        </sld:AnchorPoint>
                        <sld:Displacement>
                            <sld:DisplacementX>
                                <ogc:Literal>0.0</ogc:Literal>
                            </sld:DisplacementX>
                            <sld:DisplacementY>
                                <ogc:Literal>0.0</ogc:Literal>
                            </sld:DisplacementY>
                        </sld:Displacement>
                        <sld:Rotation>
                            <ogc:Literal>0.0</ogc:Literal>
                        </sld:Rotation>
                    </sld:PointPlacement>
                </sld:LabelPlacement>
                <sld:Fill>
                    <sld:CssParameter name="fill">#000000</sld:CssParameter>
                </sld:Fill>
            </sld:TextSymbolizer>
        </sld:Rule>
      </sld:FeatureTypeStyle>
    </sld:UserStyle>
  </sld:NamedLayer>
</sld:StyledLayerDescriptor>

javascript代码如下:

	// making this a global variable so that it is accessible for
    	// debugging/inspecting in Firebug
    	var map = null;

        function init(){

        	format = 'image/png';
            var bounds = new OpenLayers.Bounds(
                    112.715, 34.263,
                    114.217, 34.989
                );
            var options = {
                controls: [],
                maxExtent: bounds,
                maxResolution: 0.0058671875,
                projection: "EPSG:2011",
                units: 'm'
            };
            map = new OpenLayers.Map('map',options);

            ol_wms = new OpenLayers.Layer.WMS(
                "OpenLayers WMS",
                "http://localhost:6001/geoserver/wms",
                {
                    width: '682',
                    layers: 'zhengzhou',
                    styles: '',
                    srs: 'EPSG:2011',
                    height: '330',
                    format: format,
                    tiled: 'true',
                    sld:http://localhost:8080/test/sld.xml                },
                {buffer: 0} 
            );

            map.addLayers([ol_wms]);
            map.addControl(new OpenLayers.Control.PanZoomBar({
                position: new OpenLayers.Pixel(2, 15)
            }));
            map.addControl(new OpenLayers.Control.Navigation());
            map.addControl(new OpenLayers.Control.Scale($('scale')));
            map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));
            map.zoomToExtent(bounds);

        }

javascript调用sld.xml(新生成的sld样式文件),这样就实现的动态修改地图的样式!

抱歉!评论已关闭.