问题
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