yocontra / node-gdal-next

Node.js bindings for GDAL (Geospatial Data Abstraction Library) [Fork]
https://contra.io/node-gdal-next/
Apache License 2.0
75 stars 35 forks source link

gdal.spatialReference.isSame() returns false whereas spatial references look the same #46

Closed thom4parisot closed 3 years ago

thom4parisot commented 3 years ago

Hello,

I can't wrap my head around this problem:

// ...
const wgs84 = gdal.SpatialReference.fromProj4('+init=epsg:4326')

const ds = gdal.open(sourceFile, 'r')
const layer = ds.layers.get(0)

// in reality, I append multiple polygons to the multipolygon
// I kept it in case in this example in case it could affect the outcome of this issue
layer.setSpatialFilter(new gdal.MultiPolygon().unionCascaded())

console.log(layer.srs.isSame(wgs84)) // false
console.log(layer.srs.getProj4())    // +proj=longlat +datum=WGS84 +no_defs
console.log(wgs84.getProj4())        // +proj=longlat +datum=WGS84 +no_defs

I'm a bit lost because I would have expected the layers to be projected the same way. I use this test to determine if I need to reproject geometries as I filter them (using feature.getGeometry().clone().transformTo(wgs84)).

Am I missing something?

yocontra commented 3 years ago

You're likely running into this - https://github.com/contra/node-gdal-next#breaking-changes

gdal.SpatialReference.fromProj4('+init=epsg:4326') is not equal to plain EPSG:4326.

EPSG:4326: lat, lon fromProj4('+init=epsg:4326'): lon, lat

yocontra commented 3 years ago

If you provide a runnable code sample I can provide more guidance but I can't really see the larger picture from the code provided. I would assume that the file is being parsed as the new EPSG:4326 and all of your other code is using the old fromProj4('+init=epsg:4326') projection?

thom4parisot commented 3 years ago

Okay, it makes total sense.

I'm opening a Shapefile, projected as WGS84 with ogr2ogr, and I'm filtering it with a gdal-next created layer using gdal.SpatialReference.fromProj4('+init=epsg:4326') as a spatial reference.

I now understand why they are not stricly equal.

Thank you for the pointer 👍