Ignore:
Timestamp:
2017-05-31T22:27:33+02:00 (8 years ago)
Author:
donvip
Message:

fix #josm10495 - New transformation formulas support in opendata (by embedding ESRI WKID database copied from https://github.com/Esri/geoportal-server-catalog/blob/10f07d57a176404f5afa7b84fa2727512ee1731b/geoportal/src/main/webapp/viewer/jimu.js/SpatialReference/wkid.json)

Location:
applications/editors/josm/plugins/opendata
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/OdConstants.java

    r33245 r33356  
    124124    public static final String RESOURCE_PATH = "/resources/org/openstreetmap/josm/plugins/opendata/core/resources/";
    125125    public static final String DICTIONARY_FR = RESOURCE_PATH+"dictionary.fr.csv";
     126    public static final String ESRI_WKID = RESOURCE_PATH+"wkid.json";
    126127}
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/GeographicReader.java

    r33245 r33356  
    66import java.awt.Component;
    77import java.awt.GraphicsEnvironment;
     8import java.io.IOException;
    89import java.io.InputStream;
     10import java.lang.reflect.Field;
    911import java.util.ArrayList;
    1012import java.util.Arrays;
     
    1315import java.util.List;
    1416import java.util.Map;
    15 
     17import java.util.TreeMap;
     18
     19import javax.json.JsonArray;
     20import javax.json.JsonObject;
     21import javax.json.JsonReader;
     22import javax.json.spi.JsonProvider;
    1623import javax.swing.Icon;
    1724import javax.swing.JOptionPane;
     
    1926import org.geotools.factory.Hints;
    2027import org.geotools.geometry.jts.JTS;
     28import org.geotools.metadata.iso.citation.Citations;
    2129import org.geotools.referencing.AbstractIdentifiedObject;
    2230import org.geotools.referencing.CRS;
     31import org.geotools.referencing.NamedIdentifier;
    2332import org.geotools.referencing.crs.AbstractCRS;
    2433import org.geotools.referencing.crs.AbstractDerivedCRS;
     
    5463import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    5564import org.openstreetmap.josm.tools.UserCancelException;
     65import org.openstreetmap.josm.tools.Utils;
    5666
    5767import com.vividsolutions.jts.geom.LineString;
     
    7888    protected CoordinateReferenceSystem crs;
    7989    protected MathTransform transform;
     90
     91    private static final Map<String, Integer> esriWkid = new TreeMap<>();
    8092
    8193    public GeographicReader(GeographicHandler handler, GeographicHandler[] defaultHandlers) {
     
    287299    }
    288300
     301    private static void loadEsriWkid() throws IOException {
     302        Main.info("Loading ESRI WKID database...");
     303        try (InputStream in = GeographicReader.class.getResourceAsStream(OdConstants.ESRI_WKID);
     304            JsonReader json = JsonProvider.provider().createReader(in)) {
     305            JsonObject root = json.readObject();
     306            JsonArray wkids = root.getJsonArray("wkids");
     307            JsonArray labels = root.getJsonArray("labels");
     308            if (wkids.size() != labels.size()) {
     309                throw new IllegalStateException();
     310            }
     311            for (int i = 0; i < wkids.size(); i++) {
     312                esriWkid.put(labels.getString(i), wkids.getInt(i));
     313            }
     314        }
     315        Main.info("ESRI WKID database loaded");
     316    }
     317
     318    private static Integer getEpsgCodeFromEsriWkid(String wkid) {
     319        if (esriWkid.isEmpty()) {
     320            try {
     321                loadEsriWkid();
     322            } catch (IOException e) {
     323                Main.error(e);
     324            }
     325        }
     326        return esriWkid.get(wkid);
     327    }
     328
    289329    protected void findMathTransform(Component parent, boolean findSimiliarCrs)
    290330            throws FactoryException, UserCancelException, GeoMathTransformException {
    291331        try {
     332            // Geotools relies on Authority identifiers to find suitable transformations
     333            // but this information is optional in WKT and not defined in ESRI WKID database, so often missing in real-world files
     334            if (crs instanceof AbstractIdentifiedObject && crs.getIdentifiers().isEmpty()) {
     335                // If no identifier, attempt to find one in embedded ESRI database
     336                String wkid = crs.getName().getCode();
     337                if (wkid != null) {
     338                    Integer epsgCode = getEpsgCodeFromEsriWkid(wkid);
     339                    if (epsgCode != null) {
     340                        try {
     341                            Field f = AbstractIdentifiedObject.class.getDeclaredField("identifiers");
     342                            Utils.setObjectsAccessible(f);
     343                            f.set(crs, Collections.singleton(new NamedIdentifier(Citations.fromName("EPSG"), epsgCode.toString())));
     344                        } catch (ReflectiveOperationException | SecurityException e) {
     345                            Main.error(e);
     346                        }
     347                    }
     348                }
     349            }
     350            // Find math transformation
    292351            transform = CRS.findMathTransform(crs, wgs84);
    293352        } catch (OperationNotFoundException e) {
Note: See TracChangeset for help on using the changeset viewer.