Skip to content

Make OSM files compatible with API v0.6 #2223

Closed

Description

@easbar

OSM v0.6 from 2009 is the latest version and some tools like Osmosis, libosmium and JOSM strictly enforce this specification. Some of the OSM files we use for tests do not comply with this spec. I think they should be converted so they can be used with recent tools.

Related to this discussion: #2193 (comment)

Activity

  1. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    I had a closer look at this and to my surprise the 'v0.5/6' API definition is not very strict at all. First of all I could not find a definite resource about the exact specification of v0.6. Osmosis, osmium-tool and JOSM all claim to 'require' v0.6, but what exactly does this even mean? The wiki has this page: https://wiki.openstreetmap.org/wiki/API_v0.6/DTD, but it says 'N.B.: This schema is incomplete!'.

    So I tried opening test-osm.xml with the different tools and the result was:

    • osmosis complaint about missing 'version' attributes. After adding version="1" to all nodes and ways it complaint about missing timestamps. But when I used the enableDateParsing=no option it accepted the file. I was also able to use the --migrate option with osmosis 0.35.1 in combination with --read-xml-0.5 which is supposed to migrate to v0.6 and apparently simply adds the current timestamp, user="" and version="1".
    • JOSM also complaint about the missing version attribute, but after adding this it accepted the file
    • osmium accepted the file without any problem, even without adding the version attribute

    Strangely, the version attribute required by JOSM and osmosis is listed as 'implied'(=optional) in above linked DTD, so this does not seem very helpful.

    Conclusion: Simply adding some "version" attribute to all nodes,ways,relations makes our files compatible with the latest versions of osmosis, osmium and JOSM and I do not think there is a reason to do anything beyond this for now.

  2. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    We should also sort our XML files. I ran into a few problems when trying to convert unsorted XMLs into pbfs as this yielded unsorted pbfs which are not supported by a few tools.

  3. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    Sort such that nodes come before ways and ways before relations? Which ones are not sorted like this already?

  4. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    From the Osmium manual:

    Objects are sorted by type, ID, and version. IDs are sorted negative IDs first, the positive IDs, both ordered by their absolute values. So the sort order for types and IDs is:

    node -1, node -2, …, node 1, node 2, …, way -1, way -2, …, way 1, way 2, …, relation -1, relation -2, …, relation 1, relation 2, …

    I think it was limited to only 2 or 3 test files

  5. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    Is this the format osmium expects or the output format of osmium's sort command? And what are we trying to achieve here? In case we want to remove xml support we need a pre-processing step that converts xml to pbf right? How do you think should this be done? Osmosis might be easier to integrate because its Java. Osmium would require an extra installation step. If we decide to use Osmium of course we can sort the 2 or 3 unsorted test files.

    But honestly I am not entirely sold on the idea that we even need to remove the XML support yet. Can you explain why exactly this would be beneficial?

  6. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    Most tools nowadays require pbfs to be sorted like e.g osm2pgsql 1.4.0. This would require an extra step if we keep our XML files unsorted and want to convert them on-the-fly to pbfs.

    But honestly I am not entirely sold on the idea that we even need to remove the XML support yet. Can you explain why exactly this would be beneficial?

    Better maintainability and simpler code would be the biggest arguments for this. And the fact that pbfs are the de facto standard format for OSM data (at least for any larger datasets)

  7. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    if we keep our XML files unsorted

    OSMReader already requires that nodes come before ways and like I said sure we can sort the few files that do not follow the other rule (ids ordered by their absolute values and negative ids before positive ids) if this is really required.

    Better maintainability and simpler code would be the biggest arguments for this.

    Ok I am just wondering if its more maintainable if we need the pre-processing step instead of what we are doing right now. Where can we simplify our current code if we remove xml support?

  8. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    The OSMInputFile currently needs to deal with both formats and could be trimmed down significantly if we drop XML support:

    master vs my branch

  9. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    Ok and would you use Osmosis or Osmium for the conversion? Osmosis seems easier to use as we could simply pull it in via maven with test scope?

  10. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    I'd strongly favor Osmosis as it would be very easy to integrate into our unittests. My first idea was to replace all XML paths with calls to a converter utility:

            final GraphHopper gh = createGraphHopper("car").
                    setStoreOnFlush(true).
                    setGraphHopperLocation(ghLoc).
                    setDataReaderFile(OSMConverter.toPbf("../core/files/monaco.osm.xml"));
            gh.importOrLoad();

    If we generate the resulting pbfs files within target and avoid recreating them(timestamp check?) we should be fine.

  11. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor
    OSMConverter.toPbf("../core/files/monaco.osm.xml")
    

    Yes that seems like a good approach. Did you try this yet? I can try if you haven't already.

  12. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    The class i used for my tests:

    public class OSMConverter {
    
        private OSMConverter() {
        }
        
        public static String toPbf(String path) {
            Path source = Paths.get(path).normalize().toAbsolutePath();
            String sourceFilename = source.getFileName().toString();
            String targetFilename = sourceFilename;
            if (targetFilename.endsWith(".xml")) {
                targetFilename = targetFilename.substring(0, targetFilename.length() - 4);
            }
            targetFilename = targetFilename + ".pbf";
            Path target = source.getParent().resolve(targetFilename);
            
            try {
                convert(source, target);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            
            return target.toString();
        }
        
        private static void convert(Path source, Path target) throws IOException {
            if (!Files.isReadable(source)) {
                throw new FileNotFoundException(source.toString());
            }
            
            try (OutputStream out = Files.newOutputStream(target, CREATE, TRUNCATE_EXISTING);
                            BufferedOutputStream bufferedOut = new BufferedOutputStream(out)) {
                BlockOutputStream blockOut = new BlockOutputStream(bufferedOut);
                OsmosisSerializer serializer = new OsmosisSerializer(blockOut);
                FastXmlReader reader = new FastXmlReader(source.toFile(), true, CompressionMethod.None);
                reader.setSink(serializer);
                reader.run();
            }
        }
    }
  13. otbutz commented on Jan 5, 2021

    @otbutz
    Contributor

    We could add a check based on Files.getLastModifiedTime() to avoid converting the files on each test run but i'm not sure if it's worth the hassle.

  14. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    The class i used for my tests:

    Ok thanks!

    We could add a check based on Files.getLastModifiedTime() to avoid converting the files on each test run but i'm not sure if it's worth the hassle.

    Not worth the hassle if you ask me. Unless the conversion is really slow, but I do not think so.

  15. easbar commented on Jan 5, 2021

    @easbar
    MemberAuthor

    Two more things:

    1. FastXmlReader requires the OSM files to contain timestamps (and some more attributes) that we currently do not have in these files. But using XmlReader they are not required. XmlReader is also the one that is used from the command line tool with the --read-xml option.

    2. OSMReaderTest loads the OSM files via the classpath currently while GraphHopperTest loads them via the file system. This must be addressed.

    I tried this here: 883ae3c and its working for osm.xml files in both OSMReaderTest and GraphHopperTest. The converter is still missing support for the .osm.gz files, but that shouldn't be too hard because the osmosis CLI tool can handle these as well. Another problem was that the conversion seems to add/change some of the timestamps. Maybe we are just missing some option to suppress this, not sure.

    However, I am still wondering if this approach is really improving things. Sure we get rid of some XML handling in our reader, but at the same time we are adding the code that does the same via the osmosis dependencies. Not sure I think either way is fine for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions