Пример #1
0
  /**
   * Adds the previously removed relations again to the outer way. If there are multiple
   * multipolygon relations where the joined areas were in "outer" role a new relation is created
   * instead with all members of both. This function depends on multigon relations to be valid
   * already, it won't fix them.
   *
   * @param rels List of relations with roles the (original) ways were part of
   * @param outer The newly created outer area/way
   * @param ownMultipol elements to directly add as outer
   * @param relationsToDelete set of relations to delete.
   */
  private void fixRelations(
      List<RelationRole> rels,
      Way outer,
      RelationRole ownMultipol,
      Set<Relation> relationsToDelete) {
    List<RelationRole> multiouters = new ArrayList<RelationRole>();

    if (ownMultipol != null) {
      multiouters.add(ownMultipol);
    }

    for (RelationRole r : rels) {
      if (r.rel.isMultipolygon() && r.role.equalsIgnoreCase("outer")) {
        multiouters.add(r);
        continue;
      }
      // Add it back!
      Relation newRel = new Relation(r.rel);
      newRel.addMember(new RelationMember(r.role, outer));
      cmds.add(new ChangeCommand(r.rel, newRel));
    }

    Relation newRel;
    switch (multiouters.size()) {
      case 0:
        return;
      case 1:
        // Found only one to be part of a multipolygon relation, so just add it back as well
        newRel = new Relation(multiouters.get(0).rel);
        newRel.addMember(new RelationMember(multiouters.get(0).role, outer));
        cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel));
        return;
      default:
        // Create a new relation with all previous members and (Way)outer as outer.
        newRel = new Relation();
        for (RelationRole r : multiouters) {
          // Add members
          for (RelationMember rm : r.rel.getMembers())
            if (!newRel.getMembers().contains(rm)) {
              newRel.addMember(rm);
            }
          // Add tags
          for (String key : r.rel.keySet()) {
            newRel.put(key, r.rel.get(key));
          }
          // Delete old relation
          relationsToDelete.add(r.rel);
        }
        newRel.addMember(new RelationMember("outer", outer));
        cmds.add(new AddCommand(newRel));
    }
  }
Пример #2
0
  public void produceRelation(HashSet<Channel> subset, int n) {
    if (isProduced(subset)) {
      return;
    }
    LinkedList<OsmPrimitive> ways = new LinkedList<OsmPrimitive>();
    Iterator<Channel> cit = subset.iterator();
    while (cit.hasNext()) {
      Channel c = cit.next();
      // System.out.println(c.getWay().getId());
      if (!(ways.contains(
          plugin.getOsmlayer().data.getPrimitiveById(c.getWay().getId(), OsmPrimitiveType.WAY)))) {
        ways.add(
            plugin.getOsmlayer().data.getPrimitiveById(c.getWay().getId(), OsmPrimitiveType.WAY));
      }
    }
    Main.map.mapView.setActiveLayer(plugin.getOsmlayer());
    plugin.getOsmlayer().data.setSelected(ways);

    Relation jrelation = new Relation();
    jrelation.put("type", "junction");
    jrelation.put("n", Integer.toString(n));
    for (int i = 0; i < ways.size(); i++) {
      jrelation.addMember(new RelationMember("part of", ways.get(i)));
    }

    plugin.getOsmlayer().data.addPrimitive(jrelation);
  }
Пример #3
0
  @Override
  public void mouseClicked(MouseEvent e) {
    if (Main.main.getCurrentDataSet() == null) return;

    Way way = Main.map.mapView.getNearestWay(e.getPoint(), OsmPrimitive.isUsablePredicate);
    Collection<Relation> selectedRelations = Main.main.getCurrentDataSet().getSelectedRelations();

    if (way != null) {

      if (selectedRelations.isEmpty()) {
        JOptionPane.showMessageDialog(Main.parent, tr("No relation is selected"));
      }

      for (OsmPrimitive rel : selectedRelations) {
        Relation r = (Relation) rel;
        RelationMember foundMember = null;
        for (RelationMember member : r.getMembers()) {
          if (member.getMember() == way) {
            foundMember = member;
            break;
          }
        }

        if (foundMember != null) {
          Main.main.undoRedo.add(new RemoveRelationMemberCommand(r, new RelationMember("", way)));
        } else {
          Relation newRelation = new Relation(r);
          newRelation.addMember(new RelationMember("", way));
          Main.main.undoRedo.add(new ChangeCommand(r, newRelation));
        }
      }
    }
  }
