Jquery OpenLayers 3重投影向量层(v4.3.2)
欢迎GIS爱好者和网络制作者。我有一个关于投影的问题,更准确地说,在Openlayers 4中从EPSG:3765重新投影到EPSG:3857 在下面的代码中,我试图从EPSG:3765重新投影到EPSG:3857:Jquery OpenLayers 3重投影向量层(v4.3.2),jquery,openlayers,openlayers-3,geojson,proj,Jquery,Openlayers,Openlayers 3,Geojson,Proj,欢迎GIS爱好者和网络制作者。我有一个关于投影的问题,更准确地说,在Openlayers 4中从EPSG:3765重新投影到EPSG:3857 在下面的代码中,我试图从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
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,但位置错误,如下所示:
我希望它在这里,就像这样:
我做错了什么?这是geojson坐标格式的问题,还是我需要以某种方式重新投影geojson,使其与第二张图片中的一样
更新
从这里你可以下载geojson并测试你是否喜欢
以帕夫洛斯为例,我编辑了几行,或者更准确地说;我正试图使用这段代码加载本地geojson
var geojsonObject = $.ajax({
dataType: "json",
url: "test_1.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确实渲染,但渲染位置不对,就像第一张图片一样。请帮忙……我真的没有眼泪了。我想你的问题是坐标或源EPSG 看,投影的边界在意大利东部,而你期望你的几何图形在意大利西部 有了这个投影,x坐标应该是负的
我猜您的geojson文件没有使用EPSG:3765,我认为您的问题在于坐标或源EPSG 看,投影的边界在意大利东部,而你期望你的几何图形在意大利西部 有了这个投影,x坐标应该是负的
我猜您的geojson文件没有使用EPSG:3765,我不确定您的问题是什么,但您的geojson文件可能不包括
crs
定义。可能proj4js版本不正确
检查这把小提琴-->。
我用的是你的投影图,似乎在正确的位置
更新:
您还需要稍微更改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+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+units=m+no_defs”)代码>
因此,您需要知道文件中存在的投影名称。我不确定您的问题是什么,但您的geojson文件可能不包含crs
定义。可能proj4js版本不正确
检查这把小提琴-->。
我用的是你的投影图,似乎在正确的位置
更新:
您还需要稍微更改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+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+units=m+no_defs”)代码>
所以您需要知道文件中存在的投影名称。我希望我的几何图形位于意大利东部,或者更准确地说,位于克罗地亚行政边界或3765投影边界内(如第二张图片所示)。不过,谢谢你们的努力,我真的很感激你们,我希望我的几何图形位于意大利东部,或者更准确地说,位于克罗地亚行政边界或3765投影边界内(如第二张图片所示)。尽管如此,还是要感谢您的努力,我真的很感激您在您的项目中包括proj4js javascript库吗?@pavlos是的,我有,否则proj4.defs
会给我一个错误。但是感谢您在geojson文件“crs”中询问:{“type”:“name”,“properties”:{“name”:“urn:ogc:def:crs:EPSG::3765”}
需要更改“name”:“EPSG:3765”
检查fiddle@pavlos是的,就是这样。请编辑答案以便接受。:)公平地说,我不会在一百万年内猜到它。为什么会发生这种情况,为什么我需要更改geojson,你能解释一下吗?我又给出了一个选项和一点解释。你的项目中是否包含proj4js javascript库??@pavlos是的,否则proj4.defs
会给我一个错误。但感谢您在geojson文件“crs”:{“type”:“name”,“properties”:{“name”:“urn:ogc:def:crs:EPSG::3765”}