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

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

fix #15880 - support ESRI projections and update to EPSG database v9.2 (from proj.4), see:

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