Skip to content

Commit

Permalink
add command-line flag --output and closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
rovaniemi committed Jun 26, 2017
1 parent ccab791 commit 8a06f0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Java program that parses OSM XML files into a json graph representation.

## Example

In output file you will have nodes and edges (highways in real world) with weight (unit: `centimetres`).
In output file you will have nodes and edges with weight (unit: `centimetres`).
```json
[
{
Expand Down Expand Up @@ -53,21 +53,32 @@ In output file you will have nodes and edges (highways in real world) with weigh
i = main array index, if the i is 0 it refers to the first element in the array
w = weight
```

### Command-line flags

```
-f, --files osm files to be parsed
-i, --includeWays way tags to include
-e, --excludeWays way tags to exclude (overrides includeWay)
-o, --output output file name
-q, --quiet suppress console output
```
## Getting Started
1. [Download](https://github.com/rovaniemi/osm-graph-parser/releases) latest version under the releases tab.
2. [Download](https://www.openstreetmap.org/) openstreetmap data. (click export, select area, and then export)
3. Make directory called `map` in the directory where you have downloaded .jar file.
4. Name osm file to map-01.osm, if you have multiple osm files name them like `map-01.map`, `map-02.map` etc. and put them in map directory.
5. Now your tree should look like this. ![img](http://imgur.com/ntvFUQN.png)
6. Run the jar file. (terminal `java -jar <jar-file-name>.jar`)
7. Now you have `graph.json` in the same directory where the .jar file is.
3. Use -f flag to select osm files.
4. Use -i flag to select ways. You can view osm map features [here.](http://wiki.openstreetmap.org/wiki/Map_Features)
5. Use -o flag to defining output file name.
6. Run the jar file. (terminal `java -jar <jar-file-name>.jar -f files -i selected ways -o output file name`)
7. Now you have `json` in the same directory where the .jar file is.
8. If you use this data in your own service read [openstreetmap licence.](https://opendatacommons.org/licenses/odbl/1.0/)
### Possible errors
1. Parsing take much time or program crash.
- You need more memory for the program. Change java run command to `java -jar -Xmx4096m <jar-file-name>.jar`. That will increase java heap max size to 4gb. You will need 4gb ram for that. If you parse huge map you will need bigger heap size.
- The program does not have enought memory. Use `-Xmx` flag with java. Example run command `java -jar -Xmx4096m <jar-file-name>.jar`. That will increase java heap max size to 4gb. You will need 4gb ram for that.
### Prerequisites
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ apply plugin: 'maven'
apply plugin: 'jacoco'

group = 'com.karlin.osm-graph-parser'
version = '0.12'
version = '0.13'

description = "Java program that parses OSM XML files into a json graph representation.Java program that parses OSM XML files into a json graph representation."

sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
mavenCentral()
maven { url "http://repo.maven.apache.org/maven2" }
maven {
url("https://plugins.gradle.org/m2/")
}
}

task fatJar(type: Jar) {
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/osmparser/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

public class Main {
private static String PROGRAM_NAME = "osmparser";
Expand All @@ -17,6 +14,7 @@ public static void main(String[] args) throws IOException {
Options options = createOptions();

CommandLine cmd = null;

try {
cmd = parser.parse(options, args);
} catch (ParseException ex) {
Expand All @@ -43,8 +41,14 @@ public static void main(String[] args) throws IOException {
info("Excluded tags: " + Arrays.asList(excludedTags));
}

String[] output = cmd.getOptionValues("output");

This comment has been minimized.

Copy link
@cxcorp

cxcorp Jun 26, 2017

Contributor

After changing the option builder call from .hasArgs() to .hasArg(), this should probably be changed to cmd.getOptionValue since only one value is supported anyways.


if(notQuiet && output[0] != null) {
info("Output file name: " + output[0]);
}

try {
createOsmparser(files, includedTags, excludedTags).start();
createOsmparser(files, output, includedTags, excludedTags).start();
} catch (RuntimeException ex) {
if (!(ex.getCause() instanceof IOException)) {
throw ex;
Expand All @@ -71,17 +75,18 @@ private static WayTag[] parseWayTagsFromOptions(String[] optionValues) {
return tags;
}

private static Osmparser createOsmparser(String[] files, WayTag[] includeWays, WayTag[] excludeWays) {
private static Osmparser createOsmparser(String[] files, String[] output, WayTag[] includeWays, WayTag[] excludeWays) {
StreamingXmlGraphParser graphParser = new StreamingXmlGraphParser(includeWays, excludeWays);
return new Osmparser(files, "graph.json", graphParser);
if(output[0] == null) return new Osmparser(files, "graph.json", graphParser);
return new Osmparser(files, output[0], graphParser);
}

private static Options createOptions() {
Options options = new Options();

Option files = Option.builder("f")
.longOpt("files")
.desc("the XML map files to be parsed")
.desc("the osm (xml) map files to be parsed")
.hasArgs()
.required()
.build();
Expand All @@ -107,6 +112,12 @@ private static Options createOptions() {
.build();
options.addOption(excludeWays);

Option output = Option.builder("o")
.longOpt("output")
.desc("output file name")
.hasArgs()

This comment has been minimized.

Copy link
@cxcorp

cxcorp Jun 26, 2017

Contributor

Perhaps .hasArg() is better since we can there can only be one output file:

.build();
options.addOption(output);
return options;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/osmparser/Osmparser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Osmparser {

public Osmparser(String[] mapFiles, String outFile, GraphParser graphParser) {
this.mapFiles = mapFiles;
if(!outFile.endsWith(".json")) outFile += ".json";

This comment has been minimized.

Copy link
@cxcorp

cxcorp Jun 26, 2017

Contributor

It's probably better that we don't force the .json filename extension if the user has left it out.

this.outFile = outFile;
this.parser = graphParser;
}
Expand Down

0 comments on commit 8a06f0d

Please sign in to comment.