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; }
private OsmDataLayer makeLayer( String name, FilePlacement placement, OsmBuilder.Mode mode, ProgressMonitor monitor) { monitor.beginTask(tr("Building JOSM layer"), 100); OsmBuilder builder = new OsmBuilder(placement); DataSet data = builder.build(this.data.getLayers(), mode, monitor.createSubTaskMonitor(50, false)); monitor.setTicks(50); monitor.setCustomText(tr("Postprocessing layer")); OsmDataLayer result = new OsmDataLayer(data, name, null); result.onPostLoadFromFile(); monitor.finishTask(); return result; }
/** * Read the data from the osm server address. * * @return A data set containing all data retrieved from that url */ @Override public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { progressMonitor.beginTask(tr("Contacting OSM Server..."), 10); InputStream in = null; try { DataSet ds = null; progressMonitor.indeterminateSubTask(null); if (crosses180th) { // API 0.6 does not support requests crossing the 180th meridian, so make two requests in = getInputStream( getRequestForBbox(lon1, lat1, 180.0, lat2), progressMonitor.createSubTaskMonitor(9, false)); if (in == null) return null; ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false)); in = getInputStream( getRequestForBbox(-180.0, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false)); if (in == null) return null; DataSet ds2 = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false)); if (ds2 == null) return null; ds.mergeFrom(ds2); } else { // Simple request in = getInputStream( getRequestForBbox(lon1, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false)); if (in == null) return null; ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false)); } return ds; } catch (OsmTransferException e) { throw e; } catch (Exception e) { throw new OsmTransferException(e); } finally { progressMonitor.finishTask(); if (in != null) { try { in.close(); } catch (IOException e) { } } activeConnection = null; } }
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; }
private void saveLayer(java.io.File file, FilePlacement placement, ProgressMonitor monitor) { monitor.beginTask(tr("Saving to file."), 1000); OsmBuilder builder = new OsmBuilder(placement); DataSet data = builder.build( this.data.getLayers(), OsmBuilder.Mode.Final, monitor.createSubTaskMonitor(500, false)); OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file); monitor.setCustomText(tr(" Writing to file")); monitor.setTicks(500); OsmExporter exporter = new OsmExporter(); try { exporter.exportData(file, layer); } catch (IOException e) { // TODO: } monitor.finishTask(); }
@Override public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException, MoreNotesException { progressMonitor.beginTask("Downloading notes"); CheckParameterUtil.ensureThat(noteLimit > 0, "Requested note limit is less than 1."); // see result_limit in // https://github.com/openstreetmap/openstreetmap-website/blob/master/app/controllers/notes_controller.rb CheckParameterUtil.ensureThat( noteLimit <= 10000, "Requested note limit is over API hard limit of 10000."); CheckParameterUtil.ensureThat(daysClosed >= -1, "Requested note limit is less than -1."); String url = "notes?limit=" + noteLimit + "&closed=" + daysClosed + "&bbox=" + lon1 + ',' + lat1 + ',' + lon2 + ',' + lat2; try { InputStream is = getInputStream(url, progressMonitor.createSubTaskMonitor(1, false)); NoteReader reader = new NoteReader(is); final List<Note> notes = reader.parse(); if (notes.size() == noteLimit) { throw new MoreNotesException(notes, noteLimit); } return notes; } catch (IOException e) { throw new OsmTransferException(e); } catch (SAXException e) { throw new OsmTransferException(e); } finally { progressMonitor.finishTask(); } }
private PathOptimizer loadPDF(File fileName, ProgressMonitor monitor) { monitor.beginTask("", 100); monitor.setTicks(0); monitor.setCustomText(tr("Preparing")); double nodesTolerance = 0.0; Color color = null; int maxPaths = Integer.MAX_VALUE; if (this.mergeCloseNodesCheck.isSelected()) { try { nodesTolerance = Double.parseDouble(this.mergeCloseNodesTolerance.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(Main.parent, tr("Tolerance is not a number")); return null; } } if (this.colorFilterCheck.isSelected()) { try { String colString = this.colorFilterColor.getText().replace("#", ""); color = new Color(Integer.parseInt(colString, 16)); } catch (Exception e) { JOptionPane.showMessageDialog(Main.parent, tr("Could not parse color")); return null; } } if (this.limitPathCountCheck.isSelected()) { try { maxPaths = Integer.parseInt(this.limitPathCount.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(Main.parent, tr("Could not parse max path count")); return null; } } monitor.setTicks(10); monitor.setCustomText(tr("Parsing file")); PathOptimizer data = new PathOptimizer(nodesTolerance, color, this.splitOnColorChangeCheck.isSelected()); try { PdfBoxParser parser = new PdfBoxParser(data); parser.parse(fileName, maxPaths, monitor.createSubTaskMonitor(80, false)); } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(Main.parent, tr("File not found.")); return null; } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing: {0}", e.getMessage())); return null; } monitor.setTicks(80); if (this.removeParallelSegmentsCheck.isSelected()) { try { double tolerance = Double.parseDouble(this.removeParallelSegmentsTolerance.getText()); monitor.setCustomText(tr("Removing parallel segments")); data.removeParallelLines(tolerance); } catch (Exception e) { JOptionPane.showMessageDialog(Main.parent, tr("Max distance is not a number")); return null; } } if (nodesTolerance > 0.0) { monitor.setTicks(83); monitor.setCustomText(tr("Joining nodes")); data.mergeNodes(); } monitor.setTicks(85); monitor.setCustomText(tr("Joining adjacent segments")); data.mergeSegments(); if (this.removeSmallObjectsCheck.isSelected()) { try { double tolerance = Double.parseDouble(this.removeSmallObjectsSize.getText()); monitor.setTicks(90); monitor.setCustomText(tr("Removing small objects")); data.removeSmallObjects(tolerance); } catch (Exception e) { JOptionPane.showMessageDialog(Main.parent, tr("Tolerance is not a number")); return null; } } if (this.removeLargeObjectsCheck.isSelected()) { try { double tolerance = Double.parseDouble(this.removeLargeObjectsSize.getText()); monitor.setTicks(90); monitor.setCustomText(tr("Removing large objects")); data.removeLargeObjects(tolerance); } catch (Exception e) { JOptionPane.showMessageDialog(Main.parent, tr("Tolerance is not a number")); return null; } } monitor.setTicks(95); monitor.setCustomText(tr("Finalizing layers")); data.splitLayersByPathKind( this.splitOnShapeClosedCheck.isSelected(), this.splitOnSingleSegmentCheck.isSelected(), this.splitOnOrthogonalCheck.isSelected()); data.finish(); monitor.finishTask(); return data; }
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); } }