Changeset 30575 in osm for applications/editors/josm
- Timestamp:
- 2014-08-08T00:47:24+02:00 (10 years ago)
- Location:
- applications/editors/josm/plugins/opendata
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/AbstractMapInfoReader.java
r30563 r30575 4 4 import java.io.BufferedReader; 5 5 import java.io.File; 6 import java.io.FileInputStream;7 import java.io.FileNotFoundException;8 6 import java.io.IOException; 9 7 import java.io.InputStreamReader; 10 8 import java.nio.charset.Charset; 9 import java.nio.charset.IllegalCharsetNameException; 10 import java.nio.charset.UnsupportedCharsetException; 11 import java.nio.file.Files; 11 12 import java.util.ArrayList; 12 13 import java.util.List; … … 47 48 } 48 49 49 protected final BufferedReader getDataReader(File headerFile, String extension, Charset charset) throws FileNotFoundException {50 protected final BufferedReader getDataReader(File headerFile, String extension, Charset charset) throws IOException { 50 51 File dataFile = getDataFile(headerFile, extension); 51 return dataFile.exists() ? new BufferedReader(new InputStreamReader(new FileInputStream(dataFile), charset)) : null;52 return dataFile.exists() ? Files.newBufferedReader(dataFile.toPath(), charset) : null; 52 53 } 53 54 54 protected Charset parseCharset(String[] words) {55 protected Charset parseCharset(String[] words) throws IllegalCharsetNameException, UnsupportedCharsetException { 55 56 return parseCharset(words, 1); 56 57 } 57 58 58 protected Charset parseCharset(String[] words, int index) {59 protected Charset parseCharset(String[] words, int index) throws IllegalCharsetNameException, UnsupportedCharsetException { 59 60 words[index] = words[index].replace("\"", ""); 60 61 if (words[index].equalsIgnoreCase(CHARSET_WINDOWS_LATIN)) { -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/MifReader.java
r30574 r30575 12 12 import java.io.InputStream; 13 13 import java.io.InputStreamReader; 14 import java.io.Reader; 14 15 import java.io.UnsupportedEncodingException; 15 16 import java.nio.charset.Charset; … … 31 32 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 32 33 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 34 import org.openstreetmap.josm.plugins.opendata.core.io.InputStreamReaderUnbuffered; 33 35 import org.openstreetmap.josm.plugins.opendata.core.util.OdUtils; 34 36 … … 48 50 READING_POINTS, 49 51 END_POLYGON, 50 START_POLYLINE ,52 START_POLYLINE_SEGMENT, 51 53 END_POLYLINE 52 54 } 53 55 56 private File file; 57 private InputStream stream; 58 protected Charset charset; 54 59 protected BufferedReader midReader; 55 60 … … 86 91 private int numpolygons = -1; 87 92 private int numpts = -1; 93 94 // PLine clause 95 private int numsections = -1; 88 96 89 97 public static DataSet parseDataSet(InputStream in, File file, … … 281 289 int index = parseAffineUnits(words); 282 290 283 // FIXME: no idea what projection has to be used for real with "non-earth" mode...284 josmProj = Projections.getProjectionByCode("EPSG:4326"); // WGS 84285 286 291 units = words[index+1]; 287 292 … … 317 322 case "nonearth": 318 323 parseCoordSysSyntax2(words); 324 325 // Syntax2 is not meant to be used for maps, and still... # 9592 happened 326 // From MapInfo documentation: 327 // http://testdrive.mapinfo.com/TDC/mxtreme4java.nsf/22fbc128f401ad818525666a00646bda/50100fdbe3e0a85085256a770053be1a/$FILE/coordsys.txt 328 // Use syntax 1 (above) to explicitly define a coordinate system for an Earth map (a map having coordinates which are specified with respect to a 329 // location on the surface of the Earth). The optional Projection parameters dictate what map projection, if any, should be used in conjunction with 330 // the coordinate system. If the Projection clause is omitted, MapBasic uses a longitude, latitude coordinate system using the North American Datum of 1927 (NAD-27). 331 // Use syntax 2 to explicitly define a non-Earth coordinate system, such as the coordinate system used in a floor plan or other CAD drawing. 332 333 // FIXME: allow user to choose projection ? 334 josmProj = new CustomProjection(null); 319 335 break; 320 336 case "layout": 321 337 case "table": 322 338 case "window": 323 // TODO: support Layout, Table, Window clauses 324 Main.warn("TODO: "+line); 339 Main.error("Unsupported CoordSys clause: "+line); 325 340 break; 326 341 default: 327 Main. warn("Line "+lineNum+". Invalid CoordSys clause: "+line);342 Main.error("Line "+lineNum+". Invalid CoordSys clause: "+line); 328 343 } 329 344 } … … 358 373 } 359 374 360 private void startPolyLine() throws IOException { 375 private void startPolyLineSegment(boolean initial) throws IOException { 376 Way previousPolyline = polyline; 361 377 polyline = new Way(); 362 378 ds.addPrimitive(polyline); 363 readAttributes(polyline); 379 if (initial) { 380 readAttributes(polyline); 381 } else if (previousPolyline != null) { 382 // Not sure about how to handle multiple segments. In doubt we create a new way with the same tags 383 polyline.setKeys(previousPolyline.getKeys()); 384 } 364 385 state = State.READING_POINTS; 365 386 } 366 387 367 388 private void parsePLine(String[] words) throws IOException { 389 numsections = 1; 368 390 if (words.length <= 1 || "MULTIPLE".equalsIgnoreCase(words[1])) { 369 // TODO: pline with multiple sections370 391 numpts = -1; 371 state = State.START_POLYLINE; 392 state = State.START_POLYLINE_SEGMENT; 393 if (words.length >= 3) { 394 // pline with multiple sections 395 numsections = Integer.parseInt(words[2]); 396 } 372 397 } else { 373 398 numpts = Integer.parseInt(words[1]); // Not described in PDF but found in real files: PLINE XX, with XX = numpoints 374 startPolyLine ();399 startPolyLineSegment(true); 375 400 } 376 401 } … … 414 439 } 415 440 416 private DataSet parse(InputStream in, File file, ProgressMonitor instance, Charset charset) throws IOException { 441 private void initializeReaders(InputStream in, File f, Charset cs, int bufSize) throws IOException { 442 stream = in; 443 charset = cs; 444 file = f; 445 Reader isr; 446 // Did you know ? new InputStreamReader(in, charset) has a non-configurable buffer of 8kb :( 447 if (bufSize < 8192) { 448 isr = new InputStreamReaderUnbuffered(in, charset); 449 } else { 450 isr = new InputStreamReader(in, charset); 451 } 452 headerReader = new BufferedReader(isr, bufSize); 453 if (midReader != null) { 454 midReader.close(); 455 } 456 midReader = getDataReader(file, ".mid", charset); 457 } 458 459 private DataSet parse(InputStream in, File file, ProgressMonitor instance, Charset cs) throws IOException { 417 460 try { 418 headerReader = new BufferedReader(new InputStreamReader(in, charset)); 419 midReader = getDataReader(file, ".mid", charset); 420 parseHeader(); 421 if (midReader != null) { 422 midReader.close(); 461 try { 462 // Read header byte per byte until we determine correct charset 463 initializeReaders(in, file, cs, 1); 464 parseHeader(); 465 return ds; 466 } finally { 467 if (midReader != null) { 468 midReader.close(); 469 } 423 470 } 424 return ds;425 471 } catch (UnsupportedEncodingException e) { 426 472 throw new IOException(e); … … 433 479 parseVersion(words); 434 480 } else if (words[0].equalsIgnoreCase("Charset")) { 435 parseCharset(words); 481 // Reinitialize readers with an efficient buffer value now we know for sure the good charset 482 initializeReaders(stream, file, parseCharset(words), 8192); 436 483 } else if (words[0].equalsIgnoreCase("Delimiter")) { 437 484 parseDelimiter(words); … … 460 507 state = State.READING_POINTS; 461 508 462 } else if (state == State.START_POLYLINE ) {509 } else if (state == State.START_POLYLINE_SEGMENT) { 463 510 numpts = Integer.parseInt(words[0]); 464 startPolyLine ();511 startPolyLineSegment(polyline != null); 465 512 466 513 } else if (state == State.READING_POINTS && numpts > 0) { … … 485 532 } 486 533 } else if (polyline != null) { 487 state = State.UNKNOWN; 488 polyline = null; 534 if (--numsections > 0) { 535 state = State.START_POLYLINE_SEGMENT; 536 } else { 537 state = State.UNKNOWN; 538 polyline = null; 539 } 489 540 } 490 541 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/TabReader.java
r30563 r30575 8 8 import java.io.InputStreamReader; 9 9 import java.nio.charset.Charset; 10 import java.nio.charset.IllegalCharsetNameException; 11 import java.nio.charset.UnsupportedCharsetException; 10 12 import java.util.ArrayList; 11 13 import java.util.List; 12 14 13 import org.geotools.data.shapefile.files.TabFiles;14 15 import org.geotools.data.shapefile.dbf.DbaseFileReader; 15 16 import org.geotools.data.shapefile.dbf.DbaseFileReader.Row; 17 import org.geotools.data.shapefile.files.TabFiles; 16 18 import org.openstreetmap.josm.data.osm.DataSet; 17 19 import org.openstreetmap.josm.gui.progress.ProgressMonitor; … … 111 113 } 112 114 113 private void parseType(String[] words) {115 private void parseType(String[] words) throws IllegalCharsetNameException, UnsupportedCharsetException { 114 116 if (words[1].equalsIgnoreCase("NATIVE") && words[2].equalsIgnoreCase("Charset")) { 115 117 datCharset = parseCharset(words, 3); -
applications/editors/josm/plugins/opendata/test/unit/org/openstreetmap/josm/plugins/opendata/core/io/NonRegFunctionalTests.java
r30573 r30575 16 16 17 17 import org.openstreetmap.josm.TestUtils; 18 import org.openstreetmap.josm.data.coor.LatLon; 18 19 import org.openstreetmap.josm.data.osm.DataSet; 19 20 import org.openstreetmap.josm.data.osm.Node; … … 32 33 Collection<Node> nodes = ds.getNodes(); 33 34 assertFalse("No nodes in dataset for "+context, nodes.isEmpty()); 34 // Nodes should all have coordinates35 // Nodes should all have valid coordinates 35 36 for (Node n : nodes) { 36 assertTrue("Node without coordinate found for "+context, n.getCoor() != null); 37 LatLon latlon = n.getCoor(); 38 assertTrue("Node without coordinate found for "+context, latlon != null); 39 assertTrue("Node with invalid coordinate ("+latlon+") found for "+context, latlon.isValid()); 37 40 } 38 41 // and no empty ways
Note:
See TracChangeset
for help on using the changeset viewer.