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.Arrays;
|
---|
10 | import java.util.LinkedHashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Locale;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.TreeMap;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
---|
17 | import org.openstreetmap.josm.data.projection.CustomProjection.Param;
|
---|
18 | import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
|
---|
19 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
20 | import org.openstreetmap.josm.data.projection.Projections.ProjectionDefinition;
|
---|
21 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Generates the list of projections by combining two sources: The list from the
|
---|
25 | * proj.4 project and a list maintained by the JOSM team.
|
---|
26 | */
|
---|
27 | public class BuildProjectionDefinitions {
|
---|
28 |
|
---|
29 | private static final String PROJ_DIR = "data_nodist/projection";
|
---|
30 | private static final String JOSM_EPSG_FILE = "josm-epsg";
|
---|
31 | private static final String PROJ4_EPSG_FILE = "epsg";
|
---|
32 | private static final String PROJ4_ESRI_FILE = "esri";
|
---|
33 | private static final String OUTPUT_EPSG_FILE = "data/projection/custom-epsg";
|
---|
34 |
|
---|
35 | private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
|
---|
36 | private static final Map<String, ProjectionDefinition> esriProj4 = new LinkedHashMap<>();
|
---|
37 | private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
|
---|
38 |
|
---|
39 | private static final boolean printStats = false;
|
---|
40 |
|
---|
41 | // statistics:
|
---|
42 | private static int noInJosm = 0;
|
---|
43 | private static int noInProj4 = 0;
|
---|
44 | private static int noDeprecated = 0;
|
---|
45 | private static int noGeocent = 0;
|
---|
46 | private static int noBaseProjection = 0;
|
---|
47 | private static int noEllipsoid = 0;
|
---|
48 | private static int noNadgrid = 0;
|
---|
49 | private static int noDatumgrid = 0;
|
---|
50 | private static int noJosm = 0;
|
---|
51 | private static int noProj4 = 0;
|
---|
52 | private static int noEsri = 0;
|
---|
53 | private static int noOmercNoBounds = 0;
|
---|
54 | private static int noEquatorStereo = 0;
|
---|
55 |
|
---|
56 | private static final Map<String, Integer> baseProjectionMap = new TreeMap<>();
|
---|
57 | private static final Map<String, Integer> ellipsoidMap = new TreeMap<>();
|
---|
58 | private static final Map<String, Integer> nadgridMap = new TreeMap<>();
|
---|
59 | private static final Map<String, Integer> datumgridMap = new TreeMap<>();
|
---|
60 |
|
---|
61 | private static List<String> knownGeoidgrids;
|
---|
62 | private static List<String> knownNadgrids;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Program entry point
|
---|
66 | * @param args command line arguments (not used)
|
---|
67 | * @throws IOException if any I/O error occurs
|
---|
68 | */
|
---|
69 | public static void main(String[] args) throws IOException {
|
---|
70 | buildList(args[0]);
|
---|
71 | }
|
---|
72 |
|
---|
73 | static List<String> initList(String baseDir, String ext) {
|
---|
74 | return Arrays.asList(new File(baseDir + File.separator + PROJ_DIR)
|
---|
75 | .list((dir, name) -> !name.contains(".") || name.toLowerCase(Locale.ENGLISH).endsWith(ext)));
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void initMap(String baseDir, String file, Map<String, ProjectionDefinition> map) throws IOException {
|
---|
79 | List<ProjectionDefinition> list = Projections.loadProjectionDefinitions(
|
---|
80 | baseDir + File.separator + PROJ_DIR + File.separator + file);
|
---|
81 | if (list.isEmpty())
|
---|
82 | throw new AssertionError("EPSG file seems corrupted");
|
---|
83 | for (ProjectionDefinition pd : list) {
|
---|
84 | map.put(pd.code, pd);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void buildList(String baseDir) throws IOException {
|
---|
89 | initMap(baseDir, JOSM_EPSG_FILE, epsgJosm);
|
---|
90 | initMap(baseDir, PROJ4_EPSG_FILE, epsgProj4);
|
---|
91 | initMap(baseDir, PROJ4_ESRI_FILE, esriProj4);
|
---|
92 |
|
---|
93 | knownGeoidgrids = initList(baseDir, ".gtx");
|
---|
94 | knownNadgrids = initList(baseDir, ".gsb");
|
---|
95 |
|
---|
96 | try (FileOutputStream output = new FileOutputStream(baseDir + File.separator + OUTPUT_EPSG_FILE);
|
---|
97 | BufferedWriter out = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
|
---|
98 | out.write("## This file is autogenerated, do not edit!\n");
|
---|
99 | out.write("## Run ant task \"epsg\" to rebuild.\n");
|
---|
100 | out.write(String.format("## Source files are %s (can be changed), %s and %s (copied from the proj.4 project).%n",
|
---|
101 | JOSM_EPSG_FILE, PROJ4_EPSG_FILE, PROJ4_ESRI_FILE));
|
---|
102 | out.write("##\n");
|
---|
103 | out.write("## Entries checked and maintained by the JOSM team:\n");
|
---|
104 | for (ProjectionDefinition pd : epsgJosm.values()) {
|
---|
105 | write(out, pd);
|
---|
106 | noJosm++;
|
---|
107 | }
|
---|
108 | out.write("## Other supported projections (source: proj.4):\n");
|
---|
109 | for (ProjectionDefinition pd : epsgProj4.values()) {
|
---|
110 | if (doInclude(pd, true, false)) {
|
---|
111 | write(out, pd);
|
---|
112 | noProj4++;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | out.write("## ESRI-specific projections (source: proj.4):\n");
|
---|
116 | for (ProjectionDefinition pd : esriProj4.values()) {
|
---|
117 | pd = new ProjectionDefinition(pd.code, "ESRI: " + pd.name, pd.definition);
|
---|
118 | if (doInclude(pd, true, true)) {
|
---|
119 | write(out, pd);
|
---|
120 | noEsri++;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (printStats) {
|
---|
126 | System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
|
---|
127 | System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
|
---|
128 | System.out.println(String.format("loaded %d entries from %s", esriProj4.size(), PROJ4_ESRI_FILE));
|
---|
129 | System.out.println();
|
---|
130 | System.out.println("some entries from proj.4 have not been included:");
|
---|
131 | System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
|
---|
132 | if (noInProj4 > 0) {
|
---|
133 | System.out.println(String.format(" * ESRI already in the standard EPSG list: %d entries", noInProj4));
|
---|
134 | }
|
---|
135 | System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
|
---|
136 | System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
|
---|
137 | if (noEllipsoid > 0) {
|
---|
138 | System.out.println(String.format(" * unsupported ellipsoids: %d entries", noEllipsoid));
|
---|
139 | System.out.println(" in particular: " + ellipsoidMap);
|
---|
140 | }
|
---|
141 | if (noBaseProjection > 0) {
|
---|
142 | System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
|
---|
143 | System.out.println(" in particular: " + baseProjectionMap);
|
---|
144 | }
|
---|
145 | if (noDatumgrid > 0) {
|
---|
146 | System.out.println(String.format(" * requires data file for vertical datum conversion: %d entries", noDatumgrid));
|
---|
147 | System.out.println(" in particular: " + datumgridMap);
|
---|
148 | }
|
---|
149 | if (noNadgrid > 0) {
|
---|
150 | System.out.println(String.format(" * requires data file for datum conversion: %d entries", noNadgrid));
|
---|
151 | System.out.println(" in particular: " + nadgridMap);
|
---|
152 | }
|
---|
153 | if (noOmercNoBounds > 0) {
|
---|
154 | System.out.println(String.format(" * projection is Oblique Mercator (requires bounds), but no bounds specified: %d entries", noOmercNoBounds));
|
---|
155 | }
|
---|
156 | if (noEquatorStereo > 0) {
|
---|
157 | System.out.println(String.format(" * projection is Equatorial Stereographic (see #15970): %d entries", noEquatorStereo));
|
---|
158 | }
|
---|
159 | System.out.println();
|
---|
160 | System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
|
---|
161 | System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
|
---|
162 | System.out.println(String.format("written %d entries from %s", noEsri, PROJ4_ESRI_FILE));
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void write(BufferedWriter out, ProjectionDefinition pd) throws IOException {
|
---|
167 | out.write("# " + pd.name + "\n");
|
---|
168 | out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
|
---|
169 | }
|
---|
170 |
|
---|
171 | static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm, boolean noIncludeProj4) {
|
---|
172 |
|
---|
173 | boolean result = true;
|
---|
174 |
|
---|
175 | if (noIncludeJosm) {
|
---|
176 | // we already have this projection
|
---|
177 | if (epsgJosm.containsKey(pd.code)) {
|
---|
178 | result = false;
|
---|
179 | noInJosm++;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | if (noIncludeProj4) {
|
---|
183 | // we already have this projection
|
---|
184 | if (epsgProj4.containsKey(pd.code)) {
|
---|
185 | result = false;
|
---|
186 | noInProj4++;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | // exclude deprecated/discontinued projections
|
---|
191 | // EPSG:4296 is also deprecated, but this is not mentioned in the name
|
---|
192 | String lowName = pd.name.toLowerCase(Locale.ENGLISH);
|
---|
193 | if (lowName.contains("deprecated") || lowName.contains("discontinued") || pd.code.equals("EPSG:4296")) {
|
---|
194 | result = false;
|
---|
195 | noDeprecated++;
|
---|
196 | }
|
---|
197 |
|
---|
198 | Map<String, String> parameters;
|
---|
199 | try {
|
---|
200 | parameters = CustomProjection.parseParameterList(pd.definition, true);
|
---|
201 | } catch (ProjectionConfigurationException ex) {
|
---|
202 | throw new RuntimeException(pd.code+":"+ex);
|
---|
203 | }
|
---|
204 | String proj = parameters.get(CustomProjection.Param.proj.key);
|
---|
205 | if (proj == null) {
|
---|
206 | result = false;
|
---|
207 | }
|
---|
208 |
|
---|
209 | // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
|
---|
210 | // JOSM as we only deal with 2D maps
|
---|
211 | if ("geocent".equals(proj)) {
|
---|
212 | result = false;
|
---|
213 | noGeocent++;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // no support for NAD27 datum, as it requires a conversion database
|
---|
217 | String datum = parameters.get(CustomProjection.Param.datum.key);
|
---|
218 | if ("NAD27".equals(datum)) {
|
---|
219 | result = false;
|
---|
220 | noDatumgrid++;
|
---|
221 | }
|
---|
222 |
|
---|
223 | // requires vertical datum conversion database (.gtx)
|
---|
224 | String geoidgrids = parameters.get("geoidgrids");
|
---|
225 | if (geoidgrids != null && !"@null".equals(geoidgrids) && !knownGeoidgrids.contains(geoidgrids)) {
|
---|
226 | result = false;
|
---|
227 | noDatumgrid++;
|
---|
228 | incMap(datumgridMap, geoidgrids);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // requires datum conversion database (.gsb)
|
---|
232 | String nadgrids = parameters.get("nadgrids");
|
---|
233 | if (nadgrids != null && !"@null".equals(nadgrids) && !knownNadgrids.contains(nadgrids)) {
|
---|
234 | result = false;
|
---|
235 | noNadgrid++;
|
---|
236 | incMap(nadgridMap, nadgrids);
|
---|
237 | }
|
---|
238 |
|
---|
239 | // exclude entries where we don't support the base projection
|
---|
240 | Proj bp = Projections.getBaseProjection(proj);
|
---|
241 | if (result && !"utm".equals(proj) && bp == null) {
|
---|
242 | result = false;
|
---|
243 | noBaseProjection++;
|
---|
244 | if (!"geocent".equals(proj)) {
|
---|
245 | incMap(baseProjectionMap, proj);
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | // exclude entries where we don't support the base ellipsoid
|
---|
250 | String ellps = parameters.get("ellps");
|
---|
251 | if (result && ellps != null && Projections.getEllipsoid(ellps) == null) {
|
---|
252 | result = false;
|
---|
253 | noEllipsoid++;
|
---|
254 | incMap(ellipsoidMap, ellps);
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (result && "omerc".equals(proj) && !parameters.containsKey(CustomProjection.Param.bounds.key)) {
|
---|
258 | result = false;
|
---|
259 | noOmercNoBounds++;
|
---|
260 | }
|
---|
261 |
|
---|
262 | final double EPS10 = 1.e-10;
|
---|
263 |
|
---|
264 | String lat0 = parameters.get("lat_0");
|
---|
265 | if (lat0 != null) {
|
---|
266 | try {
|
---|
267 | final double latitudeOfOrigin = Math.toRadians(CustomProjection.parseAngle(lat0, Param.lat_0.key));
|
---|
268 | // TODO: implement equatorial stereographic, see https://josm.openstreetmap.de/ticket/15970
|
---|
269 | if (result && "stere".equals(proj) && Math.abs(latitudeOfOrigin) < EPS10) {
|
---|
270 | result = false;
|
---|
271 | noEquatorStereo++;
|
---|
272 | }
|
---|
273 |
|
---|
274 | // exclude entries which need geodesic computation (equatorial/oblique azimuthal equidistant)
|
---|
275 | if (result && "aeqd".equals(proj)) {
|
---|
276 | final double HALF_PI = Math.PI / 2;
|
---|
277 | if (Math.abs(latitudeOfOrigin - HALF_PI) >= EPS10 &&
|
---|
278 | Math.abs(latitudeOfOrigin + HALF_PI) >= EPS10) {
|
---|
279 | // See https://josm.openstreetmap.de/ticket/16129#comment:21
|
---|
280 | result = false;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | } catch (NumberFormatException | ProjectionConfigurationException e) {
|
---|
284 | e.printStackTrace();
|
---|
285 | result = false;
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (result && "0.0".equals(parameters.get("rf"))) {
|
---|
290 | // Proj fails with "reciprocal flattening (1/f) = 0" for
|
---|
291 | result = false; // FIXME Only for some projections?
|
---|
292 | }
|
---|
293 |
|
---|
294 | String k_0 = parameters.get("k_0");
|
---|
295 | if (result && k_0 != null && k_0.startsWith("-")) {
|
---|
296 | // Proj fails with "k <= 0" for ESRI:102470
|
---|
297 | result = false;
|
---|
298 | }
|
---|
299 |
|
---|
300 | return result;
|
---|
301 | }
|
---|
302 |
|
---|
303 | private static void incMap(Map<String, Integer> map, String key) {
|
---|
304 | map.putIfAbsent(key, 0);
|
---|
305 | map.put(key, map.get(key)+1);
|
---|
306 | }
|
---|
307 | }
|
---|