Changeset 9925 in josm


Ignore:
Timestamp:
2016-03-05T10:22:33+01:00 (8 years ago)
Author:
simon04
Message:

see #6401 - Orthogonalize objects based on selected nodes

This is done by assembling a virtual way from the selected nodes. This is sensitive to the selection order.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java

    r9923 r9925  
    180180                nodeList.add((Node) p);
    181181            } else if (p instanceof Way) {
    182                 wayDataList.add(new WayData((Way) p));
    183             } else
     182                wayDataList.add(new WayData(((Way) p).getNodes()));
     183            } else {
    184184                throw new InvalidUserInputException(tr("Selection must consist only of ways and nodes."));
    185         }
    186         if (wayDataList.isEmpty()) {
     185            }
     186        }
     187        if (wayDataList.isEmpty() && !nodeList.isEmpty()) {
     188            final WayData data = new WayData(nodeList);
     189            final Collection<Command> commands = orthogonalize(Collections.singletonList(data), Collections.<Node>emptyList());
     190            return new SequenceCommand(tr("Orthogonalize"), commands);
     191        } else if (wayDataList.isEmpty()) {
    187192            throw new InvalidUserInputException("usage");
    188193        } else  {
     
    235240            WayData candidate = remaining.get(i);
    236241            if (candidate == null) continue;
    237             if (!Collections.disjoint(candidate.way.getNodes(), newGroupMember.way.getNodes())) {
     242            if (!Collections.disjoint(candidate.wayNodes, newGroupMember.wayNodes)) {
    238243                remaining.set(i, null);
    239244                extendGroupRec(group, candidate, remaining);
     
    300305        final Set<Node> allNodes = new HashSet<>();
    301306        for (WayData w : wayDataList) {
    302             for (Node n : w.way.getNodes()) {
     307            for (Node n : w.wayNodes) {
    303308                allNodes.add(n);
    304309            }
     
    345350                    for (WayData w : wayDataList) {
    346351                        for (int i = 0; i < w.nSeg; ++i) {
    347                             Node n1 = w.way.getNodes().get(i);
    348                             Node n2 = w.way.getNodes().get(i+1);
     352                            Node n1 = w.wayNodes.get(i);
     353                            Node n2 = w.wayNodes.get(i+1);
    349354                            if (Arrays.asList(orientation).contains(w.segDirections[i])) {
    350355                                if (cs.contains(n1) && !cs.contains(n2)) {
     
    417422     */
    418423    private static class WayData {
    419         public final Way way;             // The assigned way
     424        public final List<Node> wayNodes;             // The assigned way
    420425        public final int nSeg;            // Number of Segments of the Way
    421426        public final int nNode;           // Number of Nodes of the Way
     
    426431        public double heading;            // heading of segSum == approximate heading of the way
    427432
    428         WayData(Way pWay) {
    429             way = pWay;
    430             nNode = way.getNodes().size();
    431             nSeg = nNode - 1;
     433        WayData(List<Node> wayNodes) {
     434            this.wayNodes = wayNodes;
     435            this.nNode = wayNodes.size();
     436            this.nSeg = nNode - 1;
    432437        }
    433438
     
    441446         */
    442447        public void calcDirections(Direction pInitialDirection) throws InvalidUserInputException {
    443             final EastNorth[] en = new EastNorth[nNode]; // alias: way.getNodes().get(i).getEastNorth() ---> en[i]
     448            final EastNorth[] en = new EastNorth[nNode]; // alias: wayNodes.get(i).getEastNorth() ---> en[i]
    444449            for (int i = 0; i < nNode; i++) {
    445                 en[i] = new EastNorth(way.getNodes().get(i).getEastNorth().east(), way.getNodes().get(i).getEastNorth().north());
     450                en[i] = wayNodes.get(i).getEastNorth();
    446451            }
    447452            segDirections = new Direction[nSeg];
  • trunk/test/data/orthogonalize.osm

    r9923 r9925  
    11<?xml version='1.0' encoding='UTF-8'?>
    2 <osm version='0.6' upload='true' generator='JOSM'>
    3   <node id='-444' action='modify' visible='true' lat='8.53882631502' lon='55.72982273659' />
    4   <node id='-446' action='modify' visible='true' lat='8.53692765033' lon='55.73148512506' />
    5   <node id='-448' action='modify' visible='true' lat='8.53901816563' lon='55.7313279173' />
    6   <node id='-450' action='modify' visible='true' lat='8.53673579867' lon='55.72997994436' />
    7   <node id='-452' action='modify' visible='true' lat='8.53609408724' lon='55.73073587955' />
    8   <node id='-454' action='modify' visible='true' lat='8.5381846071' lon='55.73057867179' />
    9   <node id='-456' action='modify' visible='true' lat='8.534711427' lon='55.73000670312'>
     2<osm version='0.6' upload='false' generator='JOSM'>
     3  <node id='-164' action='modify' visible='true' lat='8.53882631502' lon='55.72982273659' />
     4  <node id='-166' action='modify' visible='true' lat='8.53692765033' lon='55.73148512506' />
     5  <node id='-168' action='modify' visible='true' lat='8.53901816563' lon='55.7313279173' />
     6  <node id='-170' action='modify' visible='true' lat='8.53673579867' lon='55.72997994436' />
     7  <node id='-172' action='modify' visible='true' lat='8.53609408724' lon='55.73073587955' />
     8  <node id='-174' action='modify' visible='true' lat='8.5381846071' lon='55.73057867179' />
     9  <node id='-176' action='modify' visible='true' lat='8.534711427' lon='55.73000670312'>
    1010    <tag k='entrance' v='yes' />
    1111    <tag k='name' v='ClosedWayWithReferenceNodes' />
    1212  </node>
    13   <node id='-458' action='modify' visible='true' lat='8.53545899034' lon='55.7305284991' />
    14   <node id='-460' action='modify' visible='true' lat='8.5395738703' lon='55.73034453257' />
    15   <node id='-462' action='modify' visible='true' lat='8.53490327967' lon='55.73151188383' />
    16   <node id='-464' action='modify' visible='true' lat='8.53748335805' lon='55.73050174033' />
    17   <node id='-466' action='modify' visible='true' lat='8.53406971216' lon='55.73076263832'>
     13  <node id='-178' action='modify' visible='true' lat='8.53545899034' lon='55.7305284991' />
     14  <node id='-180' action='modify' visible='true' lat='8.5395738703' lon='55.73034453257' />
     15  <node id='-182' action='modify' visible='true' lat='8.53490327967' lon='55.73151188383' />
     16  <node id='-184' action='modify' visible='true' lat='8.53748335805' lon='55.73050174033' />
     17  <node id='-186' action='modify' visible='true' lat='8.53406971216' lon='55.73076263832'>
    1818    <tag k='entrance' v='yes' />
    1919    <tag k='name' v='ClosedWayWithReferenceNodes' />
    2020  </node>
    21   <way id='-468' action='modify' visible='true'>
    22     <nd ref='-444' />
    23     <nd ref='-460' />
    24     <nd ref='-448' />
    25     <nd ref='-454' />
    26     <nd ref='-444' />
     21  <node id='-188' action='modify' visible='true' lat='8.53273196095' lon='55.72987441027'>
     22    <tag k='entrance' v='yes' />
     23    <tag k='name' v='NodeToRectify-01' />
     24  </node>
     25  <node id='-190' action='modify' visible='true' lat='8.53347952816' lon='55.73039620625'>
     26    <tag k='entrance' v='yes' />
     27    <tag k='name' v='NodeToRectify-02' />
     28  </node>
     29  <node id='-192' action='modify' visible='true' lat='8.53292381462' lon='55.73137959098'>
     30    <tag k='entrance' v='yes' />
     31    <tag k='name' v='NodeToRectify-03' />
     32  </node>
     33  <node id='-194' action='modify' visible='true' lat='8.53209024278' lon='55.73063034547'>
     34    <tag k='entrance' v='yes' />
     35    <tag k='name' v='NodeToRectify-04' />
     36  </node>
     37  <node id='-196' action='modify' visible='true' lat='8.53332193268' lon='55.73067508568' />
     38  <node id='-198' action='modify' visible='true' lat='8.53308888534' lon='55.73108748376' />
     39  <node id='-200' action='modify' visible='true' lat='8.53317579063' lon='55.73111687863' />
     40  <node id='-202' action='modify' visible='true' lat='8.53326720503' lon='55.73111040645' />
     41  <node id='-204' action='modify' visible='true' lat='8.53334921153' lon='55.73106905258' />
     42  <node id='-206' action='modify' visible='true' lat='8.5334093254' lon='55.73099911274' />
     43  <node id='-208' action='modify' visible='true' lat='8.53343839484' lon='55.73091123466' />
     44  <node id='-210' action='modify' visible='true' lat='8.53343199432' lon='55.73081879697' />
     45  <node id='-212' action='modify' visible='true' lat='8.53339109825' lon='55.73073587248' />
     46  <way id='-214' action='modify' visible='true'>
     47    <nd ref='-164' />
     48    <nd ref='-180' />
     49    <nd ref='-168' />
     50    <nd ref='-174' />
     51    <nd ref='-164' />
    2752    <tag k='building' v='yes' />
    2853    <tag k='name' v='ClosedWay' />
    2954  </way>
    30   <way id='-470' action='modify' visible='true'>
    31     <nd ref='-450' />
    32     <nd ref='-464' />
    33     <nd ref='-446' />
     55  <way id='-216' action='modify' visible='true'>
     56    <nd ref='-170' />
     57    <nd ref='-184' />
     58    <nd ref='-166' />
    3459    <tag k='building' v='yes' />
    3560    <tag k='name' v='TwoWaysFormingClosedWay' />
    3661  </way>
    37   <way id='-472' action='modify' visible='true'>
    38     <nd ref='-456' />
    39     <nd ref='-458' />
    40     <nd ref='-462' />
    41     <nd ref='-466' />
    42     <nd ref='-456' />
     62  <way id='-218' action='modify' visible='true'>
     63    <nd ref='-176' />
     64    <nd ref='-178' />
     65    <nd ref='-182' />
     66    <nd ref='-186' />
     67    <nd ref='-176' />
    4368    <tag k='building' v='yes' />
    4469    <tag k='name' v='ClosedWayWithReferenceNodes' />
    4570  </way>
    46   <way id='-474' action='modify' visible='true'>
    47     <nd ref='-446' />
    48     <nd ref='-452' />
    49     <nd ref='-450' />
     71  <way id='-220' action='modify' visible='true'>
     72    <nd ref='-166' />
     73    <nd ref='-172' />
     74    <nd ref='-170' />
    5075    <tag k='building' v='yes' />
    5176    <tag k='name' v='TwoWaysFormingClosedWay' />
    5277  </way>
     78  <way id='-222' action='modify' visible='true'>
     79    <nd ref='-188' />
     80    <nd ref='-190' />
     81    <nd ref='-196' />
     82    <nd ref='-212' />
     83    <nd ref='-210' />
     84    <nd ref='-208' />
     85    <nd ref='-206' />
     86    <nd ref='-204' />
     87    <nd ref='-202' />
     88    <nd ref='-200' />
     89    <nd ref='-198' />
     90    <nd ref='-192' />
     91    <nd ref='-194' />
     92    <nd ref='-188' />
     93    <tag k='building' v='yes' />
     94  </way>
    5395</osm>
  • trunk/test/unit/org/openstreetmap/josm/actions/OrthogonalizeActionTest.java

    r9924 r9925  
    55
    66import java.io.FileInputStream;
     7import java.util.ArrayList;
     8import java.util.List;
    79
    810import org.junit.BeforeClass;
     
    1315import org.openstreetmap.josm.data.coor.LatLon;
    1416import org.openstreetmap.josm.data.osm.DataSet;
     17import org.openstreetmap.josm.data.osm.Node;
    1518import org.openstreetmap.josm.data.osm.Way;
    1619import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    6972    }
    7073
    71     DataSet performTest(String search) throws Exception {
     74    @Test
     75    public void testFourNodes() throws Exception {
     76        final DataSet ds = performTest(
     77                "name=NodeToRectify-01", "name=NodeToRectify-02", "name=NodeToRectify-03", "name=NodeToRectify-04");
     78        final List<Node> nodes = new ArrayList<>(ds.getSelectedNodes());
     79        assertEquals(new LatLon(8.532735415272217, 55.72986948949525), nodes.get(0).getCoor());
     80        assertEquals(new LatLon(8.533520827858515, 55.73043325105434), nodes.get(1).getCoor());
     81        assertEquals(new LatLon(8.532914283300173, 55.73129729115582), nodes.get(2).getCoor());
     82        assertEquals(new LatLon(8.532055019939826, 55.73068052126457), nodes.get(3).getCoor());
     83    }
     84
     85    DataSet performTest(String... search) throws Exception {
    7286        try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "orthogonalize.osm")) {
    7387            final DataSet ds = OsmReader.parseDataSet(in, null);
    74             ds.setSelected(Utils.filter(ds.allPrimitives(), SearchCompiler.compile(search)));
     88            for (String s : search) {
     89                ds.addSelected(Utils.filter(ds.allPrimitives(), SearchCompiler.compile(s)));
     90            }
    7591            OrthogonalizeAction.orthogonalize(ds.getSelected()).executeCommand();
    7692            return ds;
Note: See TracChangeset for help on using the changeset viewer.