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