99import java .awt .geom .AffineTransform ;
1010import org .geotools .api .feature .simple .SimpleFeatureType ;
1111import org .geotools .api .referencing .crs .CoordinateReferenceSystem ;
12- import org .geotools .api .referencing .operation .MathTransform ;
12+ import org .geotools .api .referencing .cs .AxisDirection ;
13+ import org .geotools .api .referencing .cs .CoordinateSystem ;
1314import org .geotools .api .referencing .operation .TransformException ;
1415import org .geotools .feature .simple .SimpleFeatureTypeBuilder ;
1516import org .geotools .geometry .jts .ReferencedEnvelope ;
@@ -144,8 +145,8 @@ public static void topBorderLabel(
144145 final LabelPositionCollector labels ,
145146 final String unit ,
146147 final AffineTransform worldToScreenTransform ,
147- final MathTransform toLabelProjection ,
148- final GridLabelFormat labelFormat ,
148+ final GridParam layerData ,
149+ final CoordinateReferenceSystem mapCrs ,
149150 final Geometry intersections ) {
150151
151152 if (intersections .getNumPoints () > 0 ) {
@@ -154,10 +155,10 @@ public static void topBorderLabel(
154155 worldToScreenTransform .transform (
155156 new double [] {borderIntersection .x , borderIntersection .y }, 0 , screenPoints , 0 , 1 );
156157
157- double [] labelProj = transformToLabelProjection (toLabelProjection , borderIntersection );
158+ double [] labelProj = transformToLabelProjection (layerData , mapCrs , borderIntersection );
158159 labels .add (
159160 new GridLabel (
160- createLabel (labelProj [0 ], unit , labelFormat ),
161+ createLabel (labelProj [0 ], unit , layerData . getGridLabelFormat () ),
161162 (int ) screenPoints [0 ],
162163 (int ) screenPoints [1 ],
163164 TOP ));
@@ -176,17 +177,17 @@ public static void bottomBorderLabel(
176177 final LabelPositionCollector labels ,
177178 final String unit ,
178179 final AffineTransform worldToScreenTransform ,
179- final MathTransform toLabelProjection ,
180- final GridLabelFormat labelFormat ,
180+ final GridParam layerData ,
181+ final CoordinateReferenceSystem mapCrs ,
181182 final Geometry intersections ) {
182183
183184 if (intersections .getNumPoints () > 0 ) {
184185 double [] screenPoints = new double [2 ];
185186 double [] labelProj =
186- getLabelProj (worldToScreenTransform , toLabelProjection , intersections , screenPoints );
187+ getLabelProj (worldToScreenTransform , layerData , mapCrs , intersections , screenPoints );
187188 labels .add (
188189 new GridLabel (
189- createLabel (labelProj [0 ], unit , labelFormat ),
190+ createLabel (labelProj [0 ], unit , layerData . getGridLabelFormat () ),
190191 (int ) screenPoints [0 ],
191192 (int ) screenPoints [1 ],
192193 BOTTOM ));
@@ -206,17 +207,17 @@ public static void rightBorderLabel(
206207 final LabelPositionCollector labels ,
207208 final String unit ,
208209 final AffineTransform worldToScreenTransform ,
209- final MathTransform toLabelProjection ,
210- final GridLabelFormat labelFormat ,
210+ final GridParam layerData ,
211+ final CoordinateReferenceSystem mapCrs ,
211212 final Geometry intersections ) {
212213
213214 if (intersections .getNumPoints () > 0 ) {
214215 double [] screenPoints = new double [2 ];
215216 double [] labelProj =
216- getLabelProj (worldToScreenTransform , toLabelProjection , intersections , screenPoints );
217+ getLabelProj (worldToScreenTransform , layerData , mapCrs , intersections , screenPoints );
217218 labels .add (
218219 new GridLabel (
219- createLabel (labelProj [1 ], unit , labelFormat ),
220+ createLabel (labelProj [1 ], unit , layerData . getGridLabelFormat () ),
220221 (int ) screenPoints [0 ],
221222 (int ) screenPoints [1 ],
222223 RIGHT ));
@@ -225,15 +226,16 @@ public static void rightBorderLabel(
225226
226227 private static double [] getLabelProj (
227228 final AffineTransform worldToScreenTransform ,
228- final MathTransform toLabelProjection ,
229+ final GridParam layerData ,
230+ final CoordinateReferenceSystem mapCrs ,
229231 final Geometry intersections ,
230232 final double [] screenPoints ) {
231233 int idx = intersections instanceof LineString ? 1 : 0 ;
232234 Coordinate borderIntersection = intersections .getGeometryN (0 ).getCoordinates ()[idx ];
233235 worldToScreenTransform .transform (
234236 new double [] {borderIntersection .x , borderIntersection .y }, 0 , screenPoints , 0 , 1 );
235237
236- return transformToLabelProjection (toLabelProjection , borderIntersection );
238+ return transformToLabelProjection (layerData , mapCrs , borderIntersection );
237239 }
238240
239241 /**
@@ -248,8 +250,8 @@ static void leftBorderLabel(
248250 final LabelPositionCollector labels ,
249251 final String unit ,
250252 final AffineTransform worldToScreenTransform ,
251- final MathTransform toLabelProjection ,
252- final GridLabelFormat labelFormat ,
253+ final GridParam layerData ,
254+ final CoordinateReferenceSystem mapCrs ,
253255 final Geometry intersections ) {
254256
255257 if (intersections .getNumPoints () > 0 ) {
@@ -258,23 +260,42 @@ static void leftBorderLabel(
258260 worldToScreenTransform .transform (
259261 new double [] {borderIntersection .x , borderIntersection .y }, 0 , screenPoints , 0 , 1 );
260262
261- double [] labelProj = transformToLabelProjection (toLabelProjection , borderIntersection );
263+ double [] labelProj = transformToLabelProjection (layerData , mapCrs , borderIntersection );
262264
263265 labels .add (
264266 new GridLabel (
265- createLabel (labelProj [1 ], unit , labelFormat ),
267+ createLabel (labelProj [1 ], unit , layerData . getGridLabelFormat () ),
266268 (int ) screenPoints [0 ],
267269 (int ) screenPoints [1 ],
268270 LEFT ));
269271 }
270272 }
271273
274+ /** Get the horizontal and vertical coordinates on the map. */
272275 private static double [] transformToLabelProjection (
273- final MathTransform toLabelProjection , final Coordinate borderIntersection ) {
276+ final GridParam layerData ,
277+ final CoordinateReferenceSystem mapCrs ,
278+ final Coordinate borderIntersection ) {
274279 try {
275280 double [] labelProj = new double [2 ];
276- toLabelProjection .transform (
277- new double [] {borderIntersection .x , borderIntersection .y }, 0 , labelProj , 0 , 1 );
281+ layerData
282+ .calculateLabelTransform (mapCrs )
283+ .transform (new double [] {borderIntersection .x , borderIntersection .y }, 0 , labelProj , 0 , 1 );
284+
285+ // Get axis directions from the target CRS
286+ CoordinateReferenceSystem labelCrs = layerData .getLabelCRS ();
287+ if (labelCrs == null ) {
288+ labelCrs = mapCrs ;
289+ }
290+ if (labelCrs != null ) {
291+ CoordinateSystem cs = labelCrs .getCoordinateSystem ();
292+ AxisDirection dir0 = cs .getAxis (0 ).getDirection ();
293+ AxisDirection dir1 = cs .getAxis (1 ).getDirection ();
294+ // If first axis is NORTH or SOUTH, swap axes
295+ if (dir0 == AxisDirection .NORTH || dir0 == AxisDirection .SOUTH ) {
296+ return new double [] {labelProj [1 ], labelProj [0 ]};
297+ }
298+ }
278299 return labelProj ;
279300 } catch (TransformException e ) {
280301 throw new RuntimeException (e );
0 commit comments