Ignore:
Timestamp:
2019-04-07T07:14:21+02:00 (6 years ago)
Author:
gerdp
Message:

fix #17568: newline missing in exported *.poly file when only a single ring is written
Further changes

  • use PrinterWriter
  • add Unit test for export of simple polygon
Location:
applications/editors/josm/plugins/poly
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/poly/src/poly/PolyExporter.java

    r34860 r34966  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedWriter;
    76import java.io.File;
    87import java.io.IOException;
     8import java.io.PrintWriter;
    99import java.nio.charset.StandardCharsets;
    1010import java.nio.file.Files;
     
    4343                throw new IOException(tr("Data contains unclosed ways."));
    4444            }
    45             try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
     45            try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))) {
    4646                DataSet ds = ((OsmDataLayer) layer).getDataSet();
    4747                HashSet<Way> written = new HashSet<>();
     
    5656                    if (rel.isMultipolygon()) {
    5757                        if (!firstFile) {
    58                             writer.newLine();
     58                            writer.println();
    5959                        }
    6060                        writeRelation(writer, fileName, rel, written);
     
    6464               
    6565                if (firstFile) {
    66                     writer.write(fileName);
     66                    writer.println(fileName);
    6767                }
    6868                int counter = 1;
     
    7272                    }
    7373                }
    74                 writer.write("END");
    75                 writer.newLine();
     74                writer.println("END");
    7675            }
    7776        }
    7877    }
    7978
    80     private static void writeRelation(BufferedWriter writer, String fileName, Relation rel, Set<Way> written) throws IOException {
     79    private static void writeRelation(PrintWriter writer, String fileName, Relation rel, Set<Way> written) {
    8180        String polygonName = fileName;
    8281        if (rel.getName() != null)
    8382            polygonName = rel.getName();
    84         writer.write(polygonName);
    85         writer.newLine();
     83        writer.println(polygonName);
    8684        int counter = 1;
    8785        for (RelationMember rm: rel.getMembers()) {
     
    9694    }
    9795
    98     private static int writeWay(BufferedWriter writer, Way w, int counter) throws IOException {
     96    private static int writeWay(PrintWriter writer, Way w, int counter) {
    9997        String name = w.getName();
    10098        if (name == null) {
    10199            name = String.valueOf(counter++);
    102100        }
    103         writer.write(name);
    104         writer.newLine();
     101        writer.println(name);
    105102
    106103        for (Node n : w.getNodes()) {
    107             writer.write(String.format(Locale.ENGLISH, "   %f   %f", n.getCoor().lon(), n.getCoor().lat()));
    108             writer.newLine();
     104            writer.println(String.format(Locale.ENGLISH, "   %f   %f", n.getCoor().lon(), n.getCoor().lat()));
    109105        }
    110         writer.write("END");
    111         writer.newLine();
     106        writer.println("END");
    112107        return counter;
    113108    }
  • applications/editors/josm/plugins/poly/test/unit/poly/PolyExporterTest.java

    r34860 r34966  
    3232     */
    3333    @Test
     34    public void testSimpleExport() throws Exception {
     35        DataSet dsIn1 = new PolyImporter().parseDataSet(TestUtils.getTestDataRoot() + "/simple.poly");
     36        assertNotNull(dsIn1);
     37        assertEquals(4, dsIn1.getNodes().size());
     38        assertEquals(1, dsIn1.getWays().size());
     39        assertEquals(0, dsIn1.getRelations().size());
     40
     41        Path out = Files.createTempFile("simple-out", "poly");
     42        new PolyExporter().exportData(out.toFile(), new OsmDataLayer(dsIn1, null, null));
     43        DataSet dsIn2 = new PolyImporter().parseDataSet(out.toString());
     44        assertNotNull(dsIn2);
     45        assertEquals(4, dsIn2.getNodes().size());
     46        assertEquals(1, dsIn2.getWays().size());
     47        assertEquals(0, dsIn2.getRelations().size());
     48       
     49        Files.delete(out);
     50    }
     51
     52    /**
     53     * Import file, export it, import the exported file and compare content
     54     * @throws Exception if an error occurs
     55     */
     56    @Test
    3457    public void testExport() throws Exception {
    3558        DataSet dsIn1 = new PolyImporter().parseDataSet(TestUtils.getTestDataRoot() + "/holes.poly");
Note: See TracChangeset for help on using the changeset viewer.