/**
   * Parse the given input source and return the dataset.
   *
   * @param source the source input stream. Must not be null.
   * @param progressMonitor the progress monitor. If null, {@see NullProgressMonitor#INSTANCE} is
   *     assumed
   * @return the dataset with the parsed data
   * @throws IllegalDataException thrown if the an error was found while parsing the data from the
   *     source
   * @throws IllegalArgumentException thrown if source is null
   */
  public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor)
      throws IllegalDataException {
    ProgressMonitor monitor =
        progressMonitor == null ? NullProgressMonitor.INSTANCE : progressMonitor;
    CheckParameterUtil.ensureParameterNotNull(source, "source");

    PbfReader reader = new PbfReader();

    try {
      monitor.beginTask(tr("Prepare OSM data...", 2));
      monitor.indeterminateSubTask(tr("Reading OSM data..."));

      reader.parse(source);
      monitor.worked(1);

      monitor.indeterminateSubTask(tr("Preparing data set..."));
      reader.prepareDataSet();
      monitor.worked(1);
      return reader.getDataSet();
    } catch (IllegalDataException e) {
      throw e;
    } catch (Exception e) {
      throw new IllegalDataException(e);
    } finally {
      monitor.finishTask();
    }
  }
Exemple #2
0
  protected DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor)
      throws IllegalDataException {
    if (progressMonitor == null) {
      progressMonitor = NullProgressMonitor.INSTANCE;
    }
    CheckParameterUtil.ensureParameterNotNull(source, "source");
    try {
      progressMonitor.beginTask(tr("Prepare OSM data...", 2));
      progressMonitor.indeterminateSubTask(tr("Parsing OSM data..."));

      InputStreamReader ir = UTFInputStreamReader.create(source, "UTF-8");
      XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(ir);
      setParser(parser);
      parse();
      progressMonitor.worked(1);

      progressMonitor.indeterminateSubTask(tr("Preparing data set..."));
      prepareDataSet();
      progressMonitor.worked(1);

      // iterate over registered postprocessors and give them each a chance
      // to modify the dataset we have just loaded.
      if (postprocessors != null) {
        for (OsmServerReadPostprocessor pp : postprocessors) {
          pp.postprocessDataSet(getDataSet(), progressMonitor);
        }
      }
      return getDataSet();
    } catch (IllegalDataException e) {
      throw e;
    } catch (OsmParsingException e) {
      throw new IllegalDataException(e.getMessage(), e);
    } catch (XMLStreamException e) {
      String msg = e.getMessage();
      Pattern p = Pattern.compile("Message: (.+)");
      Matcher m = p.matcher(msg);
      if (m.find()) {
        msg = m.group(1);
      }
      if (e.getLocation() != null)
        throw new IllegalDataException(
            tr(
                    "Line {0} column {1}: ",
                    e.getLocation().getLineNumber(), e.getLocation().getColumnNumber())
                + msg,
            e);
      else throw new IllegalDataException(msg, e);
    } catch (Exception e) {
      throw new IllegalDataException(e);
    } finally {
      progressMonitor.finishTask();
    }
  }
Exemple #3
0
 /**
  * Visits all primitives to be tested. These primitives are always visited in the same order:
  * nodes first, then ways.
  *
  * @param selection The primitives to be tested
  */
 public void visit(Collection<OsmPrimitive> selection) {
   progressMonitor.setTicksCount(selection.size());
   for (OsmPrimitive p : selection) {
     if (p.isUsable()) {
       p.visit(this);
     }
     progressMonitor.worked(1);
   }
 }
Exemple #4
0
    @Override
    protected void realRun() {
      try {
        foundMatches = 0;
        SearchCompiler.Match matcher = SearchCompiler.compile(setting);

        if (setting.mode == SearchMode.replace) {
          selection.clear();
        } else if (setting.mode == SearchMode.in_selection) {
          foundMatches = selection.size();
        }

        Collection<OsmPrimitive> all;
        if (setting.allElements) {
          all = ds.allPrimitives();
        } else {
          all = ds.getPrimitives(OsmPrimitive::isSelectable);
        }
        final ProgressMonitor subMonitor =
            getProgressMonitor().createSubTaskMonitor(all.size(), false);
        subMonitor.beginTask(
            trn("Searching in {0} object", "Searching in {0} objects", all.size(), all.size()));

        for (OsmPrimitive osm : all) {
          if (canceled) {
            return;
          }
          if (setting.mode == SearchMode.replace) {
            if (matcher.match(osm)) {
              selection.add(osm);
              ++foundMatches;
            }
          } else if (setting.mode == SearchMode.add && !predicate.test(osm) && matcher.match(osm)) {
            selection.add(osm);
            ++foundMatches;
          } else if (setting.mode == SearchMode.remove
              && predicate.test(osm)
              && matcher.match(osm)) {
            selection.remove(osm);
            ++foundMatches;
          } else if (setting.mode == SearchMode.in_selection
              && predicate.test(osm)
              && !matcher.match(osm)) {
            selection.remove(osm);
            --foundMatches;
          }
          subMonitor.worked(1);
        }
        subMonitor.finishTask();
      } catch (ParseError e) {
        Main.debug(e);
        JOptionPane.showMessageDialog(
            Main.parent, e.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE);
      }
    }
