Changeset 2982 in josm


Ignore:
Timestamp:
Feb 14, 2010 3:01:00 PM (3 years ago)
Author:
jttt
Message:

Calculate bbox for relations, use it in mappaint.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/BBox.java

    r2500 r2982  
    6767        ymax = Math.max(ymax, y); 
    6868        sanity(); 
     69    } 
     70 
     71    public void add(BBox box) { 
     72        add(box.getTopLeft()); 
     73        add(box.getBottomRight()); 
    6974    } 
    7075 
     
    139144 
    140145    @Override 
     146    public int hashCode() { 
     147        return (int)(ymin * xmin); 
     148    } 
     149 
     150    @Override 
     151    public boolean equals(Object o) { 
     152        if (o instanceof BBox) { 
     153            BBox b = (BBox)o; 
     154            return b.xmax == xmax && b.ymax == ymax && b.xmin == xmin && b.ymin == ymin; 
     155        } else 
     156            return false; 
     157    } 
     158 
     159    @Override 
    141160    public String toString() { 
    142161        return "[ x: " + xmin + " -> " + xmax + 
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r2944 r2982  
    136136    } 
    137137 
     138    public List<Relation> searchRelations(BBox bbox) { 
     139        // QuadBuckets might be useful here (don't forget to do reindexing after some of rm is changed) 
     140        List<Relation> result = new ArrayList<Relation>(); 
     141        for (Relation r: relations) { 
     142            if (r.getBBox().intersects(bbox)) { 
     143                result.add(r); 
     144            } 
     145        } 
     146        return result; 
     147    } 
     148 
    138149    /** 
    139150     * All data sources of this DataSet. 
     
    200211     * Adds a primitive to the dataset 
    201212     * 
    202      * @param primitive the primitive. Ignored if null. 
     213     * @param primitive the primitive. 
    203214     */ 
    204215    public void addPrimitive(OsmPrimitive primitive) { 
     
    779790    } 
    780791 
    781     /** 
    782      * Reindex all nodes and ways after their coordinates were changed. This is a temporary solution, reindexing should 
    783      * be automatic in the future 
    784      * @deprecated Reindexing should be automatic 
    785      */ 
    786     @Deprecated 
    787     public void reindexAll() { 
    788         List<Node> ntmp = new ArrayList<Node>(nodes); 
    789         nodes.clear(); 
    790         nodes.addAll(ntmp); 
    791         List<Way> wtmp = new ArrayList<Way>(ways); 
    792         ways.clear(); 
    793         ways.addAll(wtmp); 
    794     } 
    795  
    796792    private void reindexNode(Node node) { 
    797793        nodes.remove(node); 
    798794        nodes.add(node); 
    799         for (Way way:OsmPrimitive.getFilteredList(node.getReferrers(), Way.class)) { 
    800             ways.remove(way); 
    801             way.updatePosition(); 
    802             ways.add(way); 
     795        for (OsmPrimitive primitive: node.getReferrers()) { 
     796            if (primitive instanceof Way) { 
     797                reindexWay((Way)primitive); 
     798            } else { 
     799                reindexRelation((Relation) primitive); 
     800            } 
    803801        } 
    804802    } 
    805803 
    806804    private void reindexWay(Way way) { 
     805        BBox before = way.getBBox(); 
    807806        ways.remove(way); 
    808807        way.updatePosition(); 
    809808        ways.add(way); 
     809        if (!way.getBBox().equals(before)) { 
     810            for (OsmPrimitive primitive: way.getReferrers()) { 
     811                reindexRelation((Relation)primitive); 
     812            } 
     813        } 
     814    } 
     815 
     816    private void reindexRelation(Relation relation) { 
     817        BBox before = relation.getBBox(); 
     818        relation.updatePosition(); 
     819        if (!before.equals(relation.getBBox())) { 
     820            for (OsmPrimitive primitive: relation.getReferrers()) { 
     821                reindexRelation((Relation) primitive); 
     822            } 
     823        } 
    810824    } 
    811825 
     
    874888 
    875889    void fireRelationMembersChanged(Relation r) { 
     890        reindexRelation(r); 
    876891        fireEvent(new RelationMembersChangedEvent(this, r)); 
    877892    } 
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r2970 r2982  
    2323     */ 
    2424    private final List<RelationMember> members = new ArrayList<RelationMember>(); 
     25 
     26    private BBox bbox; 
    2527 
    2628    /** 
     
    336338    @Override 
    337339    public BBox getBBox() { 
    338         return new BBox(0, 0, 0, 0); 
     340        if (bbox == null) { 
     341            calculateBBox(new HashSet<PrimitiveId>()); 
     342            if (bbox == null) { 
     343                bbox = new BBox(0, 0, 0, 0); // No members 
     344            } 
     345        } 
     346        return  bbox; 
     347    } 
     348 
     349    private BBox calculateBBox(Set<PrimitiveId> visitedRelations) { 
     350        if (visitedRelations.contains(this)) 
     351            return null; 
     352        visitedRelations.add(this); 
     353        if (members.isEmpty()) 
     354            return null; 
     355        else { 
     356            BBox result = null; 
     357            for (RelationMember rm:members) { 
     358                BBox box = rm.isRelation()?rm.getRelation().calculateBBox(visitedRelations):rm.getMember().getBBox(); 
     359                if (box != null) { 
     360                    if (result == null) { 
     361                        result = box; 
     362                    } else { 
     363                        result.add(box); 
     364                    } 
     365                } 
     366            } 
     367            return result; 
     368        } 
    339369    } 
    340370 
    341371    @Override 
    342372    public void updatePosition() { 
    343         // Do nothing for now 
     373        bbox = calculateBBox(new HashSet<PrimitiveId>()); 
    344374    } 
    345375 
     
    355385            for (RelationMember rm: members) { 
    356386                if (rm.getMember().getDataSet() != dataSet) 
    357                     throw new DataIntegrityProblemException("Relation member must be part of the same dataset as relation"); 
     387                    throw new DataIntegrityProblemException(String.format("Relation member must be part of the same dataset as relation(%s, %s)", getPrimitiveId(), rm.getMember().getPrimitiveId())); 
    358388            } 
    359389        } 
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPaintVisitor.java

    r2954 r2982  
    244244 
    245245    public void drawRestriction(Relation r) { 
     246 
    246247        Way fromWay = null; 
    247248        Way toWay = null; 
     
    674675 
    675676            /*** RELATIONS ***/ 
    676             for (final Relation osm: data.getRelations()) { 
     677            for (final Relation osm: data.searchRelations(bbox)) { 
    677678                if (osm.isDrawable()) { 
    678679                    paintUnselectedRelation(osm); 
     
    699700 
    700701            /*** RELATIONS ***/ 
    701             for (final Relation osm: data.getRelations()) { 
     702            for (final Relation osm: data.searchRelations(bbox)) { 
    702703                if (osm.isDrawable()) { 
    703704                    paintUnselectedRelation(osm); 
  • trunk/src/org/openstreetmap/josm/gui/conflict/pair/ListMerger.java

    r2945 r2982  
    641641            ImageIcon icon = ImageProvider.get("dialogs/conflict", "useallleft.png"); 
    642642            putValue(Action.SMALL_ICON, icon); 
    643             putValue(Action.SHORT_DESCRIPTION, tr("Use all mine elements")); 
     643            putValue(Action.SHORT_DESCRIPTION, tr("Use all my elements")); 
    644644        } 
    645645 
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r2907 r2982  
    397397    protected PurgePrimitivesCommand buildPurgeCommand() { 
    398398        ArrayList<OsmPrimitive> toPurge = new ArrayList<OsmPrimitive>(); 
    399         conflictLoop: for (Conflict<?> c: conflicts) { 
    400             if (c.getMy().isDeleted() && !c.getTheir().isVisible()) { 
    401                 // Local and server version of the primitive are deleted. We 
    402                 // can purge it from the local dataset. 
    403                 // 
    404                 toPurge.add(c.getMy()); 
    405             } else if (!c.getMy().isModified() && ! c.getTheir().isVisible()) { 
    406                 // We purge deleted *ways* and *relations* automatically if they are 
    407                 // deleted on the server and if they aren't modified in the local 
    408                 // dataset. 
    409                 // 
    410                 if (c.getMy() instanceof Way || c.getMy() instanceof Relation) { 
     399        conflictLoop: 
     400            for (Conflict<?> c: conflicts) { 
     401                if (c.getMy().isDeleted() && !c.getTheir().isVisible()) { 
     402                    // Local and server version of the primitive are deleted. We 
     403                    // can purge it from the local dataset. 
     404                    // 
    411405                    toPurge.add(c.getMy()); 
    412                     continue conflictLoop; 
    413                 } 
    414                 // We only purge nodes if they aren't part of a modified way. 
    415                 // Otherwise the number of nodes of a modified way could drop 
    416                 // below 2 and we would lose the modified data when the way 
    417                 // gets purged. 
    418                 // 
    419                 for (OsmPrimitive parent: c.getMy().getReferrers()) { 
    420                     if (parent.isModified() && parent instanceof Way) { 
     406                } else if (!c.getMy().isModified() && ! c.getTheir().isVisible()) { 
     407                    // We purge deleted *ways* and *relations* automatically if they are 
     408                    // deleted on the server and if they aren't modified in the local 
     409                    // dataset. 
     410                    // 
     411                    if (c.getMy() instanceof Way || c.getMy() instanceof Relation) { 
     412                        toPurge.add(c.getMy()); 
    421413                        continue conflictLoop; 
    422414                    } 
     415                    // We only purge nodes if they aren't part of a modified way. 
     416                    // Otherwise the number of nodes of a modified way could drop 
     417                    // below 2 and we would lose the modified data when the way 
     418                    // gets purged. 
     419                    // 
     420                    for (OsmPrimitive parent: c.getMy().getReferrers()) { 
     421                        if (parent.isModified() && parent instanceof Way) { 
     422                            continue conflictLoop; 
     423                        } 
     424                    } 
     425                    toPurge.add(c.getMy()); 
    423426                } 
    424                 toPurge.add(c.getMy()); 
    425             } 
    426         } 
     427            } 
    427428        if (toPurge.isEmpty()) return null; 
    428429        PurgePrimitivesCommand cmd = new PurgePrimitivesCommand(this, toPurge); 
  • trunk/test/unit/org/openstreetmap/josm/data/osm/RelationTest.java

    r1806 r2982  
    22package org.openstreetmap.josm.data.osm; 
    33 
     4import static org.junit.Assert.assertFalse; 
     5 
     6import org.junit.Assert; 
    47import org.junit.Test; 
    5  
    6 import static org.junit.Assert.*; 
     8import org.openstreetmap.josm.data.coor.LatLon; 
    79 
    810public class RelationTest { 
     11 
    912    @Test(expected=NullPointerException.class) 
    1013    public void createNewRelation() { 
     
    1821    } 
    1922 
     23    @Test 
     24    public void testBBox() { 
     25        DataSet ds = new DataSet(); 
     26 
     27        Node n1 = new Node(new LatLon(10, 10)); 
     28        Node n2 = new Node(new LatLon(20, 20)); 
     29        Node n3 = new Node(new LatLon(30, 30)); 
     30        Way w1 = new Way(); 
     31        w1.addNode(n1); 
     32        w1.addNode(n2); 
     33        Relation r1 = new Relation(); 
     34        Relation r2 = new Relation(); 
     35        ds.addPrimitive(r1); 
     36        ds.addPrimitive(r2); 
     37        ds.addPrimitive(n1); 
     38        ds.addPrimitive(n2); 
     39        ds.addPrimitive(n3); 
     40        ds.addPrimitive(w1); 
     41        r1.addMember(new RelationMember("", n1)); 
     42        r1.addMember(new RelationMember("", w1)); 
     43        r1.addMember(new RelationMember("", r1)); 
     44        r1.addMember(new RelationMember("", r2)); 
     45        r2.addMember(new RelationMember("", r1)); 
     46        r2.addMember(new RelationMember("", n3)); 
     47 
     48        BBox bbox = new BBox(w1); 
     49        bbox.add(n3.getBBox()); 
     50        Assert.assertEquals(bbox, r1.getBBox()); 
     51        Assert.assertEquals(bbox, r2.getBBox()); 
     52 
     53        n3.setCoor(new LatLon(40, 40)); 
     54        bbox.add(n3.getBBox()); 
     55        Assert.assertEquals(bbox, r1.getBBox()); 
     56        Assert.assertEquals(bbox, r2.getBBox()); 
     57 
     58        r1.removeMembersFor(r2); 
     59        Assert.assertEquals(w1.getBBox(), r1.getBBox()); 
     60        Assert.assertEquals(bbox, r2.getBBox()); 
     61 
     62        w1.addNode(n3); 
     63        Assert.assertEquals(w1.getBBox(), r1.getBBox()); 
     64        Assert.assertEquals(w1.getBBox(), r2.getBBox()); 
     65    } 
     66 
    2067} 
Note: See TracChangeset for help on using the changeset viewer.