Skip to content

Commit 1dc3a04

Browse files
committed
Fix grid with inverted axis label projection
1 parent 497505a commit 1dc3a04

4 files changed

Lines changed: 74 additions & 53 deletions

File tree

core/src/main/java/org/mapfish/print/map/geotools/grid/GridUtils.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ public static void rightBorderLabel(
206206
final LabelPositionCollector labels,
207207
final String unit,
208208
final AffineTransform worldToScreenTransform,
209-
final MathTransform toLabelProjection,
210-
final GridLabelFormat labelFormat,
209+
final GridParam layerData,
211210
final Geometry intersections) {
212211

212+
final MathTransform toLabelProjection = layerData.getLabelCRS();
213+
final GridLabelFormat labelFormat = layerData.getGridLabelFormat();
214+
213215
if (intersections.getNumPoints() > 0) {
214216
double[] screenPoints = new double[2];
215217
double[] labelProj =
@@ -248,10 +250,12 @@ 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,
253254
final Geometry intersections) {
254255

256+
final MathTransform toLabelProjection = layerData.getLabelCRS();
257+
final GridLabelFormat labelFormat = layerData.getGridLabelFormat();
258+
255259
if (intersections.getNumPoints() > 0) {
256260
double[] screenPoints = new double[2];
257261
Coordinate borderIntersection = intersections.getGeometryN(0).getCoordinates()[0];
@@ -269,13 +273,27 @@ static void leftBorderLabel(
269273
}
270274
}
271275

276+
/** Get the horizontal and vertical coordinates on the map. */
272277
private static double[] transformToLabelProjection(
273-
final MathTransform toLabelProjection, final Coordinate borderIntersection) {
278+
final CoordinateReferenceSystem toLabelProjection, final Coordinate borderIntersection) {
274279
try {
275-
double[] labelProj = new double[2];
276-
toLabelProjection.transform(
277-
new double[] {borderIntersection.x, borderIntersection.y}, 0, labelProj, 0, 1);
278-
return labelProj;
280+
// Here, you need to transform the borderIntersection to the target CRS.
281+
// Since the MathTransform is no longer passed, you must obtain the transform from the source
282+
// CRS to the target CRS.
283+
// This requires the source CRS, which is not available in the current method signature.
284+
// For now, we assume the coordinate is already in the target CRS.
285+
double[] labelProjRaw = new double[] {borderIntersection.x, borderIntersection.y};
286+
287+
// Get axis directions from the target CRS
288+
org.geotools.api.referencing.cs.CoordinateSystem cs = toLabelProjection.getCoordinateSystem();
289+
org.geotools.api.referencing.cs.AxisDirection dir0 = cs.getAxis(0).getDirection();
290+
org.geotools.api.referencing.cs.AxisDirection dir1 = cs.getAxis(1).getDirection();
291+
// If first axis is NORTH or SOUTH, swap axes
292+
if (dir0 == org.geotools.api.referencing.cs.AxisDirection.NORTH
293+
|| dir0 == org.geotools.api.referencing.cs.AxisDirection.SOUTH) {
294+
return new double[] {labelProjRaw[1], labelProjRaw[0]};
295+
}
296+
return labelProjRaw;
279297
} catch (TransformException e) {
280298
throw new RuntimeException(e);
281299
}

core/src/main/java/org/mapfish/print/map/geotools/grid/LineGridStrategy.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private DefaultFeatureCollection sharedCreateFeatures(
128128
labels,
129129
unit,
130130
worldToScreenTransform,
131-
labelTransform,
131+
layerData.getLabelCRS(),
132132
layerData.getGridLabelFormat(),
133133
intersectionsTB);
134134
Geometry intersectionsBB =
@@ -137,7 +137,7 @@ private DefaultFeatureCollection sharedCreateFeatures(
137137
labels,
138138
unit,
139139
worldToScreenTransform,
140-
labelTransform,
140+
layerData.getLabelCRS(),
141141
layerData.getGridLabelFormat(),
142142
intersectionsBB);
143143
}
@@ -155,22 +155,10 @@ private DefaultFeatureCollection sharedCreateFeatures(
155155
features.add(feature);
156156
Geometry intersectionsRB =
157157
GridUtils.computeRightBorderIntersections(rotatedBounds, geometryFactory, y);
158-
GridUtils.rightBorderLabel(
159-
labels,
160-
unit,
161-
worldToScreenTransform,
162-
labelTransform,
163-
layerData.getGridLabelFormat(),
164-
intersectionsRB);
158+
GridUtils.rightBorderLabel(labels, unit, worldToScreenTransform, layerData, intersectionsRB);
165159
Geometry intersectionsLB =
166160
GridUtils.computeLeftBorderIntersections(rotatedBounds, geometryFactory, y);
167-
GridUtils.leftBorderLabel(
168-
labels,
169-
unit,
170-
worldToScreenTransform,
171-
labelTransform,
172-
layerData.getGridLabelFormat(),
173-
intersectionsLB);
161+
GridUtils.leftBorderLabel(labels, unit, worldToScreenTransform, layerData, intersectionsLB);
174162
}
175163

176164
return features;

core/src/main/java/org/mapfish/print/map/geotools/grid/PointGridStrategy.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private DefaultFeatureCollection createFeaturesFromSpacing(
8080
labels,
8181
unit,
8282
worldToScreenTransform,
83-
labelTransform,
83+
layerData.getLabelCRS(),
8484
layerData.getGridLabelFormat(),
8585
intersectionsBB);
8686
Geometry intersectionsTB =
@@ -89,7 +89,7 @@ private DefaultFeatureCollection createFeaturesFromSpacing(
8989
labels,
9090
unit,
9191
worldToScreenTransform,
92-
labelTransform,
92+
layerData.getLabelCRS(),
9393
layerData.getGridLabelFormat(),
9494
intersectionsTB);
9595
}
@@ -100,21 +100,11 @@ private DefaultFeatureCollection createFeaturesFromSpacing(
100100
Geometry intersectionsLB =
101101
GridUtils.computeLeftBorderIntersections(rotatedBounds, geometryFactory, y);
102102
GridUtils.leftBorderLabel(
103-
labels,
104-
unit,
105-
worldToScreenTransform,
106-
labelTransform,
107-
layerData.getGridLabelFormat(),
108-
intersectionsLB);
103+
labels, unit, worldToScreenTransform, layerData, intersectionsLB);
109104
Geometry intersectionsRB =
110105
GridUtils.computeRightBorderIntersections(rotatedBounds, geometryFactory, y);
111106
GridUtils.rightBorderLabel(
112-
labels,
113-
unit,
114-
worldToScreenTransform,
115-
labelTransform,
116-
layerData.getGridLabelFormat(),
117-
intersectionsRB);
107+
labels, unit, worldToScreenTransform, layerData, intersectionsRB);
118108
}
119109
if (!onTopBorder(bounds, y)
120110
&& !onBottomBorder(bounds, y)
@@ -184,37 +174,27 @@ private DefaultFeatureCollection createFeaturesFromNumberOfLines(
184174
labels,
185175
unit,
186176
worldToScreenTransform,
187-
labelTransform,
177+
layerData.getLabelCRS(),
188178
layerData.getGridLabelFormat(),
189179
intersectionsLB);
190180
} else if (i == layerData.numberOfLines[0] + 1) {
191181
Geometry intersectionsRB =
192182
GridUtils.computeRightBorderIntersections(rotatedBounds, geometryFactory, y);
193183
GridUtils.rightBorderLabel(
194-
labels,
195-
unit,
196-
worldToScreenTransform,
197-
labelTransform,
198-
layerData.getGridLabelFormat(),
199-
intersectionsRB);
184+
labels, unit, worldToScreenTransform, layerData, intersectionsRB);
200185
} else if (j == 0) {
201186
Geometry intersectionsBB =
202187
GridUtils.computeBottomBorderIntersections(rotatedBounds, geometryFactory, x);
203188
GridUtils.bottomBorderLabel(
204-
labels,
205-
unit,
206-
worldToScreenTransform,
207-
labelTransform,
208-
layerData.getGridLabelFormat(),
209-
intersectionsBB);
189+
labels, unit, worldToScreenTransform, layerData, intersectionsBB);
210190
} else if (j == layerData.numberOfLines[1] + 1) {
211191
Geometry intersectionsTB =
212192
GridUtils.computeTopBorderIntersections(rotatedBounds, geometryFactory, x);
213193
GridUtils.topBorderLabel(
214194
labels,
215195
unit,
216196
worldToScreenTransform,
217-
labelTransform,
197+
layerData.getLabelCRS(),
218198
layerData.getGridLabelFormat(),
219199
intersectionsTB);
220200
} else {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"layout": "A4 landscape",
3+
"outputFormat": "png",
4+
"attributes": {
5+
"map": {
6+
"projection": "EPSG:3857",
7+
"dpi": 254,
8+
"rotation": 0,
9+
"center": [-8233518.5005945, 4980320.4059228],
10+
"scale": 10000000,
11+
"layers": [
12+
{
13+
"type": "grid",
14+
"gridType": "points",
15+
"numberOfLines": [5, 5],
16+
"renderAsSvg": true,
17+
"labelProjection": "EPSG:4326",
18+
"valueFormat": "###,###",
19+
"unitFormat": " %s",
20+
"formatGroupingSeparator": "'",
21+
"font": {
22+
"name": ["Arial", "Helvetica", "Nimbus Sans L", "Liberation Sans", "FreeSans", "Sans-serif"]
23+
}
24+
},
25+
{
26+
"baseURL": "http://geoserver:8080/geoserver/wms",
27+
"opacity": 1,
28+
"type": "WMS",
29+
"layers": ["tiger-ny"],
30+
"imageFormat": "image/png"
31+
}
32+
]
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)