Changeset 143 in josm
- Timestamp:
- 2006-09-30T19:05:43+02:00 (18 years ago)
- Files:
-
- 10 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/actions/ExternalToolsAction.java
r142 r143 7 7 import java.io.FileReader; 8 8 import java.io.IOException; 9 import java.io. OutputStream;9 import java.io.PrintWriter; 10 10 import java.util.Arrays; 11 11 import java.util.Collection; … … 33 33 import org.openstreetmap.josm.data.osm.Way; 34 34 import org.openstreetmap.josm.data.osm.visitor.AddVisitor; 35 import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor; 35 36 import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor; 36 37 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 38 import org.openstreetmap.josm.gui.layer.Layer; 39 import org.openstreetmap.josm.gui.layer.RawGpsLayer; 40 import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint; 41 import org.openstreetmap.josm.io.GpxWriter; 37 42 import org.openstreetmap.josm.io.OsmReader; 38 43 import org.openstreetmap.josm.io.OsmWriter; … … 55 60 56 61 private final class ExecuteToolRunner extends PleaseWaitRunnable { 57 private final Process p; 58 private DataSet dataSet; 59 private DataSet fromDataSet; 60 private ExecuteToolRunner(String msg, Process p) { 61 super(msg); 62 this.p = p; 63 currentAction.setText(tr("Executing {0}",getValue(NAME))); 64 } 65 66 @Override protected void realRun() throws SAXException, IOException { 67 if (!input.isEmpty()) { 68 fromDataSet = new DataSet(); 69 AddVisitor adder = new AddVisitor(fromDataSet, flags.contains("include_references")); 70 if (input.contains("selection")) { 71 Collection<OsmPrimitive> sel = Main.ds.getSelected(); 72 for (OsmPrimitive osm : sel) 73 osm.visit(adder); 74 if (flags.contains("include_back_references")) { 75 CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds); 76 for (OsmPrimitive osm : sel) 77 osm.visit(v); 78 AddVisitor a = new AddVisitor(fromDataSet); 79 for (OsmPrimitive osm : v.data) 80 osm.visit(a); 81 } 82 } 83 if (input.contains("all")) 84 for (OsmPrimitive osm : Main.ds.allPrimitives()) 85 osm.visit(adder); 86 if (input.contains("screen")) { 87 if (Main.map == null) { 88 errorMessage = tr("The Tool requires some data to be loaded."); 89 cancel(); 90 return; 91 } 92 LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight()); 93 LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0); 94 Bounds b = new Bounds(bottomLeft, topRight); 95 Collection<Node> nodes = new HashSet<Node>(); 96 for (Node n : Main.ds.nodes) { 97 if (n.coor.isWithin(b)) { 98 n.visit(adder); 99 nodes.add(n); 100 } 101 } 102 Collection<Segment> segments = new HashSet<Segment>(); 103 for (Segment s : Main.ds.segments) { 104 if (nodes.contains(s.from) || nodes.contains(s.to)) { 105 s.visit(adder); 106 segments.add(s); 107 } 108 } 109 for (Way w : Main.ds.ways) { 110 for (Segment s : w.segments) { 111 if (segments.contains(s)) { 112 w.visit(adder); 113 break; 114 } 115 } 116 } 117 } 118 OutputStream out = p.getOutputStream(); 119 OsmWriter.output(out, fromDataSet, false); 120 out.close(); 121 } 122 if (output != null) 123 dataSet = OsmReader.parseDataSet(p.getInputStream(), currentAction, progress); 124 } 125 126 @Override protected void cancel() { 127 p.destroy(); 128 } 129 130 @Override protected void finish() { 131 if (dataSet == null || output == null || output.equals("discard")) 132 return; // user cancelled or no stdout to process 133 Collection<OsmPrimitive> allNew = dataSet.allPrimitives(); 134 Collection<OsmPrimitive> allOld = fromDataSet.allPrimitives(); 135 if (output.equals("replace")) { 136 Command cmd = createCommand(allOld, allNew); 137 if (cmd != null) { 138 Main.main.editLayer().add(cmd); 139 Main.ds.clearSelection(); 140 } 141 } else if (output.equals("selection")) { 142 Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>(); 143 for (OsmPrimitive osm : Main.ds.allPrimitives()) 144 if (allNew.contains(osm)) 145 sel.add(osm); 146 Main.ds.setSelected(sel); 147 } 148 } 149 150 /** 151 * Create a command that replaces all objects in from with those in to. The lists will be 152 * changed by createCommand. 153 */ 154 private Command createCommand(Collection<OsmPrimitive> from, Collection<OsmPrimitive> to) { 155 // remove all objects in from/to, that are present in both lists. 156 for (Iterator<OsmPrimitive> toIt = to.iterator(); toIt.hasNext();) { 157 OsmPrimitive osm = toIt.next(); 158 for (Iterator<OsmPrimitive> fromIt = from.iterator(); fromIt.hasNext();) { 159 if (fromIt.next().realEqual(osm)) { 160 toIt.remove(); 161 fromIt.remove(); 162 break; 163 } 164 } 165 } 166 167 Collection<Command> cmd = new LinkedList<Command>(); 168 169 // extract all objects that have changed 170 for (Iterator<OsmPrimitive> toIt = to.iterator(); toIt.hasNext();) { 171 OsmPrimitive toOsm = toIt.next(); 172 for (Iterator<OsmPrimitive> fromIt = from.iterator(); fromIt.hasNext();) { 173 OsmPrimitive fromOsm = fromIt.next(); 174 if (fromOsm.equals(toOsm)) { 175 toIt.remove(); 176 fromIt.remove(); 177 cmd.add(new ChangeCommand(fromOsm, toOsm)); 178 break; 179 } 180 } 181 } 182 for (OsmPrimitive fromOsm : Main.ds.allPrimitives()) { 183 for (Iterator<OsmPrimitive> it = to.iterator(); it.hasNext();) { 184 OsmPrimitive toOsm = it.next(); 185 if (fromOsm.equals(toOsm)) { 186 it.remove(); 187 cmd.add(new ChangeCommand(fromOsm, toOsm)); 188 break; 189 } 190 } 191 } 192 193 // extract all added objects 194 for (OsmPrimitive osm : to) 195 cmd.add(new AddCommand(osm)); 196 197 // extract all deleted objects. Delete references as well. 198 CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds); 199 for (OsmPrimitive osm : from) 200 osm.visit(v); 201 v.data.addAll(from); 202 if (!v.data.isEmpty()) 203 cmd.add(new DeleteCommand(v.data)); 204 205 if (cmd.isEmpty()) 206 return null; 207 return new SequenceCommand(tr("Executing {0}",getValue(NAME)), cmd); 208 } 209 } 62 private final Process p; 63 private DataSet dataSet; 64 private DataSet fromDataSet; 65 private ExecuteToolRunner(String msg, Process p) { 66 super(msg); 67 this.p = p; 68 currentAction.setText(tr("Executing {0}",getValue(NAME))); 69 } 70 71 @Override protected void realRun() throws SAXException, IOException { 72 if (!input.isEmpty()) { 73 fromDataSet = new DataSet(); 74 final Collection<GpsPoint> gpxPoints = new LinkedList<GpsPoint>(); 75 final boolean addOsm = !flags.contains("noosm"); 76 final boolean addGpx = flags.contains("gpx"); 77 78 AddVisitor adder = new AddVisitor(fromDataSet, flags.contains("include_references")); 79 if (input.contains("selection")) { 80 Collection<OsmPrimitive> sel = Main.ds.getSelected(); 81 if (addOsm) { 82 for (OsmPrimitive osm : sel) 83 osm.visit(adder); 84 if (flags.contains("include_back_references")) { 85 CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds); 86 for (OsmPrimitive osm : sel) 87 osm.visit(v); 88 AddVisitor a = new AddVisitor(fromDataSet); 89 for (OsmPrimitive osm : v.data) 90 osm.visit(a); 91 } 92 } 93 if (addGpx) { 94 AllNodesVisitor v = new AllNodesVisitor(); 95 for (OsmPrimitive osm : sel) 96 osm.visit(v); 97 Bounds b = new Bounds(); 98 for (Node n : v.nodes) 99 b.extend(n.coor); 100 for (Layer l : Main.map.mapView.getAllLayers()) { 101 if (!(l instanceof RawGpsLayer)) 102 continue; 103 RawGpsLayer layer = (RawGpsLayer)l; 104 for (Collection<GpsPoint> c : layer.data) 105 for (GpsPoint p : c) 106 if (p.latlon.isWithin(b)) 107 gpxPoints.add(p); 108 } 109 } 110 } 111 if (input.contains("all")) { 112 if (addOsm) 113 for (OsmPrimitive osm : Main.ds.allPrimitives()) 114 osm.visit(adder); 115 for (Layer l : Main.map.mapView.getAllLayers()) 116 if (l instanceof RawGpsLayer) 117 for (Collection<GpsPoint> c : ((RawGpsLayer)l).data) 118 for (GpsPoint p : c) 119 gpxPoints.add(p); 120 } 121 if (input.contains("screen")) { 122 if (Main.map == null) { 123 errorMessage = tr("The Tool requires some data to be loaded."); 124 cancel(); 125 return; 126 } 127 LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight()); 128 LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0); 129 Bounds b = new Bounds(bottomLeft, topRight); 130 if (addOsm) { 131 Collection<Node> nodes = new HashSet<Node>(); 132 for (Node n : Main.ds.nodes) { 133 if (n.coor.isWithin(b)) { 134 n.visit(adder); 135 nodes.add(n); 136 } 137 } 138 Collection<Segment> segments = new HashSet<Segment>(); 139 for (Segment s : Main.ds.segments) { 140 if (nodes.contains(s.from) || nodes.contains(s.to)) { 141 s.visit(adder); 142 segments.add(s); 143 } 144 } 145 for (Way w : Main.ds.ways) { 146 for (Segment s : w.segments) { 147 if (segments.contains(s)) { 148 w.visit(adder); 149 break; 150 } 151 } 152 } 153 } 154 if (addGpx) { 155 for (Layer l : Main.map.mapView.getAllLayers()) 156 if (l instanceof RawGpsLayer) 157 for (Collection<GpsPoint> c : ((RawGpsLayer)l).data) 158 for (GpsPoint p : c) 159 if (p.latlon.isWithin(b)) 160 gpxPoints.add(p); 161 } 162 } 163 OsmWriter.output(p.getOutputStream(), new OsmWriter.Osm(){ 164 public void write(PrintWriter out) { 165 if (addOsm) 166 new OsmWriter.All(fromDataSet, false).write(out); 167 if (addGpx) { 168 Collection<Collection<GpsPoint>> c = new LinkedList<Collection<GpsPoint>>(); 169 c.add(gpxPoints); 170 GpxWriter.Trk writer = new GpxWriter.Trk(c); 171 writer.header(out); 172 if (!gpxPoints.isEmpty()) 173 writer.write(out); 174 writer.footer(out); 175 } 176 } 177 }); 178 } 179 if (output != null) 180 dataSet = OsmReader.parseDataSet(p.getInputStream(), currentAction, progress); 181 } 182 183 @Override protected void cancel() { 184 p.destroy(); 185 } 186 187 @Override protected void finish() { 188 if (dataSet == null || output == null || output.equals("discard")) 189 return; // user cancelled or no stdout to process 190 Collection<OsmPrimitive> allNew = dataSet.allPrimitives(); 191 Collection<OsmPrimitive> allOld = fromDataSet.allPrimitives(); 192 if (output.equals("replace")) { 193 Command cmd = createCommand(allOld, allNew); 194 if (cmd != null) { 195 Main.main.editLayer().add(cmd); 196 Main.ds.clearSelection(); 197 } 198 } else if (output.equals("selection")) { 199 Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>(); 200 for (OsmPrimitive osm : Main.ds.allPrimitives()) 201 if (allNew.contains(osm)) 202 sel.add(osm); 203 Main.ds.setSelected(sel); 204 } 205 } 206 207 /** 208 * Create a command that replaces all objects in from with those in to. The lists will be 209 * changed by createCommand. 210 */ 211 private Command createCommand(Collection<OsmPrimitive> from, Collection<OsmPrimitive> to) { 212 // remove all objects in from/to, that are present in both lists. 213 for (Iterator<OsmPrimitive> toIt = to.iterator(); toIt.hasNext();) { 214 OsmPrimitive osm = toIt.next(); 215 for (Iterator<OsmPrimitive> fromIt = from.iterator(); fromIt.hasNext();) { 216 if (fromIt.next().realEqual(osm)) { 217 toIt.remove(); 218 fromIt.remove(); 219 break; 220 } 221 } 222 } 223 224 Collection<Command> cmd = new LinkedList<Command>(); 225 226 // extract all objects that have changed 227 for (Iterator<OsmPrimitive> toIt = to.iterator(); toIt.hasNext();) { 228 OsmPrimitive toOsm = toIt.next(); 229 for (Iterator<OsmPrimitive> fromIt = from.iterator(); fromIt.hasNext();) { 230 OsmPrimitive fromOsm = fromIt.next(); 231 if (fromOsm.equals(toOsm)) { 232 toIt.remove(); 233 fromIt.remove(); 234 cmd.add(new ChangeCommand(fromOsm, toOsm)); 235 break; 236 } 237 } 238 } 239 for (OsmPrimitive fromOsm : Main.ds.allPrimitives()) { 240 for (Iterator<OsmPrimitive> it = to.iterator(); it.hasNext();) { 241 OsmPrimitive toOsm = it.next(); 242 if (fromOsm.equals(toOsm)) { 243 it.remove(); 244 cmd.add(new ChangeCommand(fromOsm, toOsm)); 245 break; 246 } 247 } 248 } 249 250 // extract all added objects 251 for (OsmPrimitive osm : to) 252 cmd.add(new AddCommand(osm)); 253 254 // extract all deleted objects. Delete references as well. 255 CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds); 256 for (OsmPrimitive osm : from) 257 osm.visit(v); 258 v.data.addAll(from); 259 if (!v.data.isEmpty()) 260 cmd.add(new DeleteCommand(v.data)); 261 262 if (cmd.isEmpty()) 263 return null; 264 return new SequenceCommand(tr("Executing {0}",getValue(NAME)), cmd); 265 } 266 } 210 267 211 268 public ExternalToolsAction(String name, String[] exec, String[] flags, String[] input, String output) { … … 242 299 @Override public void startDocument() { 243 300 current.push(main); 244 301 } 245 302 @Override public void startElement(String ns, String lname, String qname, Attributes a) throws SAXException { 246 303 if (qname.equals("group")) { -
src/org/openstreetmap/josm/actions/GpxExportAction.java
r113 r143 30 30 import org.openstreetmap.josm.gui.layer.RawGpsLayer; 31 31 import org.openstreetmap.josm.io.GpxWriter; 32 import org.openstreetmap.josm.io.XmlWriter; 32 33 import org.openstreetmap.josm.tools.GBC; 33 34 … … 117 118 Main.pref.put("lastCopyright", copyright.getText()); 118 119 119 try { 120 GpxWriter w = new GpxWriter(new FileOutputStream(file), layer.name, desc.getText(), 120 XmlWriter.OsmWriterInterface w; 121 if (layer instanceof RawGpsLayer) 122 w = new GpxWriter.Trk(((RawGpsLayer)layer).data); 123 else 124 w = new GpxWriter.All(Main.ds, layer.name, desc.getText(), 121 125 authorName.getText(), email.getText(), copyright.getText(), 122 126 copyrightYear.getText(), keywords.getText()); 123 if (layer instanceof RawGpsLayer) 124 w.output(((RawGpsLayer)layer).data); 125 else 126 w.output(Main.ds); 127 w.close(); 127 try { 128 XmlWriter.output(new FileOutputStream(file), w); 128 129 } catch (IOException x) { 129 130 x.printStackTrace(); -
src/org/openstreetmap/josm/actions/SaveAction.java
r142 r143 54 54 GpxExportAction.exportGpx(file, layer); 55 55 } else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(file.getPath())) { 56 OsmWriter.output(new FileOutputStream(file), Main.ds, false); 56 OsmWriter.output(new FileOutputStream(file), new OsmWriter.All(Main.ds, false)); 57 57 } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(file.getPath())) { 58 58 JOptionPane.showMessageDialog(Main.parent, tr("CSV output not supported yet.")); -
src/org/openstreetmap/josm/data/osm/visitor/Visitor.java
r86 r143 1 1 package org.openstreetmap.josm.data.osm.visitor; 2 2 3 import org.openstreetmap.josm.data.osm.Node; 3 4 import org.openstreetmap.josm.data.osm.Segment; 4 import org.openstreetmap.josm.data.osm.Node;5 5 import org.openstreetmap.josm.data.osm.Way; 6 6 -
src/org/openstreetmap/josm/gui/dialogs/HistoryDialog.java
r129 r143 77 77 */ 78 78 private JTable history = new JTable(data); 79 private JScrollPane historyPane = new JScrollPane(history); 79 80 80 81 private Map<OsmPrimitive, List<HistoryItem>> cache = new HashMap<OsmPrimitive, List<HistoryItem>>(); … … 85 86 public HistoryDialog() { 86 87 super(tr("History"), "history", tr("Display the history of all selected items."), KeyEvent.VK_H, 150); 87 history.setVisible(false); 88 historyPane.setVisible(false); 88 89 notLoaded.setVisible(true); 89 90 … … 115 116 JPanel centerPanel = new JPanel(new GridBagLayout()); 116 117 centerPanel.add(notLoaded, GBC.eol().fill(GBC.BOTH)); 117 centerPanel.add( new JScrollPane(history), GBC.eol().fill(GBC.BOTH));118 centerPanel.add(historyPane, GBC.eol().fill(GBC.BOTH)); 118 119 add(centerPanel, BorderLayout.CENTER); 119 120 … … 160 161 Collection<OsmPrimitive> sel = Main.ds.getSelected(); 161 162 if (!cache.keySet().containsAll(sel)) { 162 history.setVisible(false); 163 historyPane.setVisible(false); 163 164 notLoaded.setVisible(true); 164 165 } else { … … 169 170 for (HistoryItem i : orderedHistory) 170 171 data.addRow(new Object[]{i.osm, i.osm.timestamp, i.visible}); 171 history.setVisible(true); 172 historyPane.setVisible(true); 172 173 notLoaded.setVisible(false); 173 174 } -
src/org/openstreetmap/josm/io/GpxWriter.java
r103 r143 1 1 package org.openstreetmap.josm.io; 2 2 3 import java.io.OutputStream;4 import java.io.OutputStreamWriter;5 3 import java.io.PrintWriter; 6 import java.io.UnsupportedEncodingException;7 4 import java.util.Collection; 8 5 import java.util.LinkedList; … … 16 13 import org.openstreetmap.josm.data.osm.Way; 17 14 import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint; 18 import org.openstreetmap.josm.tools.XmlWriter;19 15 20 16 /** … … 28 24 * @author imi 29 25 */ 30 public class GpxWriter { 31 32 /** 33 * This is the output writer to store the resulting data in. 34 */ 35 private PrintWriter out; 36 37 public GpxWriter(OutputStream os, String name, String desc, String author, String email, String copyright, String year, String keywords) { 38 try { 39 out = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); 40 } catch (UnsupportedEncodingException e) { 41 throw new RuntimeException(e); 42 } 43 out.println(XmlWriter.header()); 44 out.println("<gpx version='1.1' creator='JOSM' xmlns='http://www.topografix.com/GPX/1/1'>"); 45 out.println(" <metadata>"); 46 if (!name.equals("")) 47 out.println(" <name>"+XmlWriter.encode(name)+"</name>"); 48 if (!desc.equals("")) 49 out.println(" <desc>"+XmlWriter.encode(desc)+"</desc>"); 50 if (!author.equals("")) { 51 out.println(" <author>"); 52 out.println(" <name>"+XmlWriter.encode(author)+"</name>"); 53 if (!email.equals("")) 54 out.println(" <email>"+XmlWriter.encode(email)+"</email>"); 55 out.println(" </author>"); 56 if (!copyright.equals("")) { 57 out.println(" <copyright author='"+XmlWriter.encode(author)+"'>"); 58 if (!year.equals("")) 59 out.println(" <year>"+XmlWriter.encode(year)+"</year>"); 60 out.println(" <license>"+XmlWriter.encode(copyright)+"</license>"); 61 out.println(" </copyright>"); 62 } 63 } 64 if (!keywords.equals("")) { 65 out.println(" <keywords>"+XmlWriter.encode(keywords)+"</keywords>"); 66 } 67 // don't finish here, to give output functions the chance to add <bounds> 26 public class GpxWriter extends XmlWriter { 27 28 public GpxWriter(PrintWriter out) { 29 super(out); 68 30 } 69 31 … … 75 37 * all remaining nodes are added as wpt. 76 38 */ 77 public void output(DataSet data) { 78 Collection<OsmPrimitive> all = data.allNonDeletedPrimitives(); 79 if (all.isEmpty()) { 39 public static final class All implements XmlWriter.OsmWriterInterface { 40 private final DataSet data; 41 private final String name; 42 private final String desc; 43 private final String author; 44 private final String email; 45 private final String copyright; 46 private final String year; 47 private final String keywords; 48 private boolean metadataClosed = false; 49 50 public All(DataSet data, String name, String desc, String author, String email, String copyright, String year, String keywords) { 51 this.data = data; 52 this.name = name; 53 this.desc = desc; 54 this.author = author; 55 this.email = email; 56 this.copyright = copyright; 57 this.year = year; 58 this.keywords = keywords; 59 } 60 61 public void header(PrintWriter out) { 62 out.println("<gpx version='1.1' creator='JOSM' xmlns='http://www.topografix.com/GPX/1/1'>"); 63 out.println(" <metadata>"); 64 if (!name.equals("")) 65 out.println(" <name>"+XmlWriter.encode(name)+"</name>"); 66 if (!desc.equals("")) 67 out.println(" <desc>"+XmlWriter.encode(desc)+"</desc>"); 68 if (!author.equals("")) { 69 out.println(" <author>"); 70 out.println(" <name>"+XmlWriter.encode(author)+"</name>"); 71 if (!email.equals("")) 72 out.println(" <email>"+XmlWriter.encode(email)+"</email>"); 73 out.println(" </author>"); 74 if (!copyright.equals("")) { 75 out.println(" <copyright author='"+XmlWriter.encode(author)+"'>"); 76 if (!year.equals("")) 77 out.println(" <year>"+XmlWriter.encode(year)+"</year>"); 78 out.println(" <license>"+XmlWriter.encode(copyright)+"</license>"); 79 out.println(" </copyright>"); 80 } 81 } 82 if (!keywords.equals("")) { 83 out.println(" <keywords>"+XmlWriter.encode(keywords)+"</keywords>"); 84 } 85 // don't finish here, to give output functions the chance to add <bounds> 86 } 87 88 public void write(PrintWriter out) { 89 Collection<OsmPrimitive> all = data.allNonDeletedPrimitives(); 90 if (all.isEmpty()) 91 return; 92 GpxWriter writer = new GpxWriter(out); 93 // calculate bounds 94 Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE)); 95 for (Node n : data.nodes) 96 if (!n.deleted) 97 b.extend(n.coor); 98 out.println(" <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />"); 80 99 out.println(" </metadata>"); 100 metadataClosed = true; 101 102 // add ways 103 for (Way w : data.ways) { 104 if (w.deleted) 105 continue; 106 out.println(" <trk>"); 107 Segment oldLs = null; 108 for (Segment ls : w.segments) { 109 if (ls.incomplete) 110 continue; 111 // end old segemnt, if no longer match a chain 112 if (oldLs != null && !oldLs.to.coor.equals(ls.from.coor)) { 113 out.println(" </trkseg>"); 114 writer.outputNode(oldLs.to, false); 115 all.remove(oldLs.to); 116 oldLs = null; 117 } 118 // start new segment if necessary 119 if (oldLs == null) 120 out.println(" <trkseg>"); 121 writer.outputNode(ls.from, false); 122 all.remove(ls.from); 123 oldLs = ls; 124 all.remove(ls); 125 } 126 // write last node if there 127 if (oldLs != null) { 128 writer.outputNode(oldLs.to, false); 129 all.remove(oldLs.to); 130 out.println(" </trkseg>"); 131 } 132 out.println(" </trk>"); 133 all.remove(w); 134 } 135 136 // add remaining segments 137 Collection<Segment> segments = new LinkedList<Segment>(); 138 for (OsmPrimitive osm : all) 139 if (osm instanceof Segment && !((Segment)osm).incomplete) 140 segments.add((Segment)osm); 141 if (!segments.isEmpty()) { 142 out.println(" <trk>"); 143 for (Segment ls : segments) { 144 out.println(" <trkseg>"); 145 writer.outputNode(ls.from, false); 146 all.remove(ls.from); 147 writer.outputNode(ls.to, false); 148 all.remove(ls.to); 149 out.println(" </trkseg>"); 150 all.remove(ls); 151 } 152 out.println(" </trk>"); 153 } 154 155 // finally add the remaining nodes 156 for (OsmPrimitive osm : all) 157 if (osm instanceof Node) 158 writer.outputNode((Node)osm, true); 159 } 160 161 public void footer(PrintWriter out) { 162 if (!metadataClosed) 163 out.println(" </metadata>"); 81 164 out.println("</gpx>"); 82 return; 83 } 84 // calculate bounds 85 Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE)); 86 for (Node n : data.nodes) 87 if (!n.deleted) 88 b.extend(n.coor); 89 out.println(" <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />"); 90 out.println(" </metadata>"); 91 92 // add ways 93 for (Way w : data.ways) { 94 if (w.deleted) 95 continue; 96 out.println(" <trk>"); 97 Segment oldLs = null; 98 for (Segment ls : w.segments) { 99 if (ls.incomplete) 100 continue; 101 // end old segemnt, if no longer match a chain 102 if (oldLs != null && !oldLs.to.coor.equals(ls.from.coor)) { 103 out.println(" </trkseg>"); 104 outputNode(oldLs.to, false); 105 all.remove(oldLs.to); 106 oldLs = null; 107 } 108 // start new segment if necessary 109 if (oldLs == null) 110 out.println(" <trkseg>"); 111 outputNode(ls.from, false); 112 all.remove(ls.from); 113 oldLs = ls; 114 all.remove(ls); 115 } 116 // write last node if there 117 if (oldLs != null) { 118 outputNode(oldLs.to, false); 119 all.remove(oldLs.to); 120 out.println(" </trkseg>"); 121 } 122 out.println(" </trk>"); 123 all.remove(w); 124 } 125 126 // add remaining segments 127 Collection<Segment> segments = new LinkedList<Segment>(); 128 for (OsmPrimitive osm : all) 129 if (osm instanceof Segment && !((Segment)osm).incomplete) 130 segments.add((Segment)osm); 131 if (!segments.isEmpty()) { 132 out.println(" <trk>"); 133 for (Segment ls : segments) { 134 out.println(" <trkseg>"); 135 outputNode(ls.from, false); 136 all.remove(ls.from); 137 outputNode(ls.to, false); 138 all.remove(ls.to); 139 out.println(" </trkseg>"); 140 all.remove(ls); 141 } 142 out.println(" </trk>"); 143 } 144 145 // finally add the remaining nodes 146 for (OsmPrimitive osm : all) 147 if (osm instanceof Node) 148 outputNode((Node)osm, true); 149 150 out.println("</gpx>"); 165 } 151 166 } 152 167 … … 156 171 * trk with as many trkseg as there are collections in the outer collection. 157 172 */ 158 public void output(Collection<Collection<GpsPoint>> data) { 159 if (data.size() == 0) { 173 public static final class Trk implements XmlWriter.OsmWriterInterface { 174 private final Collection<Collection<GpsPoint>> data; 175 public Trk(Collection<Collection<GpsPoint>> data) { 176 this.data = data; 177 } 178 179 public void header(PrintWriter out) { 180 out.println("<gpx version='1.1' creator='JOSM' xmlns='http://www.topografix.com/GPX/1/1'>"); 181 } 182 183 public void write(PrintWriter out) { 184 if (data.size() == 0) 185 return; 186 // calculate bounds 187 Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE)); 188 for (Collection<GpsPoint> c : data) 189 for (GpsPoint p : c) 190 b.extend(p.latlon); 160 191 out.println(" </metadata>"); 192 out.println(" <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />"); 193 out.println(" </metadata>"); 194 195 out.println(" <trk>"); 196 for (Collection<GpsPoint> c : data) { 197 out.println(" <trkseg>"); 198 LatLon last = null; 199 for (GpsPoint p : c) { 200 // skip double entries 201 if (p.latlon.equals(last)) 202 continue; 203 last = p.latlon; 204 LatLon ll = p.latlon; 205 out.print(" <trkpt lat='"+ll.lat()+"' lon='"+ll.lon()+"'"); 206 if (p.time != null && p.time.length()!=0) { 207 out.println(">"); 208 out.println(" <time>"+p.time+"</time>"); 209 out.println(" </trkpt>"); 210 } else 211 out.println(" />"); 212 } 213 out.println(" </trkseg>"); 214 } 215 out.println(" </trk>"); 216 } 217 218 public void footer(PrintWriter out) { 161 219 out.println("</gpx>"); 162 return; 163 } 164 // calculate bounds 165 Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE)); 166 for (Collection<GpsPoint> c : data) 167 for (GpsPoint p : c) 168 b.extend(p.latlon); 169 out.println(" <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />"); 170 out.println(" </metadata>"); 171 172 out.println(" <trk>"); 173 for (Collection<GpsPoint> c : data) { 174 out.println(" <trkseg>"); 175 LatLon last = null; 176 for (GpsPoint p : c) { 177 // skip double entries 178 if (p.latlon.equals(last)) 179 continue; 180 last = p.latlon; 181 LatLon ll = p.latlon; 182 out.print(" <trkpt lat='"+ll.lat()+"' lon='"+ll.lon()+"'"); 183 if (p.time != null && p.time.length()!=0) { 184 out.println(">"); 185 out.println(" <time>"+p.time+"</time>"); 186 out.println(" </trkpt>"); 187 } else 188 out.println(" />"); 189 } 190 out.println(" </trkseg>"); 191 } 192 out.println(" </trk>"); 193 out.println("</gpx>"); 220 } 194 221 } 195 222 … … 224 251 out.println(" />"); 225 252 } 226 227 public void close() {228 out.flush();229 out.close();230 }231 253 } -
src/org/openstreetmap/josm/io/OsmServerWriter.java
r113 r143 164 164 if (addBody) { 165 165 OutputStream out = activeConnection.getOutputStream(); 166 OsmWriter.outputSingle( out,osm, true);166 OsmWriter.output(out, new OsmWriter.Single(osm, true)); 167 167 out.close(); 168 168 } … … 178 178 if (retCode != 200) { 179 179 ByteArrayOutputStream o = new ByteArrayOutputStream(); 180 OsmWriter.outputSingle( o,osm, true);180 OsmWriter.output(o, new OsmWriter.Single(osm, true)); 181 181 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 182 182 throw new RuntimeException(retCode+" "+retMsg); -
src/org/openstreetmap/josm/io/OsmWriter.java
r103 r143 1 1 package org.openstreetmap.josm.io; 2 2 3 import java.io.OutputStream;4 import java.io.OutputStreamWriter;5 3 import java.io.PrintWriter; 6 import java.io.UnsupportedEncodingException;7 4 import java.util.HashMap; 8 5 import java.util.Map.Entry; … … 14 11 import org.openstreetmap.josm.data.osm.Way; 15 12 import org.openstreetmap.josm.data.osm.visitor.Visitor; 16 import org.openstreetmap.josm.tools.XmlWriter;17 13 18 14 /** … … 21 17 * @author imi 22 18 */ 23 public class OsmWriter implements Visitor { 24 25 /** 26 * The output writer to save the values to. 27 */ 28 private PrintWriter out; 19 public class OsmWriter extends XmlWriter implements Visitor { 29 20 30 21 /** … … 40 31 private final boolean osmConform; 41 32 33 public abstract static class Osm implements OsmWriterInterface { 34 public void header(PrintWriter out) { 35 out.println("<osm version='0.3' generator='JOSM'>"); 36 } 37 public void footer(PrintWriter out) { 38 out.println("</osm>"); 39 } 40 } 41 42 42 /** 43 * Output the data to the stream 44 * @param osmConform <code>true</code>, if the xml should be 100% osm conform. In this 45 * case, not all information can be retrieved later (as example, modified state 46 * is lost and id's remain 0 instead of decrementing from -1) 43 * An output writer for function output that writes everything of the given dataset into 44 * the xml 47 45 */ 48 public static void output(OutputStream out, DataSet ds, boolean osmConform) { 49 OsmWriter writer = new OsmWriter(out, osmConform); 50 writer.out.println("<?xml version='1.0' encoding='UTF-8'?>"); 51 writer.out.println("<osm version='0.3' generator='JOSM'>"); 52 for (Node n : ds.nodes) 53 writer.visit(n); 54 for (Segment ls : ds.segments) 55 writer.visit(ls); 56 for (Way w : ds.ways) 57 writer.visit(w); 58 writer.out.println("</osm>"); 59 writer.close(); 46 public static final class All extends Osm { 47 private final DataSet ds; 48 private final boolean osmConform; 49 50 /** 51 * Construct an writer function 52 * @param osmConform <code>true</code>, if the xml should be 100% osm conform. In this 53 * case, not all information can be retrieved later (as example, modified state 54 * is lost and id's remain 0 instead of decrementing from -1) 55 */ 56 public All(DataSet ds, boolean osmConform) { 57 this.ds = ds; 58 this.osmConform = osmConform; 59 } 60 61 public void write(PrintWriter out) { 62 Visitor writer = new OsmWriter(out, osmConform); 63 for (Node n : ds.nodes) 64 writer.visit(n); 65 for (Segment ls : ds.segments) 66 writer.visit(ls); 67 for (Way w : ds.ways) 68 writer.visit(w); 69 } 60 70 } 61 71 62 public static void outputSingle(OutputStream out, OsmPrimitive osm, boolean osmConform) { 63 OsmWriter writer = new OsmWriter(out, osmConform); 64 writer.out.println(XmlWriter.header()); 65 writer.out.println("<osm version='0.3' generator='JOSM'>"); 66 osm.visit(writer); 67 writer.out.println("</osm>"); 68 writer.close(); 72 /** 73 * An output writer for functino output that writes only one specific primitive into 74 * the xml 75 */ 76 public static final class Single extends Osm { 77 private final OsmPrimitive osm; 78 private final boolean osmConform; 79 80 public Single(OsmPrimitive osm, boolean osmConform) { 81 this.osm = osm; 82 this.osmConform = osmConform; 83 } 84 85 public void write(PrintWriter out) { 86 osm.visit(new OsmWriter(out, osmConform)); 87 } 69 88 } 70 89 71 private OsmWriter(OutputStream out, boolean osmConform) { 72 try { 73 this.out = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); 74 } catch (UnsupportedEncodingException e) { 75 throw new RuntimeException(e); 76 } 90 private OsmWriter(PrintWriter out, boolean osmConform) { 91 super(out); 77 92 this.osmConform = osmConform; 78 93 } … … 146 161 } 147 162 } 148 149 public void close() {150 out.close();151 }152 163 } -
src/org/openstreetmap/josm/io/XmlWriter.java
r141 r143 1 package org.openstreetmap.josm. tools;1 package org.openstreetmap.josm.io; 2 2 3 import java.io.OutputStream; 4 import java.io.OutputStreamWriter; 5 import java.io.PrintWriter; 6 import java.io.UnsupportedEncodingException; 3 7 import java.util.HashMap; 4 8 … … 9 13 */ 10 14 public class XmlWriter { 15 16 /** 17 * The interface to write the data into an Osm stream 18 * @author immanuel.scholz 19 */ 20 public static interface OsmWriterInterface { 21 void header(PrintWriter out); 22 void write(PrintWriter out); 23 void footer(PrintWriter out); 24 } 25 26 27 protected XmlWriter(PrintWriter out) { 28 this.out = out; 29 } 11 30 12 31 /** … … 29 48 30 49 /** 31 * @return The standard XML1.0 header. Encoding is utf-8 50 * Write the header and start tag, then call the runnable to add all real tags and finally 51 * "closes" the xml by writing the footer. 32 52 */ 33 public static String header() { 34 return "<?xml version='1.0' encoding='UTF-8'?>"; 53 public static void output(OutputStream outStream, OsmWriterInterface outputWriter) { 54 PrintWriter out; 55 try { 56 out = new PrintWriter(new OutputStreamWriter(outStream, "UTF-8")); 57 } catch (UnsupportedEncodingException e) { 58 throw new RuntimeException(e); 59 } 60 out.println("<?xml version='1.0' encoding='UTF-8'?>"); 61 outputWriter.header(out); 62 outputWriter.write(out); 63 outputWriter.footer(out); 64 out.flush(); 65 out.close(); 35 66 } 36 67 37 38 68 69 70 /** 71 * The output writer to save the values to. 72 */ 73 protected final PrintWriter out; 39 74 final private static HashMap<Character, String> encoding = new HashMap<Character, String>(); 40 75 static { -
test/org/openstreetmap/josm/data/osm/visitor/MergeVisitorTest.java
r135 r143 11 11 import org.openstreetmap.josm.data.osm.Segment; 12 12 import org.openstreetmap.josm.data.osm.Way; 13 import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;14 13 import org.openstreetmap.josm.testframework.Bug; 15 14 import org.openstreetmap.josm.testframework.DataSetTestCaseHelper; … … 267 266 /** 268 267 * Deleted segments should be deleted when merged over unchanged segments. 269 * Deleted segments should also raise an conflict when merged over changed segments. 270 */ 271 //TODO 272 268 */ 269 public void testMergeDeletedOverUnchangedDeletes() { 270 DataSet ds = new DataSet(); 271 Segment oldSegment = createSegment(ds, false, false, 23); 272 Segment s = createSegment(null, false, true, 23); 273 274 MergeVisitor v = new MergeVisitor(ds); 275 v.visit(s); 276 v.fixReferences(); 277 278 assertEquals(true, oldSegment.deleted); 279 } 280 281 282 /** 283 * Deleted segments should raise an conflict when merged over changed segments. 284 */ 285 public void testMergeDeletedOverChangedConflict() { 286 DataSet ds = new DataSet(); 287 createSegment(ds, false, false, 23).modified = true; 288 Segment s = createSegment(null, false, true, 23); 289 290 MergeVisitor v = new MergeVisitor(ds); 291 v.visit(s); 292 v.fixReferences(); 293 294 assertEquals(1, v.conflicts.size()); 295 } 296 297 298 private Segment createSegment(DataSet ds, boolean incomplete, boolean deleted, int id) { 299 Node n1 = DataSetTestCaseHelper.createNode(ds); 300 Node n2 = DataSetTestCaseHelper.createNode(ds); 301 Segment s = DataSetTestCaseHelper.createSegment(ds, n1, n2); 302 s.incomplete = incomplete; 303 s.id = id; 304 s.deleted = deleted; 305 return s; 306 } 307 273 308 /** 274 309 * Create that amount of nodes and add them to the dataset. The id will be 1,2,3,4... -
test/org/openstreetmap/josm/testframework/DataSetTestCaseHelper.java
r135 r143 44 44 Way t = new Way(); 45 45 t.segments.addAll(Arrays.asList(segments)); 46 ds.ways.add(t); 46 if (ds != null) 47 ds.ways.add(t); 47 48 return t; 48 49 } … … 53 54 public static Segment createSegment(DataSet ds, Node n1, Node n2) { 54 55 Segment ls = new Segment(n1, n2); 55 ds.segments.add(ls); 56 if (ds != null) 57 ds.segments.add(ls); 56 58 return ls; 57 59 }
Note:
See TracChangeset
for help on using the changeset viewer.