Can turfjs provide a coordinate transform between geo coords and application coords

假装没事ソ 提交于 2020-07-09 06:56:05

问题


I need a transform from geo coord system to/from another coord system. I'd think the obvious way to do this would be to give the two bounding boxes of the systems.

So if I had a geo bbox, in lon/lat coords, and a non-geo bbox that corresponds to that but in my own coordinates, I'd like a xfm that could convert geo-to-me and me-to-geo.

I need this for an agent based programming environment that has its own coord system based on minX, minY, maxX, maxY, just as Turf expresses bboxes. It would also work for transforming between geo coords and pixels in a canvas, for example.

I couldn't find such a coord transform in Turf but I may be missing it, or there may be an easy way to do it with Turf primitives.

Is there a way to use Turf for such a coord transform?


回答1:


I'd use proj4js which handles projection conversions. Your canvas can be represented in Pseudo-Mercator ([x,y]) while turf will continue working in WGS84 ([lon, lat]).

const xyCoordsToLatLong = (xy_pair) => {
    return proj4(PSEUDO_MERC_DATUM, WGS84_DATUM, xy_pair);
}

const latLongCoordsToXY = (latlong_pair) => {
    return proj4(WGS84_DATUM, PSEUDO_MERC_DATUM, latlong_pair);
}

Then, a canvas of bbox [0,0,500,200] would be represented as [0, 0, 0.004491576420597608, 0.0017966305679443192].

Full demo here.



来源:https://stackoverflow.com/questions/61491927/can-turfjs-provide-a-coordinate-transform-between-geo-coords-and-application-coo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!