| 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 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Program entry point
|
|---|
| 47 | * @param args command line arguments (not used)
|
|---|
| 48 | * @throws IOException if any I/O error occurs
|
|---|
| 49 | */
|
|---|
| 50 | public static void main(String[] args) throws IOException {
|
|---|
| 51 | buildList(args[0]);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | static void buildList(String baseDir) throws IOException {
|
|---|
| 55 | List<ProjectionDefinition> pdJosm = Projections.loadProjectionDefinitions(baseDir + File.separator + JOSM_EPSG_FILE);
|
|---|
| 56 | for (ProjectionDefinition pd : pdJosm) {
|
|---|
| 57 | epsgJosm.put(pd.code, pd);
|
|---|
| 58 | }
|
|---|
| 59 | List<ProjectionDefinition> pdProj4 = Projections.loadProjectionDefinitions(baseDir + File.separator + PROJ4_EPSG_FILE);
|
|---|
| 60 | for (ProjectionDefinition pd : pdProj4) {
|
|---|
| 61 | epsgProj4.put(pd.code, pd);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
|---|
| 65 | new FileOutputStream(baseDir + File.separator + OUTPUT_EPSG_FILE), StandardCharsets.UTF_8))) {
|
|---|
| 66 | out.write("## This file is autogenerated, do not edit!\n");
|
|---|
| 67 | out.write("## Run ant task \"epsg\" to rebuild.\n");
|
|---|
| 68 | 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));
|
|---|
| 69 | out.write("##\n");
|
|---|
| 70 | out.write("## Entries checked and maintained by the JOSM team:\n");
|
|---|
| 71 | for (ProjectionDefinition pd : epsgJosm.values()) {
|
|---|
| 72 | write(out, pd);
|
|---|
| 73 | noJosm++;
|
|---|
| 74 | }
|
|---|
| 75 | out.write("## Other supported projections (source: proj.4):\n");
|
|---|
| 76 | for (ProjectionDefinition pd : epsgProj4.values()) {
|
|---|
| 77 | if (doInclude(pd, true)) {
|
|---|
| 78 | write(out, pd);
|
|---|
| 79 | noProj4++;
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (printStats) {
|
|---|
| 85 | System.out.println(String.format("loaded %d entries from %s", epsgJosm.size(), JOSM_EPSG_FILE));
|
|---|
| 86 | System.out.println(String.format("loaded %d entries from %s", epsgProj4.size(), PROJ4_EPSG_FILE));
|
|---|
| 87 | System.out.println();
|
|---|
| 88 | System.out.println("some entries from proj.4 have not been included:");
|
|---|
| 89 | System.out.println(String.format(" * already in the maintained JOSM list: %d entries", noInJosm));
|
|---|
| 90 | System.out.println(String.format(" * deprecated: %d entries", noDeprecated));
|
|---|
| 91 | System.out.println(String.format(" * using +proj=geocent, which is 3D (X,Y,Z) and not useful in JOSM: %d entries", noGeocent));
|
|---|
| 92 | System.out.println(String.format(" * unsupported base projection: %d entries", noBaseProjection));
|
|---|
| 93 | System.out.println(" in particular: " + baseProjectionMap);
|
|---|
| 94 | System.out.println(String.format(" * requires data file for datum conversion: %d entries", noDatumgrid));
|
|---|
| 95 | System.out.println();
|
|---|
| 96 | System.out.println(String.format("written %d entries from %s", noJosm, JOSM_EPSG_FILE));
|
|---|
| 97 | System.out.println(String.format("written %d entries from %s", noProj4, PROJ4_EPSG_FILE));
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | static void write(BufferedWriter out, ProjectionDefinition pd) throws IOException {
|
|---|
| 103 | out.write("# " + pd.name + "\n");
|
|---|
| 104 | out.write("<"+pd.code.substring("EPSG:".length())+"> "+pd.definition+" <>\n");
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | static boolean doInclude(ProjectionDefinition pd, boolean noIncludeJosm) {
|
|---|
| 108 |
|
|---|
| 109 | boolean result = true;
|
|---|
| 110 |
|
|---|
| 111 | if (noIncludeJosm) {
|
|---|
| 112 | // we already have this projection
|
|---|
| 113 | if (epsgJosm.containsKey(pd.code)) {
|
|---|
| 114 | result = false;
|
|---|
| 115 | noInJosm++;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // exclude deprecated projections
|
|---|
| 120 | // EPSG:4296 is also deprecated, but this is not mentioned in the name
|
|---|
| 121 | if (pd.name.contains("deprecated") || pd.code.equals("EPSG:4296")) {
|
|---|
| 122 | result = false;
|
|---|
| 123 | noDeprecated++;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | Map<String, String> parameters;
|
|---|
| 127 | try {
|
|---|
| 128 | parameters = CustomProjection.parseParameterList(pd.definition, true);
|
|---|
| 129 | } catch (ProjectionConfigurationException ex) {
|
|---|
| 130 | throw new RuntimeException(pd.code+":"+ex);
|
|---|
| 131 | }
|
|---|
| 132 | String proj = parameters.get(CustomProjection.Param.proj.key);
|
|---|
| 133 |
|
|---|
| 134 | // +proj=geocent is 3D (X,Y,Z) "projection" - this is not useful in
|
|---|
| 135 | // JOSM as we only deal with 2D maps
|
|---|
| 136 | if ("geocent".equals(proj)) {
|
|---|
| 137 | result = false;
|
|---|
| 138 | noGeocent++;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | // exclude entries where we don't support the base projection
|
|---|
| 142 | Proj bp = Projections.getBaseProjection(proj);
|
|---|
| 143 | if (!"utm".equals(proj) && bp == null) {
|
|---|
| 144 | result = false;
|
|---|
| 145 | noBaseProjection++;
|
|---|
| 146 | if (!"geocent".equals(proj)) {
|
|---|
| 147 | if (!baseProjectionMap.containsKey(proj)) {
|
|---|
| 148 | baseProjectionMap.put(proj, 0);
|
|---|
| 149 | }
|
|---|
| 150 | baseProjectionMap.put(proj, baseProjectionMap.get(proj)+1);
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | // no support for NAD27 datum, as it requires a conversion database
|
|---|
| 155 | String datum = parameters.get(CustomProjection.Param.datum.key);
|
|---|
| 156 | if ("NAD27".equals(datum)) {
|
|---|
| 157 | result = false;
|
|---|
| 158 | noDatumgrid++;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | // requires datum conversion database
|
|---|
| 162 | if (parameters.containsKey("geoidgrids")) {
|
|---|
| 163 | result = false;
|
|---|
| 164 | noDatumgrid++;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | return result;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|