admin管理员组文章数量:1391951
I have GeoJSON files containing a FeatureCollection
using EPSG:27700 and for Maplibre I believe the coordinates need to be specified in EPSG:4326).
What is the best way of either getting Maplibre to handle a GeoJSON layer with EPSG:27700 coordinates or mapping the coordinates from EPSG:27700 to EPSG:4326 ?
I have GeoJSON files containing a FeatureCollection
using EPSG:27700 and for Maplibre I believe the coordinates need to be specified in EPSG:4326).
What is the best way of either getting Maplibre to handle a GeoJSON layer with EPSG:27700 coordinates or mapping the coordinates from EPSG:27700 to EPSG:4326 ?
Share Improve this question asked Mar 14 at 6:25 vogomatixvogomatix 5,0912 gold badges26 silver badges49 bronze badges1 Answer
Reset to default 0Solution seems to be using proj4js to do the job. Created this routine to remap
import proj4 from 'proj4';
import type { Geometry, Position } from 'geojson';
proj4.defs(
'EPSG:27700',
'+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs'
);
// Transform coordinates.
export const transformCoords = function (arr: [number, number]): { lng: number; lat: number } {
const projArray = proj4('EPSG:27700', 'EPSG:4326', arr);
return { lng: projArray[0], lat: projArray[1] };
};
// Transform GeoJSON Geometry from one Coordinate Reference System (CRS) to another
export const transformGeometry = (original: Geometry, from = 'EPSG:27700', to = 'EPSG:4326'): Geometry => {
const mapCoords = ( p: Position) => proj4( from, to, p);
const gType = original.type;
let geo: Geometry;
switch (gType) {
case 'Point':
// coords are single array
geo = {
type: 'Point',
coordinates: mapCoords(original.coordinates)
};
break;
case 'MultiPoint':
case 'LineString':
geo = {
type: gType,
coordinates: original.coordinates.map((p: Position) => mapCoords(p)
)
};
break;
case 'Polygon':
case 'MultiLineString':
geo = {
type: gType,
coordinates: original.coordinates.map((item) =>
item.map((p: Position) => mapCoords(p))
)
};
break;
case 'MultiPolygon':
geo = {
type: gType,
coordinates: original.coordinates.map((items) =>
items.map((item) => item.map((p: Position) => mapCoords(p)))
)
};
break;
case 'GeometryCollection':
// recursive call
geo = {
type: gType,
geometries: original.geometries.map((geo) => transformGeometry(geo, from, to))
};
break;
}
return geo;
};
本文标签: javascriptTransforming GeoJSON coordinatesStack Overflow
版权声明:本文标题:javascript - Transforming GeoJSON coordinates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744670953a2618818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论