問候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座標格式中是否存在問題,或者我需要以某種方式重新設置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的不渲染,但在錯誤的位置,就像在第一張照片。請幫助....我真的沒有更多的眼淚了..
你在項目中包含proj4js JavaScript庫嗎? – pavlos
@pavlos是的,否則'proj4.defs'會給我一個錯誤。但是,感謝您在geojson文件「crs」中詢問 – Svinjica
:{「type」:「name」,「properties」:{「name」:「urn:ogc:def:crs:EPSG :: 3765」}}'needs改變名稱:「EPSG:3765」'檢查小提琴 – pavlos