Пример #1
0
 private GpxData downloadRawGps(String url, ProgressMonitor progressMonitor)
     throws IOException, OsmTransferException, SAXException {
   boolean done = false;
   GpxData result = null;
   for (int i = 0; !done; ++i) {
     progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
     InputStream in = getInputStream(url + i, progressMonitor.createSubTaskMonitor(1, true));
     if (in == null) {
       break;
     }
     progressMonitor.setTicks(0);
     GpxReader reader = new GpxReader(in);
     gpxParsedProperly = reader.parse(false);
     GpxData currentGpx = reader.data;
     if (result == null) {
       result = currentGpx;
     } else if (currentGpx.hasTrackPoints()) {
       result.mergeFrom(currentGpx);
     } else {
       done = true;
     }
     in.close();
     activeConnection = null;
   }
   result.fromServer = true;
   return result;
 }
 @Override
 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
   progressMonitor.beginTask("", 1);
   try {
     progressMonitor.indeterminateSubTask(getTaskName());
     if (crosses180th) {
       // API 0.6 does not support requests crossing the 180th meridian, so make two requests
       GpxData result = downloadRawGps(new Bounds(lat1, lon1, lat2, 180.0), progressMonitor);
       result.mergeFrom(downloadRawGps(new Bounds(lat1, -180.0, lat2, lon2), progressMonitor));
       return result;
     } else {
       // Simple request
       return downloadRawGps(new Bounds(lat1, lon1, lat2, lon2), progressMonitor);
     }
   } catch (IllegalArgumentException e) {
     // caused by HttpUrlConnection in case of illegal stuff in the response
     if (cancel) return null;
     throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
   } catch (IOException e) {
     if (cancel) return null;
     throw new OsmTransferException(e);
   } catch (SAXException e) {
     throw new OsmTransferException(e);
   } catch (OsmTransferException e) {
     throw e;
   } catch (RuntimeException e) {
     if (cancel) return null;
     throw e;
   } finally {
     progressMonitor.finishTask();
   }
 }
Пример #3
0
  public static GpxData toGpxData(DataSet data, File file) {
    GpxData gpxData = new GpxData();
    gpxData.storageFile = file;
    HashSet<Node> doneNodes = new HashSet<Node>();
    for (Way w : data.ways) {
      if (w.incomplete || w.deleted) continue;
      ImmutableGpxTrack trk =
          new ImmutableGpxTrack(
              new LinkedList<Collection<WayPoint>>(), new HashMap<String, Object>());
      gpxData.tracks.add(trk);

      if (w.get("name") != null) trk.attr.put("name", w.get("name"));

      ArrayList<WayPoint> trkseg = null;
      for (Node n : w.nodes) {
        if (n.incomplete || n.deleted) {
          trkseg = null;
          continue;
        }
        if (trkseg == null) {
          trkseg = new ArrayList<WayPoint>();
          trk.trackSegs.add(trkseg);
        }
        if (!n.isTagged()) {
          doneNodes.add(n);
        }
        WayPoint wpt = new WayPoint(n.getCoor());
        if (!n.isTimestampEmpty()) {
          wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp()));
          wpt.setTime();
        }
        trkseg.add(wpt);
      }
    }

    // what is this loop meant to do? it creates waypoints but never
    // records them?
    for (Node n : data.nodes) {
      if (n.incomplete || n.deleted || doneNodes.contains(n)) continue;
      WayPoint wpt = new WayPoint(n.getCoor());
      if (!n.isTimestampEmpty()) {
        wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp()));
        wpt.setTime();
      }
      if (n.keys != null && n.keys.containsKey("name")) {
        wpt.attr.put("name", n.keys.get("name"));
      }
    }
    return gpxData;
  }
 private GpxData downloadRawGps(Bounds b, ProgressMonitor progressMonitor)
     throws IOException, OsmTransferException, SAXException {
   boolean done = false;
   GpxData result = null;
   String url =
       "trackpoints?bbox="
           + b.getMinLon()
           + ','
           + b.getMinLat()
           + ','
           + b.getMaxLon()
           + ','
           + b.getMaxLat()
           + "&page=";
   for (int i = 0; !done; ++i) {
     progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, (i + 1) * 5000));
     try (InputStream in =
         getInputStream(url + i, progressMonitor.createSubTaskMonitor(1, true))) {
       if (in == null) {
         break;
       }
       progressMonitor.setTicks(0);
       GpxReader reader = new GpxReader(in);
       gpxParsedProperly = reader.parse(false);
       GpxData currentGpx = reader.getGpxData();
       if (result == null) {
         result = currentGpx;
       } else if (currentGpx.hasTrackPoints()) {
         result.mergeFrom(currentGpx);
       } else {
         done = true;
       }
     }
     activeConnection = null;
   }
   if (result != null) {
     result.fromServer = true;
     result.dataSources.add(new DataSource(b, "OpenStreetMap server"));
   }
   return result;
 }
Пример #5
0
 /**
  * Replies the new GPX and marker layers corresponding to the specified GPX data.
  *
  * @param data The GPX data
  * @param parsedProperly True if GPX data has been properly parsed by {@link GpxReader#parse}
  * @param gpxLayerName The GPX layer name
  * @param markerLayerName The marker layer name
  * @return the new GPX and marker layers corresponding to the specified GPX data, to be used with
  *     {@link #addLayers}
  * @see #addLayers
  */
 public static GpxImporterData loadLayers(
     final GpxData data,
     final boolean parsedProperly,
     final String gpxLayerName,
     String markerLayerName) {
   GpxLayer gpxLayer = null;
   MarkerLayer markerLayer = null;
   if (data.hasRoutePoints() || data.hasTrackPoints()) {
     gpxLayer = new GpxLayer(data, gpxLayerName, data.storageFile != null);
   }
   if (Main.pref.getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) {
     markerLayer = new MarkerLayer(data, markerLayerName, data.storageFile, gpxLayer);
     if (markerLayer.data.isEmpty()) {
       markerLayer = null;
     }
   }
   Runnable postLayerTask =
       () -> {
         if (!parsedProperly) {
           String msg;
           if (data.storageFile == null) {
             msg =
                 tr(
                     "Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
                     gpxLayerName);
           } else {
             msg =
                 tr(
                     "Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
                     data.storageFile.getPath());
           }
           JOptionPane.showMessageDialog(null, msg);
         }
       };
   return new GpxImporterData(gpxLayer, markerLayer, postLayerTask);
 }
