Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WrapX doesn't work When I use custom coordinate system BD09; #15844

Open
ajhxk opened this issue May 18, 2024 · 6 comments
Open

WrapX doesn't work When I use custom coordinate system BD09; #15844

ajhxk opened this issue May 18, 2024 · 6 comments

Comments

@ajhxk
Copy link

ajhxk commented May 18, 2024

When I use the custom coordinate system BD09, the WrapX configuration does not take effect, and it cannot be dragged infinitely

openlayer version: 6.1.0

当我使用自定义百度的BD09坐标系,发现不能无限制的拖动至全球范围,

@mike-000
Copy link
Contributor

Using the example in #10374 (comment) you would need to make the projection global for the view to wrap. However the Baidu tiles overflow the projection extent and do not render correctly when the view wraps if the source projection is set as global. To overcome that you must define two projections https://codesandbox.io/s/xyz-forked-hj52y7?file=/index.js

@ajhxk
Copy link
Author

ajhxk commented May 20, 2024

Using the example in #10374 (comment) you would need to make the projection global for the view to wrap. However the Baidu tiles overflow the projection extent and do not render correctly when the view wraps if the source projection is set as global. To overcome that you must define two projections https://codesandbox.io/s/xyz-forked-hj52y7?file=/index.js

感谢回复, 我需要定义两个坐标系是嘛?
Thanks for the reply, I need to define two coordinate systems, right?

但是下面这个地址 我不能访问成功
But below this address, I can't access it successfully

https://codesandbox.io/s/xyz-forked-hj52y7?file=/index.js

@mike-000
Copy link
Contributor

import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import Tile from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import TileGrid from "ol/tilegrid/TileGrid";
import {
  Projection,
  addProjection,
  addCoordinateTransforms,
  transform
} from "ol/proj";
import projzh from "projzh";

var bd09Extent = [-20037726.37, -12474104.17, 20037726.37, 12474104.17];

var baiduMercator = new Projection({
  code: "baidu",
  extent: bd09Extent,
  units: "m",
});
addProjection(baiduMercator);
addCoordinateTransforms(
  "EPSG:4326",
  baiduMercator,
  projzh.ll2bmerc,
  projzh.bmerc2ll
);
addCoordinateTransforms(
  "EPSG:3857",
  baiduMercator,
  projzh.smerc2bmerc,
  projzh.bmerc2smerc
);

var baiduMercatorG = new Projection({
  code: "baiduG",
  extent: bd09Extent,
  units: "m",
  global: true,
});
addProjection(baiduMercatorG);
addCoordinateTransforms(
  "EPSG:4326",
  baiduMercatorG,
  projzh.ll2bmerc,
  projzh.bmerc2ll
);
addCoordinateTransforms(
  "EPSG:3857",
  baiduMercatorG,
  projzh.smerc2bmerc,
  projzh.bmerc2smerc
);

var bmercResolutions = new Array(19);
for (var i = 0; i < 19; ++i) {
  bmercResolutions[i] = Math.pow(2, 18 - i);
}

var urls = [0, 1, 2, 3].map(function(sub) {
  return (
    "http://maponline" +
    sub +
    ".bdimg.com/tile/?qt=vtile&x={x}&y={y}&z={z}&styles=pl&scaler=1&udt=20191119"
  );
});
var baiduSource = new XYZ({
  projection: baiduMercator,
  wrapX: true,
  url:
    "http://maponline{0-3}.bdimg.com/tile/?qt=vtile&x={x}&y={y}&z={z}&styles=pl&scaler=1&udt=20191119",
  tileGrid: new TileGrid({
    minZoom: 3,
    resolutions: bmercResolutions,
    origin: [0, 0],
    extent: bd09Extent
  })
});
var xyzTileUrlFunction = baiduSource.getTileUrlFunction();
var tmsTileUrlFunction = function([z, x, y]) {
  return xyzTileUrlFunction([z, x, -y - 1]);
};
baiduSource.setTileUrlFunction(tmsTileUrlFunction);
var baidu = new Tile({
  source: baiduSource
});
new Map({
  target: "map",
  layers: [baidu],
  view: new View({
    center: transform([121.51, 31.55], "EPSG:4326", "baidu"),
    zoom: 1,
    projection: baiduMercatorG
  })
});

