【问题标题】:OpenLayers 3: Offset stroke styleOpenLayers 3:偏移描边样式
【发布时间】:2019-12-16 16:10:45
【问题描述】:

我的 LineString 功能由一个笔划设置样式。其中一些特征代表两件事,所以除了最初的笔触颜色之外,我想在它旁边有一条不同颜色的平行线。如果ol/style/Stroke 有像ol/style/Image 这样的偏移属性,这将是一件很容易的事,但遗憾的是,它没有。如果我为这些特征创建了偏移几何,它们将不会从缩小的级别中可见。我有哪些选择?

【问题讨论】:

    标签: javascript openlayers-3


    【解决方案1】:

    您可以根据分辨率偏移几何图形

    var style = function(feature, resolution) {
    var colors = ['green', 'yellow', 'red'];
    var width = 4;
    var styles = [];
    for (var line = 0; line < colors.length; line++) {
        var dist = width * resolution * (line - (colors.length-1)/2);
        var geom = feature.getGeometry();
        var coords = [];
        var counter = 0;
        geom.forEachSegment(function(from, to) {
            var angle = Math.atan2(to[1] - from[1], to[0] - from[0]);
            var newFrom = [
                Math.sin(angle) * dist + from[0],
                -Math.cos(angle) * dist + from[1]
            ];
            var newTo = [
                Math.sin(angle) * dist + to[0],
                -Math.cos(angle) * dist + to[1]
            ];
            coords.push(newFrom);
            coords.push(newTo);
            if (coords.length > 2) {
                var intersection = math.intersect(coords[counter], coords[counter+1], coords[counter+2], coords[counter+3]);
                coords[counter+1] = (intersection) ? intersection : coords[counter+1];
                coords[counter+2] = (intersection) ? intersection : coords[counter+2];
                counter += 2;
            }
        });
        styles.push(
            new ol.style.Style({
                geometry: new ol.geom.LineString(coords),
                stroke: new ol.style.Stroke({
                    color: colors[line],
                    width: width
                })
            })
        );
    }
    return styles;
    };
    
    
    var raster = new ol.layer.Tile({
      source:  new ol.source.OSM() 
    });
    
    var source = new ol.source.Vector();
    
    var vector = new ol.layer.Vector({
      source: source,
      style: style
    });
    
    var map = new ol.Map({
      layers: [raster, vector],
      target: 'map',
      view: new ol.View({
    center: [-11000000, 4600000],
    zoom: 4
      })
    });
    
    map.addInteraction(new ol.interaction.Draw({
      source: source,
      type: 'LineString'
    }));
    html, body, .map {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
    }
    <link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet" />
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.4.1/math.min.js"></script>
    <div id="map" class="map"></div>

    相交测试可防止此类对锐角的影响

    对于要素几何的每个分段,样式函数计算平行分段,设置为相隔 4 像素,而不管分辨率(在这种情况下,线宽和间距相同)以原始分段为中心的样式几何(例如如果三种颜色,中心样式线将跟随原始样式线,外部样式线偏移 +/- 4,如果两种颜色,每条线将偏移 +/- 2) 最初平行线段的长度与原始线段相同,但需要在外角处增加或在内角处减小,因此math.intersect 用于找到它们应该连接以形成连续线的点。最后,对于每种颜色,将完整的线串用作颜色样式的几何图形,并返回包含每种样式的数组。

    这是第二个示例,其中每条偏移线都有自己的宽度和笔划图案,而不是简单的颜色和固定宽度。

    var white = [255, 255, 255, 1];
    var blue = [0, 153, 255, 1];
    var width = 3;
    
    var pointStyle = new ol.style.Style({
    image: new ol.style.Circle({
        radius: width * 2,
        fill: new ol.style.Fill({
            color: blue
        }),
        stroke: new ol.style.Stroke({
            color: white,
            width: width / 2
        })
    }),
    zIndex: Infinity
    });
    
    var style = function(feature, resolution) {
    var widths = [10, 10];
    var strokes = [
        [
            {
                width: 4,
                color: '#7af500'
             }, {
                 width: 4,
                 color: '#55aa00',
                 lineDash: [7,9]
             }
        ], [
            {
                width: 2,
                color: 'black'
             }, {
                 width: 8,
                 color: 'black',
                 lineDash: [2,14],
                 lineCap: 'butt'
             }
        ]
    ];
    var styles = [pointStyle];
    var totalWidth = 0
    for (var line = 0; line < widths.length; line++) {
        totalWidth += widths[line];
    }
    var width = 0;
    for (var line = 0; line < widths.length; line++) {
      var dist = (width + widths[line]/2 - totalWidth/2) * resolution;
      width += widths[line];
      var geom = feature.getGeometry();
      if (geom.forEachSegment) {
        var coords = [];
        var counter = 0;
        geom.forEachSegment(function(from, to) {
            var angle = Math.atan2(to[1] - from[1], to[0] - from[0]);
            var newFrom = [
                Math.sin(angle) * dist + from[0],
                -Math.cos(angle) * dist + from[1]
            ];
            var newTo = [
                Math.sin(angle) * dist + to[0],
                -Math.cos(angle) * dist + to[1]
            ];
            coords.push(newFrom);
            coords.push(newTo);
            if (coords.length > 2) {
                var intersection = math.intersect(coords[counter], coords[counter+1], coords[counter+2], coords[counter+3]);
                coords[counter+1] = (intersection) ? intersection : coords[counter+1];
                coords[counter+2] = (intersection) ? intersection : coords[counter+2];
                counter += 2;
            }
        });
        strokes[line].forEach( function(stroke) {
            styles.push(
                new ol.style.Style({
                    geometry: new ol.geom.LineString(coords),
                    stroke: new ol.style.Stroke(stroke)
                })
            );
        });
      }
    }
    return styles;
    };
    
    
    var raster = new ol.layer.Tile({
      source:  new ol.source.OSM() 
    });
    
    var source = new ol.source.Vector();
    
    var vector = new ol.layer.Vector({
      source: source,
      style: style
    });
    
    var map = new ol.Map({
      layers: [raster, vector],
      target: 'map',
      view: new ol.View({
    center: [-11000000, 4600000],
    zoom: 4
      })
    });
    
    map.addInteraction(new ol.interaction.Draw({
      source: source,
      style: style,
      type: 'LineString'
    }));
    html, body, .map {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
    }
    <link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet" />
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.4.1/math.min.js"></script>
    <div id="map" class="map"></div>

    【讨论】:

    • var intersection = math.intersect 行中的math 是什么?我猜你的意思不是Math,因为没有Math.intersect
    • 如果没有 if 块,它的效果非常好,谢谢。你能解释一下你的方法是什么,对于整个代码。再次感谢。
    • 感谢编辑解释if 块,但你能看一下主要部分的数学吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多