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