Пример #4
0
 private SearchContext(String state) throws ParseError {
   m = SearchCompiler.compile(state);
   n = SearchCompiler.compile('-' + state);
   ds.addPrimitive(n1);
   ds.addPrimitive(n2);
   w1.addNode(n1);
   w1.addNode(n2);
   w2.addNode(n1);
   w2.addNode(n2);
   ds.addPrimitive(w1);
   ds.addPrimitive(w2);
   r1.addMember(new RelationMember("", w1));
   r1.addMember(new RelationMember("", w2));
   r2.addMember(new RelationMember("", w1));
   r2.addMember(new RelationMember("", w2));
   ds.addPrimitive(r1);
   ds.addPrimitive(r2);
 }
Пример #5
0
 private static final boolean addStopToRoute(Relation route, OsmPrimitive stop) {
   if (route.getMembersCount() == 0
       || !route.getMember(route.getMembersCount() - 1).getMember().equals(stop)) {
     route.addMember(new RelationMember(stop.get(OSM_PUBLIC_TRANSPORT), stop));
     return true;
   } else {
     return false;
   }
 }
Пример #6
0
  public void addTurn(List<Road> via, Road.End to) {
    assert equals(to.getJunction());

    Relation existing = null;
    for (Turn t : to.getTurns()) {
      if (t.getFrom().getOutgoingRoadEnd().equals(getOutgoingRoadEnd()) && t.getVia().equals(via)) {
        if (t.getFrom().equals(this)) {
          // was already added
          return;
        }

        existing = t.getRelation();
      }
    }

    final Relation r;
    if (existing == null) {
      r = new Relation();
      r.put("type", Constants.TYPE_TURNS);

      r.addMember(new RelationMember(Constants.TURN_ROLE_FROM, getOutgoingRoadEnd().getWay()));
      if (via.isEmpty()) {
        r.addMember(new RelationMember(Constants.TURN_ROLE_VIA, getOutgoingJunction().getNode()));
      } else {
        for (Way w :
            Utils.flattenVia(getOutgoingJunction().getNode(), via, to.getJunction().getNode())) {
          r.addMember(new RelationMember(Constants.TURN_ROLE_VIA, w));
        }
      }
      r.addMember(new RelationMember(Constants.TURN_ROLE_TO, to.getWay()));

      getOutgoingJunction().getNode().getDataSet().addPrimitive(r);
    } else {
      r = existing;
    }

    final String key = isExtra() ? Constants.TURN_KEY_EXTRA_LANES : Constants.TURN_KEY_LANES;
    final List<Integer> lanes = Turn.indices(r, key);
    lanes.add(getIndex());
    r.put(key, Turn.join(lanes));
  }
 protected static void populateTestDataSetWithRelations(DataSet ds) {
   for (int i = 0; i < 10; i++) {
     Relation r = new Relation();
     r.put("name", "relation-" + i);
     for (int j = 0; j < 10; j++) {
       RelationMember member = new RelationMember("node-" + j, lookupNode(ds, i + j));
       r.addMember(member);
     }
     for (int j = 0; j < 5; j++) {
       RelationMember member = new RelationMember("way-" + j, lookupWay(ds, i + j));
       r.addMember(member);
     }
     if (i > 5) {
       for (int j = 0; j < 3; j++) {
         RelationMember member = new RelationMember("relation-" + j, lookupRelation(ds, j));
         logger.info(MessageFormat.format("adding relation {0} to relation {1}", j, i));
         r.addMember(member);
       }
     }
     ds.addPrimitive(r);
   }
 }
Пример #8
0
  /**
   * Will add own multipolygon relation to the "previously existing" relations. Fixup is done by
   * fixRelations
   *
   * @param inner List of already closed inner ways
   * @param outer The outer way
   * @return The list of relation with roles to add own relation to
   */
  private RelationRole addOwnMultigonRelation(Collection<Way> inner, Way outer) {
    if (inner.isEmpty()) return null;
    // Create new multipolygon relation and add all inner ways to it
    Relation newRel = new Relation();
    newRel.put("type", "multipolygon");
    for (Way w : inner) {
      newRel.addMember(new RelationMember("inner", w));
    }
    cmds.add(new AddCommand(newRel));

    // We don't add outer to the relation because it will be handed to fixRelations()
    // which will then do the remaining work.
    return new RelationRole(newRel, "outer");
  }
