vector - Openlayers 3 重新投影 EPSG :4326 vector to EPSG:3857

标签 vector openlayers openlayers-3

我需要将 GeoJSON 矢量数据从 EPSG:4326 转换为 EPSG:3857 ...

我有 map ...

var olMapDiv = document.getElementById('olmap');
            control.map = new ol.Map({
                target: olMapDiv,
                renderer: 'canvas',
                layers: layers,
                interactions: ol.interaction.defaults({
                    altShiftDragRotate: false,
                    dragPan: false,
                    rotate: false
                }).extend([new ol.interaction.DragPan({ kinetic: null })]),
                pixelRatio: 1,
                loadTilesWhileAnimating: true,
                loadTilesWhileInteracting: true,
                view: view
            });

和 View ...
var view = new ol.View({
                // make sure the view doesn't go beyond the 22 zoom levels of Google Maps
                maxZoom: 21,
                projection: 'EPSG:3857',
                center: [0, 0],
                zoom: 0
            });

我定义了我的 geoJson 对象...
var geoJsonObj = {
                        'type': 'Feature',
                        'geometry': JSON.parse(shape),
                        'name': 'V',
                        'id': V.vID

                    }

我尝试将特征读入一个开放的层 Vector 对象并提供投影参数...
var vectorSource = new ol.source.Vector({
                        features: (new ol.format.GeoJSON()).readFeatures(geoJsonObj, {defaultDataProjection:"EPSG:4326",featureProjection:"EPSG:3857"})
                    });

然后我在新的 Vector 层中使用上面的“vectorSource”...
vectors = new ol.layer.Vector({                           
                        title: V.vID,
                        source: vectorSource,
                        id: V.vID,
                        name: 'V',
                        label: response.VList[key].Acres,
                        fill: response.VList[key].Shade,
                        stroke: defaultStrokeHex,
                        style: function (feature, resolution) {
                            var text = resolution * 100000 < 10 ? response.VList[key].Acres : '';

                            if (text != "") {
                                styleCache[text] = [new ol.style.Style({
                                    stroke: new ol.style.Stroke({
                                        color: '#319FD3',
                                        width: 1
                                    }),
                                    text: new ol.style.Text({
                                        font: '12px Calibri,sans-serif',
                                        text: text,
                                        fill: new ol.style.Fill({
                                            color: '#000'
                                        }),
                                        stroke: new ol.style.Stroke({
                                            color: '#fff',
                                            width: 3
                                        })
                                    }),
                                    fill: new ol.style.Fill({
                                        color: rcisWebMapUtilities.convertHex(response.VList[key].Shade, '0.5')
                                    })
                                })];
                            }
                            else if (text == "") {
                                styleCache[text] = [new ol.style.Style({
                                    fill: new ol.style.Fill({
                                        color: rcisWebMapUtilities.convertHex(response.VList[key].Shade, '0.5')
                                    })
                                })
                                ]
                            } return styleCache[text];
                        }


                    });

无论我做什么,我要么看到绘制的矢量...但在 EPSG:4326 或没有加载...

我花了太多时间试图弄清楚如何让 OpenLayers3 做到这一点......非常感谢任何帮助!

最佳答案

如果您在 View 中使用 EPSG:4326 那么您的 geojson 矢量声明应该是

var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject, { 
dataProjection: 'EPSG:4326',
featureProjection:'EPSG:4326' })
});

如果您在 View 中使用 EPSG:3857,请使用以下命令:
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject, { 
dataProjection: 'EPSG:4326',
featureProjection:'EPSG:3857' })
});

只是为了解释dataProjection是源坐标。表示 geojson 文件中坐标的 epsg。虽然 featureProjection是您 View 的 EPSG,因此是您的 map 的 EPSG。意思是应该转换EPSG原始坐标。

所以尽量记住这个规则:featureProjectionol.View投影声明应该相等。

请注意,我假设您的 geojson 坐标投影在 EPSG:4326 中。

关于vector - Openlayers 3 重新投影 EPSG :4326 vector to EPSG:3857,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44957431/

相关文章:

javascript - 如何设置wms参数(bbox、宽度、高度、x、y)

javascript - 不能将 openlayers-3 与 webpack 一起使用

javascript - 如何在Openlayers 3中显示ESRI矢量 basemap

openlayers-3 - Openlayers 3 读取扩展 GPX

java - 如何在java中找到两个 vector 之间的顺时针角度

c++ - 如何将 vector 拆分为 n 个 "almost equal"部分

C++ 排序错误 "No instance of overloaded function.."

C++ 转换指针 vector

PHP、geojson 和 openlayers

javascript - 如何在Leaflet中绘制天梭椭圆(天梭指示线)