Changeset 33791 in osm


Ignore:
Timestamp:
2017-11-06T00:58:05+01:00 (7 years ago)
Author:
donvip
Message:

fix #josm11804 - import tracks from KML/KMZ files

File:
1 edited

Legend:

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

    r33245 r33791  
    3030import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
    3131import org.openstreetmap.josm.plugins.opendata.core.io.ProjectionPatterns;
     32import org.openstreetmap.josm.tools.date.DateUtils;
    3233
    3334public class KmlReader extends AbstractReader {
     
    4546    public static final String KML_LINEAR_RING = "LinearRing";
    4647    public static final String KML_COORDINATES = "coordinates";
     48    public static final String KML_WHEN = "when";
     49
     50    public static final String KML_EXT_TRACK = "Track";
     51    public static final String KML_EXT_COORD = "coord";
    4752    // CHECKSTYLE.ON: SingleSpaceSeparator
    4853
     
    9398    private void parsePlaceMark(DataSet ds) throws XMLStreamException {
    9499        List<OsmPrimitive> list = new ArrayList<>();
     100        long when = 0;
    95101        Way way = null;
    96102        Node node = null;
     
    127133                        relation.addMember(new RelationMember(role, way));
    128134                    }
    129                 } else if (parser.getLocalName().equals(KML_LINE_STRING)) {
     135                } else if (parser.getLocalName().equals(KML_LINE_STRING) || parser.getLocalName().equals(KML_EXT_TRACK)) {
    130136                    ds.addPrimitive(way = new Way());
    131137                    list.add(way);
     
    133139                    String[] tab = parser.getElementText().trim().split("\\s");
    134140                    for (int i = 0; i < tab.length; i++) {
    135                         String[] values = tab[i].split(",");
    136                         if (values.length >= 2) {
    137                             LatLon ll = new LatLon(Double.valueOf(values[1]), Double.valueOf(values[0])).getRoundedToOsmPrecision();
    138                             node = nodes.get(ll);
    139                             if (node == null) {
    140                                 ds.addPrimitive(node = new Node(ll));
    141                                 nodes.put(ll, node);
    142                                 if (values.length > 2 && !values[2].equals("0")) {
    143                                     node.put("ele", values[2]);
    144                                 }
    145                             }
    146                             if (way != null) {
    147                                 way.addNode(node);
    148                             }
    149                         }
     141                        node = parseNode(ds, way, node, tab[i].split(","));
    150142                    }
     143                } else if (parser.getLocalName().equals(KML_EXT_COORD)) {
     144                    node = parseNode(ds, way, node, parser.getElementText().trim().split("\\s"));
     145                    if (node != null && when > 0) {
     146                        node.setRawTimestamp((int) when);
     147                    }
     148                } else if (parser.getLocalName().equals(KML_WHEN)) {
     149                    when = DateUtils.tsFromString(parser.getElementText().trim());
    151150                }
    152151            } else if (event == XMLStreamConstants.END_ELEMENT) {
     
    164163        }
    165164    }
     165
     166    private Node parseNode(DataSet ds, Way way, Node node, String[] values) {
     167        if (values.length >= 2) {
     168            LatLon ll = new LatLon(Double.valueOf(values[1]), Double.valueOf(values[0])).getRoundedToOsmPrecision();
     169            node = nodes.get(ll);
     170            if (node == null) {
     171                ds.addPrimitive(node = new Node(ll));
     172                nodes.put(ll, node);
     173                if (values.length > 2 && !values[2].equals("0")) {
     174                    node.put("ele", values[2]);
     175                }
     176            }
     177            if (way != null) {
     178                way.addNode(node);
     179            }
     180        }
     181        return node;
     182    }
    166183}
Note: See TracChangeset for help on using the changeset viewer.