1 | // License: GPL. For details, see LICENSE file.
|
---|
2 |
|
---|
3 | import java.io.BufferedWriter;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.OutputStreamWriter;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.LinkedHashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
---|
15 | import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
|
---|
16 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
17 | import org.openstreetmap.josm.data.projection.Projections.ProjectionDefinition;
|
---|
18 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Generates the list of projections by combining two sources: The list from the
|
---|
22 | * proj.4 project and a list maintained by the JOSM team.
|
---|
23 | */
|
---|
24 | public class BuildProjectionDefinitions {
|
---|
25 |
|
---|
26 | private static final String JOSM_EPSG_FILE = "data_nodist/projection/josm-epsg";
|
---|
27 | private static final String PROJ4_EPSG_FILE = "data_nodist/projection/epsg";
|
---|
28 | private static final String OUTPUT_EPSG_FILE = "data/projection/custom-epsg";
|
---|
29 |
|
---|
30 | private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
|
---|
31 | private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
|
---|
32 |
|
---|
33 | private static final boolean printStats = false;
|
---|
34 |
|
---|
35 | // statistics:
|
---|
36 | private static int noInJosm = 0;
|
---|
37 | private static int noDeprecated = 0;
|
---|
38 | private static int noGeocent = 0;
|
---|
39 | private static int noBaseProjection = 0;
|
---|
40 | private static final Map<String, Integer> baseProjectionMap = new HashMap<>();
|
---|
41 | private static int noDatumgrid = 0;
|
---|
42 | private static int noJosm = 0;
|
---|
43 | private static int noProj4 = 0;
|
---|
44 | private static int noOmercNoBounds = 0;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Program entry point
|
---|
48 | * @param args command line arguments (not used)
|
---|
49 | * @throws IOException if any I/O error occurs
|
---|
50 | */
|
---|
51 | public static void main(String[] args) throws IOException {
|
---|
52 | buildList(args[0]);
|
---|
53 | }
|
---|
54 |
|
---|
55 | static void buildList(String baseDir) throws IOException {
|
---|
56 | List<ProjectionDefinition> pdJosm = Projections.loadProjectionDefinitions(baseDir + File.separator + JOSM_EPSG_FILE);
|
---|
57 | for (ProjectionDefinition pd : pdJosm) {
|
---|
58 | epsgJosm.put(pd.code, pd);
|
---|
59 | }
|
---|
60 | List<ProjectionDefinition> pdProj4 = Projections.loadProjectionDefinitions(baseDir + File.separator + PROJ4_EPSG_FILE);
|
---|
61 | for (ProjectionDefinition pd : pdProj4) {
|
---|
62 | epsgProj4.put(pd.code, pd);
|
---|
63 | }
|
---|
64 |
|
---|
65 | try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
---|
66 | new FileOutputStream(baseDir + File.separator + OUTPUT_EPSG_FILE), StandardCharsets.UTF_8))) {
|
---|
67 | out.write("## This file is autogenerated, do not edit!\n");
|
---|
68 | out.write("## Run ant task \"epsg\" to rebuild.\n");
|
---|
69 | out.write(String.format("## Source files are %s (can be changed) and %s (copied from the proj.4 project).%n", JOSM_EPSG_FILE, PROJ4_EPSG_FILE));
|
---|
70 | out.write("##\n");
|
---|
71 | out.write("## Entries checked and maintained by the JOSM team:\n");
|
---|
72 | for (ProjectionDefinition pd : epsgJosm.values()) {
|
---|
73 | write(out, pd);
|
---|
74 | noJosm++;
|
---|
75 | }
|
---|
76 | out.write("## Other supported projections (source: proj.4):\n");
|
---|
77 | for (ProjectionDefinition pd : epsgProj4.values()) {
|
---|
78 | if (doInclude(pd, true)) {
|
---|
79 | write(out, pd);
|
---|
80 | noProj4++;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (printStats) {
|
---|
86 | System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
|
---|
87 | System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
|
---|
88 | System.out.println();
|
---|
89 | System.out.println("some entries from proj.4 have not been included:");
|
---|
90 | System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
|
---|
91 | System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
|
---|
92 | System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
|
---|
93 | System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
|
---|
94 | System.out.println(" in particular: " + baseProjectionMap);
|
---|
95 | System.out.println(String.format(" * requires data file for datum conversion: %d entries", noDatumgrid));
|
---|
96 | if (noOmercNoBounds > 0) {
|
---|
97 | System.out.println(String.format(" * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries", noOmercNoBounds));
|
---|
98 | }
|
---|
99 | System.out.println();
|
---|
100 | System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
|
---|
101 | System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void write(BufferedWriter out, ProjectionDefinition pd) throws IOException {
|
---|
107 | out.write("# " + pd.name + "\n");
|
---|
108 | out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
|
---|
109 | }
|
---|
110 |
|
---|
111 | static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm) {
|
---|
112 |
|
---|
113 | boolean result = true;
|
---|
114 |
|
---|
115 | if (noIncludeJosm) {
|
---|
116 | // we already have this projection
|
---|
117 | if (epsgJosm.containsKey(pd.code)) {
|
---|
118 | result = false;
|
---|
119 | noInJosm++;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | // exclude deprecated projections
|
---|
124 | // EPSG:4296 is also deprecated, but this is not mentioned in the name
|
---|
125 | if (pd.name.contains("deprecated") || pd.code.equals("EPSG:4296")) {
|
---|
126 | result = false;
|
---|
127 | noDeprecated++;
|
---|
128 | }
|
---|
129 |
|
---|
130 | Map<String, String> parameters;
|
---|
131 | try {
|
---|
132 | parameters = CustomProjection.parseParameterList(pd.definition, true);
|
---|
133 | } catch (ProjectionConfigurationException ex) {
|
---|
134 | throw new RuntimeException(pd.code+":"+ex);
|
---|
135 | }
|
---|
136 | String proj = parameters.get(CustomProjection.Param.proj.key);
|
---|
137 |
|
---|
138 | // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
|
---|
139 | // JOSM as we only deal with 2D maps
|
---|
140 | if ("geocent".equals(proj)) {
|
---|
141 | result = false;
|
---|
142 | noGeocent++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | // no support for NAD27 datum, as it requires a conversion database
|
---|
146 | String datum = parameters.get(CustomProjection.Param.datum.key);
|
---|
147 | if ("NAD27".equals(datum)) {
|
---|
148 | result = false;
|
---|
149 | noDatumgrid++;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // requires datum conversion database
|
---|
153 | if (parameters.containsKey("geoidgrids")) {
|
---|
154 | result = false;
|
---|
155 | noDatumgrid++;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // exclude entries where we don't support the base projection
|
---|
159 | Proj bp = Projections.getBaseProjection(proj);
|
---|
160 | if (result && !"utm".equals(proj) && bp == null) {
|
---|
161 | result = false;
|
---|
162 | noBaseProjection++;
|
---|
163 | if (!"geocent".equals(proj)) {
|
---|
164 | if (!baseProjectionMap.containsKey(proj)) {
|
---|
165 | baseProjectionMap.put(proj, 0);
|
---|
166 | }
|
---|
167 | baseProjectionMap.put(proj, baseProjectionMap.get(proj)+1);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
|
---|
172 | result = false;
|
---|
173 | noOmercNoBounds++;
|
---|
174 | }
|
---|
175 |
|
---|
176 | return result;
|
---|
177 | }
|
---|
178 | }
|
---|