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

Last change on this file since 13555 was 13441, checked in by Don-vip, 6 years ago

see #15880, see #15970 - ignore equatorial stereographic projections for now

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