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

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

see #16129 - hopefully the last update

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