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