首页 > 解决方案 > 带孔的 GeoJson LineString 特征集合

问题描述

我有一长串坐标(由 GPS 传感器发送),代表资产的移动。

我正在使用传单渲染 GeoJSON,如果我将 LineString 渲染为单个特征,它可以正常工作,但如果我将它分解为多个特征(在 FeatureCollection 内 - 应用不同的动态颜色)我开始看到“洞“特征之间。

在此处输入图像描述

我很确定这是因为我收到的数据中实际上存在一个“漏洞”。但是为什么它作为一个单一的 LineString 功能工作呢?有没有办法来解决这个问题?

这是 GeoJSON 的摘录(非常大的对象)

对象的 866 个特征中有 3 个

{
          "type":"Feature",
          "properties":{
             "type":"traffic",
             "color":"#ffa600"
          },
          "geometry":{
             "type":"LineString",
             "coordinates":[
                [
                   7.583125,
                   45.0485616
                ],
                [
                   7.5830532999999996,
                   45.0485816
                ],
                [
                   7.58299,
                   45.0486133
                ],
                [
                   7.582893299999999,
                   45.0486066
                ],
                [
                   7.5828682999999995,
                   45.04859
                ]
             ]
          }
       },

链接到 bin

https://jsbin.com/nexijajake/edit?html,输出

具有单一特征的示例

https://jsbin.com/guqihajalu/1/edit?html,输出

标签: javascriptgeometrygeojsongeo

解决方案


实际上,渲染并没有错。您的data数组(在您的 jsbin 链接中)是一个不相互连接的线串数组。你有一个这样的模式(想象每一行都是一个线串):

[ A-B-C点]

[ D-E-F点]

为了使您的线连续,每个线串的最后一个点应作为第一个点存在于下一个线串中:

[ A-B-C点]

[ C-D-E-F点]

这样,您的线路将是连续的。

例如,以下示例(取自您的 jsbin)有一个空白:

const data = [
   {
      "type":"Feature",
      "properties":{
         "type":"traffic",
         "color":"#ffa600"
      },
      "geometry":{
         "type":"LineString",
         "coordinates":[
            [
               7.583125,
               45.0485616
            ],
            [
               7.5830532999999996,
               45.0485816
            ],
            [
               7.58299,
               45.0486133
            ],
            [
               7.582893299999999,
               45.0486066
            ],
            [
               7.5828682999999995,
               45.04859
            ]
         ]
      }
   },
   {
      "type":"Feature",
      "properties":{
         "type":"normal",
         "color":"#07e36a"
      },
      "geometry":{
         "type":"LineString",
         "coordinates":[
            [
               7.582795,
               45.0485149
            ],
            [
               7.582624999999999,
               45.0483233
            ],
            [
               7.581984899999999,
               45.047521599999996
            ]
         ]
      }
   }
];

间隙是固定的(第二个线串的第一个点是第一个线串的最后一个点):

const data = [
   {
      "type":"Feature",
      "properties":{
         "type":"traffic",
         "color":"#ffa600"
      },
      "geometry":{
         "type":"LineString",
         "coordinates":[
            [
               7.583125,
               45.0485616
            ],
            [
               7.5830532999999996,
               45.0485816
            ],
            [
               7.58299,
               45.0486133
            ],
            [
               7.582893299999999,
               45.0486066
            ],
            [
               7.5828682999999995,
               45.04859
            ]
         ]
      }
   },
   {
      "type":"Feature",
      "properties":{
         "type":"normal",
         "color":"#07e36a"
      },
      "geometry":{
         "type":"LineString",
         "coordinates":[
            //the first point here is the last of previous linestring
            [
               7.5828682999999995,
               45.04859
            ],
            [
               7.582795,
               45.0485149
            ],
            [
               7.582624999999999,
               45.0483233
            ],
            [
               7.581984899999999,
               45.047521599999996
            ]
         ]
      }
   }
];

推荐阅读