Пример #6
0
  private void writeMetaData() {
    Map<String, Object> attr = data.attr;
    openln("metadata");

    // write the description
    if (attr.containsKey(META_DESC)) {
      simpleTag("desc", data.getString(META_DESC));
    }

    // write the author details
    if (attr.containsKey(META_AUTHOR_NAME) || attr.containsKey(META_AUTHOR_EMAIL)) {
      openln("author");
      // write the name
      simpleTag("name", data.getString(META_AUTHOR_NAME));
      // write the email address
      if (attr.containsKey(META_AUTHOR_EMAIL)) {
        String[] tmp = data.getString(META_AUTHOR_EMAIL).split("@");
        if (tmp.length == 2) {
          inline("email", "id=\"" + tmp[0] + "\" domain=\"" + tmp[1] + '\"');
        }
      }
      // write the author link
      gpxLink((GpxLink) data.get(META_AUTHOR_LINK));
      closeln("author");
    }

    // write the copyright details
    if (attr.containsKey(META_COPYRIGHT_LICENSE) || attr.containsKey(META_COPYRIGHT_YEAR)) {
      openAtt("copyright", "author=\"" + data.get(META_COPYRIGHT_AUTHOR) + '\"');
      if (attr.containsKey(META_COPYRIGHT_YEAR)) {
        simpleTag("year", (String) data.get(META_COPYRIGHT_YEAR));
      }
      if (attr.containsKey(META_COPYRIGHT_LICENSE)) {
        simpleTag("license", encode((String) data.get(META_COPYRIGHT_LICENSE)));
      }
      closeln("copyright");
    }

    // write links
    if (attr.containsKey(META_LINKS)) {
      for (GpxLink link : data.<GpxLink>getCollection(META_LINKS)) {
        gpxLink(link);
      }
    }

    // write keywords
    if (attr.containsKey(META_KEYWORDS)) {
      simpleTag("keywords", data.getString(META_KEYWORDS));
    }

    Bounds bounds = data.recalculateBounds();
    if (bounds != null) {
      String b =
          "minlat=\""
              + bounds.getMinLat()
              + "\" minlon=\""
              + bounds.getMinLon()
              + "\" maxlat=\""
              + bounds.getMaxLat()
              + "\" maxlon=\""
              + bounds.getMaxLon()
              + '\"';
      inline("bounds", b);
    }

    if (data.fromServer) {
      openln("extensions");
      simpleTag("josm:from-server", "true");
      closeln("extensions");
    }

    closeln("metadata");
  }
Пример #7
0
    @Override
    public void actionPerformed(ActionEvent arg0) {
      FileFilter filter =
          new FileFilter() {
            @Override
            public boolean accept(File f) {
              return f.isDirectory() || Utils.hasExtension(f, "gpx", "gpx.gz");
            }

            @Override
            public String getDescription() {
              return tr("GPX Files (*.gpx *.gpx.gz)");
            }
          };
      AbstractFileChooser fc =
          DiskAccessAction.createAndOpenFileChooser(
              true, false, null, filter, JFileChooser.FILES_ONLY, null);
      if (fc == null) return;
      File sel = fc.getSelectedFile();

      try {
        outerPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        for (int i = gpxLst.size() - 1; i >= 0; i--) {
          GpxDataWrapper wrapper = gpxLst.get(i);
          if (wrapper.file != null && sel.equals(wrapper.file)) {
            cbGpx.setSelectedIndex(i);
            if (!sel.getName().equals(wrapper.name)) {
              JOptionPane.showMessageDialog(
                  Main.parent,
                  tr("File {0} is loaded yet under the name \"{1}\"", sel.getName(), wrapper.name),
                  tr("Error"),
                  JOptionPane.ERROR_MESSAGE);
            }
            return;
          }
        }
        GpxData data = null;
        try (InputStream iStream = createInputStream(sel)) {
          GpxReader reader = new GpxReader(iStream);
          reader.parse(false);
          data = reader.getGpxData();
          data.storageFile = sel;

        } catch (SAXException x) {
          Main.error(x);
          JOptionPane.showMessageDialog(
              Main.parent,
              tr("Error while parsing {0}", sel.getName()) + ": " + x.getMessage(),
              tr("Error"),
              JOptionPane.ERROR_MESSAGE);
          return;
        } catch (IOException x) {
          Main.error(x);
          JOptionPane.showMessageDialog(
              Main.parent,
              tr("Could not read \"{0}\"", sel.getName()) + '\n' + x.getMessage(),
              tr("Error"),
              JOptionPane.ERROR_MESSAGE);
          return;
        }

        loadedGpxData.add(data);
        if (gpxLst.get(0).file == null) {
          gpxLst.remove(0);
        }
        gpxLst.add(new GpxDataWrapper(sel.getName(), data, sel));
        cbGpx.setSelectedIndex(cbGpx.getItemCount() - 1);
      } finally {
        outerPanel.setCursor(Cursor.getDefaultCursor());
      }
    }