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

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

see #16129 - projections rework for new ESRI file

  • Property svn:eol-style set to native
File size: 14.3 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.Arrays;
10import java.util.LinkedHashMap;
11import java.util.List;
12import java.util.Locale;
13import java.util.Map;
14import java.util.TreeMap;
15import java.util.regex.Matcher;
16import java.util.regex.Pattern;
17
18import org.openstreetmap.josm.data.projection.CustomProjection;
19import org.openstreetmap.josm.data.projection.CustomProjection.Param;
20import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
21import org.openstreetmap.josm.data.projection.Projections;
22import org.openstreetmap.josm.data.projection.Projections.ProjectionDefinition;
23import org.openstreetmap.josm.data.projection.proj.Proj;
24
25/**
26 * Generates the list of projections by combining two sources: The list from the
27 * proj.4 project and a list maintained by the JOSM team.
28 */
29public class BuildProjectionDefinitions {
30
31 private static final String PROJ_DIR = "data_nodist/projection";
32 private static final String JOSM_EPSG_FILE = "josm-epsg";
33 private static final String PROJ4_EPSG_FILE = "epsg";
34 private static final String PROJ4_ESRI_FILE = "esri";
35 private static final String OUTPUT_EPSG_FILE = "data/projection/custom-epsg";
36
37 private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
38 private static final Map<String, ProjectionDefinition> esriProj4 = new LinkedHashMap<>();
39 private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
40
41 private static final boolean printStats = false;
42
43 // statistics:
44 private static int noInJosm = 0;
45 private static int noInProj4 = 0;
46 private static int noDeprecated = 0;
47 private static int noGeocent = 0;
48 private static int noBaseProjection = 0;
49 private static int noEllipsoid = 0;
50 private static int noNadgrid = 0;
51 private static int noDatumgrid = 0;
52 private static int noJosm = 0;
53 private static int noProj4 = 0;
54 private static int noEsri = 0;
55 private static int noOmercNoBounds = 0;
56 private static int noEquatorStereo = 0;
57
58 private static final Map<String, Integer> baseProjectionMap = new TreeMap<>();
59 private static final Map<String, Integer> ellipsoidMap = new TreeMap<>();
60 private static final Map<String, Integer> nadgridMap = new TreeMap<>();
61 private static final Map<String, Integer> datumgridMap = new TreeMap<>();
62
63 private static List<String> knownGeoidgrids;
64 private static List<String> knownNadgrids;
65
66 /**
67 * Program entry point
68 * @param args command line arguments (not used)
69 * @throws IOException if any I/O error occurs
70 */
71 public static void main(String[] args) throws IOException {
72 buildList(args[0]);
73 }
74
75 static List<String> initList(String baseDir, String ext) {
76 return Arrays.asList(new File(baseDir + File.separator + PROJ_DIR)
77 .list((dir, name) -> !name.contains(".") || name.toLowerCase(Locale.ENGLISH).endsWith(ext)));
78 }
79
80 static void initMap(String baseDir, String file, Map<String, ProjectionDefinition> map) throws IOException {
81 List<ProjectionDefinition> list = Projections.loadProjectionDefinitions(
82 baseDir + File.separator + PROJ_DIR + File.separator + file);
83 if (list.isEmpty())
84 throw new AssertionError("EPSG file seems corrupted");
85 Pattern badDmsPattern = Pattern.compile("(\\d+(?:\\.\\d+)?d\\d+(?:\\.\\d+)?')(N|S|E|W)");
86 for (ProjectionDefinition pd : list) {
87 // DMS notation without second causes problems with cs2cs, add 0"
88 Matcher matcher = badDmsPattern.matcher(pd.definition);
89 StringBuffer sb = new StringBuffer();
90 while (matcher.find()) {
91 matcher.appendReplacement(sb, matcher.group(1) + "0\"" + matcher.group(2));
92 }
93 matcher.appendTail(sb);
94 map.put(pd.code, new ProjectionDefinition(pd.code, pd.name, sb.toString()));
95 }
96 }
97
98 static void buildList(String baseDir) throws IOException {
99 initMap(baseDir, JOSM_EPSG_FILE, epsgJosm);
100 initMap(baseDir, PROJ4_EPSG_FILE, epsgProj4);
101 initMap(baseDir, PROJ4_ESRI_FILE, esriProj4);
102
103 knownGeoidgrids = initList(baseDir, ".gtx");
104 knownNadgrids = initList(baseDir, ".gsb");
105
106 try (FileOutputStream output = new FileOutputStream(baseDir + File.separator + OUTPUT_EPSG_FILE);
107 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
108 out.write("## This file is autogenerated, do not edit!\n");
109 out.write("## Run ant task \"epsg\" to rebuild.\n");
110 out.write(String.format("## Source files are %s (can be changed), %s and %s (copied from the proj.4 project).%n",
111 JOSM_EPSG_FILE, PROJ4_EPSG_FILE, PROJ4_ESRI_FILE));
112 out.write("##\n");
113 out.write("## Entries checked and maintained by the JOSM team:\n");
114 for (ProjectionDefinition pd : epsgJosm.values()) {
115 write(out, pd);
116 noJosm++;
117 }
118 out.write("## Other supported projections (source: proj.4):\n");
119 for (ProjectionDefinition pd : epsgProj4.values()) {
120 if (doInclude(pd, true, false)) {
121 write(out, pd);
122 noProj4++;
123 }
124 }
125 out.write("## ESRI-specific projections (source: ESRI):\n");
126 for (ProjectionDefinition pd : esriProj4.values()) {
127 pd = new ProjectionDefinition(pd.code, "ESRI: " + pd.name, pd.definition);
128 if (doInclude(pd, true, true)) {
129 write(out, pd);
130 noEsri++;
131 }
132 }
133 }
134
135 if (printStats) {
136 System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
137 System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
138 System.out.println(String.format("loaded %d entries from %s", esriProj4.size(), PROJ4_ESRI_FILE));
139 System.out.println();
140 System.out.println("some entries from proj.4 have not been included:");
141 System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
142 if (noInProj4 > 0) {
143 System.out.println(String.format(" * ESRI already in the standard EPSG list: %d entries", noInProj4));
144 }
145 System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
146 System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
147 if (noEllipsoid > 0) {
148 System.out.println(String.format(" * unsupported ellipsoids: %d entries", noEllipsoid));
149 System.out.println(" in particular: " + ellipsoidMap);
150 }
151 if (noBaseProjection > 0) {
152 System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
153 System.out.println(" in particular: " + baseProjectionMap);
154 }
155 if (noDatumgrid > 0) {
156 System.out.println(String.format(" * requires data file for vertical datum conversion: %d entries", noDatumgrid));
157 System.out.println(" in particular: " + datumgridMap);
158 }
159 if (noNadgrid > 0) {
160 System.out.println(String.format(" * requires data file for datum conversion: %d entries", noNadgrid));
161 System.out.println(" in particular: " + nadgridMap);
162 }
163 if (noOmercNoBounds > 0) {
164 System.out.println(String.format(" * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries", noOmercNoBounds));
165 }
166 if (noEquatorStereo > 0) {
167 System.out.println(String.format(" * projection is Equatorial Stereographic (see #15970): %d entries", noEquatorStereo));
168 }
169 System.out.println();
170 System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
171 System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
172 System.out.println(String.format("written %d entries from %s", noEsri, PROJ4_ESRI_FILE));
173 }
174 }
175
176 static void write(BufferedWriter out, ProjectionDefinition pd) throws IOException {
177 out.write("# " + pd.name + "\n");
178 out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
179 }
180
181 static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm, boolean noIncludeProj4) {
182
183 boolean result = true;
184
185 if (noIncludeJosm) {
186 // we already have this projection
187 if (epsgJosm.containsKey(pd.code)) {
188 result = false;
189 noInJosm++;
190 }
191 }
192 if (noIncludeProj4) {
193 // we already have this projection
194 if (epsgProj4.containsKey(pd.code)) {
195 result = false;
196 noInProj4++;
197 }
198 }
199
200 // exclude deprecated/discontinued projections
201 // EPSG:4296 is also deprecated, but this is not mentioned in the name
202 String lowName = pd.name.toLowerCase(Locale.ENGLISH);
203 if (lowName.contains("deprecated") || lowName.contains("discontinued") || pd.code.equals("EPSG:4296")) {
204 result = false;
205 noDeprecated++;
206 }
207
208 // exclude projections failing
209 if (Arrays.asList("EPSG:53025", "EPSG:54025", "EPSG:65062",
210 "EPSG:102061", "EPSG:102062", "EPSG:102121", "EPSG:102212", "EPSG:102366", "EPSG:102445",
211 "EPSG:102491", "EPSG:102591", "EPSG:102631", "EPSG:103232", "EPSG:103235", "EPSG:103238",
212 "EPSG:103241", "EPSG:103371", "EPSG:103471", "EPSG:103474", "EPSG:103475"
213 ).contains(pd.code)) {
214 // 53025/54025: Unsuitable parameters 'lat_1' and 'lat_2' for two point method
215 // Others: cs2cs errors to investigate
216 result = false;
217 }
218
219 Map<String, String> parameters;
220 try {
221 parameters = CustomProjection.parseParameterList(pd.definition, true);
222 } catch (ProjectionConfigurationException ex) {
223 throw new RuntimeException(pd.code+":"+ex);
224 }
225 String proj = parameters.get(CustomProjection.Param.proj.key);
226 if (proj == null) {
227 result = false;
228 }
229
230 // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
231 // JOSM as we only deal with 2D maps
232 if ("geocent".equals(proj)) {
233 result = false;
234 noGeocent++;
235 }
236
237 // no support for NAD27 datum, as it requires a conversion database
238 String datum = parameters.get(CustomProjection.Param.datum.key);
239 if ("NAD27".equals(datum)) {
240 result = false;
241 noDatumgrid++;
242 }
243
244 // requires vertical datum conversion database (.gtx)
245 String geoidgrids = parameters.get("geoidgrids");
246 if (geoidgrids != null && !"@null".equals(geoidgrids) && !knownGeoidgrids.contains(geoidgrids)) {
247 result = false;
248 noDatumgrid++;
249 incMap(datumgridMap, geoidgrids);
250 }
251
252 // requires datum conversion database (.gsb)
253 String nadgrids = parameters.get("nadgrids");
254 if (nadgrids != null && !"@null".equals(nadgrids) && !knownNadgrids.contains(nadgrids)) {
255 result = false;
256 noNadgrid++;
257 incMap(nadgridMap, nadgrids);
258 }
259
260 // exclude entries where we don't support the base projection
261 Proj bp = Projections.getBaseProjection(proj);
262 if (result && !"utm".equals(proj) && bp == null) {
263 result = false;
264 noBaseProjection++;
265 if (!"geocent".equals(proj)) {
266 incMap(baseProjectionMap, proj);
267 }
268 }
269
270 // exclude entries where we don't support the base ellipsoid
271 String ellps = parameters.get("ellps");
272 if (result && ellps != null && Projections.getEllipsoid(ellps) == null) {
273 result = false;
274 noEllipsoid++;
275 incMap(ellipsoidMap, ellps);
276 }
277
278 if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
279 result = false;
280 noOmercNoBounds++;
281 }
282
283 final double EPS10 = 1.e-10;
284
285 String lat0 = parameters.get("lat_0");
286 if (lat0 != null) {
287 try {
288 final double latitudeOfOrigin = Math.toRadians(CustomProjection.parseAngle(lat0, Param.lat_0.key));
289 // TODO: implement equatorial stereographic, see https://josm.openstreetmap.de/ticket/15970
290 if (result && "stere".equals(proj) && Math.abs(latitudeOfOrigin) < EPS10) {
291 result = false;
292 noEquatorStereo++;
293 }
294
295 // exclude entries which need geodesic computation (equatorial/oblique azimuthal equidistant)
296 if (result && "aeqd".equals(proj)) {
297 final double HALF_PI = Math.PI / 2;
298 if (Math.abs(latitudeOfOrigin - HALF_PI) >= EPS10 &&
299 Math.abs(latitudeOfOrigin + HALF_PI) >= EPS10) {
300 // See https://josm.openstreetmap.de/ticket/16129#comment:21
301 result = false;
302 }
303 }
304 } catch (NumberFormatException | ProjectionConfigurationException e) {
305 e.printStackTrace();
306 result = false;
307 }
308 }
309
310 if (result && "0.0".equals(parameters.get("rf"))) {
311 // Proj fails with "reciprocal flattening (1/f) = 0" for
312 result = false; // FIXME Only for some projections?
313 }
314
315 String k_0 = parameters.get("k_0");
316 if (result && k_0 != null && k_0.startsWith("-")) {
317 // Proj fails with "k <= 0" for ESRI:102470
318 result = false;
319 }
320
321 return result;
322 }
323
324 private static void incMap(Map<String, Integer> map, String key) {
325 map.putIfAbsent(key, 0);
326 map.put(key, map.get(key)+1);
327 }
328}
Note: See TracBrowser for help on using the repository browser.