我正在尝试重新投影EPSG中的WMS源:3395到EPSG:32661在OpenLayers 3.我有proj4js并且加载了投影定义但没有显示任何内容。
奇怪的是,甚至http://openlayers.org/en/v3.12.1/examples/reprojection-by-code.html的例子都没有起作用。
答案 0 :(得分:0)
我正在调试为什么会中断并发现将以下coord从32661转换为3857时会抛出第一个错误。您可以通过在控制台中运行以下JS来单独重新创建它,假设EPSG:32661 proj4js def已加载:
proj4("EPSG:32661", "EPSG:3857", [2000000.0000000005, 2000000])
- >符
然而,对于WGS84,
proj4("EPSG:32661", "EPSG:4326", [2000000.0000000005, 2000000])
- > [90,90]
我通过以下查询验证了proj.4本身的相同内容:
https://mygeodata.cloud/cs2cs/
我不知道这是好还是错误。有趣的是它发生在基本上是北极的地方,所以在这个极端点可能有一些舍入错误或数学错误?
答案 1 :(得分:0)
当期望包含x和y属性的对象时,OpenLayers调用的proj4代码处理空值时,将发生错误。根据所使用的proj4.js版本的不同,它可以发生在任何变换中,其中一个坐标在另一个投影中无效,但在转换为EPSG或从EPSG转换时似乎更可能少一些:4326 OpenLayers示例尝试通过设置范围来防止错误投影,但是如果要在国家投影中显示某个国家,但又要显示周围的国家(尽管有些失真),或者在UTM投影中包含相邻区域,则不希望这样做。我使用ol.proj.addCoordinateTransforms
来确保所有转换都通过EPSG:4326进行,这消除了大多数错误,并添加了try / catch来覆盖所有剩余的错误(尽管如果使用的是proj4.js最新版本2.5.0,try / catch catch似乎是多余的,即使该版本产生直接在其他投影之间转换的错误)。但是,即使使用最新的proj4.js,ol.proj.getPointResolution()
所需的从EPSG:2163到EPSG:4326的某些转换也会失败,从而无法在某些位置集中或缩小地图。幸运的是,在proj4中预定义了EPSG:4269,当它用作中间产品而不是EPSG:4326时,它允许对涉及EPSG:4326的错误进行尝试/捕获处理(除非应在使用EPSG:4326的情况下将投影投影到EPSG:4269或从EPSG:4269重新投影! )
这是对OpenLayers示例的重写,其中所有转换都是通过EPSG:4269进行的,并且对投影范围没有限制
function reprojectionErrorHandler (projections, opt_intermediate) {
var intermediate = opt_intermediate || 'EPSG:4269';
function transform (projA, projB) {
return function (input, opt_output, opt_dimension) {
var length = input.length;
var dimension = opt_dimension !== undefined ? opt_dimension : 2;
var output = opt_output !== undefined ? opt_output : new Array(length);
var ll, point, i, j;
try {
for (i = 0; i < length; i += dimension) {
ll = ol.proj.transform([input[i], input[i + 1]], projA, intermediate);
point = ol.proj.transform([ll[i], ll[i + 1]], intermediate, projB);
output[i] = point[0];
output[i + 1] = point[1];
for (j = dimension - 1; j >= 2; --j) {
output[i + j] = input[i + j];
}
}
} catch (e) {}
return output;
};
}
if (Array.isArray(projections)) {
for (i = 0; i < projections.length-1; i++) {
for (j = i+1; j < projections.length; j++) {
if (ol.proj.get(projections[i]).getCode() != ol.proj.get(projections[j]).getCode() &&
ol.proj.get(projections[i]).getCode() != ol.proj.get(intermediate).getCode() &&
ol.proj.get(projections[j]).getCode() != ol.proj.get(intermediate).getCode() ) {
ol.proj.addCoordinateTransforms(
projections[i],
projections[j],
transform(projections[i], projections[j]),
transform(projections[j], projections[i])
);
ol.proj.addCoordinateTransforms(
projections[j],
projections[i],
transform(projections[j], projections[i]),
transform(projections[i], projections[j])
);
}
}
}
}
}
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
projection: 'EPSG:3857',
center: [0, 0],
zoom: 1
})
});
var queryInput = document.getElementById('epsg-query');
var searchButton = document.getElementById('epsg-search');
var resultSpan = document.getElementById('epsg-result');
var renderEdgesCheckbox = document.getElementById('render-edges');
function setProjection(code, name, proj4def, bbox) {
if (code === null || name === null || proj4def === null || bbox === null) {
resultSpan.innerHTML = 'Nothing usable found, using EPSG:3857...';
map.setView(new ol.View({
projection: 'EPSG:3857',
center: [0, 0],
zoom: 1
}));
return;
}
resultSpan.innerHTML = '(' + code + ') ' + name;
var newProjCode = 'EPSG:' + code;
if (newProjCode != 'EPSG:4269') {
// 'EPSG:4269' is predefined in proj4 but not OL so proj4 must be registered if using OL5
proj4.defs(newProjCode, proj4def);
}
if (ol.proj.proj4 && ol.proj.proj4.register) { ol.proj.proj4.register(proj4); };
var newProj = ol.proj.get(newProjCode);
// var fromLonLat = ol.proj.getTransform('EPSG:4326', newProj);
if (newProjCode != 'EPSG:4326' && newProjCode != 'EPSG:3857') {
reprojectionErrorHandler(['EPSG:4326', newProj]);
reprojectionErrorHandler(['EPSG:3857', newProj]);
}
// very approximate calculation of projection extent
// var extent = ol.extent.applyTransform(
// [bbox[1], bbox[2], bbox[3], bbox[0]], fromLonLat);
// newProj.setExtent(extent);
var newView = new ol.View({
projection: newProj,
center: [0, 0],
zoom: 4
});
map.setView(newView);
// newView.fit(extent);
}
function search(query) {
resultSpan.innerHTML = 'Searching ...';
fetch('https://epsg.io/?format=json&q=' + query).then(function(response) {
return response.json();
}).then(function(json) {
var results = json['results'];
if (results && results.length > 0) {
for (var i = 0, ii = results.length; i < ii; i++) {
var result = results[i];
if (result) {
var code = result['code'], name = result['name'],
proj4def = result['proj4'], bbox = result['bbox'];
// if (code && code.length > 0 && proj4def && proj4def.length > 0 &&
// bbox && bbox.length == 4) {
setProjection(code, name, proj4def, bbox);
return;
// }
}
}
}
setProjection(null, null, null, null);
});
}
/**
* Handle click event.
* @param {Event} event The event.
*/
searchButton.onclick = function(event) {
search(queryInput.value);
event.preventDefault();
};
/**
* Handle change event.
*/
renderEdgesCheckbox.onchange = function() {
map.getLayers().forEach(function(layer) {
if (layer instanceof ol.layer.Tile) {
var source = layer.getSource();
if (source instanceof ol.source.TileImage) {
source.setRenderReprojectionEdges(renderEdgesCheckbox.checked);
}
}
});
};
html,
body {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
}
.map {
width: 100%;
}
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
<div id="map" class="map"></div>
<form class="form-inline">
<label for="epsg-query">Search projection:</label>
<input type="text" id="epsg-query" placeholder="4326, 27700, US National Atlas, Swiss, France, ..." class="form-control" size="50" />
<button id="epsg-search" class="btn">Search</button>
<span id="epsg-result"></span>
<div>
<label for="render-edges">
Render reprojection edges
<input type="checkbox" id="render-edges">
</label>
</div>
</form>