Ignore:
Timestamp:
2007-09-24T01:36:24+02:00 (18 years ago)
Author:
framm
Message:

This commit is a manual merge of all changes that have been made to
the intermediate "core_0.5" branch on the main OSM repository,
bevore JOSM was moved to openstreetmap.de.

Changes incorporated here:

r4464@svn.openstreetmap.org
r4466@svn.openstreetmap.org
r4468@svn.openstreetmap.org
r4469@svn.openstreetmap.org
r4479@svn.openstreetmap.org

Location:
branch/0.5/src/org/openstreetmap/josm/io
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branch/0.5/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java

    r298 r329  
    103103                Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
    104104                final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg);
    105                 String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.4");
     105                String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5");
    106106                Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2));
    107107                        DataSource src = new DataSource(bounds, origin);
  • branch/0.5/src/org/openstreetmap/josm/io/GpxWriter.java

    r298 r329  
    1111import org.openstreetmap.josm.data.osm.Node;
    1212import org.openstreetmap.josm.data.osm.OsmPrimitive;
    13 import org.openstreetmap.josm.data.osm.Segment;
    1413import org.openstreetmap.josm.data.osm.Way;
    1514import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
     
    3231
    3332        /**
    34          * Export the dataset to gpx. Only the physical segment structure is
    35          * exported. To do this, the list of ways is processed. If a way span a
    36          * sequence of segments, this is added as one trkseg.
    37          * Then, all remaining segments are added in one extra trk. Finally,
    38          * all remaining nodes are added as wpt.
     33         * Export the dataset to gpx.  The ways are converted to trksegs, each in
     34         * a seperate trk.  Finally, all remaining nodes are added as wpt.
    3935         */
    4036        public static final class All implements XmlWriter.OsmWriterInterface {
     
    106102                                        continue;
    107103                                out.println("  <trk>");
    108                                 Segment oldLs = null;
    109                                 for (Segment ls : w.segments) {
    110                                         if (ls.incomplete)
    111                                                 continue;
    112                                         // end old segemnt, if no longer match a chain
    113                                         if (oldLs != null && !oldLs.to.coor.equals(ls.from.coor)) {
    114                                                 out.println("    </trkseg>");
    115                                                 writer.outputNode(oldLs.to, false);
    116                                                 all.remove(oldLs.to);
    117                                                 oldLs = null;
    118                                         }
    119                                         // start new segment if necessary
    120                                         if (oldLs == null)
    121104                                                out.println("    <trkseg>");
    122                                         writer.outputNode(ls.from, false);
    123                                         all.remove(ls.from);
    124                                         oldLs = ls;
    125                                         all.remove(ls);
    126                                 }
    127                                 // write last node if there
    128                                 if (oldLs != null) {
    129                                         writer.outputNode(oldLs.to, false);
    130                                         all.remove(oldLs.to);
     105                                for (Node n : w.nodes) {
     106                                        writer.outputNode(n, false);
     107                                        all.remove(n);
     108                        }
    131109                                        out.println("    </trkseg>");
    132                                 }
    133110                                out.println("  </trk>");
    134111                                all.remove(w);
    135                         }
    136 
    137                         // add remaining segments
    138                         Collection<Segment> segments = new LinkedList<Segment>();
    139                         for (OsmPrimitive osm : all)
    140                                 if (osm instanceof Segment && !((Segment)osm).incomplete)
    141                                         segments.add((Segment)osm);
    142                         if (!segments.isEmpty()) {
    143                                 out.println("  <trk>");
    144                                 for (Segment ls : segments) {
    145                                         out.println("    <trkseg>");
    146                                         writer.outputNode(ls.from, false);
    147                                         all.remove(ls.from);
    148                                         writer.outputNode(ls.to, false);
    149                                         all.remove(ls.to);
    150                                         out.println("    </trkseg>");
    151                                         all.remove(ls);
    152                                 }
    153                                 out.println("  </trk>");
    154112                        }
    155113
  • branch/0.5/src/org/openstreetmap/josm/io/OsmIdReader.java

    r319 r329  
    3131
    3232        @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    33                 if (qName.equals("node") || qName.equals("segment") || qName.equals("way")) {
     33                if (qName.equals("node") || qName.equals("way")) {
    3434                        try {
    3535                                entries.put(Long.valueOf(atts.getValue("id")), qName);
  • branch/0.5/src/org/openstreetmap/josm/io/OsmReader.java

    r319 r329  
    2424import org.openstreetmap.josm.data.osm.DataSet;
    2525import org.openstreetmap.josm.data.osm.DataSource;
     26import org.openstreetmap.josm.data.osm.Relation;
     27import org.openstreetmap.josm.data.osm.RelationMember;
    2628import org.openstreetmap.josm.data.osm.Node;
    2729import org.openstreetmap.josm.data.osm.OsmPrimitive;
    28 import org.openstreetmap.josm.data.osm.Segment;
    2930import org.openstreetmap.josm.data.osm.User;
    3031import org.openstreetmap.josm.data.osm.Way;
     
    4445 * all nodes are read and stored. Other information than nodes are stored in a raw list
    4546 *
    46  * The second phase reads from the raw list all segments and create Segment objects.
    47  *
    48  * The third phase read all ways out of the remaining objects in the raw list.
     47 * The second phase read all ways out of the remaining objects in the raw list.
    4948 *
    5049 * @author Imi
     
    9190
    9291        /**
    93          * Data structure for the remaining segment objects
    94          * Maps the raw attributes to key/value pairs.
    95          */
    96         private Map<OsmPrimitiveData, long[]> segs = new HashMap<OsmPrimitiveData, long[]>();
     92         * Used as a temporary storage for relation members, before they
     93         * are resolved into pointers to real objects.
     94         */
     95        private static class RelationMemberData {
     96                public String type;
     97                public long id;
     98                public RelationMember relationMember;
     99        }
    97100
    98101        /**
     
    100103         */
    101104        private Map<OsmPrimitiveData, Collection<Long>> ways = new HashMap<OsmPrimitiveData, Collection<Long>>();
     105
     106        /**
     107         * Data structure for relation objects
     108         */
     109        private Map<OsmPrimitiveData, Collection<RelationMemberData>> relations = new HashMap<OsmPrimitiveData, Collection<RelationMemberData>>();
    102110
    103111        /**
     
    132140                                                ds.dataSources.add(src);
    133141                                        }
     142                                       
     143                                // ---- PARSING NODES AND WAYS ----
     144                                       
    134145                                } else if (qName.equals("node")) {
    135146                                        current = new Node(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
    136147                                        readCommon(atts, current);
    137148                                        nodes.put(current.id, (Node)current);
    138                                 } else if (qName.equals("segment")) {
    139                                         current = new OsmPrimitiveData();
    140                                         readCommon(atts, current);
    141                                         segs.put((OsmPrimitiveData)current, new long[]{getLong(atts, "from"), getLong(atts, "to")});
    142149                                } else if (qName.equals("way")) {
    143150                                        current = new OsmPrimitiveData();
    144151                                        readCommon(atts, current);
    145152                                        ways.put((OsmPrimitiveData)current, new LinkedList<Long>());
    146                                 } else if (qName.equals("seg")) {
     153                                } else if (qName.equals("nd")) {
    147154                                        Collection<Long> list = ways.get(current);
    148155                                        if (list == null)
    149                                                 throw new SAXException(tr("Found <seg> tag on non-way."));
    150                                         long id = getLong(atts, "id");
     156                                                throw new SAXException(tr("Found <nd> element in non-way."));
     157                                        long id = getLong(atts, "ref");
    151158                                        if (id == 0)
    152                                                 throw new SAXException(tr("Incomplete segment with id=0"));
     159                                                throw new SAXException(tr("<nd> has zero ref"));
    153160                                        list.add(id);
    154                                 } else if (qName.equals("tag"))
     161
     162                                // ---- PARSING RELATIONS ----                 
     163
     164                                } else if (qName.equals("relation")) {
     165                                        current = new OsmPrimitiveData();
     166                                        readCommon(atts, current);
     167                                        relations.put((OsmPrimitiveData)current, new LinkedList<RelationMemberData>());
     168                                } else if (qName.equals("member")) {
     169                                        Collection<RelationMemberData> list = relations.get(current);
     170                                        if (list == null)
     171                                                throw new SAXException(tr("Found <member> tag on non-relation."));
     172                                        RelationMemberData emd = new RelationMemberData();
     173                                        emd.relationMember = new RelationMember();
     174                                        emd.id = getLong(atts, "ref");
     175                                        emd.type=atts.getValue("type");
     176                                        emd.relationMember.role = atts.getValue("role");
     177                                       
     178                                        if (emd.id == 0)
     179                                                throw new SAXException(tr("Incomplete <member> specification with ref=0"));
     180                                       
     181                                        list.add(emd);
     182                                       
     183                                // ---- PARSING TAGS (applicable to all objects) ----
     184                                       
     185                                } else if (qName.equals("tag")) {
    155186                                        current.put(atts.getValue("k"), atts.getValue("v"));
     187                                }
    156188                        } catch (NumberFormatException x) {
    157189                                x.printStackTrace(); // SAXException does not chain correctly
     
    173205        public OsmReader() {
    174206                // first add the main server version
    175                 allowedVersions.add(Main.pref.get("osm-server.version", "0.4"));
     207                allowedVersions.add(Main.pref.get("osm-server.version", "0.5"));
    176208                // now also add all compatible versions
    177209                String[] additionalVersions =
    178                         Main.pref.get("osm-server.additional-versions", "0.3").split("/,/");
     210                        Main.pref.get("osm-server.additional-versions", "").split("/,/");
     211                if (additionalVersions.length == 1 && additionalVersions[0].length() == 0)
     212                        additionalVersions = new String[] {};
    179213                allowedVersions.addAll(Arrays.asList(additionalVersions));     
    180214        }
     
    224258                        throw new SAXException(tr("Missing required attribute \"{0}\".",value));
    225259                return Long.parseLong(s);
    226         }
    227 
    228         private void createSegments() {
    229                 for (Entry<OsmPrimitiveData, long[]> e : segs.entrySet()) {
    230                         Node from = findNode(e.getValue()[0]);
    231                         Node to = findNode(e.getValue()[1]);
    232                         if (from == null || to == null)
    233                                 continue; //TODO: implement support for incomplete nodes.
    234                         Segment s = new Segment(from, to);
    235                         e.getKey().copyTo(s);
    236                         segments.put(s.id, s);
    237                         adder.visit(s);
    238                 }
    239260        }
    240261
     
    253274    }
    254275
    255         private Segment findSegment(long id) {
    256                 Segment s = segments.get(id);
    257                 if (s != null)
    258                         return s;
    259                 for (Segment seg : references.segments)
    260                         if (seg.id == id)
    261                                 return seg;
    262                 // TODO: This has to be changed to support multiple layers.
    263                 for (Segment seg : Main.ds.segments)
    264                         if (seg.id == id)
    265                                 return new Segment(seg);
    266                 return null;
    267         }
    268 
    269276        private void createWays() {
    270277                for (Entry<OsmPrimitiveData, Collection<Long>> e : ways.entrySet()) {
    271278                        Way w = new Way();
     279                        boolean failed = false;
    272280                        for (long id : e.getValue()) {
    273                                 Segment s = findSegment(id);
    274                                 if (s == null) {
    275                                         s = new Segment(id); // incomplete line segment
    276                                         adder.visit(s);
     281                                Node n = findNode(id);
     282                                if (n == null) {
     283                                        failed = true;
     284                                        break;
    277285                                }
    278                                 w.segments.add(s);
     286                                w.nodes.add(n);
    279287                        }
     288                        if (failed) continue;
    280289                        e.getKey().copyTo(w);
    281290                        adder.visit(w);
     
    284293
    285294        /**
    286          * All read segments after phase 2.
    287          */
    288         private Map<Long, Segment> segments = new HashMap<Long, Segment>();
     295         * Return the Way object with the given id, or null if it doesn't
     296         * exist yet. This method only looks at ways stored in the data set.
     297         *
     298         * @param id
     299         * @return way object or null
     300         */
     301        private Way findWay(long id) {
     302                for (Way wy : ds.ways)
     303                        if (wy.id == id)
     304                                return wy;
     305                for (Way wy : Main.ds.ways)
     306                        if (wy.id == id)
     307                                return wy;
     308                return null;
     309        }
     310
     311        /**
     312         * Return the Relation object with the given id, or null if it doesn't
     313         * exist yet. This method only looks at relations stored in the data set.
     314         *
     315         * @param id
     316         * @return relation object or null
     317         */
     318        private Relation findRelation(long id) {
     319                for (Relation e : ds.relations)
     320                        if (e.id == id)
     321                                return e;
     322                for (Relation e : Main.ds.relations)
     323                        if (e.id == id)
     324                                return e;
     325                return null;
     326        }
     327
     328        /**
     329         * Create relations. This is slightly different than n/s/w because
     330         * unlike other objects, relations may reference other relations; it
     331         * is not guaranteed that a referenced relation will have been created
     332         * before it is referenced. So we have to create all relations first,
     333         * and populate them later.
     334         */
     335        private void createRelations() {
     336               
     337                // pass 1 - create all relations
     338                for (Entry<OsmPrimitiveData, Collection<RelationMemberData>> e : relations.entrySet()) {
     339                        Relation en = new Relation();
     340                        e.getKey().copyTo(en);
     341                        adder.visit(en);
     342                }
     343
     344                // pass 2 - sort out members
     345                for (Entry<OsmPrimitiveData, Collection<RelationMemberData>> e : relations.entrySet()) {
     346                        Relation en = findRelation(e.getKey().id);
     347                        if (en == null) throw new Error("Failed to create relation " + e.getKey().id);
     348                       
     349                        for (RelationMemberData emd : e.getValue()) {
     350                                RelationMember em = emd.relationMember;
     351                                if (emd.type.equals("node")) {
     352                                        em.member = findNode(emd.id);
     353                                        if (em.member == null) {
     354                                                em.member = new Node(emd.id);
     355                                                adder.visit((Node)em.member);
     356                                        }
     357                                } else if (emd.type.equals("way")) {
     358                                        em.member = findWay(emd.id);
     359                                        if (em.member == null) {
     360                                                em.member = new Way(emd.id);
     361                                                adder.visit((Way)em.member);
     362                                        }
     363                                } else if (emd.type.equals("relation")) {
     364                                        em.member = findRelation(emd.id);
     365                                        if (em.member == null) {
     366                                                em.member = new Relation(emd.id);
     367                                                adder.visit((Relation)em.member);
     368                                        }
     369                                } else {
     370                                        // this is an error.
     371                                }
     372                                en.members.add(em);
     373                        }
     374                }
     375        }
    289376
    290377        /**
     
    298385                osm.references = ref == null ? new DataSet() : ref;
    299386
    300                 // phase 1: Parse nodes and read in raw segments and ways
     387                // phase 1: Parse nodes and read in raw ways
    301388                InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
    302389                try {
     
    314401
    315402                try {
    316                         osm.createSegments();
    317403                        osm.createWays();
     404                        osm.createRelations();
    318405                } catch (NumberFormatException e) {
    319406                        e.printStackTrace();
  • branch/0.5/src/org/openstreetmap/josm/io/OsmServerReader.java

    r298 r329  
    3030         */
    3131        protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
    32                 String version = Main.pref.get("osm-server.version", "0.4");
     32                String version = Main.pref.get("osm-server.version", "0.5");
    3333                urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr;
    3434                System.out.println("download: "+urlStr);
  • branch/0.5/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r298 r329  
    1717
    1818import org.openstreetmap.josm.Main;
     19import org.openstreetmap.josm.data.osm.Relation;
    1920import org.openstreetmap.josm.data.osm.Node;
    2021import org.openstreetmap.josm.data.osm.OsmPrimitive;
    21 import org.openstreetmap.josm.data.osm.Segment;
    2222import org.openstreetmap.josm.data.osm.Way;
    2323import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
     
    9696
    9797        /**
    98          * Upload a segment (without the nodes).
    99          */
    100         public void visit(Segment ls) {
    101                 if (ls.id == 0 && !ls.deleted && ls.get("created_by") == null) {
    102                         ls.put("created_by", "JOSM");
    103                         sendRequest("PUT", "segment", ls, true);
    104                 } else if (ls.deleted) {
    105                         sendRequest("DELETE", "segment", ls, false);
    106                 } else {
    107                         sendRequest("PUT", "segment", ls, true);
    108                 }
    109                 processed.add(ls);
    110         }
    111 
    112         /**
    113          * Upload a whole way with the complete segment id list.
     98         * Upload a whole way with the complete node id list.
    11499         */
    115100        public void visit(Way w) {
     
    125110        }
    126111
     112        /**
     113         * Upload an relation with all members.
     114         */
     115        public void visit(Relation e) {
     116                if (e.id == 0 && !e.deleted && e.get("created_by") == null) {
     117                        e.put("created_by", "JOSM");
     118                        sendRequest("PUT", "relation", e, true);
     119                } else if (e.deleted) {
     120                        sendRequest("DELETE", "relation", e, false);
     121                } else {
     122                        sendRequest("PUT", "relation", e, true);
     123                }
     124                processed.add(e);
     125        }
    127126        /**
    128127         * Read a long from the input stream and return it.
     
    154153                        OsmPrimitive osm, boolean addBody) {
    155154                try {
    156                         String version = Main.pref.get("osm-server.version", "0.4");
     155                        String version = Main.pref.get("osm-server.version", "0.5");
    157156                        URL url = new URL(
    158157                                        Main.pref.get("osm-server.url") +
    159158                                        "/" + version +
    160159                                        "/" + urlSuffix +
    161                                         "/" + ((version.equals("0.4") && osm.id==0) ? "create":osm.id));
     160                                        "/" + (osm.id==0 ? "create" : osm.id));
    162161                        System.out.println("upload to: "+url);
    163162                        activeConnection = (HttpURLConnection)url.openConnection();
  • branch/0.5/src/org/openstreetmap/josm/io/OsmWriter.java

    r298 r329  
    99import org.openstreetmap.josm.data.osm.DataSet;
    1010import org.openstreetmap.josm.data.osm.DataSource;
     11import org.openstreetmap.josm.data.osm.Relation;
     12import org.openstreetmap.josm.data.osm.RelationMember;
    1113import org.openstreetmap.josm.data.osm.Node;
    1214import org.openstreetmap.josm.data.osm.OsmPrimitive;
    13 import org.openstreetmap.josm.data.osm.Segment;
    1415import org.openstreetmap.josm.data.osm.Way;
    1516import org.openstreetmap.josm.data.osm.visitor.Visitor;
     
    3738                public void header(PrintWriter out) {
    3839                        out.print("<osm version='");
    39                         out.print(Main.pref.get("osm-server.version", "0.4"));
     40                        out.print(Main.pref.get("osm-server.version", "0.5"));
    4041                        out.println("' generator='JOSM'>");
    4142                }
     
    4445                }
    4546        }
     47       
     48        // simple helper to write the object's class to the out stream
     49        private Visitor typeWriteVisitor = new Visitor() {
     50                public void visit(Node n) { out.print("node"); }
     51                public void visit(Way w) { out.print("way"); }
     52                public void visit(Relation e) { out.print("relation"); }
     53        };
    4654       
    4755        /**
     
    6977                                if (shouldWrite(n))
    7078                                        writer.visit(n);
    71                         for (Segment ls : ds.segments)
    72                                 if (shouldWrite(ls))
    73                                         writer.visit(ls);
    7479                        for (Way w : ds.ways)
    7580                                if (shouldWrite(w))
    7681                                        writer.visit(w);
     82                        for (Relation e : ds.relations)
     83                                if (shouldWrite(e))
     84                                        writer.visit(e);
    7785        }
    7886
     
    123131        }
    124132
    125         public void visit(Segment ls) {
    126                 if (ls.incomplete)
    127                         return; // Do not write an incomplete segment
    128                 addCommon(ls, "segment");
    129                 out.print(" from='"+getUsedId(ls.from)+"' to='"+getUsedId(ls.to)+"'");
    130                 addTags(ls, "segment", true);
    131         }
    132 
    133133        public void visit(Way w) {
    134134                addCommon(w, "way");
    135135                out.println(">");
    136                 for (Segment ls : w.segments)
    137                         out.println("    <seg id='"+getUsedId(ls)+"' />");
     136                for (Node n : w.nodes)
     137                        out.println("    <nd ref='"+getUsedId(n)+"' />");
    138138                addTags(w, "way", false);
    139139        }
     140
     141        public void visit(Relation e) {
     142                addCommon(e, "relation");
     143                out.println(">");
     144                for (RelationMember em : e.members) {
     145                        out.print("    <member type='");
     146                        em.member.visit(typeWriteVisitor);
     147                        out.println("' ref='"+getUsedId(em.member)+"' role='" +
     148                                XmlWriter.encode(em.role) + "' />");
     149                }
     150                addTags(e, "relation", false);
     151        }
     152       
    140153
    141154        /**
  • branch/0.5/src/org/openstreetmap/josm/io/XmlWriter.java

    r298 r329  
    6767        }
    6868
    69 
    70 
    7169        /**
    7270         * The output writer to save the values to.
Note: See TracChangeset for help on using the changeset viewer.