3

I would like to have the ferry duration [1] taken into account, when transforming OSM files to SQL.

But I'm not sure if I need to write my own waytagresolver, or it is possible to simply add it to the osm_meta tag by adding a parameter or by config? I'm hoping for the latter, as this of cause is the simples by far :)

[1] http://wiki.openstreetmap.org/wiki/Tag:route%3Dferry

3 Answers 3

3

I have succeded in creating a WayTagResolver that parses the duration and puts it into the osm_meta column. I therefore share the solution here, for others to use this or deveelop futher on it.

  1. Download osm2po
  2. In the plugins folder, extract osm2po-plugins-4.7.7-src.zip
  3. Enter the extracted folder, and STAY there :)
  4. Create a java file for storing the WayTagResolver in e.g. de/cm/osm2po/samples/FerryDurationResolver.java - code for this file will be shown below.
  5. Compile the Java file: javac -d . -classpath ../../osm2po-core-4.7.7-signed.jar de/cm/osm2po/samples/FerryDurationResolver.java
  6. Create a new Jar file: jar cf ../osm2po-plugins-4.7.7_custom.jar *
  7. in your osm2po.config file, add wayTagResolver.class=de.cm.osm2po.samples.FerryDurationResolver
  8. When running osm2po (back to main folder), remember to include the newly created jar file in your classpath. I personally execute: java -Xmx 4g -classpath osm2po-core-4.7.7-signed.jar:osm2po-plugins/osm2po-plugins-4.7.7_custom.jar:de.cm.osm2po.Main cmd=tjsp prefix=some_name "url"

And here is the code for my FerryDurationResolver.java: package de.cm.osm2po.samples; import de.cm.osm2po.converter.DefaultWayTagResolver;

public class FerryDurationResolver extends DefaultWayTagResolver  {

    private String metadata;

    @Override
    public void onTags(long entityId) {
        this.metadata = null;
        super.onTags(entityId);
    }

    @Override
    public void onTag(String key, String value) {
        if ("duration".equals(key)){
            String[] durations = value.split(":");
            if (durations.length == 2){
                this.metadata = new Integer(Integer.parseInt(durations[0])*60+Integer.parseInt(durations[1])).toString();
            }
        }
        super.onTag(key, value);
    }

    @Override
    public String getMeta() {
        return this.metadata;
    }

}

What the code does is to read the duration tag and translate the hh:mm into minutes only. Please be aware, that duration MIGHT take other formats: http://wiki.openstreetmap.org/wiki/Key:duration

I hope I haven't missed anything, and this could be useful for others, if they want to import duration (or other variables) into the osm_meta field of the SQL output.

3

The FerryDurationResolver is absolutely correct. Good work! But you can make things easier. I assume you are using Eclipse. Then just create a new project. Add osm2po.jar as library and create a new Run-Config. Develop the FerryDurationResolver as usual. Debugging is possible.

enter image description hereenter image description here

When everything works as expected, just create a jar-file using Eclipse's "Export Jar-File" option. Finally copy the created jar into the plugins-folder and modify the config to wayTagResolver.class=de.cm.osm2po.samples.FerryDurationResolver. Alternatively you can even set this parameter on the command line where it overrides the default.

2
  • Thank you for the suggestion, that is even simpler :) I have one problem right now though, the output SQL file contains about 2 million road segments, while before it contained only approximately 650.000 segments. I haven't had the time to figure out why yet. It also happens for "my" suggestion. Do you have any simple clue? Commented Sep 1, 2013 at 9:07
  • Never mind, it was a problem with another nodetagresolver I've been playing around with :) Commented Sep 2, 2013 at 6:12
1

Theoretically it is possible to let osm2po handle timetables, even for ferries. Everything is prepared for it. Nevertheless, you need some basic understanding of the Java-API. The default config is shipped with a very pragmatic approach. Namely, givin a ferry the same impact as e.g. an ordinary street in shape of its maxSpeed. The duration, your are asking for is sth. very dynamic. It may be assigned to a way as well as to a relation. The latter may make things very complicated. But as I said before, almost everything is possible. Please ask me via mail.

1
  • Okay, let me clarify, that I do not need the duration for performing routing, neither that osm2po can handle the timetables. For my purpose, I only need to get the content of the duration variable into SQL so I can fetch it into my PostgreSQL DB. I saw the osm_meta variable, which seems to empty, so hoped it might be possible to easy fill it with the duration string :) I will look at the API and contact you via mail. Thanks so far! Commented Aug 29, 2013 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.