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

Last change on this file since 14637 was 14637, checked in by simon04, 7 years ago

Run Checkstyle on scripts/ and apply fixes

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