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

Last change on this file since 16062 was 16006, checked in by Don-vip, 5 years ago

see #18140 - reorganization of data(_nodist), images(_nodist), styles(_nodist), IDE and native files in a more practical file tree.

  • Everything belonging to the jar is now in resources (data, images, styles)
  • Everything not belonging to the jar is now in nodist (data, images, styles)
  • Everything related to OS native functions is now in native (linux, macosx, windows)
  • Everything related to an IDE is now in ide (eclipse, netbeans)
  • Property svn:eol-style set to native
File size: 16.6 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[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 try (Writer out = Files.newBufferedWriter(Paths.get(baseDir).resolve(OUTPUT_EPSG_FILE), StandardCharsets.UTF_8)) {
117 out.write("## This file is autogenerated, do not edit!\n");
118 out.write("## Run ant task \"epsg\" to rebuild.\n");
119 out.write(String.format("## Source files are %s (can be changed), %s and %s (copied from the proj.4 project).%n",
120 JOSM_EPSG_FILE, PROJ4_EPSG_FILE, PROJ4_ESRI_FILE));
121 out.write("##\n");
122 out.write("## Entries checked and maintained by the JOSM team:\n");
123 for (ProjectionDefinition pd : epsgJosm.values()) {
124 write(out, pd);
125 noJosm++;
126 }
127 out.write("## Other supported projections (source: proj.4):\n");
128 for (ProjectionDefinition pd : epsgProj4.values()) {
129 if (doInclude(pd, true, false)) {
130 write(out, pd);
131 noProj4++;
132 }
133 }
134 out.write("## ESRI-specific projections (source: ESRI):\n");
135 for (ProjectionDefinition pd : esriProj4.values()) {
136 pd = new ProjectionDefinition(pd.code, "ESRI: " + pd.name, pd.definition);
137 if (doInclude(pd, true, true)) {
138 write(out, pd);
139 noEsri++;
140 }
141 }
142 }
143
144 if (printStats) {
145 System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
146 System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
147 System.out.println(String.format("loaded %d entries from %s", esriProj4.size(), PROJ4_ESRI_FILE));
148 System.out.println();
149 System.out.println("some entries from proj.4 have not been included:");
150 System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
151 if (noInProj4 > 0) {
152 System.out.println(String.format(" * ESRI already in the standard EPSG list: %d entries", noInProj4));
153 }
154 System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
155 System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
156 if (noEllipsoid > 0) {
157 System.out.println(String.format(" * unsupported ellipsoids: %d entries", noEllipsoid));
158 System.out.println(" in particular: " + ellipsoidMap);
159 }
160 if (noBaseProjection > 0) {
161 System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
162 System.out.println(" in particular: " + baseProjectionMap);
163 }
164 if (noDatumgrid > 0) {
165 System.out.println(String.format(" * requires data file for vertical datum conversion: %d entries", noDatumgrid));
166 System.out.println(" in particular: " + datumgridMap);
167 }
168 if (noNadgrid > 0) {
169 System.out.println(String.format(" * requires data file for datum conversion: %d entries", noNadgrid));
170 System.out.println(" in particular: " + nadgridMap);
171 }
172 if (noOmercNoBounds > 0) {
173 System.out.println(String.format(
174 " * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries", noOmercNoBounds));
175 }
176 if (noEquatorStereo > 0) {
177 System.out.println(String.format(" * projection is Equatorial Stereographic (see #15970): %d entries", noEquatorStereo));
178 }
179 System.out.println();
180 System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
181 System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
182 System.out.println(String.format("written %d entries from %s", noEsri, PROJ4_ESRI_FILE));
183 }
184 }
185
186 static void write(Writer out, ProjectionDefinition pd) throws IOException {
187 out.write("# " + pd.name + "\n");
188 out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
189 }
190
191 static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm, boolean noIncludeProj4) {
192
193 boolean result = true;
194
195 if (noIncludeJosm) {
196 // we already have this projection
197 if (epsgJosm.containsKey(pd.code)) {
198 result = false;
199 noInJosm++;
200 }
201 }
202 if (noIncludeProj4) {
203 // we already have this projection
204 if (epsgProj4.containsKey(pd.code)) {
205 result = false;
206 noInProj4++;
207 }
208 }
209
210 // exclude deprecated/discontinued projections
211 // EPSG:4296 is also deprecated, but this is not mentioned in the name
212 String lowName = pd.name.toLowerCase(Locale.ENGLISH);
213 if (lowName.contains("deprecated") || lowName.contains("discontinued") || pd.code.equals("EPSG:4296")) {
214 result = false;
215 noDeprecated++;
216 }
217
218 // exclude projections failing
219 // CHECKSTYLE.OFF: LineLength
220 if (Arrays.asList(
221 // Unsuitable parameters 'lat_1' and 'lat_2' for two point method
222 "EPSG:53025", "EPSG:54025", "EPSG:65062",
223 // ESRI projection defined as UTM 55N but covering a much bigger area
224 "EPSG:102449",
225 // Others: errors to investigate
226 "EPSG:102061", // omerc/evrst69 - Everest_Modified_1969_RSO_Malaya_Meters [Everest Modified 1969 RSO Malaya Meters]
227 "EPSG:102062", // omerc/evrst48 - Kertau_RSO_Malaya_Meters [Kertau RSO Malaya Meters]
228 "EPSG:102121", // omerc/NAD83 - NAD_1983_Michigan_GeoRef_Feet_US [NAD 1983 Michigan GeoRef (US Survey Feet)]
229 "EPSG:102212", // lcc/NAD83 - NAD_1983_WyLAM [NAD 1983 WyLAM]
230 "EPSG:102366", // omerc/GRS80 - NAD_1983_CORS96_StatePlane_Alaska_1_FIPS_5001 [NAD 1983 (CORS96) SPCS Alaska Zone 1]
231 "EPSG:102445", // omerc/GRS80 - NAD_1983_2011_StatePlane_Alaska_1_FIPS_5001_Feet [NAD 1983 2011 SPCS Alaska Zone 1 (US Feet)]
232 "EPSG:102491", // lcc/clrk80ign - Nord_Algerie_Ancienne_Degree [Voirol 1875 (degrees) Nord Algerie Ancienne]
233 "EPSG:102591", // lcc - Nord_Algerie_Degree [Voirol Unifie (degrees) Nord Algerie]
234 "EPSG:102631", // omerc/NAD83 - NAD_1983_StatePlane_Alaska_1_FIPS_5001_Feet [NAD 1983 SPCS Alaska 1 (Feet)]
235 "EPSG:103232", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_I_FIPS_0401 [NAD 1983 (CORS96) SPCS California I]
236 "EPSG:103235", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_IV_FIPS_0404 [NAD 1983 (CORS96) SPCS California IV]
237 "EPSG:103238", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_I_FIPS_0401_Ft_US [NAD 1983 (CORS96) SPCS California I (US Feet)]
238 "EPSG:103241", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_California_IV_FIPS_0404_Ft_US [NAD 1983 (CORS96) SPCS California IV (US Feet)]
239 "EPSG:103371", // lcc/GRS80 - NAD_1983_HARN_WISCRS_Wood_County_Meters [NAD 1983 HARN Wisconsin CRS Wood (meters)]
240 "EPSG:103471", // lcc/GRS80 - NAD_1983_HARN_WISCRS_Wood_County_Feet [NAD 1983 HARN Wisconsin CRS Wood (US feet)]
241 "EPSG:103474", // lcc/GRS80 - NAD_1983_CORS96_StatePlane_Nebraska_FIPS_2600 [NAD 1983 (CORS96) SPCS Nebraska]
242 "EPSG:103475" // lcc/GRS80 - NAD_1983_CORS96_StatePlane_Nebraska_FIPS_2600_Ft_US [NAD 1983 (CORS96) SPCS Nebraska (US Feet)]
243 ).contains(pd.code)) {
244 result = false;
245 }
246 // CHECKSTYLE.ON: LineLength
247
248 Map<String, String> parameters;
249 try {
250 parameters = CustomProjection.parseParameterList(pd.definition, true);
251 } catch (ProjectionConfigurationException ex) {
252 throw new IllegalStateException(pd.code + ":" + ex, ex);
253 }
254 String proj = parameters.get(CustomProjection.Param.proj.key);
255 if (proj == null) {
256 result = false;
257 }
258
259 // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
260 // JOSM as we only deal with 2D maps
261 if ("geocent".equals(proj)) {
262 result = false;
263 noGeocent++;
264 }
265
266 // no support for NAD27 datum, as it requires a conversion database
267 String datum = parameters.get(CustomProjection.Param.datum.key);
268 if ("NAD27".equals(datum)) {
269 result = false;
270 noDatumgrid++;
271 }
272
273 // requires vertical datum conversion database (.gtx)
274 String geoidgrids = parameters.get("geoidgrids");
275 if (geoidgrids != null && !"@null".equals(geoidgrids) && !knownGeoidgrids.contains(geoidgrids)) {
276 result = false;
277 noDatumgrid++;
278 incMap(datumgridMap, geoidgrids);
279 }
280
281 // requires datum conversion database (.gsb)
282 String nadgrids = parameters.get("nadgrids");
283 if (nadgrids != null && !"@null".equals(nadgrids) && !knownNadgrids.contains(nadgrids)) {
284 result = false;
285 noNadgrid++;
286 incMap(nadgridMap, nadgrids);
287 }
288
289 // exclude entries where we don't support the base projection
290 Proj bp = Projections.getBaseProjection(proj);
291 if (result && !"utm".equals(proj) && bp == null) {
292 result = false;
293 noBaseProjection++;
294 if (!"geocent".equals(proj)) {
295 incMap(baseProjectionMap, proj);
296 }
297 }
298
299 // exclude entries where we don't support the base ellipsoid
300 String ellps = parameters.get("ellps");
301 if (result && ellps != null && Projections.getEllipsoid(ellps) == null) {
302 result = false;
303 noEllipsoid++;
304 incMap(ellipsoidMap, ellps);
305 }
306
307 if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
308 result = false;
309 noOmercNoBounds++;
310 }
311
312 final double eps10 = 1.e-10;
313
314 String lat0 = parameters.get("lat_0");
315 if (lat0 != null) {
316 try {
317 final double latitudeOfOrigin = Math.toRadians(CustomProjection.parseAngle(lat0, Param.lat_0.key));
318 // TODO: implement equatorial stereographic, see https://josm.openstreetmap.de/ticket/15970
319 if (result && "stere".equals(proj) && Math.abs(latitudeOfOrigin) < eps10) {
320 result = false;
321 noEquatorStereo++;
322 }
323
324 // exclude entries which need geodesic computation (equatorial/oblique azimuthal equidistant)
325 if (result && "aeqd".equals(proj)) {
326 final double halfPi = Math.PI / 2;
327 if (Math.abs(latitudeOfOrigin - halfPi) >= eps10 &&
328 Math.abs(latitudeOfOrigin + halfPi) >= eps10) {
329 // See https://josm.openstreetmap.de/ticket/16129#comment:21
330 result = false;
331 }
332 }
333 } catch (NumberFormatException | ProjectionConfigurationException e) {
334 e.printStackTrace();
335 result = false;
336 }
337 }
338
339 if (result && "0.0".equals(parameters.get("rf"))) {
340 // Proj fails with "reciprocal flattening (1/f) = 0" for
341 result = false; // FIXME Only for some projections?
342 }
343
344 String k0 = parameters.get("k_0");
345 if (result && k0 != null && k0.startsWith("-")) {
346 // Proj fails with "k <= 0" for ESRI:102470
347 result = false;
348 }
349
350 return result;
351 }
352
353 private static void incMap(Map<String, Integer> map, String key) {
354 map.putIfAbsent(key, 0);
355 map.put(key, map.get(key)+1);
356 }
357}
Note: See TracBrowser for help on using the repository browser.