如何从 PostGIS 坐标居中 openlayers 地图?
How to center openlayers map from PostGIS coordinates?
我正在尝试使用从我的数据库中使用 PostGIS 函数获得的坐标来居中我的 openlayers 地图:ST_AsGeoJSON(ST_SetSRID(ST_Centroid(geom),3857))
。我数据库中的几何图形投影在 EPSG:25830 中,我的 openlayers 地图投影在 EPSG:3857.
中
我在客户端调用服务端得到的坐标是这样的:
613056.633587271 4738175.03852526
然后我使用 openlayers 地图的视图和收到的坐标执行以下操作:
view.animate({
center: coordinatefromserver,
duration: 500
});
我的地图移动到一个完全不同的位置。
我知道工作正常的那种坐标是这个:
-180099.9705826787,5279777.85057039
我错过了什么? PostGIS 功能?任何类型转换?预测?抱歉,我对这方面还很陌生!
谢谢。
当从 EPSG:25830 转换为 EPSG:3857 时,这些坐标会产生 [-180081.82283603796, 5279725.360517778],因此尽管 SetSRID 为 3857,服务器仍会返回 EPSG:25830。
您可以在 OpenLayers 中进行转换:
proj4.defs('EPSG:25830', '+proj=utm +zone=30 +ellps=GRS80 +units=m +no_defs ');
if (ol.proj.proj4 && ol.proj.proj4.register) { ol.proj.proj4.register(proj4); } // only needed for OL5
view.animate({
center: ol.proj.transform(coordinatefromserver, 'EPSG:25830', view.getProjection())
duration: 500
});
很确定问题出在您的查询没有按照您的要求执行。如果你想从一个 srid 转换到另一个,你需要使用 st_transform 而不是 st_setsrid.
即您的查询应该是:
ST_AsGeoJSON(ST_transform(ST_Centroid(geom),3857))
我正在尝试使用从我的数据库中使用 PostGIS 函数获得的坐标来居中我的 openlayers 地图:ST_AsGeoJSON(ST_SetSRID(ST_Centroid(geom),3857))
。我数据库中的几何图形投影在 EPSG:25830 中,我的 openlayers 地图投影在 EPSG:3857.
我在客户端调用服务端得到的坐标是这样的:
613056.633587271 4738175.03852526
然后我使用 openlayers 地图的视图和收到的坐标执行以下操作:
view.animate({
center: coordinatefromserver,
duration: 500
});
我的地图移动到一个完全不同的位置。
我知道工作正常的那种坐标是这个:
-180099.9705826787,5279777.85057039
我错过了什么? PostGIS 功能?任何类型转换?预测?抱歉,我对这方面还很陌生!
谢谢。
当从 EPSG:25830 转换为 EPSG:3857 时,这些坐标会产生 [-180081.82283603796, 5279725.360517778],因此尽管 SetSRID 为 3857,服务器仍会返回 EPSG:25830。
您可以在 OpenLayers 中进行转换:
proj4.defs('EPSG:25830', '+proj=utm +zone=30 +ellps=GRS80 +units=m +no_defs ');
if (ol.proj.proj4 && ol.proj.proj4.register) { ol.proj.proj4.register(proj4); } // only needed for OL5
view.animate({
center: ol.proj.transform(coordinatefromserver, 'EPSG:25830', view.getProjection())
duration: 500
});
很确定问题出在您的查询没有按照您的要求执行。如果你想从一个 srid 转换到另一个,你需要使用 st_transform 而不是 st_setsrid.
即您的查询应该是:
ST_AsGeoJSON(ST_transform(ST_Centroid(geom),3857))