【openlayers】修改源码支持SLD的graphicfill属性
前段时间一群友有这样一个需求:在ol中画面,但面的填充物为图片,在使用sld样式时ol无法识别图片,经几番搜索原来官方版本暂不支持,可按如下方法解决
需要修改如下三个源码文件:
openlayers/lib/OpenLayers/Renderer/SVG.js
openlayers/lib/OpenLayers/Renderer/VML.js
openlayers/lib/OpenLayers/Format/SLD/v1.js
详情如下:‘+’后面是新增代码 ‘-’后面是删除代码
openlayers/lib/OpenLayers/Renderer/SVG.js /*** @requires OpenLayers/Renderer/Elements.js+ * @requires OpenLayers/Console.js*/setStyle: function(node, style, options) {- style = style || node._style;+ style = style || node._style;options = options || node._options;var r = parseFloat(node.getAttributeNS(null, "r"));var widthFactor = 1;}if (options.isFilled) {- node.setAttributeNS(null, "fill", style.fillColor);- node.setAttributeNS(null, "fill-opacity", style.fillOpacity);+ if (style.externalGraphic) {+ var pid = this.createImagePattern(style);+ node.setAttributeNS(null, "fill", "url(#" + pid + ")");+ node.setAttributeNS(null, "fill-opacity", 1);+ } else if (style.graphicName && node._geometryClass !== "OpenLayers.Geometry.Point") {+ //this can also happen if a rule based style applies to both points and other types of geometries. TODO: better handling of rule based styles!+ OpenLayers.Console.error('WellKnownName is not yet supported as GraphicFill by the SVG renderer!');+ //var pid = this.createMarkPattern(style);+ //node.setAttributeNS(null, "fill", "url(#" + pid + ")");+ //node.setAttributeNS(null, "fill-opacity", 1);+ } else {+ node.setAttributeNS(null, "fill", style.fillColor);+ node.setAttributeNS(null, "fill-opacity", style.fillOpacity);+ }} else {node.setAttributeNS(null, "fill", "none");}this.rendererRoot.appendChild(defs);return defs;},+ + /**+ * Method: createImagePattern+ *+ * Returns:+ * {String} The id of the created pattern+ */+ createImagePattern: function(style) {+ // reuse the pattern if the same externalGraphic with the same size has already been used+ var id = this.container.id + "-" + style.externalGraphic + ((style.pointRadius) ? "-" + style.pointRadius : "");+ var patternNode = OpenLayers.Util.getElement(id);+ if (!patternNode) {+ // to follow SLD spec we need to know image size+ // to get the image size we must load the image+ var img = new Image();++ img.onload = OpenLayers.Function.bind(function() {+ if (!this.defs) {+ // create svg defs tag+ this.defs = this.createDefs();+ }+ + // according to SLD specification image should be scaled by its inherent dimensions if no Size is given+ var height = img.height * 72 / 96;+ var width = img.width * 72 / 96;+ + // if Size is given, it is used as height and width is scaled to original aspect+ if (style.pointRadius) {+ var aspect = width / height;+ height = (style.pointRadius * 2) * 72 / 96;+ width = height * aspect;+ }+ + height = height + "pt";+ width = width + "pt";+ + patternNode = this.nodeFactory(id, "pattern");+ patternNode.setAttributeNS(null, "x", "0");+ patternNode.setAttributeNS(null, "y", "0");+ patternNode.setAttributeNS(null, "height", height);+ patternNode.setAttributeNS(null, "width", width);+ patternNode.setAttributeNS(null, "patternUnits", "userSpaceOnUse");+ + var imageNode = this.nodeFactory(null, "image");+ patternNode.appendChild(imageNode);+ imageNode.setAttributeNS(this.xlinkns, "href", style.externalGraphic);+ imageNode.setAttributeNS(null, "height", height);+ imageNode.setAttributeNS(null, "width", width);+ imageNode.setAttributeNS(null, "style", "opacity: " + (style.graphicOpacity || style.fillOpacity || 1));+ if (typeof style.rotation != "undefined") {+ var rotation = OpenLayers.String.format("rotate(${0})", [style.rotation]);+ imageNode.setAttributeNS(null, "transform", rotation);+ }+ this.defs.appendChild(patternNode);+ }, this);++ img.src = style.externalGraphic; + } + + return id; + }, + openlayers/lib/OpenLayers/Renderer/VML.js /*** @requires OpenLayers/Renderer/Elements.js+ * @requires OpenLayers/Console.js*/// fill if (options.isFilled) { - node.fillcolor = fillColor; + if (!style.externalGraphic) {+ node.fillcolor = fillColor;+ } else {+ node.fillcolor = "none";+ }} else { node.filled = "false"; }}fill.opacity = style.fillOpacity;- if (node._geometryClass === "OpenLayers.Geometry.Point" &&- style.externalGraphic) {-- // override fillOpacity- if (style.graphicOpacity) {- fill.opacity = style.graphicOpacity;- }- - fill.src = style.externalGraphic;- fill.type = "frame";- - if (!(style.graphicWidth && style.graphicHeight)) {- fill.aspect = "atmost";- } - }+ if (style.externalGraphic) {+ // reuse the fill node if the same externalGraphic with the same size has already been used+ if (fill.src != style.externalGraphic ||+ ((fill.size) ? parseFloat(fill.size.value.split(",")[1]): 0) != (style.pointRadius * 2) * 72 / 96) {+ // override fillOpacity+ if (style.graphicOpacity) {+ fill.opacity = style.graphicOpacity;+ }+ + fill.src = style.externalGraphic;+ fill.type = (node._geometryClass === "OpenLayers.Geometry.Point") ? "frame" : "tile";+ + // to follow SLD spec we need to know image size+ // to get the image size we must load the image+ var img = new Image();+ + img.onload = OpenLayers.Function.bind(function() {+ var height = img.height * 72 / 96;+ var width = img.width * 72 / 96;+ if (style.pointRadius) {+ var aspect = width / height;+ height = (style.pointRadius * 2) * 72 / 96;+ width = height * aspect;+ }+ fill.size = width + "pt," + height + "pt";+ });+ + // load the image+ img.src = style.externalGraphic;+ + if (!(style.graphicWidth && style.graphicHeight)) {+ fill.aspect = "atmost";+ }+ }+ } else if (style.graphicName && node._geometryClass !== "OpenLayers.Geometry.Point") {+ //this can also happen if a rule based style applies to both points and other types of geometries. TODO: better handling of rule based styles!+ OpenLayers.Console.error('WellKnownName is not yet supported as GraphicFill by the VML renderer!');+ }if (fill.parentNode != node) {node.appendChild(fill);}openlayers/lib/OpenLayers/Format/SLD/v1.js }}}, + "GraphicFill": function(node, symbolizer) { + symbolizer.pointRadius = null; + this.readChildNodes(node, symbolizer); + },"Graphic": function(node, symbolizer) {symbolizer.graphic = true;var graphic = {};"Fill": function(symbolizer) {var node = this.createElementNSPlus("sld:Fill");- // GraphicFill here - - // add in CssParameters - if(symbolizer.fillColor) { - this.writeNode( - "CssParameter", - {symbolizer: symbolizer, key: "fillColor"}, - node - ); - } - if(symbolizer.fillOpacity != null) { - this.writeNode( - "CssParameter", - {symbolizer: symbolizer, key: "fillOpacity"}, - node - ); - } + // if externalGraphic write a GraphicFill node to the Fill node + if (symbolizer.externalGraphic) { + this.writeNode("GraphicFill", symbolizer, node); + } else { + // add in CssParameters + if(symbolizer.fillColor) { + this.writeNode( + "CssParameter", + {symbolizer: symbolizer, key: "fillColor"}, + node + ); + } + if(symbolizer.fillOpacity != null) { + this.writeNode( + "CssParameter", + {symbolizer: symbolizer, key: "fillOpacity"}, + node + ); + } + }return node;},"PointSymbolizer": function(symbolizer) {this.writeNode("Graphic", symbolizer, node);return node;}, + "GraphicFill": function(symbolizer) { + var node = this.createElementNSPlus("sld:GraphicFill"); + this.writeNode("Graphic", symbolizer, node); + return node; + },"Graphic": function(symbolizer) {var node = this.createElementNSPlus("sld:Graphic");if(symbolizer.externalGraphic != undefined) {
详细示例见:http://dev.openlayers.org/sandbox/ossipoff/openlayers/examples/graphicfill.html
转载于:https://www.cnblogs.com/shitao/archive/2012/08/14/2636874.html
【openlayers】修改源码支持SLD的graphicfill属性相关推荐
- QT5.12.9 修改源码支持热插拔
QT5.12.9 修改源码支持热插拔 思路来自文章:Qt之支持usb触摸屏热插拔(Qt5.7) qevdevtouchhandler.cpp新添加的内容:165行,256行,435-451行 直接替换 ...
- FFmpeg修改源码支持H265
文章目录 一. 安装FFmpeg 二. 下载源码 三. 修改源码 四. 重新编译 五.执行 一. 安装FFmpeg 如果已经安装,建议先卸载. brew uninstall ffmpeg 然后用以下命 ...
- python模块cpca修改源码支持国外地址解析
cpca模块介绍: 该模块原本的功能是在地址字符串解析出省.市.县.如:输入字符串 "徐汇区虹漕路461号58号楼5楼",cpca可以给你输出:|省 |市 |区 |地址 |adco ...
- PHP微信公众平台源码 支持多账号 仿pigcms 小猪微信 已经修改完毕 完整可用
为什么80%的码农都做不了架构师?>>> PHP微信公众平台源码 支持多账号 仿pigcms 小猪微信 已经修改完毕 完整可用 从网上下载了个pigcms 公司用 但是发现很多问题, ...
- buildroot修改QT源码支持屏幕旋转
linuxfb屏幕旋转 前言 一.源码获取 二.修改源码 三.将补丁拷贝到指定目录 四.编译 五.添加环境变量 前言 事情是这样的,在开发板上使用Qt来做一些界面开发,但是我们是用的屏幕是800*12 ...
- py6s 光谱响应函数_Windows7 64位环境6sv2.1大气传输模型修改源码添加国产高分卫星GF-1 GF-2光谱响应支持...
下面开始添加国产卫星光谱响应的支持: 以下主要参考文章"6S大气传输模型修改源码添加.自定义卫星光谱响应(以HJ-1B CCD为例)"网址:http://blog.csdn.net ...
- 二开版优化新紫色UI云开发新款壁纸小程序源码支持用户投稿在线审核
本壁纸表情包头像小程序采用(dcloud云开发)所以无需服务器与域名 无需服务器.无需域名.云开发直接上线 特点:支持用户投稿,后台审核后会发订阅消息给用户提示作品审核状态,增加用户粘性,支持后端修改 ...
- PHP开发网易云FM音乐试听程序源码+支持下载功能
正文: PHP开发网易云FM音乐试听程序源码+支持下载功能,如果您有编程开发能力,可以自己修改源码中的代码,但是不要使用记事本修改,因为会造成编码和一些其它不可预料的错误. 如果你需要更换API接口, ...
- 最新ChatGPT商业运营版网站源码+支持AI绘画+支持用户会员套餐+邀请分佣功能+支持后台一键更新+网站后台管理+永久更新!
最新ChatGPT商业运营版网站源码+支持AI绘画+支持用户会员套餐+邀请分佣功能+支持后台一键更新+网站后台管理+永久更新! AI付费创作系统: 如果后续程序有新版,直接在后台一键更新即可! 程序完 ...
最新文章
- JVM 常用的基本配置有哪些?
- 让工作与(vue)音乐相伴
- UE4 集成讯飞听写插件
- java 反射api_Java学习笔记--反射API
- java socket如何请求485协议_javaSE第十五部分 网络编程(1)Socket和ServerSocket
- 【AI面试题】AlexNet、VGGNet、GoogLeNet,ResNet等网络之间的区别是什么
- python加法赋值运算符为_Python 运算符
- python 两阶段聚类_Python,如何对多元时间序列进行聚类?
- mac os 10.10 safari java插件_OS X 10.10 Yosemite强大而漂亮的Safari 8浏览器
- 能韬光养晦,是因为面前有苏联顶着,苏联没了就不可能了
- C1. Simple Polygon Embedding(计算几何)
- netty面试题及答案
- PHP2018人资面试题
- 采样频率和带宽的关系_真正专业DSP的灵魂3——采样率
- PAT 甲级 1118 Birds in Forest (25 分)
- ESP32入门之程序烧录:烧录错误总结
- 升级OSX High Sierra 10.13遇到一些问题及解决方法
- 长线、短线在现货黄金中是什么意思的专业术语
- 23 种设计模式的#羞羞#解释,听说能看懂的人都是泡妞高手
- 威尔特拉斯定理_数学大师启示录维尔斯特拉斯.pdf