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

Last change on this file since 19035 was 18801, checked in by taylor.smock, 17 months ago

Fix #22832: Code cleanup and some simplification, documentation fixes (patch by gaben)

There should not be any functional changes in this patch; it is intended to do
the following:

  • Simplify and cleanup code (example: Arrays.asList(item) -> Collections.singletonList(item))
  • Fix typos in documentation (which also corrects the documentation to match what actually happens, in some cases)
  • Property svn:eol-style set to native
File size: 16.5 KB
RevLine 
[9133]1// License: GPL. For details, see LICENSE file.
2
[14638]3import java.io.BufferedReader;
[9133]4import java.io.IOException;
[14638]5import java.io.Writer;
[9133]6import java.nio.charset.StandardCharsets;
[14638]7import java.nio.file.Files;
8import java.nio.file.Path;
9import java.nio.file.Paths;
[13598]10import java.util.Arrays;
[9133]11import java.util.LinkedHashMap;
[13598]12import java.util.List;
13import java.util.Locale;
[9133]14import java.util.Map;
[13598]15import java.util.TreeMap;
[13602]16import java.util.regex.Matcher;
17import java.util.regex.Pattern;
[14638]18import java.util.stream.Collectors;
[9133]19
20import org.openstreetmap.josm.data.projection.CustomProjection;
[13598]21import org.openstreetmap.josm.data.projection.CustomProjection.Param;
[9133]22import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
23import org.openstreetmap.josm.data.projection.Projections;
24import org.openstreetmap.josm.data.projection.Projections.ProjectionDefinition;
25import org.openstreetmap.josm.data.projection.proj.Proj;
26
27/**
28 * Generates the list of projections by combining two sources: The list from the
29 * proj.4 project and a list maintained by the JOSM team.
30 */
[14637]31public final class BuildProjectionDefinitions {
[9133]32
[16006]33 private static final String PROJ_DIR = "nodist/data/projection";
[13598]34 private static final String JOSM_EPSG_FILE = "josm-epsg";
35 private static final String PROJ4_EPSG_FILE = "epsg";
36 private static final String PROJ4_ESRI_FILE = "esri";
[16006]37 private static final String OUTPUT_EPSG_FILE = "resources/data/projection/custom-epsg";
[9133]38
39 private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
[13395]40 private static final Map<String, ProjectionDefinition> esriProj4 = new LinkedHashMap<>();
[9133]41 private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
42
43 private static final boolean printStats = false;
44
45 // statistics:
[14638]46 private static int noInJosm;
47 private static int noInProj4;
48 private static int noDeprecated;
49 private static int noGeocent;
50 private static int noBaseProjection;
51 private static int noEllipsoid;
52 private static int noNadgrid;
53 private static int noDatumgrid;
54 private static int noJosm;
55 private static int noProj4;
56 private static int noEsri;
57 private static int noOmercNoBounds;
58 private static int noEquatorStereo;
[9133]59
[13598]60 private static final Map<String, Integer> baseProjectionMap = new TreeMap<>();
61 private static final Map<String, Integer> ellipsoidMap = new TreeMap<>();
62 private static final Map<String, Integer> nadgridMap = new TreeMap<>();
63 private static final Map<String, Integer> datumgridMap = new TreeMap<>();
64
65 private static List<String> knownGeoidgrids;
66 private static List<String> knownNadgrids;
67
[14637]68 private BuildProjectionDefinitions() {
69 }
70
[9231]71 /**
72 * Program entry point
73 * @param args command line arguments (not used)
74 * @throws IOException if any I/O error occurs
75 */
76 public static void main(String[] args) throws IOException {
[16343]77 buildList(args.length > 0 ? args[0] : ".");
[9133]78 }
79
[14638]80 static List<String> initList(String baseDir, String ext) throws IOException {
81 return Files.list(Paths.get(baseDir).resolve(PROJ_DIR))
82 .map(path -> path.getFileName().toString())
83 .filter(name -> !name.contains(".") || name.toLowerCase(Locale.ENGLISH).endsWith(ext))
84 .collect(Collectors.toList());
[13598]85 }
86
[13395]87 static void initMap(String baseDir, String file, Map<String, ProjectionDefinition> map) throws IOException {
[14638]88 final Path path = Paths.get(baseDir).resolve(PROJ_DIR).resolve(file);
89 final List<ProjectionDefinition> list;
90 try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
91 list = Projections.loadProjectionDefinitions(reader);
92 }
[13599]93 if (list.isEmpty())
94 throw new AssertionError("EPSG file seems corrupted");
[18801]95 Pattern badDmsPattern = Pattern.compile("(\\d+(?:\\.\\d+)?d\\d+(?:\\.\\d+)?')([NSEW])");
[13599]96 for (ProjectionDefinition pd : list) {
[13602]97 // DMS notation without second causes problems with cs2cs, add 0"
98 Matcher matcher = badDmsPattern.matcher(pd.definition);
99 StringBuffer sb = new StringBuffer();
100 while (matcher.find()) {
101 matcher.appendReplacement(sb, matcher.group(1) + "0\"" + matcher.group(2));
102 }
103 matcher.appendTail(sb);
104 map.put(pd.code, new ProjectionDefinition(pd.code, pd.name, sb.toString()));
[9133]105 }
[13395]106 }
[9133]107
[13395]108 static void buildList(String baseDir) throws IOException {
109 initMap(baseDir, JOSM_EPSG_FILE, epsgJosm);
110 initMap(baseDir, PROJ4_EPSG_FILE, epsgProj4);
111 initMap(baseDir, PROJ4_ESRI_FILE, esriProj4);
112
[13598]113 knownGeoidgrids = initList(baseDir, ".gtx");
114 knownNadgrids = initList(baseDir, ".gsb");
115
[16344]116 Path output = Paths.get(baseDir).resolve(OUTPUT_EPSG_FILE);
117 System.out.println("Writing file " + output);
118 try (Writer out = Files.newBufferedWriter(output, StandardCharsets.UTF_8)) {
[9133]119 out.write("## This file is autogenerated, do not edit!\n");
120 out.write("## Run ant task \"epsg\" to rebuild.\n");
[13395]121 out.write(String.format("## Source files are %s (can be changed), %s and %s (copied from the proj.4 project).%n",
122 JOSM_EPSG_FILE, PROJ4_EPSG_FILE, PROJ4_ESRI_FILE));
[9133]123 out.write("##\n");
124 out.write("## Entries checked and maintained by the JOSM team:\n");
125 for (ProjectionDefinition pd : epsgJosm.values()) {
126 write(out, pd);
127 noJosm++;
128 }
129 out.write("## Other supported projections (source: proj.4):\n");
130 for (ProjectionDefinition pd : epsgProj4.values()) {
[13395]131 if (doInclude(pd, true, false)) {
[9133]132 write(out, pd);
133 noProj4++;
134 }
135 }
[13602]136 out.write("## ESRI-specific projections (source: ESRI):\n");
[13395]137 for (ProjectionDefinition pd : esriProj4.values()) {
[13583]138 pd = new ProjectionDefinition(pd.code, "ESRI: " + pd.name, pd.definition);
[13395]139 if (doInclude(pd, true, true)) {
140 write(out, pd);
141 noEsri++;
142 }
143 }
[9133]144 }
145
146 if (printStats) {
[18801]147 System.out.printf("loaded %d entries from %s%n", epsgJosm.size(), JOSM_EPSG_FILE);
148 System.out.printf("loaded %d entries from %s%n", epsgProj4.size(), PROJ4_EPSG_FILE);
149 System.out.printf("loaded %d entries from %s%n", esriProj4.size(), PROJ4_ESRI_FILE);
[9133]150 System.out.println();
151 System.out.println("some entries from proj.4 have not been included:");
[18801]152 System.out.printf(" * already in the maintained JOSM list: %d entries%n", noInJosm);
[13598]153 if (noInProj4 > 0) {
[18801]154 System.out.printf(" * ESRI already in the standard EPSG list: %d entries%n", noInProj4);
[13598]155 }
[18801]156 System.out.printf(" * deprecated: %d entries%n", noDeprecated);
157 System.out.printf(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries%n", noGeocent);
[13598]158 if (noEllipsoid > 0) {
[18801]159 System.out.printf(" * unsupported ellipsoids: %d entries%n", noEllipsoid);
[13598]160 System.out.println(" in particular: " + ellipsoidMap);
161 }
162 if (noBaseProjection > 0) {
[18801]163 System.out.printf(" * unsupported base projection: %d entries%n", noBaseProjection);
[13598]164 System.out.println(" in particular: " + baseProjectionMap);
165 }
166 if (noDatumgrid > 0) {
[18801]167 System.out.printf(" * requires data file for vertical datum conversion: %d entries%n", noDatumgrid);
[13598]168 System.out.println(" in particular: " + datumgridMap);
169 }
170 if (noNadgrid > 0) {
[18801]171 System.out.printf(" * requires data file for datum conversion: %d entries%n", noNadgrid);
[13598]172 System.out.println(" in particular: " + nadgridMap);
173 }
[9532]174 if (noOmercNoBounds > 0) {
[18801]175 System.out.printf(
176 " * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries%n", noOmercNoBounds);
[9532]177 }
[13441]178 if (noEquatorStereo > 0) {
[18801]179 System.out.printf(" * projection is Equatorial Stereographic (see #15970): %d entries%n", noEquatorStereo);
[13441]180 }
[9133]181 System.out.println();
[18801]182 System.out.printf("written %d entries from %s%n", noJosm, JOSM_EPSG_FILE);
183 System.out.printf("written %d entries from %s%n", noProj4, PROJ4_EPSG_FILE);
184 System.out.printf("written %d entries from %s%n", noEsri, PROJ4_ESRI_FILE);
[9133]185 }
186 }
187
[14638]188 static void write(Writer out, ProjectionDefinition pd) throws IOException {
[9133]189 out.write("# " + pd.name + "\n");
190 out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
191 }
192
[13395]193 static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm, boolean noIncludeProj4) {
[9133]194
195 boolean result = true;
196
197 if (noIncludeJosm) {
198 // we already have this projection
199 if (epsgJosm.containsKey(pd.code)) {
200 result = false;
201 noInJosm++;
202 }
203 }
[13395]204 if (noIncludeProj4) {
205 // we already have this projection
206 if (epsgProj4.containsKey(pd.code)) {
207 result = false;
208 noInProj4++;
209 }
210 }
[9133]211
[13598]212 // exclude deprecated/discontinued projections
[9133]213 // EPSG:4296 is also deprecated, but this is not mentioned in the name
[13598]214 String lowName = pd.name.toLowerCase(Locale.ENGLISH);
215 if (lowName.contains("deprecated") || lowName.contains("discontinued") || pd.code.equals("EPSG:4296")) {
[9133]216 result = false;
217 noDeprecated++;
218 }
219
[13602]220 // exclude projections failing
[14637]221 // CHECKSTYLE.OFF: LineLength
[13708]222 if (Arrays.asList(
223 // Unsuitable parameters 'lat_1' and 'lat_2' for two point method
224 "EPSG:53025", "EPSG:54025", "EPSG:65062",
225 // ESRI projection defined as UTM 55N but covering a much bigger area
226 "EPSG:102449",
227 // Others: errors to investigate
228 "EPSG:102061", // omerc/evrst69 - Everest_Modified_1969_RSO_Malaya_Meters [Everest Modified 1969 RSO Malaya Meters]
229 "EPSG:102062", // omerc/evrst48 - Kertau_RSO_Malaya_Meters [Kertau RSO Malaya Meters]
230 "EPSG:102121", // omerc/NAD83 - NAD_1983_Michigan_GeoRef_Feet_US [NAD 1983 Michigan GeoRef (US Survey Feet)]
231 "EPSG:102212", // lcc/NAD83 - NAD_1983_WyLAM [NAD 1983 WyLAM]
232 "EPSG:102366", // omerc/GRS80 - NAD_1983_CORS96_StatePlane_Alaska_1_FIPS_5001 [NAD 1983 (CORS96) SPCS Alaska Zone 1]
233 "EPSG:102445", // omerc/GRS80 - NAD_1983_2011_StatePlane_Alaska_1_FIPS_5001_Feet [NAD 1983 2011 SPCS Alaska Zone 1 (US Feet)]
234 "EPSG:102491", // lcc/clrk80ign - Nord_Algerie_Ancienne_Degree [Voirol 1875 (degrees) Nord Algerie Ancienne]
235 "EPSG:102591", // lcc - Nord_Algerie_Degree [Voirol Unifie (degrees) Nord Algerie]
236 "EPSG:102631", // omerc/NAD83 - NAD_1983_StatePlane_Alaska_1_FIPS_5001_Feet [NAD 1983 SPCS Alaska 1 (Feet)]
237 "EPSG:103232", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_I_FIPS_0401 [NAD 1983 (CORS96) SPCS California I]
238 "EPSG:103235", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_IV_FIPS_0404 [NAD 1983 (CORS96) SPCS California IV]
239 "EPSG:103238", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_I_FIPS_0401_Ft_US [NAD 1983 (CORS96) SPCS California I (US Feet)]
240 "EPSG:103241", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_IV_FIPS_0404_Ft_US [NAD 1983 (CORS96) SPCS California IV (US Feet)]
241 "EPSG:103371", // lcc/GRS80 - NAD_1983_HARN_WISCRS_Wood_County_Meters [NAD 1983 HARN Wisconsin CRS Wood (meters)]
242 "EPSG:103471", // lcc/GRS80 - NAD_1983_HARN_WISCRS_Wood_County_Feet [NAD 1983 HARN Wisconsin CRS Wood (US feet)]
243 "EPSG:103474", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_Nebraska_FIPS_2600 [NAD 1983 (CORS96) SPCS Nebraska]
244 "EPSG:103475" // lcc/GRS80 - NAD_1983_CORS96_StatePlane_Nebraska_FIPS_2600_Ft_US [NAD 1983 (CORS96) SPCS Nebraska (US Feet)]
[13602]245 ).contains(pd.code)) {
246 result = false;
247 }
[14637]248 // CHECKSTYLE.ON: LineLength
[13602]249
[9133]250 Map<String, String> parameters;
251 try {
252 parameters = CustomProjection.parseParameterList(pd.definition, true);
253 } catch (ProjectionConfigurationException ex) {
[14638]254 throw new IllegalStateException(pd.code + ":" + ex, ex);
[9133]255 }
256 String proj = parameters.get(CustomProjection.Param.proj.key);
[13599]257 if (proj == null) {
258 result = false;
259 }
[9133]260
261 // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
262 // JOSM as we only deal with 2D maps
263 if ("geocent".equals(proj)) {
264 result = false;
265 noGeocent++;
266 }
267
268 // no support for NAD27 datum, as it requires a conversion database
269 String datum = parameters.get(CustomProjection.Param.datum.key);
270 if ("NAD27".equals(datum)) {
271 result = false;
272 noDatumgrid++;
273 }
274
[13598]275 // requires vertical datum conversion database (.gtx)
276 String geoidgrids = parameters.get("geoidgrids");
277 if (geoidgrids != null && !"@null".equals(geoidgrids) && !knownGeoidgrids.contains(geoidgrids)) {
[9133]278 result = false;
279 noDatumgrid++;
[13598]280 incMap(datumgridMap, geoidgrids);
[9133]281 }
[9667]282
[13598]283 // requires datum conversion database (.gsb)
284 String nadgrids = parameters.get("nadgrids");
285 if (nadgrids != null && !"@null".equals(nadgrids) && !knownNadgrids.contains(nadgrids)) {
286 result = false;
287 noNadgrid++;
288 incMap(nadgridMap, nadgrids);
289 }
290
[9567]291 // exclude entries where we don't support the base projection
292 Proj bp = Projections.getBaseProjection(proj);
293 if (result && !"utm".equals(proj) && bp == null) {
294 result = false;
295 noBaseProjection++;
296 if (!"geocent".equals(proj)) {
[13598]297 incMap(baseProjectionMap, proj);
[9567]298 }
299 }
300
[13598]301 // exclude entries where we don't support the base ellipsoid
302 String ellps = parameters.get("ellps");
303 if (result && ellps != null && Projections.getEllipsoid(ellps) == null) {
304 result = false;
305 noEllipsoid++;
306 incMap(ellipsoidMap, ellps);
307 }
308
[9532]309 if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
310 result = false;
311 noOmercNoBounds++;
312 }
[13598]313
[14638]314 final double eps10 = 1.e-10;
[13598]315
316 String lat0 = parameters.get("lat_0");
317 if (lat0 != null) {
318 try {
319 final double latitudeOfOrigin = Math.toRadians(CustomProjection.parseAngle(lat0, Param.lat_0.key));
320 // TODO: implement equatorial stereographic, see https://josm.openstreetmap.de/ticket/15970
[14638]321 if (result && "stere".equals(proj) && Math.abs(latitudeOfOrigin) < eps10) {
[13598]322 result = false;
323 noEquatorStereo++;
324 }
325
326 // exclude entries which need geodesic computation (equatorial/oblique azimuthal equidistant)
327 if (result && "aeqd".equals(proj)) {
[14638]328 final double halfPi = Math.PI / 2;
329 if (Math.abs(latitudeOfOrigin - halfPi) >= eps10 &&
330 Math.abs(latitudeOfOrigin + halfPi) >= eps10) {
[13598]331 // See https://josm.openstreetmap.de/ticket/16129#comment:21
332 result = false;
333 }
334 }
335 } catch (NumberFormatException | ProjectionConfigurationException e) {
336 e.printStackTrace();
337 result = false;
338 }
339 }
340
341 if (result && "0.0".equals(parameters.get("rf"))) {
342 // Proj fails with "reciprocal flattening (1/f) = 0" for
343 result = false; // FIXME Only for some projections?
344 }
345
[14638]346 String k0 = parameters.get("k_0");
347 if (result && k0 != null && k0.startsWith("-")) {
[13598]348 // Proj fails with "k <= 0" for ESRI:102470
[13441]349 result = false;
350 }
[9133]351
352 return result;
353 }
[13598]354
355 private static void incMap(Map<String, Integer> map, String key) {
356 map.putIfAbsent(key, 0);
357 map.put(key, map.get(key)+1);
358 }
[9667]359}
Note: See TracBrowser for help on using the repository browser.