source: josm/trunk/scripts/BuildProjectionDefinitions.java@ 12019

Last change on this file since 12019 was 11324, checked in by Don-vip, 8 years ago

findbugs

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStreamWriter;
8import java.nio.charset.StandardCharsets;
9import java.util.HashMap;
10import java.util.LinkedHashMap;
11import java.util.List;
12import java.util.Map;
13
14import org.openstreetmap.josm.data.projection.CustomProjection;
15import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
16import org.openstreetmap.josm.data.projection.Projections;
17import org.openstreetmap.josm.data.projection.Projections.ProjectionDefinition;
18import org.openstreetmap.josm.data.projection.proj.Proj;
19
20/**
21 * Generates the list of projections by combining two sources: The list from the
22 * proj.4 project and a list maintained by the JOSM team.
23 */
24public class BuildProjectionDefinitions {
25
26 private static final String JOSM_EPSG_FILE = "data_nodist/projection/josm-epsg";
27 private static final String PROJ4_EPSG_FILE = "data_nodist/projection/epsg";
28 private static final String OUTPUT_EPSG_FILE = "data/projection/custom-epsg";
29
30 private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
31 private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
32
33 private static final boolean printStats = false;
34
35 // statistics:
36 private static int noInJosm = 0;
37 private static int noDeprecated = 0;
38 private static int noGeocent = 0;
39 private static int noBaseProjection = 0;
40 private static final Map<String, Integer> baseProjectionMap = new HashMap<>();
41 private static int noDatumgrid = 0;
42 private static int noJosm = 0;
43 private static int noProj4 = 0;
44 private static int noOmercNoBounds = 0;
45
46 /**
47 * Program entry point
48 * @param args command line arguments (not used)
49 * @throws IOException if any I/O error occurs
50 */
51 public static void main(String[] args) throws IOException {
52 buildList(args[0]);
53 }
54
55 static void buildList(String baseDir) throws IOException {
56 List<ProjectionDefinition> pdJosm = Projections.loadProjectionDefinitions(baseDir + File.separator + JOSM_EPSG_FILE);
57 for (ProjectionDefinition pd : pdJosm) {
58 epsgJosm.put(pd.code, pd);
59 }
60 List<ProjectionDefinition> pdProj4 = Projections.loadProjectionDefinitions(baseDir + File.separator + PROJ4_EPSG_FILE);
61 for (ProjectionDefinition pd : pdProj4) {
62 epsgProj4.put(pd.code, pd);
63 }
64
65 try (FileOutputStream output = new FileOutputStream(baseDir + File.separator + OUTPUT_EPSG_FILE);
66 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
67 out.write("## This file is autogenerated, do not edit!\n");
68 out.write("## Run ant task \"epsg\" to rebuild.\n");
69 out.write(String.format("## Source files are %s (can be changed) and %s (copied from the proj.4 project).%n",
70 JOSM_EPSG_FILE, PROJ4_EPSG_FILE));
71 out.write("##\n");
72 out.write("## Entries checked and maintained by the JOSM team:\n");
73 for (ProjectionDefinition pd : epsgJosm.values()) {
74 write(out, pd);
75 noJosm++;
76 }
77 out.write("## Other supported projections (source: proj.4):\n");
78 for (ProjectionDefinition pd : epsgProj4.values()) {
79 if (doInclude(pd, true)) {
80 write(out, pd);
81 noProj4++;
82 }
83 }
84 }
85
86 if (printStats) {
87 System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
88 System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
89 System.out.println();
90 System.out.println("some entries from proj.4 have not been included:");
91 System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
92 System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
93 System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
94 System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
95 System.out.println(" in particular: " + baseProjectionMap);
96 System.out.println(String.format(" * requires data file for datum conversion: %d entries", noDatumgrid));
97 if (noOmercNoBounds > 0) {
98 System.out.println(String.format(" * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries", noOmercNoBounds));
99 }
100 System.out.println();
101 System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
102 System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
103 }
104
105 }
106
107 static void write(BufferedWriter out, ProjectionDefinition pd) throws IOException {
108 out.write("# " + pd.name + "\n");
109 out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
110 }
111
112 static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm) {
113
114 boolean result = true;
115
116 if (noIncludeJosm) {
117 // we already have this projection
118 if (epsgJosm.containsKey(pd.code)) {
119 result = false;
120 noInJosm++;
121 }
122 }
123
124 // exclude deprecated projections
125 // EPSG:4296 is also deprecated, but this is not mentioned in the name
126 if (pd.name.contains("deprecated") || pd.code.equals("EPSG:4296")) {
127 result = false;
128 noDeprecated++;
129 }
130
131 Map<String, String> parameters;
132 try {
133 parameters = CustomProjection.parseParameterList(pd.definition, true);
134 } catch (ProjectionConfigurationException ex) {
135 throw new RuntimeException(pd.code+":"+ex);
136 }
137 String proj = parameters.get(CustomProjection.Param.proj.key);
138
139 // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
140 // JOSM as we only deal with 2D maps
141 if ("geocent".equals(proj)) {
142 result = false;
143 noGeocent++;
144 }
145
146 // no support for NAD27 datum, as it requires a conversion database
147 String datum = parameters.get(CustomProjection.Param.datum.key);
148 if ("NAD27".equals(datum)) {
149 result = false;
150 noDatumgrid++;
151 }
152
153 // requires datum conversion database
154 if (parameters.containsKey("geoidgrids")) {
155 result = false;
156 noDatumgrid++;
157 }
158
159 // exclude entries where we don't support the base projection
160 Proj bp = Projections.getBaseProjection(proj);
161 if (result && !"utm".equals(proj) && bp == null) {
162 result = false;
163 noBaseProjection++;
164 if (!"geocent".equals(proj)) {
165 if (!baseProjectionMap.containsKey(proj)) {
166 baseProjectionMap.put(proj, 0);
167 }
168 baseProjectionMap.put(proj, baseProjectionMap.get(proj)+1);
169 }
170 }
171
172 if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
173 result = false;
174 noOmercNoBounds++;
175 }
176
177 return result;
178 }
179}
Note: See TracBrowser for help on using the repository browser.