Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 951
backspaces Avatar asked Nov 30 '25 03:11

backspaces


1 Answers

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.

like image 167
Joe - Elasticsearch Handbook Avatar answered Dec 04 '25 22:12

Joe - Elasticsearch Handbook



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!