1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.image.BufferedImage;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileNotFoundException;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.nio.file.Files;
|
---|
12 | import java.nio.file.Paths;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Locale;
|
---|
16 | import java.util.Optional;
|
---|
17 | import java.util.function.DoubleSupplier;
|
---|
18 | import java.util.logging.Level;
|
---|
19 |
|
---|
20 | import javax.imageio.ImageIO;
|
---|
21 |
|
---|
22 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
---|
23 | import org.openstreetmap.josm.CLIModule;
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.Bounds;
|
---|
26 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
27 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
28 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
29 | import org.openstreetmap.josm.data.coor.conversion.LatLonParser;
|
---|
30 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
31 | import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
|
---|
32 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
33 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
34 | import org.openstreetmap.josm.gui.mappaint.RenderingHelper.StyleData;
|
---|
35 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
36 | import org.openstreetmap.josm.io.OsmReader;
|
---|
37 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
38 | import org.openstreetmap.josm.spi.preferences.MemoryPreferences;
|
---|
39 | import org.openstreetmap.josm.tools.I18n;
|
---|
40 | import org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider;
|
---|
41 | import org.openstreetmap.josm.tools.Logging;
|
---|
42 | import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
|
---|
43 |
|
---|
44 | import gnu.getopt.Getopt;
|
---|
45 | import gnu.getopt.LongOpt;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Command line interface for rendering osm data to an image file.
|
---|
49 | *
|
---|
50 | * @since 12906
|
---|
51 | */
|
---|
52 | public class RenderingCLI implements CLIModule {
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The singleton instance of this class.
|
---|
56 | */
|
---|
57 | public static final RenderingCLI INSTANCE = new RenderingCLI();
|
---|
58 |
|
---|
59 | private static final double PIXEL_PER_METER = 96 / 2.54 * 100; // standard value of 96 dpi display resolution
|
---|
60 | private static final int DEFAULT_MAX_IMAGE_SIZE = 20000;
|
---|
61 |
|
---|
62 | private boolean argDebug;
|
---|
63 | private boolean argTrace;
|
---|
64 | private String argInput;
|
---|
65 | private String argOutput;
|
---|
66 | private List<StyleData> argStyles;
|
---|
67 | private Integer argZoom;
|
---|
68 | private Double argScale;
|
---|
69 | private Bounds argBounds;
|
---|
70 | private LatLon argAnchor;
|
---|
71 | private Double argWidthM;
|
---|
72 | private Double argHeightM;
|
---|
73 | private Integer argWidthPx;
|
---|
74 | private Integer argHeightPx;
|
---|
75 | private String argProjection;
|
---|
76 | private Integer argMaxImageSize;
|
---|
77 |
|
---|
78 | private enum Option {
|
---|
79 | HELP(false, 'h'),
|
---|
80 | DEBUG(false, '*'),
|
---|
81 | TRACE(false, '*'),
|
---|
82 | INPUT(true, 'i'),
|
---|
83 | STYLE(true, 's'),
|
---|
84 | SETTING(true, '*'),
|
---|
85 | OUTPUT(true, 'o'),
|
---|
86 | ZOOM(true, 'z'),
|
---|
87 | SCALE(true, '*'),
|
---|
88 | BOUNDS(true, 'b'),
|
---|
89 | ANCHOR(true, '*'),
|
---|
90 | WIDTH_M(true, '*'),
|
---|
91 | HEIGHT_M(true, '*'),
|
---|
92 | WIDTH_PX(true, '*'),
|
---|
93 | HEIGHT_PX(true, '*'),
|
---|
94 | PROJECTION(true, '*'),
|
---|
95 | MAX_IMAGE_SIZE(true, '*');
|
---|
96 |
|
---|
97 | private final String name;
|
---|
98 | private final boolean requiresArg;
|
---|
99 | private final char shortOption;
|
---|
100 |
|
---|
101 | Option(boolean requiresArgument, char shortOption) {
|
---|
102 | this.name = name().toLowerCase(Locale.US).replace('_', '-');
|
---|
103 | this.requiresArg = requiresArgument;
|
---|
104 | this.shortOption = shortOption;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Replies the option name
|
---|
109 | * @return The option name, in lowercase
|
---|
110 | */
|
---|
111 | public String getName() {
|
---|
112 | return name;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Determines if this option requires an argument.
|
---|
117 | * @return {@code true} if this option requires an argument, {@code false} otherwise
|
---|
118 | */
|
---|
119 | public boolean requiresArgument() {
|
---|
120 | return requiresArg;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Replies the short option (single letter) associated with this option.
|
---|
125 | * @return the short option or '*' if there is no short option
|
---|
126 | */
|
---|
127 | public char getShortOption() {
|
---|
128 | return shortOption;
|
---|
129 | }
|
---|
130 |
|
---|
131 | LongOpt toLongOpt() {
|
---|
132 | return new LongOpt(getName(), requiresArgument() ? LongOpt.REQUIRED_ARGUMENT : LongOpt.NO_ARGUMENT, null, getShortOption());
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Data class to hold return values for {@link #determineRenderingArea(DataSet)}.
|
---|
138 | *
|
---|
139 | * Package private access for unit tests.
|
---|
140 | */
|
---|
141 | static class RenderingArea {
|
---|
142 | public Bounds bounds;
|
---|
143 | public double scale; // in east-north units per pixel (unlike the --scale option, which is in meter per meter)
|
---|
144 | }
|
---|
145 |
|
---|
146 | RenderingCLI() {
|
---|
147 | // hide constructor (package private access for unit tests)
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public String getActionKeyword() {
|
---|
152 | return "render";
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public void processArguments(String[] argArray) {
|
---|
157 | try {
|
---|
158 | parseArguments(argArray);
|
---|
159 | initialize();
|
---|
160 | DataSet ds = loadDataset();
|
---|
161 | RenderingArea area = determineRenderingArea(ds);
|
---|
162 | RenderingHelper rh = new RenderingHelper(ds, area.bounds, area.scale, argStyles);
|
---|
163 | checkPreconditions(rh);
|
---|
164 | BufferedImage image = rh.render();
|
---|
165 | writeImageToFile(image);
|
---|
166 | } catch (FileNotFoundException e) {
|
---|
167 | if (Logging.isDebugEnabled()) {
|
---|
168 | e.printStackTrace();
|
---|
169 | }
|
---|
170 | System.err.println(tr("Error - file not found: ''{0}''", e.getMessage()));
|
---|
171 | System.exit(1);
|
---|
172 | } catch (IllegalArgumentException | IllegalDataException | IOException e) {
|
---|
173 | if (Logging.isDebugEnabled()) {
|
---|
174 | e.printStackTrace();
|
---|
175 | }
|
---|
176 | if (e.getMessage() != null) {
|
---|
177 | System.err.println(tr("Error: {0}", e.getMessage()));
|
---|
178 | }
|
---|
179 | System.exit(1);
|
---|
180 | }
|
---|
181 | System.exit(0);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Parse command line arguments and do some low-level error checking.
|
---|
186 | * @param argArray the arguments array
|
---|
187 | */
|
---|
188 | void parseArguments(String[] argArray) {
|
---|
189 | Getopt.setI18nHandler(I18n::tr);
|
---|
190 | Logging.setLogLevel(Level.INFO);
|
---|
191 |
|
---|
192 | LongOpt[] opts = new LongOpt[Option.values().length];
|
---|
193 | StringBuilder optString = new StringBuilder();
|
---|
194 | for (Option o : Option.values()) {
|
---|
195 | opts[o.ordinal()] = o.toLongOpt();
|
---|
196 | if (o.getShortOption() != '*') {
|
---|
197 | optString.append(o.getShortOption());
|
---|
198 | if (o.requiresArgument()) {
|
---|
199 | optString.append(':');
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | Getopt getopt = new Getopt("JOSM rendering", argArray, optString.toString(), opts);
|
---|
205 |
|
---|
206 | StyleData currentStyle = new StyleData();
|
---|
207 | argStyles = new ArrayList<>();
|
---|
208 |
|
---|
209 | int c;
|
---|
210 | while ((c = getopt.getopt()) != -1) {
|
---|
211 | switch (c) {
|
---|
212 | case 'h':
|
---|
213 | showHelp();
|
---|
214 | System.exit(0);
|
---|
215 | case 'i':
|
---|
216 | argInput = getopt.getOptarg();
|
---|
217 | break;
|
---|
218 | case 's':
|
---|
219 | if (currentStyle.styleUrl != null) {
|
---|
220 | argStyles.add(currentStyle);
|
---|
221 | currentStyle = new StyleData();
|
---|
222 | }
|
---|
223 | currentStyle.styleUrl = getopt.getOptarg();
|
---|
224 | break;
|
---|
225 | case 'o':
|
---|
226 | argOutput = getopt.getOptarg();
|
---|
227 | break;
|
---|
228 | case 'z':
|
---|
229 | try {
|
---|
230 | argZoom = Integer.valueOf(getopt.getOptarg());
|
---|
231 | } catch (NumberFormatException nfe) {
|
---|
232 | throw new IllegalArgumentException(
|
---|
233 | tr("Expected integer number for option {0}, but got ''{1}''", "--zoom", getopt.getOptarg()), nfe);
|
---|
234 | }
|
---|
235 | if (argZoom < 0)
|
---|
236 | throw new IllegalArgumentException(
|
---|
237 | tr("Expected integer number >= 0 for option {0}, but got ''{1}''", "--zoom", getopt.getOptarg()));
|
---|
238 | break;
|
---|
239 | case 'b':
|
---|
240 | if (!"auto".equals(getopt.getOptarg())) {
|
---|
241 | try {
|
---|
242 | argBounds = new Bounds(getopt.getOptarg(), ",", Bounds.ParseMethod.LEFT_BOTTOM_RIGHT_TOP, false);
|
---|
243 | } catch (IllegalArgumentException iae) { // NOPMD
|
---|
244 | throw new IllegalArgumentException(tr("Unable to parse {0} parameter: {1}", "--bounds", iae.getMessage()), iae);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | break;
|
---|
248 | case '*':
|
---|
249 | switch (Option.values()[getopt.getLongind()]) {
|
---|
250 | case DEBUG:
|
---|
251 | argDebug = true;
|
---|
252 | break;
|
---|
253 | case TRACE:
|
---|
254 | argTrace = true;
|
---|
255 | break;
|
---|
256 | case SETTING:
|
---|
257 | String keyval = getopt.getOptarg();
|
---|
258 | String[] comp = keyval.split(":");
|
---|
259 | if (comp.length != 2)
|
---|
260 | throw new IllegalArgumentException(
|
---|
261 | tr("Expected key and value, separated by '':'' character for option {0}, but got ''{1}''",
|
---|
262 | "--setting", getopt.getOptarg()));
|
---|
263 | currentStyle.settings.put(comp[0].trim(), comp[1].trim());
|
---|
264 | break;
|
---|
265 | case SCALE:
|
---|
266 | try {
|
---|
267 | argScale = JosmDecimalFormatSymbolsProvider.parseDouble(getopt.getOptarg());
|
---|
268 | } catch (NumberFormatException nfe) {
|
---|
269 | throw new IllegalArgumentException(
|
---|
270 | tr("Expected floating point number for option {0}, but got ''{1}''", "--scale", getopt.getOptarg()), nfe);
|
---|
271 | }
|
---|
272 | break;
|
---|
273 | case ANCHOR:
|
---|
274 | String[] parts = getopt.getOptarg().split(",");
|
---|
275 | if (parts.length != 2)
|
---|
276 | throw new IllegalArgumentException(
|
---|
277 | tr("Expected two coordinates, separated by comma, for option {0}, but got ''{1}''",
|
---|
278 | "--anchor", getopt.getOptarg()));
|
---|
279 | try {
|
---|
280 | double lon = LatLonParser.parseCoordinate(parts[0]);
|
---|
281 | double lat = LatLonParser.parseCoordinate(parts[1]);
|
---|
282 | argAnchor = new LatLon(lat, lon);
|
---|
283 | } catch (IllegalArgumentException iae) { // NOPMD
|
---|
284 | throw new IllegalArgumentException(tr("In option {0}: {1}", "--anchor", iae.getMessage()), iae);
|
---|
285 | }
|
---|
286 | break;
|
---|
287 | case WIDTH_M:
|
---|
288 | try {
|
---|
289 | argWidthM = JosmDecimalFormatSymbolsProvider.parseDouble(getopt.getOptarg());
|
---|
290 | } catch (NumberFormatException nfe) {
|
---|
291 | throw new IllegalArgumentException(
|
---|
292 | tr("Expected floating point number for option {0}, but got ''{1}''", "--width-m", getopt.getOptarg()), nfe);
|
---|
293 | }
|
---|
294 | if (argWidthM <= 0) throw new IllegalArgumentException(
|
---|
295 | tr("Expected floating point number > 0 for option {0}, but got ''{1}''", "--width-m", getopt.getOptarg()));
|
---|
296 | break;
|
---|
297 | case HEIGHT_M:
|
---|
298 | try {
|
---|
299 | argHeightM = JosmDecimalFormatSymbolsProvider.parseDouble(getopt.getOptarg());
|
---|
300 | } catch (NumberFormatException nfe) {
|
---|
301 | throw new IllegalArgumentException(
|
---|
302 | tr("Expected floating point number for option {0}, but got ''{1}''", "--height-m", getopt.getOptarg()), nfe);
|
---|
303 | }
|
---|
304 | if (argHeightM <= 0) throw new IllegalArgumentException(
|
---|
305 | tr("Expected floating point number > 0 for option {0}, but got ''{1}''", "--width-m", getopt.getOptarg()));
|
---|
306 | break;
|
---|
307 | case WIDTH_PX:
|
---|
308 | try {
|
---|
309 | argWidthPx = Integer.valueOf(getopt.getOptarg());
|
---|
310 | } catch (NumberFormatException nfe) {
|
---|
311 | throw new IllegalArgumentException(
|
---|
312 | tr("Expected integer number for option {0}, but got ''{1}''", "--width-px", getopt.getOptarg()), nfe);
|
---|
313 | }
|
---|
314 | if (argWidthPx <= 0) throw new IllegalArgumentException(
|
---|
315 | tr("Expected integer number > 0 for option {0}, but got ''{1}''", "--width-px", getopt.getOptarg()));
|
---|
316 | break;
|
---|
317 | case HEIGHT_PX:
|
---|
318 | try {
|
---|
319 | argHeightPx = Integer.valueOf(getopt.getOptarg());
|
---|
320 | } catch (NumberFormatException nfe) {
|
---|
321 | throw new IllegalArgumentException(
|
---|
322 | tr("Expected integer number for option {0}, but got ''{1}''", "--height-px", getopt.getOptarg()), nfe);
|
---|
323 | }
|
---|
324 | if (argHeightPx <= 0) throw new IllegalArgumentException(
|
---|
325 | tr("Expected integer number > 0 for option {0}, but got ''{1}''", "--height-px", getopt.getOptarg()));
|
---|
326 | break;
|
---|
327 | case PROJECTION:
|
---|
328 | argProjection = getopt.getOptarg();
|
---|
329 | break;
|
---|
330 | case MAX_IMAGE_SIZE:
|
---|
331 | try {
|
---|
332 | argMaxImageSize = Integer.valueOf(getopt.getOptarg());
|
---|
333 | } catch (NumberFormatException nfe) {
|
---|
334 | throw new IllegalArgumentException(
|
---|
335 | tr("Expected integer number for option {0}, but got ''{1}''", "--max-image-size", getopt.getOptarg()), nfe);
|
---|
336 | }
|
---|
337 | if (argMaxImageSize < 0) throw new IllegalArgumentException(
|
---|
338 | tr("Expected integer number >= 0 for option {0}, but got ''{1}''", "--max-image-size", getopt.getOptarg()));
|
---|
339 | break;
|
---|
340 | default:
|
---|
341 | throw new AssertionError("Unexpected option index: " + getopt.getLongind());
|
---|
342 | }
|
---|
343 | break;
|
---|
344 | case '?':
|
---|
345 | throw new IllegalArgumentException(); // getopt error
|
---|
346 | default:
|
---|
347 | throw new AssertionError("Unrecognized option: " + c);
|
---|
348 | }
|
---|
349 | }
|
---|
350 | if (currentStyle.styleUrl != null) {
|
---|
351 | argStyles.add(currentStyle);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Displays help on the console
|
---|
357 | */
|
---|
358 | public static void showHelp() {
|
---|
359 | System.out.println(getHelp());
|
---|
360 | }
|
---|
361 |
|
---|
362 | private static String getHelp() {
|
---|
363 | return tr("JOSM rendering command line interface")+"\n\n"+
|
---|
364 | tr("Usage")+":\n"+
|
---|
365 | "\tjava -jar josm.jar render <options>\n\n"+
|
---|
366 | tr("Description")+":\n"+
|
---|
367 | tr("Renders data and saves the result to an image file.")+"\n\n"+
|
---|
368 | tr("Options")+":\n"+
|
---|
369 | "\t--help|-h "+tr("Show this help")+"\n"+
|
---|
370 | "\t--input|-i <file> "+tr("Input data file name (.osm)")+"\n"+
|
---|
371 | "\t--output|-o <file> "+tr("Output image file name (.png); defaults to ''{0}''", "out.png")+"\n"+
|
---|
372 | "\t--style|-s <file> "+tr("Style file to use for rendering (.mapcss or .zip)")+"\n"+
|
---|
373 | "\t "+tr("This option can be repeated to load multiple styles.")+"\n"+
|
---|
374 | "\t--setting <key>:<value> "+tr("Style setting (in JOSM accessible in the style list dialog right click menu)")+"\n"+
|
---|
375 | "\t "+tr("Applies to the last style loaded with the {0} option.", "--style")+"\n"+
|
---|
376 | "\t--zoom|-z <lvl> "+tr("Select zoom level to render. (integer value, 0=entire earth, 18=street level)")+"\n"+
|
---|
377 | "\t--scale <scale> "+tr("Select the map scale")+"\n"+
|
---|
378 | "\t "+tr("A value of 10000 denotes a scale of 1:10000 (1 cm on the map equals 100 m on the ground; "
|
---|
379 | + "display resolution: 96 dpi)")+"\n"+
|
---|
380 | "\t "+tr("Options {0} and {1} are mutually exclusive.", "--zoom", "--scale")+"\n"+
|
---|
381 | "\t--bounds|-b auto|<min_lon>,<min_lat>,<max_lon>,<max_lat>\n"+
|
---|
382 | "\t "+tr("Area to render, default value is ''{0}''", "auto")+"\n"+
|
---|
383 | "\t "+tr("With keyword ''{0}'', the downloaded area in the .osm input file will be used (if recorded).",
|
---|
384 | "auto")+"\n"+
|
---|
385 | "\t--anchor <lon>,<lat> "+tr("Specify bottom left corner of the rendering area")+"\n"+
|
---|
386 | "\t "+tr("Used in combination with width and height options to determine the area to render.")+"\n"+
|
---|
387 | "\t--width-m <number> "+tr("Width of the rendered area, in meter")+"\n"+
|
---|
388 | "\t--height-m <number> "+tr("Height of the rendered area, in meter")+"\n"+
|
---|
389 | "\t--width-px <number> "+tr("Width of the target image, in pixel")+"\n"+
|
---|
390 | "\t--height-px <number> "+tr("Height of the target image, in pixel")+"\n"+
|
---|
391 | "\t--projection <code> "+tr("Projection to use, default value ''{0}'' (web-Mercator)", "epsg:3857")+"\n"+
|
---|
392 | "\t--max-image-size <number> "+tr("Maximum image width/height in pixel (''{0}'' means no limit), default value: {1}",
|
---|
393 | 0, Integer.toString(DEFAULT_MAX_IMAGE_SIZE))+"\n"+
|
---|
394 | "\n"+
|
---|
395 | tr("To specify the rendered area and scale, the options can be combined in various ways")+":\n"+
|
---|
396 | " * --bounds (--zoom|--scale|--width-px|--height-px)\n"+
|
---|
397 | " * --anchor (--width-m|--width-px) (--height-m|--height-px) (--zoom|--scale)\n"+
|
---|
398 | " * --anchor --width-m --height-m (--width-px|--height-px)\n"+
|
---|
399 | " * --anchor --width-px --height-px (--width-m|--height-m)\n"+
|
---|
400 | tr("If neither ''{0}'' nor ''{1}'' is given, the default value {2} takes effect "
|
---|
401 | + "and the bounds of the download area in the .osm input file are used.",
|
---|
402 | "bounds", "anchor", "--bounds=auto")+"\n\n"+
|
---|
403 | tr("Examples")+":\n"+
|
---|
404 | " java -jar josm.jar render -i data.osm -s style.mapcss -z 16\n"+
|
---|
405 | " josm render -i data.osm -s style.mapcss --scale 5000\n"+
|
---|
406 | " josm render -i data.osm -s style.mapcss -z 16 -o image.png\n"+
|
---|
407 | " josm render -i data.osm -s elemstyles.mapcss --setting hide_icons:false -z 16\n"+
|
---|
408 | " josm render -i data.osm -s style.mapcss -s another_style.mapcss -z 16 -o image.png\n"+
|
---|
409 | " josm render -i data.osm -s style.mapcss --bounds 21.151,51.401,21.152,51.402 -z 16\n"+
|
---|
410 | " josm render -i data.osm -s style.mapcss --anchor 21.151,51.401 --width-m 500 --height-m 300 -z 16\n"+
|
---|
411 | " josm render -i data.osm -s style.mapcss --anchor 21.151,51.401 --width-m 500 --height-m 300 --width-px 1800\n"+
|
---|
412 | " josm render -i data.osm -s style.mapcss --scale 5000 --projection epsg:4326\n";
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Initialization.
|
---|
417 | *
|
---|
418 | * Requires arguments to be parsed already ({@link #parseArguments(java.lang.String[])}).
|
---|
419 | */
|
---|
420 | void initialize() {
|
---|
421 | Logging.setLogLevel(getLogLevel());
|
---|
422 |
|
---|
423 | Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance()); // for right-left-hand traffic cache file
|
---|
424 | Config.setPreferencesInstance(new MemoryPreferences());
|
---|
425 | Config.getPref().putBoolean("mappaint.auto_reload_local_styles", false); // unnecessary to listen for external changes
|
---|
426 | String projCode = Optional.ofNullable(argProjection).orElse("epsg:3857");
|
---|
427 | Main.setProjection(Projections.getProjectionByCode(projCode.toUpperCase(Locale.US)));
|
---|
428 |
|
---|
429 | RightAndLefthandTraffic.initialize();
|
---|
430 | }
|
---|
431 |
|
---|
432 | private Level getLogLevel() {
|
---|
433 | if (argTrace) {
|
---|
434 | return Logging.LEVEL_TRACE;
|
---|
435 | } else if (argDebug) {
|
---|
436 | return Logging.LEVEL_DEBUG;
|
---|
437 | } else {
|
---|
438 | return Logging.LEVEL_INFO;
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Find the area to render and the scale, given certain command line options and the dataset.
|
---|
444 | * @param ds the dataset
|
---|
445 | * @return area to render and the scale
|
---|
446 | */
|
---|
447 | RenderingArea determineRenderingArea(DataSet ds) {
|
---|
448 |
|
---|
449 | Projection proj = Main.getProjection();
|
---|
450 | Double scale = null; // scale in east-north units per pixel
|
---|
451 | if (argZoom != null) {
|
---|
452 | scale = OsmMercator.EARTH_RADIUS * Math.PI * 2 / Math.pow(2, argZoom) / OsmMercator.DEFAUL_TILE_SIZE / proj.getMetersPerUnit();
|
---|
453 | }
|
---|
454 | Bounds bounds = argBounds;
|
---|
455 | ProjectionBounds pb = null;
|
---|
456 |
|
---|
457 | if (bounds == null) {
|
---|
458 | if (argAnchor != null) {
|
---|
459 | EastNorth projAnchor = proj.latlon2eastNorth(argAnchor);
|
---|
460 |
|
---|
461 | double enPerMeter = Double.NaN;
|
---|
462 | DoubleSupplier getEnPerMeter = () -> {
|
---|
463 | double shiftMeter = 10;
|
---|
464 | EastNorth projAnchorShifted = projAnchor.add(
|
---|
465 | shiftMeter / proj.getMetersPerUnit(), shiftMeter / proj.getMetersPerUnit());
|
---|
466 | LatLon anchorShifted = proj.eastNorth2latlon(projAnchorShifted);
|
---|
467 | return projAnchor.distance(projAnchorShifted) / argAnchor.greatCircleDistance(anchorShifted);
|
---|
468 | };
|
---|
469 |
|
---|
470 | if (scale == null) {
|
---|
471 | if (argScale != null) {
|
---|
472 | enPerMeter = getEnPerMeter.getAsDouble();
|
---|
473 | scale = argScale * enPerMeter / PIXEL_PER_METER;
|
---|
474 | } else if (argWidthM != null && argWidthPx != null) {
|
---|
475 | enPerMeter = getEnPerMeter.getAsDouble();
|
---|
476 | scale = argWidthM / argWidthPx * enPerMeter;
|
---|
477 | } else if (argHeightM != null && argHeightPx != null) {
|
---|
478 | enPerMeter = getEnPerMeter.getAsDouble();
|
---|
479 | scale = argHeightM / argHeightPx * enPerMeter;
|
---|
480 | } else {
|
---|
481 | throw new IllegalArgumentException(
|
---|
482 | tr("Argument {0} given, but scale cannot be determined from remaining arguments", "--anchor"));
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | double widthEn;
|
---|
487 | if (argWidthM != null) {
|
---|
488 | if (Double.isNaN(enPerMeter)) {
|
---|
489 | enPerMeter = getEnPerMeter.getAsDouble();
|
---|
490 | }
|
---|
491 | widthEn = argWidthM * enPerMeter;
|
---|
492 | } else if (argWidthPx != null) {
|
---|
493 | widthEn = argWidthPx * scale;
|
---|
494 | } else {
|
---|
495 | throw new IllegalArgumentException(
|
---|
496 | tr("Argument {0} given, expected {1} or {2}", "--anchor", "--width-m", "--width-px"));
|
---|
497 | }
|
---|
498 |
|
---|
499 | double heightEn;
|
---|
500 | if (argHeightM != null) {
|
---|
501 | if (Double.isNaN(enPerMeter)) {
|
---|
502 | enPerMeter = getEnPerMeter.getAsDouble();
|
---|
503 | }
|
---|
504 | heightEn = argHeightM * enPerMeter;
|
---|
505 | } else if (argHeightPx != null) {
|
---|
506 | heightEn = argHeightPx * scale;
|
---|
507 | } else {
|
---|
508 | throw new IllegalArgumentException(
|
---|
509 | tr("Argument {0} given, expected {1} or {2}", "--anchor", "--height-m", "--height-px"));
|
---|
510 | }
|
---|
511 | pb = new ProjectionBounds(projAnchor);
|
---|
512 | pb.extend(new EastNorth(projAnchor.east() + widthEn, projAnchor.north() + heightEn));
|
---|
513 | bounds = new Bounds(proj.eastNorth2latlon(pb.getMin()), false);
|
---|
514 | bounds.extend(proj.eastNorth2latlon(pb.getMax()));
|
---|
515 | } else {
|
---|
516 | if (ds.getDataSourceBounds().isEmpty()) {
|
---|
517 | throw new IllegalArgumentException(tr("{0} mode, but no bounds found in osm data input file", "--bounds=auto"));
|
---|
518 | }
|
---|
519 | bounds = ds.getDataSourceBounds().get(0);
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | if (pb == null) {
|
---|
524 | pb = new ProjectionBounds();
|
---|
525 | pb.extend(proj.latlon2eastNorth(bounds.getMin()));
|
---|
526 | pb.extend(proj.latlon2eastNorth(bounds.getMax()));
|
---|
527 | }
|
---|
528 |
|
---|
529 | if (scale == null) {
|
---|
530 | if (argScale != null) {
|
---|
531 | double enPerMeter = pb.getMin().distance(pb.getMax()) / bounds.getMin().greatCircleDistance(bounds.getMax());
|
---|
532 | scale = argScale * enPerMeter / PIXEL_PER_METER;
|
---|
533 | } else if (argWidthPx != null) {
|
---|
534 | scale = (pb.maxEast - pb.minEast) / argWidthPx;
|
---|
535 | } else if (argHeightPx != null) {
|
---|
536 | scale = (pb.maxNorth - pb.minNorth) / argHeightPx;
|
---|
537 | } else {
|
---|
538 | throw new IllegalArgumentException(
|
---|
539 | tr("Unable to determine scale, one of the options {0}, {1}, {2} or {3} expected",
|
---|
540 | "--zoom", "--scale", "--width-px", "--height-px"));
|
---|
541 | }
|
---|
542 | }
|
---|
543 |
|
---|
544 | RenderingArea ra = new RenderingArea();
|
---|
545 | ra.bounds = bounds;
|
---|
546 | ra.scale = scale;
|
---|
547 | return ra;
|
---|
548 | }
|
---|
549 |
|
---|
550 | private DataSet loadDataset() throws IOException, IllegalDataException {
|
---|
551 | if (argInput == null) {
|
---|
552 | throw new IllegalArgumentException(tr("Missing argument - input data file ({0})", "--input|-i"));
|
---|
553 | }
|
---|
554 | try {
|
---|
555 | return OsmReader.parseDataSet(Files.newInputStream(Paths.get(argInput)), null);
|
---|
556 | } catch (IllegalDataException e) {
|
---|
557 | throw new IllegalDataException(tr("In .osm data file ''{0}'' - ", argInput) + e.getMessage(), e);
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | private void checkPreconditions(RenderingHelper rh) {
|
---|
562 | if (argStyles.isEmpty())
|
---|
563 | throw new IllegalArgumentException(tr("Missing argument - at least one style expected ({0})", "--style"));
|
---|
564 |
|
---|
565 | Dimension imgSize = rh.getImageSize();
|
---|
566 | Logging.debug("image size (px): {0}x{1}", imgSize.width, imgSize.height);
|
---|
567 | int maxSize = Optional.ofNullable(argMaxImageSize).orElse(DEFAULT_MAX_IMAGE_SIZE);
|
---|
568 | if (maxSize != 0 && (imgSize.width > maxSize || imgSize.height > maxSize)) {
|
---|
569 | throw new IllegalArgumentException(
|
---|
570 | tr("Image dimensions ({0}x{1}) exceeds maximum image size {2} (use option {3} to change limit)",
|
---|
571 | imgSize.width, imgSize.height, maxSize, "--max-image-size"));
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | private void writeImageToFile(BufferedImage image) throws IOException {
|
---|
576 | String output = Optional.ofNullable(argOutput).orElse("out.png");
|
---|
577 | ImageIO.write(image, "png", new File(output));
|
---|
578 | }
|
---|
579 | }
|
---|