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

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

spotbugs - fix various issues found with Eclipse plugin

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