Exemple #5
0
  private void parseJos(Document doc, ProgressMonitor progressMonitor) throws IllegalDataException {
    Element root = doc.getDocumentElement();
    if (!equal(root.getTagName(), "josm-session")) {
      error(tr("Unexpected root element ''{0}'' in session file", root.getTagName()));
    }
    String version = root.getAttribute("version");
    if (!"0.1".equals(version)) {
      error(tr("Version ''{0}'' of session file is not supported. Expected: 0.1", version));
    }

    Element viewportEl = getElementByTagName(root, "viewport");
    if (viewportEl != null) {
      EastNorth center = null;
      Element centerEl = getElementByTagName(viewportEl, "center");
      if (centerEl != null && centerEl.hasAttribute("lat") && centerEl.hasAttribute("lon")) {
        try {
          LatLon centerLL =
              new LatLon(
                  Double.parseDouble(centerEl.getAttribute("lat")),
                  Double.parseDouble(centerEl.getAttribute("lon")));
          center = Projections.project(centerLL);
        } catch (NumberFormatException ex) {
        }
      }
      if (center != null) {
        Element scaleEl = getElementByTagName(viewportEl, "scale");
        if (scaleEl != null && scaleEl.hasAttribute("meter-per-pixel")) {
          try {
            double meterPerPixel = Double.parseDouble(scaleEl.getAttribute("meter-per-pixel"));
            Projection proj = Main.getProjection();
            // Get a "typical" distance in east/north units that
            // corresponds to a couple of pixels. Shouldn't be too
            // large, to keep it within projection bounds and
            // not too small to avoid rounding errors.
            double dist = 0.01 * proj.getDefaultZoomInPPD();
            LatLon ll1 = proj.eastNorth2latlon(new EastNorth(center.east() - dist, center.north()));
            LatLon ll2 = proj.eastNorth2latlon(new EastNorth(center.east() + dist, center.north()));
            double meterPerEasting = ll1.greatCircleDistance(ll2) / dist / 2;
            double scale = meterPerPixel / meterPerEasting; // unit: easting per pixel
            viewport = new ViewportData(center, scale);
          } catch (NumberFormatException ex) {
          }
        }
      }
    }

    Element layersEl = getElementByTagName(root, "layers");
    if (layersEl == null) return;

    String activeAtt = layersEl.getAttribute("active");
    try {
      active = (activeAtt != null && !activeAtt.isEmpty()) ? Integer.parseInt(activeAtt) - 1 : -1;
    } catch (NumberFormatException e) {
      Main.warn(
          "Unsupported value for 'active' layer attribute. Ignoring it. Error was: "
              + e.getMessage());
      active = -1;
    }

    MultiMap<Integer, Integer> deps = new MultiMap<Integer, Integer>();
    Map<Integer, Element> elems = new HashMap<Integer, Element>();

    NodeList nodes = layersEl.getChildNodes();

    for (int i = 0; i < nodes.getLength(); ++i) {
      Node node = nodes.item(i);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) node;
        if (equal(e.getTagName(), "layer")) {

          if (!e.hasAttribute("index")) {
            error(tr("missing mandatory attribute ''index'' for element ''layer''"));
          }
          Integer idx = null;
          try {
            idx = Integer.parseInt(e.getAttribute("index"));
          } catch (NumberFormatException ex) {
          }
          if (idx == null) {
            error(tr("unexpected format of attribute ''index'' for element ''layer''"));
          }
          if (elems.containsKey(idx)) {
            error(
                tr(
                    "attribute ''index'' ({0}) for element ''layer'' must be unique",
                    Integer.toString(idx)));
          }
          elems.put(idx, e);

          deps.putVoid(idx);
          String depStr = e.getAttribute("depends");
          if (depStr != null) {
            for (String sd : depStr.split(",")) {
              Integer d = null;
              try {
                d = Integer.parseInt(sd);
              } catch (NumberFormatException ex) {
                Main.warn(ex);
              }
              if (d != null) {
                deps.put(idx, d);
              }
            }
          }
        }
      }
    }

    List<Integer> sorted = Utils.topologicalSort(deps);
    final Map<Integer, Layer> layersMap = new TreeMap<Integer, Layer>(Collections.reverseOrder());
    final Map<Integer, SessionLayerImporter> importers =
        new HashMap<Integer, SessionLayerImporter>();
    final Map<Integer, String> names = new HashMap<Integer, String>();

    progressMonitor.setTicksCount(sorted.size());
    LAYER:
    for (int idx : sorted) {
      Element e = elems.get(idx);
      if (e == null) {
        error(tr("missing layer with index {0}", idx));
      }
      if (!e.hasAttribute("name")) {
        error(tr("missing mandatory attribute ''name'' for element ''layer''"));
      }
      String name = e.getAttribute("name");
      names.put(idx, name);
      if (!e.hasAttribute("type")) {
        error(tr("missing mandatory attribute ''type'' for element ''layer''"));
      }
      String type = e.getAttribute("type");
      SessionLayerImporter imp = getSessionLayerImporter(type);
      if (imp == null) {
        CancelOrContinueDialog dialog = new CancelOrContinueDialog();
        dialog.show(
            tr("Unable to load layer"),
            tr("Cannot load layer of type ''{0}'' because no suitable importer was found.", type),
            JOptionPane.WARNING_MESSAGE,
            progressMonitor);
        if (dialog.isCancel()) {
          progressMonitor.cancel();
          return;
        } else {
          continue;
        }
      } else {
        importers.put(idx, imp);
        List<LayerDependency> depsImp = new ArrayList<LayerDependency>();
        for (int d : deps.get(idx)) {
          SessionLayerImporter dImp = importers.get(d);
          if (dImp == null) {
            CancelOrContinueDialog dialog = new CancelOrContinueDialog();
            dialog.show(
                tr("Unable to load layer"),
                tr(
                    "Cannot load layer {0} because it depends on layer {1} which has been skipped.",
                    idx, d),
                JOptionPane.WARNING_MESSAGE,
                progressMonitor);
            if (dialog.isCancel()) {
              progressMonitor.cancel();
              return;
            } else {
              continue LAYER;
            }
          }
          depsImp.add(new LayerDependency(d, layersMap.get(d), dImp));
        }
        ImportSupport support = new ImportSupport(name, idx, depsImp);
        Layer layer = null;
        Exception exception = null;
        try {
          layer = imp.load(e, support, progressMonitor.createSubTaskMonitor(1, false));
        } catch (IllegalDataException ex) {
          exception = ex;
        } catch (IOException ex) {
          exception = ex;
        }
        if (exception != null) {
          exception.printStackTrace();
          CancelOrContinueDialog dialog = new CancelOrContinueDialog();
          dialog.show(
              tr("Error loading layer"),
              tr(
                  "<html>Could not load layer {0} ''{1}''.<br>Error is:<br>{2}</html>",
                  idx, name, exception.getMessage()),
              JOptionPane.ERROR_MESSAGE,
              progressMonitor);
          if (dialog.isCancel()) {
            progressMonitor.cancel();
            return;
          } else {
            continue;
          }
        }

        if (layer == null) throw new RuntimeException();
        layersMap.put(idx, layer);
      }
      progressMonitor.worked(1);
    }

    layers = new ArrayList<Layer>();
    for (int idx : layersMap.keySet()) {
      Layer layer = layersMap.get(idx);
      if (layer == null) {
        continue;
      }
      Element el = elems.get(idx);
      if (el.hasAttribute("visible")) {
        layer.setVisible(Boolean.parseBoolean(el.getAttribute("visible")));
      }
      if (el.hasAttribute("opacity")) {
        try {
          double opacity = Double.parseDouble(el.getAttribute("opacity"));
          layer.setOpacity(opacity);
        } catch (NumberFormatException ex) {
          Main.warn(ex);
        }
      }
    }
    for (Entry<Integer, Layer> e : layersMap.entrySet()) {
      Layer l = e.getValue();
      if (l == null) {
        continue;
      }

      l.setName(names.get(e.getKey()));
      layers.add(l);
    }
  }