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

Last change on this file since 9214 was 9133, checked in by bastiK, 8 years ago

see #12186 - add more projections
The list of projections is now generated during the build process:
scripts/BuildEpsgList.java runs and creates data/projection/custom-epsg which is generated by combining data_nodist/projection/josm-epsg (a list maintained by the JOSM team) and data_nodist/projection/epsg (an upstream list from the proj.4 project, git: 3795cdf)
The ant task epsg cares for this.

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3import java.io.BufferedWriter;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.OutputStreamWriter;
7import java.nio.charset.StandardCharsets;
8import java.util.HashMap;
9import java.util.LinkedHashMap;
10import java.util.List;
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 OUTPUT_EPSG_FILE = "data/projection/custom-epsg";
28
29 private static final Map<String, ProjectionDefinition> epsgProj4 = new LinkedHashMap<>();
30 private static final Map<String, ProjectionDefinition> epsgJosm = new LinkedHashMap<>();
31
32 private static final boolean printStats = false;
33
34 // statistics:
35 private static int noInJosm = 0;
36 private static int noDeprecated = 0;
37 private static int noGeocent = 0;
38 private static int noBaseProjection = 0;
39 private static final Map<String, Integer> baseProjectionMap = new HashMap<>();
40 private static int noDatumgrid = 0;
41 private static int noJosm = 0;
42 private static int noProj4 = 0;
43
44 public static void main(String[] args) throws IOException, InterruptedException {
45 buildList();
46 }
47
48 static void buildList() throws IOException {
49 List<ProjectionDefinition> pdJosm = Projections.loadProjectionDefinitions(JOSM_EPSG_FILE);
50 for (ProjectionDefinition pd : pdJosm) {
51 epsgJosm.put(pd.code, pd);
52 }
53 List<ProjectionDefinition> pdProj4 = Projections.loadProjectionDefinitions(PROJ4_EPSG_FILE);
54 for (ProjectionDefinition pd : pdProj4) {
55 epsgProj4.put(pd.code, pd);
56 }
57
58 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
59 new FileOutputStream(OUTPUT_EPSG_FILE), StandardCharsets.UTF_8))) {
60 out.write("## This file is autogenerated, do not edit!\n");
61 out.write("## Run ant task \"epsg\" to rebuild.\n");
62 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));
63 out.write("##\n");
64 out.write("## Entries checked and maintained by the JOSM team:\n");
65 for (ProjectionDefinition pd : epsgJosm.values()) {
66 write(out, pd);
67 noJosm++;
68 }
69 out.write("## Other supported projections (source: proj.4):\n");
70 for (ProjectionDefinition pd : epsgProj4.values()) {
71 if (doInclude(pd, true)) {
72 write(out, pd);
73 noProj4++;
74 }
75 }
76 }
77
78 if (printStats) {
79 System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
80 System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
81 System.out.println();
82 System.out.println("some entries from proj.4 have not been included:");
83 System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
84 System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
85 System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
86 System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
87 System.out.println(" in particular: " + baseProjectionMap);
88 System.out.println(String.format(" * requires data file for datum conversion: %d entries", noDatumgrid));
89 System.out.println();
90 System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
91 System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
92 }
93
94 }
95
96 static void write(BufferedWriter out, ProjectionDefinition pd) throws IOException {
97 out.write("# " + pd.name + "\n");
98 out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
99 }
100
101 static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm) {
102
103 boolean result = true;
104
105 if (noIncludeJosm) {
106 // we already have this projection
107 if (epsgJosm.containsKey(pd.code)) {
108 result = false;
109 noInJosm++;
110 }
111 }
112
113 // exclude deprecated projections
114 // EPSG:4296 is also deprecated, but this is not mentioned in the name
115 if (pd.name.contains("deprecated") || pd.code.equals("EPSG:4296")) {
116 result = false;
117 noDeprecated++;
118 }
119
120 Map<String, String> parameters;
121 try {
122 parameters = CustomProjection.parseParameterList(pd.definition, true);
123 } catch (ProjectionConfigurationException ex) {
124 throw new RuntimeException(pd.code+":"+ex);
125 }
126 String proj = parameters.get(CustomProjection.Param.proj.key);
127
128 // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
129 // JOSM as we only deal with 2D maps
130 if ("geocent".equals(proj)) {
131 result = false;
132 noGeocent++;
133 }
134
135 // exclude entries where we don't support the base projection
136 Proj bp = Projections.getBaseProjection(proj);
137 if (!"utm".equals(proj) && bp == null) {
138 result = false;
139 noBaseProjection++;
140 if (!"geocent".equals(proj)) {
141 if (!baseProjectionMap.containsKey(proj)) {
142 baseProjectionMap.put(proj, 0);
143 }
144 baseProjectionMap.put(proj, baseProjectionMap.get(proj)+1);
145 }
146 }
147
148 // no support for NAD27 datum, as it requires a conversion database
149 String datum = parameters.get(CustomProjection.Param.datum.key);
150 if ("NAD27".equals(datum)) {
151 result = false;
152 noDatumgrid++;
153 }
154
155 // requires datum conversion database
156 if (parameters.containsKey("geoidgrids")) {
157 result = false;
158 noDatumgrid++;
159 }
160
161 return result;
162 }
163}
Note: See TracBrowser for help on using the repository browser.