Ignore:
Timestamp:
2017-09-21T01:49:47+02:00 (7 years ago)
Author:
donvip
Message:

Edigeo: support DIC files and multiline strings with "NEXT" keyword

Location:
applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/EdigeoFile.java

    r33643 r33645  
    1111import java.time.LocalDate;
    1212import java.time.format.DateTimeFormatter;
     13import java.util.List;
    1314import java.util.Objects;
     15import java.util.function.Consumer;
    1416
    1517import org.openstreetmap.josm.tools.Logging;
     
    2729    static class Block {
    2830        /** RTY */ final String type;
    29         /** RID */ String identifier;
     31        /** RID */ String identifier = "";
     32
     33        // Remembers the last string read, to handle multiline text (more than 80 chars) with "NEXT" keyword
     34        Consumer<String> lastReadString;
    3035
    3136        Block(String type) {
     
    3338        }
    3439
    35         public final String getType() {
     40        public final String getBlockType() {
    3641            return type;
    3742        }
    3843
    39         public final String getIdentifier() {
     44        public final String getBlockIdentifier() {
    4045            return identifier;
    4146        }
     
    4348        void processRecord(EdigeoRecord r) {
    4449            if ("RID".equals(r.name)) {
    45                 identifier = safeGetAndLog(r, tr("Identifier"));
     50                safeGetAndLog(r, s -> identifier += s, tr("Identifier"));
     51            } else if ("NEX".equals(r.name) && lastReadString != null) {
     52                safeGet(r, lastReadString);
    4653            } else {
    4754                throw new IllegalArgumentException(r.toString());
    4855            }
     56        }
     57
     58        protected void safeGet(EdigeoRecord r, List<String> list) {
     59            safeGet(r, s -> {
     60                int idx = list.size() - 1;
     61                list.set(idx, list.get(idx) + s);
     62            });
     63        }
     64
     65        protected void safeGet(EdigeoRecord r, Consumer<String> callback) {
     66            (lastReadString = callback).accept(r.length > 0 ? r.values.get(0) : null);
     67        }
     68
     69        protected int safeGetInt(EdigeoRecord r) {
     70            return r.length > 0 ? Integer.parseInt(r.values.get(0)) : 0;
     71        }
     72
     73        protected LocalDate safeGetDate(EdigeoRecord r) {
     74            return r.length > 0 ? LocalDate.parse(r.values.get(0), dateFormatter) : null;
     75        }
     76
     77        protected void safeGetAndLog(EdigeoRecord r, Consumer<String> callback, String msg) {
     78            if (r.length > 0) {
     79                String v = r.values.get(0);
     80                Logging.info(msg + ": " + v);
     81                (lastReadString = callback).accept(v);
     82            }
     83        }
     84
     85        protected LocalDate safeGetDateAndLog(EdigeoRecord r, String msg) {
     86            if (r.length > 0) {
     87                LocalDate v = LocalDate.parse(r.values.get(0), dateFormatter);
     88                Logging.info(msg + ": " + v);
     89                return v;
     90            }
     91            return null;
    4992        }
    5093    }
     
    116159        currentBlock.processRecord(r);
    117160    }
    118 
    119     protected static String safeGet(EdigeoRecord r) {
    120         return r.length > 0 ? r.values.get(0) : null;
    121     }
    122 
    123     protected static int safeGetInt(EdigeoRecord r) {
    124         return r.length > 0 ? Integer.parseInt(r.values.get(0)) : 0;
    125     }
    126 
    127     protected static LocalDate safeGetDate(EdigeoRecord r) {
    128         return r.length > 0 ? LocalDate.parse(r.values.get(0), dateFormatter) : null;
    129     }
    130 
    131     protected static String safeGetAndLog(EdigeoRecord r, String msg) {
    132         if (r.length > 0) {
    133             String v = r.values.get(0);
    134             Logging.info(msg + ": " + v);
    135             return v;
    136         }
    137         return null;
    138     }
    139 
    140     protected static LocalDate safeGetDateAndLog(EdigeoRecord r, String msg) {
    141         if (r.length > 0) {
    142             LocalDate v = LocalDate.parse(r.values.get(0), dateFormatter);
    143             Logging.info(msg + ": " + v);
    144             return v;
    145         }
    146         return null;
    147     }
    148161}
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/EdigeoFileDIC.java

    r33643 r33645  
    44import java.io.IOException;
    55import java.nio.file.Path;
     6import java.util.ArrayList;
     7import java.util.Collections;
     8import java.util.List;
    69
    710/**
     
    1114
    1215    /**
     16     * Abstract definition.
     17     */
     18    abstract static class Def extends Block {
     19
     20        /** LAB */ String code = "";
     21        /** TEX */ EdigeoCharset charset;
     22        /** DEF */ String definition = "";
     23        /** ORI */ String origin = "";
     24        /** CAT */ String category = "";
     25
     26        Def(String type) {
     27            super(type);
     28        }
     29
     30        @Override
     31        void processRecord(EdigeoRecord r) {
     32            switch (r.name) {
     33            case "LAB": safeGet(r, s -> code += s); break;
     34            case "TEX": safeGet(r, s -> charset = EdigeoCharset.of(s)); break;
     35            case "DEF": safeGet(r, s -> definition += s); break;
     36            case "ORI": safeGet(r, s -> origin += s); break;
     37            case "CAT": safeGet(r, s -> category += s); break;
     38            default:
     39                super.processRecord(r);
     40            }
     41        }
     42
     43        /**
     44         * Returns code.
     45         * @return code
     46         */
     47        public final String getCode() {
     48            return code;
     49        }
     50
     51        /**
     52         * Returns definition.
     53         * @return definition
     54         */
     55        public final String getDefinition() {
     56            return definition;
     57        }
     58
     59        /**
     60         * Returns definition source.
     61         * @return definition source
     62         */
     63        public final String getOrigin() {
     64            return origin;
     65        }
     66
     67        /**
     68         * Returns category.
     69         * @return category
     70         */
     71        public final String getCategory() {
     72            return category;
     73        }
     74    }
     75
     76    /**
     77     * Object definition.
     78     */
     79    public static class ObjectDef extends Def {
     80        ObjectDef(String type) {
     81            super(type);
     82        }
     83    }
     84
     85    /**
     86     * Attribute definition.
     87     */
     88    public static class AttributeDef extends Def {
     89
     90        /** TYP */ String type = "";
     91        /** UNI */ String unit = "";
     92        /** AVC */ int nValues;
     93        /** AVL */ final List<String> values = new ArrayList<>();
     94        /** AVD */ final List<String> descrs = new ArrayList<>();
     95
     96        AttributeDef(String type) {
     97            super(type);
     98        }
     99
     100        @Override
     101        void processRecord(EdigeoRecord r) {
     102            switch (r.name) {
     103            case "TYP": safeGet(r, s -> type += s); break;
     104            case "UNI": safeGet(r, s -> unit += s); break;
     105            case "AVC": nValues = safeGetInt(r); break;
     106            case "AVL": values.add(""); safeGet(r, values); break;
     107            case "AVD": descrs.add(""); safeGet(r, descrs); break;
     108            default:
     109                super.processRecord(r);
     110            }
     111        }
     112
     113        /**
     114         * Returns attribute type.
     115         * @return attribute type
     116         */
     117        public final String getType() {
     118            return type;
     119        }
     120
     121        /**
     122         * Returns default unit.
     123         * @return default unit
     124         */
     125        public final String getUnit() {
     126            return unit;
     127        }
     128
     129        /**
     130         * Returns number of values.
     131         * @return number of values
     132         */
     133        public final int getNumberOfValues() {
     134            return nValues;
     135        }
     136
     137        /**
     138         * Returns pre-coded values.
     139         * @return pre-coded values
     140         */
     141        public final List<String> getValues() {
     142            return Collections.unmodifiableList(values);
     143        }
     144
     145        /**
     146         * Returns descriptions of pre-coded values.
     147         * @return descriptions of pre-coded values
     148         */
     149        public final List<String> getDescriptions() {
     150            return Collections.unmodifiableList(descrs);
     151        }
     152    }
     153
     154    /**
     155     * Relation definition.
     156     */
     157    public static class RelationDef extends Def {
     158        RelationDef(String type) {
     159            super(type);
     160        }
     161    }
     162
     163    /** DID */ List<ObjectDef> objects;
     164    /** DIA */ List<AttributeDef> attributes;
     165    /** DIR */ List<RelationDef> relations;
     166
     167    /**
    13168     * Constructs a new {@code EdigeoFileDIC}.
    14169     * @param path path to DIC file
     
    20175
    21176    @Override
     177    protected void init() {
     178        objects = new ArrayList<>();
     179        attributes = new ArrayList<>();
     180        relations = new ArrayList<>();
     181    }
     182
     183    @Override
    22184    protected Block createBlock(String type) {
    23         // TODO Auto-generated method stub
    24         return null;
    25     }
    26 
     185        switch (type) {
     186        case "DID":
     187            ObjectDef objDef = new ObjectDef(type);
     188            objects.add(objDef);
     189            return objDef;
     190        case "DIA":
     191            AttributeDef attDef = new AttributeDef(type);
     192            attributes.add(attDef);
     193            return attDef;
     194        case "DIR":
     195            RelationDef relDef = new RelationDef(type);
     196            relations.add(relDef);
     197            return relDef;
     198        default:
     199            throw new IllegalArgumentException(type);
     200        }
     201    }
     202
     203    /**
     204     * Returns list of object definitions.
     205     * @return list of object definitions
     206     */
     207    public final List<ObjectDef> getObjects() {
     208        return Collections.unmodifiableList(objects);
     209    }
     210
     211    /**
     212     * Returns list of attribute definitions.
     213     * @return list of attribute definitions
     214     */
     215    public final List<AttributeDef> getAttributes() {
     216        return Collections.unmodifiableList(attributes);
     217    }
     218
     219    /**
     220     * Returns list of relation definitions.
     221     * @return list of relation definitions
     222     */
     223    public final List<RelationDef> getRelations() {
     224        return Collections.unmodifiableList(relations);
     225    }
    27226}
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/EdigeoFileGEN.java

    r33644 r33645  
    1616     */
    1717    public static class GeoBounds extends Block {
    18         String cm1;
    19         String cm2;
     18        /** CM1 */ String min = "";
     19        /** CM2 */ String max = "";
    2020
    2121        GeoBounds(String type) {
     
    2626        void processRecord(EdigeoRecord r) {
    2727            switch (r.name) {
    28             case "CM1": cm1 = safeGet(r); break;
    29             case "CM2": cm2 = safeGet(r); break;
     28            case "CM1": safeGet(r, s -> min += s); break;
     29            case "CM2": safeGet(r, s -> max += s); break;
    3030            default:
    3131                super.processRecord(r);
     
    3737         * @return the minimal coordinates
    3838         */
    39         public final String getCm1() {
    40             return cm1;
     39        public final String getMinCm1() {
     40            return min;
    4141        }
    4242
     
    4545         * @return the maximal coordinates
    4646         */
    47         public final String getCm2() {
    48             return cm2;
     47        public final String getMaxCm2() {
     48            return max;
    4949        }
    5050    }
     
    8080        }
    8181
    82         String information;
    83         Structure structure;
    84         String offsetId;
     82        /** INF */ String information = "";
     83        /** STR */ Structure structure;
     84        /** REG */ String offsetId = "";
    8585
    8686        GeoData(String type) {
     
    9191        void processRecord(EdigeoRecord r) {
    9292            switch (r.name) {
    93             case "INF": information = safeGetAndLog(r, tr("Information")); break;
     93            case "INF": safeGetAndLog(r, s -> information += s, tr("Information")); break;
    9494            case "STR": structure = Structure.of(safeGetInt(r)); break;
    95             case "REG": offsetId = safeGet(r); break;
     95            case "REG": safeGet(r, s -> offsetId += s); break;
    9696            default:
    9797                super.processRecord(r);
     
    124124    }
    125125
    126     GeoBounds bounds;
    127     GeoData geodata;
     126    /** DEG */ GeoBounds bounds;
     127    /** GSE */ GeoData geodata;
    128128
    129129    /**
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/EdigeoFileGEO.java

    r33644 r33645  
    5656        }
    5757
    58         ReferenceType type;
    59         String name;
    60         String code;
    61         int nDim;
    62         AltitudeSystem altitudeSystem;
    63         String unit;
     58        /** RET */ ReferenceType type;
     59        /** REN */ String name = "";
     60        /** REL */ String code = "";
     61        /** DIM */ int nDim;
     62        /** ALS */ AltitudeSystem altitudeSystem;
     63        /** UNH */ String unit = "";
    6464
    6565        CoorReference(String type) {
     
    7070        void processRecord(EdigeoRecord r) {
    7171            switch (r.name) {
    72             case "RET": type = ReferenceType.of(safeGet(r)); break;
    73             case "REN": name = safeGetAndLog(r, tr("Projection")); break;
    74             case "REL": code = safeGetAndLog(r, tr("Projection")); break;
     72            case "RET": safeGet(r, s -> type = ReferenceType.of(s)); break;
     73            case "REN": safeGetAndLog(r, s -> name += s, tr("Projection")); break;
     74            case "REL": safeGetAndLog(r, s -> code += s, tr("Projection")); break;
    7575            case "DIM": nDim = safeGetInt(r); break;
    7676            case "ALS": altitudeSystem = AltitudeSystem.of(safeGetInt(r)); break;
    77             case "UNH": unit = safeGet(r); break;
     77            case "UNH": safeGet(r, s -> unit += s); break;
    7878            default:
    7979                super.processRecord(r);
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/EdigeoFileTHF.java

    r33643 r33645  
    4545        }
    4646
    47         /** AUT */ String author;
    48         /** ADR */ String recipient;
     47        /** AUT */ String author = "";
     48        /** ADR */ String recipient = "";
    4949        /** LOC */ int nLots;
    5050        /** VOC */ int nVolumes;
    5151        /** SEC */ SecurityClassification security;
    52         /** RDI */ String diffusionRestriction;
    53         /** VER */ String edigeoVersion;
     52        /** RDI */ String diffusionRestriction = "";
     53        /** VER */ String edigeoVersion = "";
    5454        /** VDA */ LocalDate edigeoDate;
    55         /** TRL */ String transmissionName;
     55        /** TRL */ String transmissionName = "";
    5656        /** EDN */ int transmissionEdition;
    5757        /** TDA */ LocalDate transmissionDate;
    58         /** INF */ String transmissionInformation;
     58        /** INF */ String transmissionInformation = "";
    5959
    6060        Support(String type) {
     
    6565        void processRecord(EdigeoRecord r) {
    6666            switch (r.name) {
    67             case "AUT": author = safeGetAndLog(r, tr("Author")); break;
    68             case "ADR": recipient = safeGetAndLog(r, tr("Recipient")); break;
     67            case "AUT": safeGetAndLog(r, s -> author += s, tr("Author")); break;
     68            case "ADR": safeGetAndLog(r, s -> recipient += s, tr("Recipient")); break;
    6969            case "LOC": nLots = safeGetInt(r); break;
    7070            case "VOC": nVolumes = safeGetInt(r); break;
    7171            case "SEC": security = SecurityClassification.of(safeGetInt(r)); break;
    72             case "RDI": diffusionRestriction = safeGetAndLog(r, tr("Diffusion restriction")); break;
    73             case "VER": edigeoVersion = safeGet(r); break;
     72            case "RDI": safeGetAndLog(r, s -> diffusionRestriction += s, tr("Diffusion restriction")); break;
     73            case "VER": safeGet(r, s -> edigeoVersion += s); break;
    7474            case "VDA": edigeoDate = safeGetDate(r); break;
    75             case "TRL": transmissionName = safeGet(r); break;
     75            case "TRL": safeGet(r, s -> transmissionName += s); break;
    7676            case "EDN": transmissionEdition = safeGetInt(r); break;
    7777            case "TDA": transmissionDate = safeGetDateAndLog(r, tr("Date")); break;
    78             case "INF": transmissionInformation = safeGetAndLog(r, tr("Information")); break;
     78            case "INF": safeGetAndLog(r, s -> transmissionInformation += s, tr("Information")); break;
    7979            default:
    8080                super.processRecord(r);
     
    184184    public static class Lot extends Block {
    185185
    186         /** LON */ String name;
    187         /** INF */ String information;
    188         /** GNN */ String genDataName;
    189         /** GNI */ String genDataId;
    190         /** GON */ String coorRefName;
    191         /** GOI */ String coorRefId;
    192         /** QAN */ String qualityName;
    193         /** QAI */ String qualityId;
    194         /** DIN */ String dictName;
    195         /** DII */ String dictId;
    196         /** SCN */ String scdName;
    197         /** SCI */ String scdId;
     186        /** LON */ String name = "";
     187        /** INF */ String information = "";
     188        /** GNN */ String genDataName = "";
     189        /** GNI */ String genDataId = "";
     190        /** GON */ String coorRefName = "";
     191        /** GOI */ String coorRefId = "";
     192        /** QAN */ String qualityName = "";
     193        /** QAI */ String qualityId = "";
     194        /** DIN */ String dictName = "";
     195        /** DII */ String dictId = "";
     196        /** SCN */ String scdName = "";
     197        /** SCI */ String scdId = "";
    198198        /** GDC */ int nGeoData;
    199199        /** GDN */ final List<String> geoDataName = new ArrayList<>();
     
    207207        void processRecord(EdigeoRecord r) {
    208208            switch (r.name) {
    209             case "LON": name = safeGetAndLog(r, tr("Name")); break;
    210             case "INF": information = safeGetAndLog(r, tr("Information")); break;
    211             case "GNN": genDataName = safeGet(r); break;
    212             case "GNI": genDataId = safeGet(r); break;
    213             case "GON": coorRefName = safeGet(r); break;
    214             case "GOI": coorRefId = safeGet(r); break;
    215             case "QAN": qualityName = safeGet(r); break;
    216             case "QAI": qualityId = safeGet(r); break;
    217             case "DIN": dictName = safeGet(r); break;
    218             case "DII": dictId = safeGet(r); break;
    219             case "SCN": scdName = safeGet(r); break;
    220             case "SCI": scdId = safeGet(r); break;
     209            case "LON": safeGetAndLog(r, s -> name += s, tr("Name")); break;
     210            case "INF": safeGetAndLog(r, s -> information += s, tr("Information")); break;
     211            case "GNN": safeGet(r, s -> genDataName += s); break;
     212            case "GNI": safeGet(r, s -> genDataId += s); break;
     213            case "GON": safeGet(r, s -> coorRefName += s); break;
     214            case "GOI": safeGet(r, s -> coorRefId += s); break;
     215            case "QAN": safeGet(r, s -> qualityName += s); break;
     216            case "QAI": safeGet(r, s -> qualityId += s); break;
     217            case "DIN": safeGet(r, s -> dictName += s); break;
     218            case "DII": safeGet(r, s -> dictId += s); break;
     219            case "SCN": safeGet(r, s -> scdName += s); break;
     220            case "SCI": safeGet(r, s -> scdId += s); break;
    221221            case "GDC": nGeoData = safeGetInt(r); break;
    222             case "GDN": geoDataName.add(safeGet(r)); break;
    223             case "GDI": geoDataId.add(safeGet(r)); break;
     222            case "GDN": geoDataName.add(""); safeGet(r, geoDataName); break;
     223            case "GDI": geoDataId.add(""); safeGet(r, geoDataId); break;
    224224            default:
    225225                super.processRecord(r);
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/EdigeoRecord.java

    r33642 r33645  
    7676            assert line.length() <= 80;
    7777            values = Arrays.asList(line.substring(8).split(";"));
    78             assert (nature == Nature.SIMPLE && values.size() == 1) || (nature == Nature.COMPOSED && values.size() > 1) : line;
     78            assert nature == Nature.RESERVED
     79                    || (nature == Nature.SIMPLE && values.size() == 1)
     80                    || (nature == Nature.COMPOSED && values.size() > 1) : line;
    7981        } else {
    8082            values = Collections.emptyList();
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/pci/EdigeoPciReader.java

    r33643 r33645  
    5959        }
    6060        DataSet ds = new DataSet();
    61         ds.setName(thf.getSupport().getIdentifier());
     61        ds.setName(thf.getSupport().getBlockIdentifier());
    6262        ds.setUploadPolicy(UploadPolicy.DISCOURAGED);
    6363        return ds;
Note: See TracChangeset for help on using the changeset viewer.