python实现矢量分级渲染_OpenLayer3实现分级渲染(初级版本)

ol2支持要素图层加载的时候进行各种渲染,例如唯一值、分级等,但是到OpenLayer3则不能进行这些操作,不知道为何。今天看官方文档的时候发现Feature类一个方法→get()方法可以获取指定属性的值,需要给该方法传进去一个字符串属性,看到这我眼前一亮,这不就是各种渲染主要的方法,废话不多说进入正题:

一、思路

1、首先我们要用到的是样式函数styleFunction,这个函数用处非常大,我们可以通过它进行条件给要素添加样式,再者我们需要用到Feature类的get方法。

2、通过在styleFunction函数中,根据传入的feature,通过get方法获取指定的属性进行判断

二、核心 函数

var vector = new ol.layer.Vector({

source: new ol.source.Vector({

format: new ol.format.GeoJSON(),

url: 'http://localhost:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetFeature&typeNames=nyc_roads:nyc_roads&outputFormat=application/json&srsname=EPSG:4326'

}),

style: function (feature, resolution) {

console.log(feature.get("feat_code"));

var id = feature.get("feat_code");

var style = null;

if (id >= 2900) {

style = new ol.style.Style({

stroke: new ol.style.Stroke({

color: "red",

width: 3

})

});

}

else {

style = new ol.style.Style({

stroke: new ol.style.Stroke({

color: "blue",

width: 3

})

});

}

return [style]

}

});

这里get方法获取的是feat_code属性的值,然后进行判断,根据满足不同的条件进行不同样式的渲染,其实我们可以把这个函数再封装下,让用户输入要分级渲染的字段,渲染样式也可以让用户指定,该方式稍微修改就能进行唯一值渲染,简单操作就可以实现想要的效果。

三、全部代码

wfs demo

var vector = new ol.layer.Vector({

source: new ol.source.Vector({

format: new ol.format.GeoJSON(),

url: 'http://localhost:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetFeature&typeNames=nyc_roads:nyc_roads&outputFormat=application/json&srsname=EPSG:4326'

}),

style: function (feature, resolution) {

console.log(feature.get("feat_code"));

var id = feature.get("feat_code");

var style = null;

if (id >= 2900) {

console.log("zhixing");

style = new ol.style.Style({

stroke: new ol.style.Stroke({

color: "red",

width: 3

})

});

}

else {

style = new ol.style.Style({

stroke: new ol.style.Stroke({

color: "blue",

width: 3

})

});

}

return [style]

}

});

var map = new ol.Map({

layers: [new ol.layer.Tile({

source: new ol.source.OSM()

}), vector],

target: 'map',

view: new ol.View({

center: [-73.99710639567148, 40.742270050255556],

maxZoom: 19,

zoom: 14,

projection: 'EPSG:4326'

})

});

四、放张图

五、总结

最后效果就根据feat_code范围进行渲染,加载的WFS要素服务(geoserver发布的),这种做法虽然比较方便,但是可扩展性比较差,所以需要进一步封装,做到利用更大化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值