Skip to content

Fix Axis.getDirection() to never return null (#80)#81

Merged
desruisseaux merged 6 commits intoOSGeo:mainfrom
Manas-Dikshit:main
Dec 21, 2025
Merged

Fix Axis.getDirection() to never return null (#80)#81
desruisseaux merged 6 commits intoOSGeo:mainfrom
Manas-Dikshit:main

Conversation

@Manas-Dikshit
Copy link
Copy Markdown
Contributor

This pull request addresses issue #80, where Axis.getDirection() could return null if the underlying Property.DIRECTION was missing or unrecognized. Returning null violates the CoordinateSystemAxis GeoAPI contract and can lead to NullPointerException in downstream code.

Approach:
Updated the getDirection() method in Axis.java to check the result of the internal search method.
If search returns null, the method now returns the default value AxisDirection.OTHER.
Preserved existing behavior for recognized axis directions.

How It Solves the Problem:
Ensures getDirection() always returns a non-null AxisDirection, maintaining compliance with GeoAPI expectations.
Prevents runtime crashes and makes PROJ-JNI safer for all consumers of CRS objects.
No breaking changes for existing valid axis directions.

Example:
AxisDirection direction = axis.getDirection(); // Never null, defaults to OTHER if unknown

Impact:
Fixes null-safety issue in Axis class.
Improves robustness and GeoAPI compliance.

@desruisseaux desruisseaux self-assigned this Dec 20, 2025
@desruisseaux desruisseaux added the bug Something isn't working label Dec 20, 2025
@desruisseaux desruisseaux added this to the 2.1 milestone Dec 20, 2025
@desruisseaux
Copy link
Copy Markdown
Collaborator

Do you have the Well Known Text (WKT) definition of the Coordinate Reference System (CRS) for which the axis direction is null? It would be for checking if we can infer a better axis direction than OTHER at least for the case that you encountered.

@Manas-Dikshit
Copy link
Copy Markdown
Contributor Author

Thank you for the feedback, @desruisseaux!

Here’s the WKT of the CRS where AxisDirection was null:

' ' ' wkt
GEOGCRS["Unknown datum based CRS",
DATUM["Unknown based datum", ELLIPSOID["GRS 1980",6378137,298.257222101]],
CS[ellipsoidal,2],
AXIS["Geodetic latitude (Lat)", north, ANGLEUNIT["degree",0.0174532925199433]],
AXIS["Geodetic longitude (Lon)", east, ANGLEUNIT["degree",0.0174532925199433]]
]
' ' '
It seems that when the Property.DIRECTION property was missing for one of the axes, the JNI wrapper didn’t resolve the direction string. Returning AxisDirection.OTHER prevents a NullPointerException, but if a consistent pattern is detectable from WKT (e.g., latitude → NORTH, longitude → EAST), we could consider inferring those directions as an enhancement.

@desruisseaux
Copy link
Copy Markdown
Collaborator

While latitude is very likely to be oriented toward North, we cannot be sure as it could also be South. Same for longitude. It is safer to not try to guess.

I do not know if the issue is on PROJ side or elsewhere. On one side, PROJ-JNI is implemented only as below, relying fully on PROJ:

value = get_shared_object<CoordinateSystemAxis>(env, object)->direction().toString().c_str();

On the other side, PROJ seems to have the information somewhere since it appears in the WKT. I suggest to keep the pull request as-is with only one amendment (I will post a comment directly on the line of code). Maybe the issue will disappear in some future PROJ version.

return search(AxisDirection.class, dir);
AxisDirection direction = search(AxisDirection.class, dir);
// Ensure a non-null return; default to OTHER if unknown
return (direction != null) ? direction : AxisDirection.OTHER;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace AxisDirection.OTHER by AxisDirection.valueOf("UNSPECIFIED") and add the following comment:

// TODO: Use AxisDirection.UNSPECIFIED in GeoAPI 3.1.

The "Unspecified" axis direction has been added in the ISO 19111:2019 international standard. GeoAPI has not yet been updated for that revision, but this is planed for GeoAPI 3.1. In the meantime, the use of valueOf(String) should allow us to anticipate.

Ensure a non-null return for axis direction, defaulting to UNSPECIFIED if unknown.
Update getDirection to handle null values
@Manas-Dikshit
Copy link
Copy Markdown
Contributor Author

Thanks, @desruisseaux — updated accordingly.
The getDirection() method now uses AxisDirection.valueOf("UNSPECIFIED") with the TODO comment referencing GeoAPI 3.1 and ISO 19111:2019, as suggested.

This approach should maintain compatibility with current GeoAPI versions while anticipating the addition of UNSPECIFIED. The logic still guarantees a non-null return value, ensuring downstream safety.

public AxisDirection getDirection() {
final String dir = impl.getStringProperty(Property.DIRECTION);
return search(AxisDirection.class, dir);
final String dir = impl.getStringProperty(Property.DIRECTION);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation.

@Manas-Dikshit
Copy link
Copy Markdown
Contributor Author

Manas-Dikshit commented Dec 21, 2025

@desruisseaux
Omitted that extra blank line & also fixed indentation.

@desruisseaux desruisseaux merged commit c247cbd into OSGeo:main Dec 21, 2025
1 check passed
@desruisseaux
Copy link
Copy Markdown
Collaborator

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants