【问题标题】:Openlayers Vector layer rendering coordinates outside typical boundariesOpenlayers 典型边界外的矢量图层渲染坐标
【发布时间】:2021-11-10 17:03:51
【问题描述】:

我开始使用矢量图层来在 OpenLayers 中呈现 SQL 空间数据。渲染this 示例时(请参阅Json),一切正常。见以下代码:

    //creates source to get json data from
let vSource = new VectorSource({
  url: '/assets/germany.json',
  format: new GeoJSON()
});
//set focus of the camera on this source
vSource.on('addfeature', () => {
  this.map.getView().fit(vSource.getExtent());
  console.log(this.map.getView().getCenter());
});
//add source to layer and afterwards load it into map
let vLayer = new VectorLayer({
  source: vSource,
  visible: true
});
//layer styling
vLayer.setStyle(new Style({
  stroke: new Stroke({
    color: '#FF5733',
    width: 8  
  })
}));
this.map.addLayer(vLayer);

地图实例化如下:

this.map = new Map({
  view: new View({
    center: [10, 10],
    zoom: 3,
  }),
  layers: [],
  target: 'ol-map'
});

但是当我想渲染this json 文件时,我面对的是一张空白地图。焦点不会设置,甚至不会出现错误。我假设这一切都与边界有关? 如果有“默认”之类的东西,如何渲染作为我们外部默认边界的多边形坐标?

例如:

[12058.4521484375, 5345.98388671875],
[11408.95703125, 5345.98388671875]

通读API我可以推断extent选项可能是解决这个问题的关键?

最好的问候
OpenLayers 的新手

【问题讨论】:

  • y 坐标在 -90 到 +90 之间,x 坐标值在 -180 到 +180 之外可能表明您的多边形处于一个包裹的世界中。您需要 wrapX: false 作为矢量源构造函数的选项才能看到它。如果 y 坐标超出该范围,则您有一个使用自定义投影的非标准 geojson。
  • 所以wrapX 将是 json 数据的解决方案,其中只有 x 坐标超出边界(尝试并工作,谢谢!),但我该如何解决自定义投影?是否可以创建具有自定义坐标系的 VectorLayer(例如:minY:-inf、minX:-inf、maxY:+inf、maxX:+inf)?
  • 使用自定义投影坐标的 GeoJSON 应该包含一个 crs 条目,如 openlayers.org/en/latest/examples/modify-test.html 但有时它们不包含,您只需做出最佳猜测。
  • 哦,好吧,我的 json 数据不是这样:/。你知道Is there a possiblity to create a VectorLayer which has a custom coordinate system(for example: minY: -inf, minX:-inf, maxY:+inf, maxX:+inf)?的答案吗
  • 这是可能的,例如,可能存在一个极投影,其中对极在所有方向上都是无限的。

标签: javascript openlayers geojson spatial


【解决方案1】:

Openlayers 默认投影是 EPSG:3857。我认为您的 geojson 和地图中心是 EPSG:4326。

this.map = new Map({
  view: new View({
    projection: 'EPSG:4326',
    center: [10, 10],
    zoom: 3,
  }),
  layers: [],
  target: 'ol-map'
});

let vSource = new VectorSource({
  url: '/assets/germany.json',
  format: new GeoJSON({ featureProjection: 'EPSG:3857' })
});

【讨论】:

  • 感谢您的帮助,但我不确定为什么投影应该是这种情况下的解决方案?此外,更改投影不会改变任何东西。m 请注意,在这种情况下,我们谈论的是默认边界之​​外的坐标(看看这些this 坐标。)
  • 调用map.getView().fit()时会重置中心和缩放
  • EPSG:4326中的点坐标怎么可能是这样的; 20527.55859375, 43.88205730390537?
  • @emincankirmizi 这是一个被包裹的世界上的意大利gist.github.com/fizzbuzz1337/fee5c83e553be4f09b37a98d92152dc7点击Raw查看坐标
  • @Mike 我知道fit() 会覆盖当前的缩放状态和查看位置,这不是问题。
猜你喜欢
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多