OpenLayers 3 reproject矢量图层(v4.3.2)

问题描述:

问候GIS爱好者和WEB制造商。我有一个关于预测一个问题,从EPSG更精确地重新投影:3765到EPSG:3857中的OpenLayers 4OpenLayers 3 reproject矢量图层(v4.3.2)

在下面的代码我试图从EPSG重新投影:3765到EPSG:3857:

proj4.defs(
     'EPSG:3765', 
     '+proj=tmerc +lat_0=0 +lon_0=16.5 +k=0.9999 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs' 
    ); 

var map = new ol.Map({ 
    view: new ol.View({ 
     zoom: 6, 
     center: ol.proj.transform([461152.65, 5108327.47], 'EPSG:3765', 'EPSG:3857') 
    }), 
    target: 'js-map', 
    layers: [ 
     new ol.layer.Tile({ 
      source: new ol.source.OSM() 
     }), 
     new ol.layer.Vector({ 
      source: new ol.source.Vector({ 
       url: 'test_1.geojson', 
       format: new ol.format.GeoJSON({ 
        dataProjection: 'EPSG:3765', 
        featureProjection: 'EPSG:3857' 
       }) 
      }) 

     }) 
    ] 
}); 

坐标中以GeoJSON文件以米,看起来像这样:

"coordinates": [ [ [ 461117.98, 5108379.85 ], [ 461124.53, 5108368.39 ], [ 461132.37, 5108354.26 ], [ 461141.13, 5108341.08 ... 

我成功呈现GeoJSON的,但在错误的位置,就像这样: enter image description here

我想它是在这里,像这样: enter image description here

什么我做错了?在geojson坐标格式中是否存在问题,或者我需要以某种方式重新设置geojson以便像在第二张图片中那样?

UPDATE

从这里如果u喜欢 https://www.dropbox.com/s/ih1bh8bj4zzgutc/test_1.geojson?dl=0

使用帕夫洛斯的例子,我编辑的几行,或者更准确地说,你可以下载GeoJSON的和测试;我想使用的代码

var geojsonObject = $.ajax({ 
    dataType: "json", 
    url: "test_1.geojson" 
}); 

此块装载本地以GeoJSON但我不断收到这种类型的错误:

Uncaught TypeError: (0 , Tm[a.type]) is not a function 

代码如下:

proj4.defs(
    'EPSG:3765', 
    '+proj=tmerc +lat_0=0 +lon_0=16.5 +k=0.9999 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs' 
); 

var geojsonObject = $.ajax({ 
    dataType: "json", 
    url: "test_1.geojson" 
    }); 
console.log(geojsonObject); 

var styles = { 
    'Polygon': new ol.style.Style({ 
     stroke: new ol.style.Stroke({ 
      color: 'red', 
      width: 5 
     }), 
     fill: new ol.style.Fill({ 
      color: 'rgba(255, 0, 0, 1)' 
     }) 
    }) 
}; 

var styleFunction = function(feature) { 
    return styles[feature.getGeometry().getType()]; 
}; 



var vectorSource = new ol.source.Vector({ 
    features: (new ol.format.GeoJSON()).readFeatures(geojsonObject, { 
     defaultDataProjection: ol.proj.get('EPSG:3765'), 
     featureProjection: 'EPSG:3857' 
    }) 
}); 
var vectorLayer = new ol.layer.Vector({ 
    source: vectorSource, 
    style: styleFunction 
}); 
var map = new ol.Map({ 
    layers: [ 
     new ol.layer.Tile({ 
      source: new ol.source.OSM() 
     }), 
     vectorLayer 
    ], 
    target: 'map', 
    controls: ol.control.defaults({ 
     attributionOptions: /** @type {olx.control.AttributionOptions} */ ({ 
      collapsible: false 
     }) 
    }), 
    view: new ol.View({ 
     center: ol.proj.transform([461117.98, 5108379.85], 'EPSG:3765', 'EPSG:3857'), 
     zoom: 12 
    }) 
}); 

更新3

我相信我更近一步,但仍然没有运气

我用jQuery和AJAX请求编辑了一下代码,并在loader属性中设置了函数。现在,代码看起来是这样的:

var vectorLayer = new ol.layer.Vector({ 
    source: new ol.source.Vector({ 
     loader: function() { 
      $.ajax({ 
       type: 'GET', 
       url:'test_1.geojson', 
       context: this 
      }).done(function(data){ 
       var format = new ol.format.GeoJSON(); 
       this.addFeatures(format.readFeatures(data, { 
        defaultDataProjection: ol.proj.get('EPSG:3765'), 
        featureProjection: 'EPSG:3857' 
       })); 

      }); 
     } 
    }) 
}); 
map.addLayer(vectorLayer); 

但现在我看不到我的GeoJSON的功能,它似乎加载,但它并没有在地图上呈现。

我必须指出的是,如果我从readFeatures法评论这两条线

defaultDataProjection: ol.proj.get('EPSG:3765'), 
featureProjection: 'EPSG:3857' 

GeoJSON的不渲染,但在错误的位置,就像在第一张照片。请帮助....我真的没有更多的眼泪了..

+0

你在项目中包含proj4js JavaScript库吗? – pavlos

+0

@pavlos是的,否则'proj4.defs'会给我一个错误。但是,感谢您在geojson文件“crs”中询问 – Svinjica

+1

:{“type”:“name”,“properties”:{“name”:“urn:ogc:def:crs:EPSG :: 3765”}}'needs改变名称:“EPSG:3765”'检查小提琴 – pavlos

我不知道可能你的问题是什么,但也许你GeoJSON的文件不包括在crs定义。也许proj4js版本是不正确的。

检查此小提琴 - >https://jsfiddle.net/p_tsagkis/s9vtoaak/。 我使用你的投影,似乎是在正确的位置。

更新: 您还需要稍微更改geojson文件。 投影的定义,应该如下:

{"type":"FeatureCollection", 
"crs": { 
      "type": "name", 
      "properties": { 
      "name": "EPSG:3765" 
      } 
     }, 
     "features":[.......... 

如果您不能编辑您的GeoJSON的文件,多一个选择是注册以GeoJSON到OL3 JS部分中的名称存在。 像这样:

proj4.defs('urn:ogc:def:crs:EPSG::3765', proj4.defs('EPSG:3765'));

或更换投影定义JS线这样的:

proj4.defs("urn:ogc:def:crs:EPSG::3765","+proj=tmerc +lat_0=0 +lon_0=16.5 +k=0.9999 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");

我还没有测试,但它应该工作。

OL3不知道urn:ogc:def:crs:EPSG::3765 你只要给proj4.defs("EPSG:3765","+proj=tmerc +lat_0=0 +lon_0=16.5 +k=0.9999 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"); 所以不知为何,你需要使它意识到对投影文件名的文件中存在。

+0

嗯,你正在做的事情,真棒的工作。我会尽量玩弄它 – Svinjica

+1

不用担心,很乐意帮忙 – pavlos

我认为你的问题是坐标或与EPSG来源。

看看https://epsg.io/3765,该投影的边界在意大利东部,而您期望您的几何图形位于意大利西部。

使用该投影,x坐标应为负值。

我的猜测是,你的GeoJSON的文件没有使用EPSG:3765

+0

我希望我的几何图形在意大利以东或更精确的克罗地亚行政边界或3765投影范围内(如第二张图所示)。但是,尽管如此,感谢你的努力,我真的很感激它 – Svinjica