Ignore:
Timestamp:
2018-08-07T00:24:36+02:00 (6 years ago)
Author:
Don-vip
Message:

see #8765, fix #11086 - Support notes in osmChange (.osc) files created by OsmAnd

Location:
trunk/src/org/openstreetmap/josm/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/NoteReader.java

    r13901 r14101  
    1111import java.util.Locale;
    1212import java.util.Optional;
     13import java.util.function.Function;
    1314
    1415import javax.xml.parsers.ParserConfigurationException;
     
    8586            if (parseMode == NoteParseMode.API) {
    8687                if ("note".equals(qName)) {
    87                     double lat = Double.parseDouble(attrs.getValue("lat"));
    88                     double lon = Double.parseDouble(attrs.getValue("lon"));
    89                     LatLon noteLatLon = new LatLon(lat, lon);
    90                     thisNote = new Note(noteLatLon);
     88                    thisNote = parseNoteBasic(attrs);
    9189                }
    9290                return;
     
    9694            switch(qName) {
    9795            case "note":
    98                 double lat = Double.parseDouble(attrs.getValue("lat"));
    99                 double lon = Double.parseDouble(attrs.getValue("lon"));
    100                 LatLon noteLatLon = new LatLon(lat, lon);
    101                 thisNote = new Note(noteLatLon);
    102                 thisNote.setId(Long.parseLong(attrs.getValue("id")));
    103                 String closedTimeStr = attrs.getValue("closed_at");
    104                 if (closedTimeStr == null) { //no closed_at means the note is still open
    105                     thisNote.setState(Note.State.OPEN);
    106                 } else {
    107                     thisNote.setState(Note.State.CLOSED);
    108                     thisNote.setClosedAt(DateUtils.fromString(closedTimeStr));
    109                 }
    110                 thisNote.setCreatedAt(DateUtils.fromString(attrs.getValue("created_at")));
     96                thisNote = parseNoteFull(attrs);
    11197                break;
    11298            case "comment":
     
    188174            parsedNotes = notes;
    189175        }
     176    }
     177
     178    static LatLon parseLatLon(Function<String, String> attrs) {
     179        double lat = Double.parseDouble(attrs.apply("lat"));
     180        double lon = Double.parseDouble(attrs.apply("lon"));
     181        return new LatLon(lat, lon);
     182    }
     183
     184    static Note parseNoteBasic(Attributes attrs) {
     185        return parseNoteBasic(attrs::getValue);
     186    }
     187
     188    static Note parseNoteBasic(Function<String, String> attrs) {
     189        return new Note(parseLatLon(attrs));
     190    }
     191
     192    static Note parseNoteFull(Attributes attrs) {
     193        return parseNoteFull(attrs::getValue);
     194    }
     195
     196    static Note parseNoteFull(Function<String, String> attrs) {
     197        Note note = parseNoteBasic(attrs);
     198        String id = attrs.apply("id");
     199        if (id != null) {
     200            note.setId(Long.parseLong(id));
     201        }
     202        String closedTimeStr = attrs.apply("closed_at");
     203        if (closedTimeStr == null) { //no closed_at means the note is still open
     204            note.setState(Note.State.OPEN);
     205        } else {
     206            note.setState(Note.State.CLOSED);
     207            note.setClosedAt(DateUtils.fromString(closedTimeStr));
     208        }
     209        String createdAt = attrs.apply("created_at");
     210        if (createdAt != null) {
     211            note.setCreatedAt(DateUtils.fromString(createdAt));
     212        }
     213        return note;
    190214    }
    191215
  • trunk/src/org/openstreetmap/josm/io/OsmChangeReader.java

    r8415 r14101  
    1010import javax.xml.stream.XMLStreamException;
    1111
     12import org.openstreetmap.josm.data.coor.LatLon;
    1213import org.openstreetmap.josm.data.osm.DataSet;
     14import org.openstreetmap.josm.data.osm.NoteData;
    1315import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1416import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     17import org.openstreetmap.josm.tools.Pair;
    1518
    1619/**
     
    2326     */
    2427    private static final String[] ACTIONS = {"create", "modify", "delete"};
     28
     29    protected final NoteData noteData = new NoteData();
    2530
    2631    /**
     
    8085                    p = parseRelation();
    8186                    break;
     87                case "note":
     88                    parseNote();
     89                    break;
    8290                default:
    8391                    parseUnknown();
     
    96104    }
    97105
     106    private void parseNote() throws XMLStreamException {
     107        LatLon location = NoteReader.parseLatLon(s -> parser.getAttributeValue(null, s));
     108        String text = null;
     109        while (parser.hasNext()) {
     110            int event = parser.next();
     111            if (event == XMLStreamConstants.START_ELEMENT) {
     112                switch (parser.getLocalName()) {
     113                case "comment":
     114                    text = parser.getAttributeValue(null, "text");
     115                    break;
     116                default:
     117                    parseUnknown();
     118                }
     119            } else if (event == XMLStreamConstants.END_ELEMENT) {
     120                break;
     121            }
     122        }
     123        if (location != null && text != null) {
     124            noteData.createNote(location, text);
     125        }
     126    }
     127
     128    /**
     129     * Replies the parsed notes data.
     130     * @return the parsed notes data
     131     * @since 14101
     132     */
     133    public final NoteData getNoteData() {
     134        return noteData;
     135    }
     136
    98137    /**
    99138     * Parse the given input source and return the dataset.
     
    110149        return new OsmChangeReader().doParseDataSet(source, progressMonitor);
    111150    }
     151
     152    /**
     153     * Parse the given input source and return the dataset and notes, if any (OsmAnd extends the osmChange format by adding notes).
     154     *
     155     * @param source the source input stream. Must not be <code>null</code>.
     156     * @param progressMonitor  the progress monitor. If <code>null</code>,
     157     * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed
     158     *
     159     * @return the dataset with the parsed data
     160     * @throws IllegalDataException if the an error was found while parsing the data from the source
     161     * @throws IllegalArgumentException if source is <code>null</code>
     162     * @since 14101
     163     */
     164    public static Pair<DataSet, NoteData> parseDataSetAndNotes(InputStream source, ProgressMonitor progressMonitor)
     165            throws IllegalDataException {
     166        OsmChangeReader osmChangeReader = new OsmChangeReader();
     167        osmChangeReader.doParseDataSet(source, progressMonitor);
     168        return new Pair<>(osmChangeReader.getDataSet(), osmChangeReader.getNoteData());
     169    }
    112170}
Note: See TracChangeset for help on using the changeset viewer.