Openlayers Openlayer 4可以使用GeoJson的crs动态地重新喷射吗
我正在从服务器读取带有crs定义的GeoJson文件:Openlayers Openlayer 4可以使用GeoJson的crs动态地重新喷射吗,openlayers,geojson,Openlayers,Geojson,我正在从服务器读取带有crs定义的GeoJson文件: {"type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32720" } }, "features": [ many featur
{"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32720" } },
"features": [ many features,.....
然后把它载入地图,但它出现在另一个大陆,靠近格林威治子午线
我阅读了文档,在很多地方都有一个featureProjection选项,
但我不知道是源还是目标
我尝试:
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/points.geojson',
format: new ol.format.GeoJSON({featureProjection: 'EPSG:32720'}) //
})
});
此外:
地图加载一个OSM图层以供参考,并正常工作:
var map = new ol.Map({
layers: [
new ol.layer.Tile({source: new ol.source.OSM()}),
vectorLayerUTMPoints
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
view: new ol.View({
projection: 'EPSG:3857',
center: [0, 0],
zoom: 2
})
});
问题是,OpenLayer 4可以从geojson文件读取crs定义并进行转换
到动态视图的crs,或者如何解决它?这里有两个问题:首先,您尝试使用的投影默认不包括在OpenLayers中:您必须使用Proj4导入它。我已经从中获取了代码来演示 首先包括proj4脚本和投影的定义: 您的第二个问题是,您错误地使用了featureProjection。需要在图层的format对象中设置两个特性:dataProjection是数据源的投影,featureProjection是要将要素转换为的。在本例中,您希望变换它们以适合您的地图投影,即EPSG:3857 因此,您的代码变成:
var myProj = ol.proj.get('EPSG:32720');
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/points.geojson',
format: new ol.format.GeoJSON({dataProjection: myProj, featureProjection: 'EPSG:3857'})
})
});
谢谢你的回复,我不能早点回答,因为 我被困住了,你的解决方案很清楚,但无法奏效。 特征显示在错误的位置 有任何方法可以知道proj4是否正确加载,但它没有有效的方法 或者类似的 我创建了一个plunker文件: 我也试试你的建议:
proj4.defs("EPSG:32720","+proj=utm +zone=20 +south +datum=WGS84 +units=m +no_defs");
var sourceProj = ol.proj.get('EPSG:32720');
var targetProj = ol.proj.get('EPSG:3857');
var UTMFormat = new ol.format.GeoJSON({dataProjection: sourceProj, featureProjection: targetProj'});
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/centroids32720.geojson',
format: UTMFormat
}),
style: styleFunction
});
此外:
proj4.defs("EPSG:32720","+proj=utm +zone=20 +south +datum=WGS84 +units=m +no_defs");
var UTMFormat = new ol.format.GeoJSON({dataProjection: 'EPSG:32720', featureProjection: 'EPSG:3857'});
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/centroids32720.geojson',
format: UTMFormat
}),
style: styleFunction
});
我在法国看到了这一点,但没有人反对
根据api文件:
defaultDataProjection和featureProjection是类似于ol.ProjectOnly的
它可以是三件事之一:
ol.ProjectionLike{ol.proj.Projection}{string}{undefined}
所以来源是好的
问候
var myProj = ol.proj.get('EPSG:32720');
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/points.geojson',
format: new ol.format.GeoJSON({dataProjection: myProj, featureProjection: 'EPSG:3857'})
})
});
proj4.defs("EPSG:32720","+proj=utm +zone=20 +south +datum=WGS84 +units=m +no_defs");
var sourceProj = ol.proj.get('EPSG:32720');
var targetProj = ol.proj.get('EPSG:3857');
var UTMFormat = new ol.format.GeoJSON({dataProjection: sourceProj, featureProjection: targetProj'});
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/centroids32720.geojson',
format: UTMFormat
}),
style: styleFunction
});
proj4.defs("EPSG:32720","+proj=utm +zone=20 +south +datum=WGS84 +units=m +no_defs");
var UTMFormat = new ol.format.GeoJSON({dataProjection: 'EPSG:32720', featureProjection: 'EPSG:3857'});
var vectorLayerUTMPoints = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/static/centroids32720.geojson',
format: UTMFormat
}),
style: styleFunction
});