Skip to content

Commit 0523141

Browse files
authored
Merge pull request #4019 from mapfish/ghci/backport/4015-to-4.0
[Backport 4.0] Fix grid with inverted axis label projection
2 parents b085a5c + 78f556c commit 0523141

File tree

7 files changed

+96
-96
lines changed

7 files changed

+96
-96
lines changed

core/src/main/java/org/mapfish/print/attribute/FeaturesAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static final class FeaturesAttributeValues {
4040
* projections. However the requester can override this by explicitly declaring that longitude
4141
* axis is first.
4242
*/
43-
@HasDefaultValue public Boolean longitudeFirst = null;
43+
@HasDefaultValue private Boolean longitudeFirst = null;
4444

4545
private SimpleFeatureCollection featuresCollection;
4646

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public final class GridParam extends AbstractLayerParams {
158158
* projections. However the requester can override this by explicitly declaring that longitude
159159
* axis is first.
160160
*/
161-
@HasDefaultValue public Boolean longitudeFirst = null;
161+
@HasDefaultValue private Boolean longitudeFirst = null;
162162

163163
/**
164164
* If true (the default), the labels will be rotated to follow the lines they belong to. Otherwise
@@ -266,6 +266,10 @@ public MathTransform calculateLabelTransform(final CoordinateReferenceSystem map
266266
return labelTransform;
267267
}
268268

269+
public CoordinateReferenceSystem getLabelCRS() {
270+
return this.labelCRS;
271+
}
272+
269273
public GridLabelFormat getGridLabelFormat() {
270274
return this.gridLabelFormat;
271275
}

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import java.awt.geom.AffineTransform;
1010
import org.geotools.api.feature.simple.SimpleFeatureType;
1111
import 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;
1314
import org.geotools.api.referencing.operation.TransformException;
1415
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
1516
import 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);

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,11 @@ private DefaultFeatureCollection sharedCreateFeatures(
125125
Geometry intersectionsTB =
126126
GridUtils.computeTopBorderIntersections(rotatedBounds, geometryFactory, x);
127127
GridUtils.topBorderLabel(
128-
labels,
129-
unit,
130-
worldToScreenTransform,
131-
labelTransform,
132-
layerData.getGridLabelFormat(),
133-
intersectionsTB);
128+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsTB);
134129
Geometry intersectionsBB =
135130
GridUtils.computeBottomBorderIntersections(rotatedBounds, geometryFactory, x);
136131
GridUtils.bottomBorderLabel(
137-
labels,
138-
unit,
139-
worldToScreenTransform,
140-
labelTransform,
141-
layerData.getGridLabelFormat(),
142-
intersectionsBB);
132+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsBB);
143133
}
144134

145135
pointSpacing = bounds.getSpan(0) / layerData.pointsInLine;
@@ -156,21 +146,11 @@ private DefaultFeatureCollection sharedCreateFeatures(
156146
Geometry intersectionsRB =
157147
GridUtils.computeRightBorderIntersections(rotatedBounds, geometryFactory, y);
158148
GridUtils.rightBorderLabel(
159-
labels,
160-
unit,
161-
worldToScreenTransform,
162-
labelTransform,
163-
layerData.getGridLabelFormat(),
164-
intersectionsRB);
149+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsRB);
165150
Geometry intersectionsLB =
166151
GridUtils.computeLeftBorderIntersections(rotatedBounds, geometryFactory, y);
167152
GridUtils.leftBorderLabel(
168-
labels,
169-
unit,
170-
worldToScreenTransform,
171-
labelTransform,
172-
layerData.getGridLabelFormat(),
173-
intersectionsLB);
153+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsLB);
174154
}
175155

176156
return features;

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

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,11 @@ private DefaultFeatureCollection createFeaturesFromSpacing(
7777
Geometry intersectionsBB =
7878
GridUtils.computeBottomBorderIntersections(rotatedBounds, geometryFactory, x);
7979
GridUtils.bottomBorderLabel(
80-
labels,
81-
unit,
82-
worldToScreenTransform,
83-
labelTransform,
84-
layerData.getGridLabelFormat(),
85-
intersectionsBB);
80+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsBB);
8681
Geometry intersectionsTB =
8782
GridUtils.computeTopBorderIntersections(rotatedBounds, geometryFactory, x);
8883
GridUtils.topBorderLabel(
89-
labels,
90-
unit,
91-
worldToScreenTransform,
92-
labelTransform,
93-
layerData.getGridLabelFormat(),
94-
intersectionsTB);
84+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsTB);
9585
}
9686
for (double y = minY; y < bounds.getMaxY(); y += incrementY) {
9787
j++;
@@ -100,21 +90,11 @@ private DefaultFeatureCollection createFeaturesFromSpacing(
10090
Geometry intersectionsLB =
10191
GridUtils.computeLeftBorderIntersections(rotatedBounds, geometryFactory, y);
10292
GridUtils.leftBorderLabel(
103-
labels,
104-
unit,
105-
worldToScreenTransform,
106-
labelTransform,
107-
layerData.getGridLabelFormat(),
108-
intersectionsLB);
93+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsLB);
10994
Geometry intersectionsRB =
11095
GridUtils.computeRightBorderIntersections(rotatedBounds, geometryFactory, y);
11196
GridUtils.rightBorderLabel(
112-
labels,
113-
unit,
114-
worldToScreenTransform,
115-
labelTransform,
116-
layerData.getGridLabelFormat(),
117-
intersectionsRB);
97+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsRB);
11898
}
11999
if (!onTopBorder(bounds, y)
120100
&& !onBottomBorder(bounds, y)
@@ -181,42 +161,22 @@ private DefaultFeatureCollection createFeaturesFromNumberOfLines(
181161
Geometry intersectionsLB =
182162
GridUtils.computeLeftBorderIntersections(rotatedBounds, geometryFactory, y);
183163
GridUtils.leftBorderLabel(
184-
labels,
185-
unit,
186-
worldToScreenTransform,
187-
labelTransform,
188-
layerData.getGridLabelFormat(),
189-
intersectionsLB);
164+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsLB);
190165
} else if (i == layerData.numberOfLines[0] + 1) {
191166
Geometry intersectionsRB =
192167
GridUtils.computeRightBorderIntersections(rotatedBounds, geometryFactory, y);
193168
GridUtils.rightBorderLabel(
194-
labels,
195-
unit,
196-
worldToScreenTransform,
197-
labelTransform,
198-
layerData.getGridLabelFormat(),
199-
intersectionsRB);
169+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsRB);
200170
} else if (j == 0) {
201171
Geometry intersectionsBB =
202172
GridUtils.computeBottomBorderIntersections(rotatedBounds, geometryFactory, x);
203173
GridUtils.bottomBorderLabel(
204-
labels,
205-
unit,
206-
worldToScreenTransform,
207-
labelTransform,
208-
layerData.getGridLabelFormat(),
209-
intersectionsBB);
174+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsBB);
210175
} else if (j == layerData.numberOfLines[1] + 1) {
211176
Geometry intersectionsTB =
212177
GridUtils.computeTopBorderIntersections(rotatedBounds, geometryFactory, x);
213178
GridUtils.topBorderLabel(
214-
labels,
215-
unit,
216-
worldToScreenTransform,
217-
labelTransform,
218-
layerData.getGridLabelFormat(),
219-
intersectionsTB);
179+
labels, unit, worldToScreenTransform, layerData, mapCrs, intersectionsTB);
220180
} else {
221181
featureBuilder.reset();
222182
Point geom = geometryFactory.createPoint(new Coordinate(x, y));
529 KB
Loading
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": 250000,
11+
"layers": [
12+
{
13+
"type": "grid",
14+
"gridType": "points",
15+
"numberOfLines": [5, 5],
16+
"renderAsSvg": true,
17+
"labelProjection": "EPSG:4326",
18+
"valueFormat": "###,###.00",
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)