Пример #9
0
  private DataSet parse(InputStream in, ProgressMonitor instance) throws JAXBException {
    root = unmarshal(ChouettePTNetworkType.class, in);

    Relation network = createNetwork(root.getPTNetwork());

    // Parsing Stop areas
    for (StopArea sa : root.getChouetteArea().getStopArea()) {
      if (sa.getStopAreaExtension().getAreaType().equals(ChouetteAreaType.COMMERCIAL_STOP_POINT)) {
        Relation stopArea = createStopArea(sa);
        stopArea.put("name", sa.getName());
        for (String childId : sa.getContains()) {
          if (childId.contains("StopArea")) {
            StopArea child = findStopArea(childId);
            if (child == null) {
              Main.warn("Cannot find StopArea: " + childId);
            } else {
              if (child
                  .getStopAreaExtension()
                  .getAreaType()
                  .equals(ChouetteAreaType.BOARDING_POSITION)) {
                for (String grandchildId : child.getContains()) {
                  if (grandchildId.contains("StopPoint")) {
                    StopPoint grandchild = findStopPoint(grandchildId);
                    if (grandchild == null) {
                      Main.warn("Cannot find StopPoint: " + grandchildId);
                    } else {
                      if (grandchild.getLongLatType().equals(LongLatTypeType.WGS_84)) {
                        Node platform = createPlatform(grandchild);
                        stopArea.addMember(new RelationMember(OSM_PLATFORM, platform));
                      } else {
                        Main.warn("Unsupported long/lat type: " + grandchild.getLongLatType());
                      }
                    }
                  } else {
                    Main.warn("Unsupported grandchild: " + grandchildId);
                  }
                }
                String centroidId = child.getCentroidOfArea();
                AreaCentroid areaCentroid = findAreaCentroid(centroidId);
                if (areaCentroid == null) {
                  Main.warn("Cannot find AreaCentroid: " + centroidId);
                } else if (!areaCentroid.getLongLatType().equals(LongLatTypeType.WGS_84)) {
                  Main.warn("Unsupported long/lat type: " + areaCentroid.getLongLatType());
                } else {
                  for (RelationMember member : stopArea.getMembers()) {
                    // Fix stop coordinates if needed
                    if (member.getRole().equals(OSM_PLATFORM)
                        && isNullLatLon(member.getNode().getCoor())) {
                      member.getNode().setCoor(createLatLon(areaCentroid));
                    }
                  }
                }
              } else {
                Main.warn("Unsupported child type: " + child.getStopAreaExtension().getAreaType());
              }
            }

          } else if (childId.contains("StopPoint")) {
            StopPoint child = findStopPoint(childId);
            if (child == null) {
              Main.warn("Cannot find StopPoint: " + childId);
            } else {
              // TODO
              Main.info("TODO: handle StopPoint " + childId);
            }

          } else {
            Main.warn("Unsupported child: " + childId);
          }
        }
      } else if (sa.getStopAreaExtension()
          .getAreaType()
          .equals(ChouetteAreaType.BOARDING_POSITION)) {
        // Main.info("skipping StopArea with type "+sa.getStopAreaExtension().getAreaType()+":
        // "+sa.getObjectId());
      } else {
        Main.warn("Unsupported StopArea type: " + sa.getStopAreaExtension().getAreaType());
      }
    }

    Relation routeMaster = createRouteMaster(root.getChouetteLineDescription().getLine());
    network.addMember(new RelationMember(null, routeMaster));

    for (ChouetteRoute cr : root.getChouetteLineDescription().getChouetteRoute()) {
      Relation route = createRoute(cr);
      routeMaster.addMember(new RelationMember(null, route));
      for (String id : cr.getPtLinkId()) {
        PTLinkType ptlink = findPtLink(id);
        if (ptlink == null) {
          Main.warn("Cannot find PTLinkType: " + id);
        } else {
          /*StopPoint start = findStopPoint(ptlink.getStartOfLink());
          StopPoint end = findStopPoint(ptlink.getEndOfLink());*/
          OsmPrimitive start = tridentObjects.get(ptlink.getStartOfLink());
          OsmPrimitive end = tridentObjects.get(ptlink.getEndOfLink());
          if (start == null) {
            Main.warn("Cannot find start StopPoint: " + ptlink.getStartOfLink());
          } else if (start.get(OSM_PUBLIC_TRANSPORT).equals(OSM_STOP)
              || start.get(OSM_PUBLIC_TRANSPORT).equals(OSM_PLATFORM)) {
            addStopToRoute(route, start);
          }

          if (end == null) {
            Main.warn("Cannot find end StopPoint: " + ptlink.getEndOfLink());
          } else if (end.get(OSM_PUBLIC_TRANSPORT).equals(OSM_STOP)
              || end.get(OSM_PUBLIC_TRANSPORT).equals(OSM_PLATFORM)) {
            addStopToRoute(route, end);
          }
        }
      }
    }

    return ds;
  }
  /**
   * builds a large data set to be used later for testing MULTI FETCH on the server
   *
   * @return a large data set
   */
  protected static DataSet buildTestDataSet() {
    DataSet ds = new DataSet();
    ds.setVersion("0.6");

    int numNodes = 1000;
    int numWays = 1000;
    int numRelations = 1000;

    ArrayList<Node> nodes = new ArrayList<>();
    ArrayList<Way> ways = new ArrayList<>();

    // create a set of nodes
    //
    for (int i = 0; i < numNodes; i++) {
      Node n = new Node();
      n.setCoor(new LatLon(-36.6, 47.6));
      n.put("name", "node-" + i);
      ds.addPrimitive(n);
      nodes.add(n);
    }

    // create a set of ways, each with a random number of nodes
    //
    for (int i = 0; i < numWays; i++) {
      Way w = new Way();
      int numNodesInWay = 2 + (int) Math.round(Math.random() * 5);
      int start = (int) Math.round(Math.random() * numNodes);
      for (int j = 0; j < numNodesInWay; j++) {
        int idx = (start + j) % numNodes;
        Node n = nodes.get(idx);
        w.addNode(n);
      }
      w.put("name", "way-" + i);
      ds.addPrimitive(w);
      ways.add(w);
    }

    // create a set of relations each with a random number of nodes, and ways
    //
    for (int i = 0; i < numRelations; i++) {
      Relation r = new Relation();
      r.put("name", "relation-" + i);
      int numNodesInRelation = (int) Math.round(Math.random() * 10);
      int start = (int) Math.round(Math.random() * numNodes);
      for (int j = 0; j < numNodesInRelation; j++) {
        int idx = (start + j) % 500;
        Node n = nodes.get(idx);
        r.addMember(new RelationMember("role-" + j, n));
      }
      int numWaysInRelation = (int) Math.round(Math.random() * 10);
      start = (int) Math.round(Math.random() * numWays);
      for (int j = 0; j < numWaysInRelation; j++) {
        int idx = (start + j) % 500;
        Way w = ways.get(idx);
        r.addMember(new RelationMember("role-" + j, w));
      }
      ds.addPrimitive(r);
    }

    return ds;
  }