The same example using StackBlitz https://stackblitz.com/edit/js-23bvsn?file=index.js,package.json,index.html

@ajhxk
Copy link
Author

ajhxk commented May 20, 2024

var bd09Extent = [-20037726.37, -12474104.17, 20037726.37, 12474104.17];

Thank you very much, tested to work fine

@ajhxk ajhxk closed this as completed May 20, 2024
@ajhxk
Copy link
Author

ajhxk commented Jun 4, 2024

@mike-000 你好 我按照上述方法 添加了个 额外的 baiduG 的坐标系,并且在初始化 view的时候 使用的是BD09G, 现在遇到一个问题 我们在缩放或者移动地图的时候 会出现 空白区域,且我看了network下瓦片请求没有出现404或者异常的情况。但当我们将view下使用的坐标系改成 BD09 则不会有此问题 。我观察了 BD09与BD09G的差异 就在与 global 配置。 请问出现这种情况 有合适的解决方案嘛 ?
下方是 简要代码:

const createBD09 = () => {
  // 创建百度墨卡托坐标系
  const baiduMercator = new Projection({
    code: BD09,
    units: 'm',
    extent: bd09Extent
  })
  // 添加百度墨卡托坐标系
  addProjection(baiduMercator)
  // 添加百度墨卡托坐标系与WGS84坐标系之间的转换
  addCoordinateTransforms(
    'EPSG:4326',
    baiduMercator,
    projzh.ll2bmerc,
    projzh.bmerc2ll
  )
  // 添加百度墨卡托坐标系与墨卡托坐标系之间的转换
  addCoordinateTransforms(
    'EPSG:3857',
    baiduMercator,
    projzh.smerc2bmerc,
    projzh.bmerc2smerc
  )
}
const createBD09G = () => {
    // 创建百度墨卡托坐标系
    const baiduMercatorG = new Projection({
      code: BD09G,
      units: 'm',
      extent: bd09Extent,
      global: true
    })
    // 添加百度墨卡托坐标系
    addProjection(baiduMercatorG)
    // 添加百度墨卡托坐标系与WGS84坐标系之间的转换
    addCoordinateTransforms(
      'EPSG:4326',
      baiduMercatorG,
      projzh.ll2bmerc,
      projzh.bmerc2ll
    )
    // 添加百度墨卡托坐标系与墨卡托坐标系之间的转换
    addCoordinateTransforms(
      'EPSG:3857',
      baiduMercatorG,
      projzh.smerc2bmerc,
      projzh.bmerc2smerc
    )
}
function getProjectionByOptions(options) {
  let gisEngine = options.gisEngine;
  let size = options.size;
  let projection = 'EPSG:3857';
  switch (gisEngine) {
  case Default.gisEngineKey.BAIDU_ONLINE:
  case Default.gisEngineKey.BAIDU_OFFLINE:
    projection = BD09G;
    // projection = BD09;
    break;
  case Default.gisEngineKey.BITMAP:
    projection = new Projection({
      code: 'xkcd-image',
      unit: 'pixels',
      extent: [0, 0, size.w, size.h]
    });
    break;
  default:
    break;
  }
  return projection;
}
....
  view = new View({
    center,
    minZoom,
    extent: extent ? extent : undefined,
    zoom,
    maxZoom,
    projection: getProjectionByOptions(params),
    constrainResolution: true,
    smoothResolutionConstraint: false
  });

@mike-000
Copy link
Contributor

mike-000 commented Jun 5, 2024

The source projection should not be global, the view projection must be global. Source projection BD09 and view projection BD09G works well.

Source projection BD09 and view projection EPSG:3857 is not so good.
image
That might be because the two projections have a slightly offset antemeridian. transform([[-180, 0], "EPSG:4326", "BD09") returns [-20037002.787653632, 603.7006072154708] which is slightly offset from the BD09 projection extent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants