proj4js icon indicating copy to clipboard operation
proj4js copied to clipboard

WKT2-based transformation fails while same proj4-based trans succeeds

Open kylebarron opened this issue 1 week ago • 2 comments

I'm having trouble with NaNs produced in transformation with this WKT2 CRS from a MODIS image:

const proj4 = require("proj4") // v2.20.2

const sourceCrs = 'PROJCS["unnamed",GEOGCS["Unknown datum based upon the custom spheroid",DATUM["Not specified (based on custom spheroid)",SPHEROID["Custom spheroid",6371007.181,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Sinusoidal"],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'

const converter = proj4(sourceCrs, "EPSG:4326");
console.log(converter.forward([-8895604.157333, 4447802.078667]));
// [NaN, NaN]

In Python, it works well:

from pyproj import Transformer

crs = 'PROJCS["unnamed",GEOGCS["Unknown datum based upon the custom spheroid",DATUM["Not specified (based on custom spheroid)",SPHEROID["Custom spheroid",6371007.181,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Sinusoidal"],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'

transformer = Transformer.from_crs(crs, "EPSG:4326", always_xy=True)
transformer.transform(-8895604.157333, 4447802.078667)
# (-104.43258313171073, 39.99999999641088)

If I use what pyproj says is the relevant proj4 string, it works well:

from pyproj import CRS
CRS(crs).to_proj4()
# '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs +type=crs'
const proj4 = require("proj4") // v2.20.2

const sourceCrs = '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs +type=crs';

const converter = proj4(sourceCrs, "EPSG:4326");
console.log(converter.forward([-8895604.157333, 4447802.078667]));
// [-104.43258313171073, 39.99999999641088]

kylebarron avatar Dec 11 '25 17:12 kylebarron

Thanks for reporting this issue, @kylebarron. This is probably because wkt-parser cannot handle "Custom spheroid", so a good starting point for debugging would be to see if the spheroid/ellipsoid values are properly populated in the definition objcect after parsing.

Did you try if PROJJSON works?

ahocevar avatar Dec 11 '25 22:12 ahocevar

Thanks for the reply. I can confirm it does work with PROJJSON:

CRS(crs).to_json()
# '{"$schema":"https://proj.org/schemas/v0.7/projjson.schema.json","type":"ProjectedCRS","name":"unnamed","base_crs":{"name":"Unknown datum based upon the custom spheroid","datum":{"type":"GeodeticReferenceFrame","name":"Not specified (based on custom spheroid)","ellipsoid":{"name":"Custom spheroid","radius":6371007.181}},"coordinate_system":{"subtype":"ellipsoidal","axis":[{"name":"Longitude","abbreviation":"lon","direction":"east","unit":"degree"},{"name":"Latitude","abbreviation":"lat","direction":"north","unit":"degree"}]}},"conversion":{"name":"unnamed","method":{"name":"Sinusoidal"},"parameters":[{"name":"Longitude of natural origin","value":0,"unit":"degree","id":{"authority":"EPSG","code":8802}},{"name":"False easting","value":0,"unit":{"type":"LinearUnit","name":"Meter","conversion_factor":1},"id":{"authority":"EPSG","code":8806}},{"name":"False northing","value":0,"unit":{"type":"LinearUnit","name":"Meter","conversion_factor":1},"id":{"authority":"EPSG","code":8807}}]},"coordinate_system":{"subtype":"Cartesian","axis":[{"name":"Easting","abbreviation":"","direction":"east","unit":{"type":"LinearUnit","name":"Meter","conversion_factor":1}},{"name":"Northing","abbreviation":"","direction":"north","unit":{"type":"LinearUnit","name":"Meter","conversion_factor":1}}]}}'

(observable repl I'm testing in)

Image

kylebarron avatar Dec 11 '25 23:12 kylebarron