Пример #11
0
  static SplitWayResult doSplitWay(
      OsmDataLayer layer,
      Way way,
      Way wayToKeep,
      List<Way> newWays,
      List<OsmPrimitive> newSelection) {

    Collection<Command> commandList = new ArrayList<>(newWays.size());
    Collection<String> nowarnroles =
        Main.pref.getCollection(
            "way.split.roles.nowarn",
            Arrays.asList(
                "outer", "inner", "forward", "backward", "north", "south", "east", "west"));

    // Change the original way
    final Way changedWay = new Way(way);
    changedWay.setNodes(wayToKeep.getNodes());
    commandList.add(new ChangeCommand(way, changedWay));
    if (!newSelection.contains(way)) {
      newSelection.add(way);
    }
    newWays.remove(wayToKeep);

    for (Way wayToAdd : newWays) {
      commandList.add(new AddCommand(layer, wayToAdd));
      newSelection.add(wayToAdd);
    }

    boolean warnmerole = false;
    boolean warnme = false;
    // now copy all relations to new way also

    for (Relation r : OsmPrimitive.getFilteredList(way.getReferrers(), Relation.class)) {
      if (!r.isUsable()) {
        continue;
      }
      Relation c = null;
      String type = r.get("type");
      if (type == null) {
        type = "";
      }

      int i_c = 0, i_r = 0;
      List<RelationMember> relationMembers = r.getMembers();
      for (RelationMember rm : relationMembers) {
        if (rm.isWay() && rm.getMember() == way) {
          boolean insert = true;
          if ("restriction".equals(type)) {
            /* this code assumes the restriction is correct. No real error checking done */
            String role = rm.getRole();
            if ("from".equals(role) || "to".equals(role)) {
              OsmPrimitive via = null;
              for (RelationMember rmv : r.getMembers()) {
                if ("via".equals(rmv.getRole())) {
                  via = rmv.getMember();
                }
              }
              List<Node> nodes = new ArrayList<>();
              if (via != null) {
                if (via instanceof Node) {
                  nodes.add((Node) via);
                } else if (via instanceof Way) {
                  nodes.add(((Way) via).lastNode());
                  nodes.add(((Way) via).firstNode());
                }
              }
              Way res = null;
              for (Node n : nodes) {
                if (changedWay.isFirstLastNode(n)) {
                  res = way;
                }
              }
              if (res == null) {
                for (Way wayToAdd : newWays) {
                  for (Node n : nodes) {
                    if (wayToAdd.isFirstLastNode(n)) {
                      res = wayToAdd;
                    }
                  }
                }
                if (res != null) {
                  if (c == null) {
                    c = new Relation(r);
                  }
                  c.addMember(new RelationMember(role, res));
                  c.removeMembersFor(way);
                  insert = false;
                }
              } else {
                insert = false;
              }
            } else if (!"via".equals(role)) {
              warnme = true;
            }
          } else if (!("route".equals(type)) && !("multipolygon".equals(type))) {
            warnme = true;
          }
          if (c == null) {
            c = new Relation(r);
          }

          if (insert) {
            if (rm.hasRole() && !nowarnroles.contains(rm.getRole())) {
              warnmerole = true;
            }

            Boolean backwards = null;
            int k = 1;
            while (i_r - k >= 0 || i_r + k < relationMembers.size()) {
              if ((i_r - k >= 0) && relationMembers.get(i_r - k).isWay()) {
                Way w = relationMembers.get(i_r - k).getWay();
                if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
                  backwards = Boolean.FALSE;
                } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
                  backwards = Boolean.TRUE;
                }
                break;
              }
              if ((i_r + k < relationMembers.size()) && relationMembers.get(i_r + k).isWay()) {
                Way w = relationMembers.get(i_r + k).getWay();
                if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
                  backwards = Boolean.TRUE;
                } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
                  backwards = Boolean.FALSE;
                }
                break;
              }
              k++;
            }

            int j = i_c;
            for (Way wayToAdd : newWays) {
              RelationMember em = new RelationMember(rm.getRole(), wayToAdd);
              j++;
              if ((backwards != null) && backwards) {
                c.addMember(i_c, em);
              } else {
                c.addMember(j, em);
              }
            }
            i_c = j;
          }
        }
        i_c++;
        i_r++;
      }

      if (c != null) {
        commandList.add(new ChangeCommand(layer, r, c));
      }
    }
    if (warnmerole) {
      new Notification(
              tr(
                  "A role based relation membership was copied to all new ways.<br>You should verify this and correct it when necessary."))
          .setIcon(JOptionPane.WARNING_MESSAGE)
          .show();
    } else if (warnme) {
      new Notification(
              tr(
                  "A relation membership was copied to all new ways.<br>You should verify this and correct it when necessary."))
          .setIcon(JOptionPane.WARNING_MESSAGE)
          .show();
    }

    return new SplitWayResult(
        new SequenceCommand(
            /* for correct i18n of plural forms - see #9110 */
            trn(
                "Split way {0} into {1} part",
                "Split way {0} into {1} parts",
                newWays.size(),
                way.getDisplayName(DefaultNameFormatter.getInstance()),
                newWays.size()),
            commandList),
        newSelection,
        way,
        newWays);
  }