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

Last change on this file since 17177 was 16344, checked in by simon04, 5 years ago

BuildProjectionDefinitions: print output filename

  • Property svn:eol-style set to native
File size: 16.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.Writer;
6import java.nio.charset.StandardCharsets;
7import java.nio.file.Files;
8import java.nio.file.Path;
9import java.nio.file.Paths;
10import java.util.Arrays;
11import java.util.LinkedHashMap;
12import java.util.List;
13import java.util.Locale;
14import java.util.Map;
15import java.util.TreeMap;
16import java.util.regex.Matcher;
17import java.util.regex.Pattern;
18import java.util.stream.Collectors;
19
20import org.openstreetmap.josm.data.projection.CustomProjection;
21import org.openstreetmap.josm.data.projection.CustomProjection.Param;
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 */
31public final class BuildProjectionDefinitions {
32
33 private static final String PROJ_DIR = "nodist/data/projection";
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";
37 private static final String OUTPUT_EPSG_FILE = "resources/data/projection/custom-epsg";
38
39 private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
40 private static final Map<String, ProjectionDefinition> esriProj4 = new LinkedHashMap<>();
41 private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
42
43 private static final boolean printStats = false;
44
45 // statistics:
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;
59
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
68 private BuildProjectionDefinitions() {
69 }
70
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 {
77 buildList(args.length > 0 ? args[0] : ".");
78 }
79
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());
85 }
86
87 static void initMap(String baseDir, String file, Map<String, ProjectionDefinition> map) throws IOException {
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 }
93 if (list.isEmpty())
94 throw new AssertionError("EPSG file seems corrupted");
95 Pattern badDmsPattern = Pattern.compile("(\\d+(?:\\.\\d+)?d\\d+(?:\\.\\d+)?')(N|S|E|W)");
96 for (ProjectionDefinition pd : list) {
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()));
105 }
106 }
107
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
113 knownGeoidgrids = initList(baseDir, ".gtx");
114 knownNadgrids = initList(baseDir, ".gsb");
115
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)) {
119 out.write("## This file is autogenerated, do not edit!\n");
120 out.write("## Run ant task \"epsg\" to rebuild.\n");
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));
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()) {
131 if (doInclude(pd, true, false)) {
132 write(out, pd);
133 noProj4++;
134 }
135 }
136 out.write("## ESRI-specific projections (source: ESRI):\n");
137 for (ProjectionDefinition pd : esriProj4.values()) {
138 pd = new ProjectionDefinition(pd.code, "ESRI: " + pd.name, pd.definition);
139 if (doInclude(pd, true, true)) {
140 write(out, pd);
141 noEsri++;
142 }
143 }
144 }
145
146 if (printStats) {
147 System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
148 System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
149 System.out.println(String.format("loaded %d entries from %s", esriProj4.size(), PROJ4_ESRI_FILE));
150 System.out.println();
151 System.out.println("some entries from proj.4 have not been included:");
152 System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
153 if (noInProj4 > 0) {
154 System.out.println(String.format(" * ESRI already in the standard EPSG list: %d entries", noInProj4));
155 }
156 System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
157 System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
158 if (noEllipsoid > 0) {
159 System.out.println(String.format(" * unsupported ellipsoids: %d entries", noEllipsoid));
160 System.out.println(" in particular: " + ellipsoidMap);
161 }
162 if (noBaseProjection > 0) {
163 System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
164 System.out.println(" in particular: " + baseProjectionMap);
165 }
166 if (noDatumgrid > 0) {
167 System.out.println(String.format(" * requires data file for vertical datum conversion: %d entries", noDatumgrid));
168 System.out.println(" in particular: " + datumgridMap);
169 }
170 if (noNadgrid > 0) {
171 System.out.println(String.format(" * requires data file for datum conversion: %d entries", noNadgrid));
172 System.out.println(" in particular: " + nadgridMap);
173 }
174 if (noOmercNoBounds > 0) {
175 System.out.println(String.format(
176 " * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries", noOmercNoBounds));
177 }
178 if (noEquatorStereo > 0) {
179 System.out.println(String.format(" * projection is Equatorial Stereographic (see #15970): %d entries", noEquatorStereo));
180 }
181 System.out.println();
182 System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
183 System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
184 System.out.println(String.format("written %d entries from %s", noEsri, PROJ4_ESRI_FILE));
185 }
186 }
187
188 static void write(Writer out, ProjectionDefinition pd) throws IOException {
189 out.write("# " + pd.name + "\n");
190 out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
191 }
192
193 static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm, boolean noIncludeProj4) {
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 }
204 if (noIncludeProj4) {
205 // we already have this projection
206 if (epsgProj4.containsKey(pd.code)) {
207 result = false;
208 noInProj4++;
209 }
210 }
211
212 // exclude deprecated/discontinued projections
213 // EPSG:4296 is also deprecated, but this is not mentioned in the name
214 String lowName = pd.name.toLowerCase(Locale.ENGLISH);
215 if (lowName.contains("deprecated") || lowName.contains("discontinued") || pd.code.equals("EPSG:4296")) {
216 result = false;
217 noDeprecated++;
218 }
219
220 // exclude projections failing
221 // CHECKSTYLE.OFF: LineLength
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)]
245 ).contains(pd.code)) {
246 result = false;
247 }
248 // CHECKSTYLE.ON: LineLength
249
250 Map<String, String> parameters;
251 try {
252 parameters = CustomProjection.parseParameterList(pd.definition, true);
253 } catch (ProjectionConfigurationException ex) {
254 throw new IllegalStateException(pd.code + ":" + ex, ex);
255 }
256 String proj = parameters.get(CustomProjection.Param.proj.key);
257 if (proj == null) {
258 result = false;
259 }
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
275 // requires vertical datum conversion database (.gtx)
276 String geoidgrids = parameters.get("geoidgrids");
277 if (geoidgrids != null && !"@null".equals(geoidgrids) && !knownGeoidgrids.contains(geoidgrids)) {
278 result = false;
279 noDatumgrid++;
280 incMap(datumgridMap, geoidgrids);
281 }
282
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
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)) {
297 incMap(baseProjectionMap, proj);
298 }
299 }
300
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
309 if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
310 result = false;
311 noOmercNoBounds++;
312 }
313
314 final double eps10 = 1.e-10;
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
321 if (result && "stere".equals(proj) && Math.abs(latitudeOfOrigin) < eps10) {
322 result = false;
323 noEquatorStereo++;
324 }
325
326 // exclude entries which need geodesic computation (equatorial/oblique azimuthal equidistant)
327 if (result && "aeqd".equals(proj)) {
328 final double halfPi = Math.PI / 2;
329 if (Math.abs(latitudeOfOrigin - halfPi) >= eps10 &&
330 Math.abs(latitudeOfOrigin + halfPi) >= eps10) {
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
346 String k0 = parameters.get("k_0");
347 if (result && k0 != null && k0.startsWith("-")) {
348 // Proj fails with "k <= 0" for ESRI:102470
349 result = false;
350 }
351
352 return result;
353 }
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 }
359}
Note: See TracBrowser for help on using the repository browser.