From 16c276129bfedb14d7992143c271eb7410909be3 Mon Sep 17 00:00:00 2001 From: Rovaniemi Date: Mon, 19 Jun 2017 19:27:38 +0300 Subject: [PATCH] java 1.8 to 1.7 because of cs.helsinki ukko cluster is old aff --- .gitignore | 5 ++++- README.md | 4 ++++ build.gradle | 16 ++++++++++----- map/.gitkeep | 0 src/main/java/osmparser/Graph.java | 6 +++++- src/main/java/osmparser/JsonMaker.java | 2 +- src/main/java/osmparser/XmlReader.java | 14 ++++++------- src/test/java/omsparser/JsonMakerTest.java | 14 ------------- src/test/java/omsparser/OsmparserTest.java | 24 ---------------------- 9 files changed, 32 insertions(+), 53 deletions(-) delete mode 100644 map/.gitkeep diff --git a/.gitignore b/.gitignore index a201fae..02b8c64 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,7 @@ gradle-app.setting !gradle-wrapper.jar # Cache of project -.gradletasknamecache \ No newline at end of file +.gradletasknamecache + +# Profiler +*.hprof diff --git a/README.md b/README.md index ebd855e..8f4b05e 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,10 @@ In output file you will have nodes and edges (highways in real world) with weigh 7. Now you have `graph.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 crash. + - You need more memory for the program. Change java run command to `java -jar -Xmx4096m .jar`. That will increase java heap max size to 4gb. You will need 4gb ram for that. ### Prerequisites diff --git a/build.gradle b/build.gradle index 39c0059..2627b78 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,10 @@ apply plugin: 'java' +jar { + manifest { + attributes 'Main-Class': 'osmparser.Osmparser' + } +} + apply plugin: 'maven' apply plugin: 'jacoco' @@ -7,8 +13,8 @@ version = '0.1' 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.8 -targetCompatibility = 1.8 +sourceCompatibility = 1.7 +targetCompatibility = 1.7 repositories { mavenCentral() @@ -17,9 +23,9 @@ repositories { task fatJar(type: Jar) { manifest { - attributes 'Implementation-Title': 'Gradle Jar File Example', - 'Implementation-Version': version, - 'Main-Class': 'com.karlin.osm-graph-parser.Osmparser' + attributes 'Implementation-Title': 'osm-graph-parser', + 'Implementation-Version': '0.1', + 'Main-Class': 'osmparser.Osmparser' } baseName = project.name + '-all' from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } diff --git a/map/.gitkeep b/map/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/osmparser/Graph.java b/src/main/java/osmparser/Graph.java index 251c47b..0d14603 100644 --- a/src/main/java/osmparser/Graph.java +++ b/src/main/java/osmparser/Graph.java @@ -26,7 +26,11 @@ public void addEdge(long from, long to){ public Map getGraph() { Map map = new HashMap<>(); - graph.keySet().stream().filter(e -> this.graph.get(e).haveEdges()).forEach(e -> map.put(e, this.graph.get(e))); + for (Long l:graph.keySet()) { + if(this.graph.get(l).haveEdges()){ + map.put(l, this.graph.get(l)); + } + } return map; } diff --git a/src/main/java/osmparser/JsonMaker.java b/src/main/java/osmparser/JsonMaker.java index 974dcb3..ecf45cf 100644 --- a/src/main/java/osmparser/JsonMaker.java +++ b/src/main/java/osmparser/JsonMaker.java @@ -47,7 +47,7 @@ public void getNodeJson(Map graph, String filename) throws IOExceptio Writer writer = new FileWriter(filename + ".json"); List list = convertIds(graph); Collections.sort(list); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); + Gson gson = new GsonBuilder().create(); gson.toJson(list, writer); } } diff --git a/src/main/java/osmparser/XmlReader.java b/src/main/java/osmparser/XmlReader.java index 5511b56..5bc56e7 100644 --- a/src/main/java/osmparser/XmlReader.java +++ b/src/main/java/osmparser/XmlReader.java @@ -8,18 +8,18 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; public class XmlReader { public long howManyDocuments(){ - try{ - return Files.list(Paths.get("map/")).filter(n -> n.toString().contains("map-") && n.toString().contains(".osm")).count(); - } catch (IOException e) { - e.printStackTrace(); + File[] files = new File("map/").listFiles(); + int counter = 0; + for (int i = 0; i < files.length; i++) { + if(files[i].getName().startsWith("map-") && files[i].getName().endsWith(".osm")){ + counter++; + } } - return 0; + return counter; } public Document getDocument(String documentName, int i){ diff --git a/src/test/java/omsparser/JsonMakerTest.java b/src/test/java/omsparser/JsonMakerTest.java index e4ace8c..0ef404a 100644 --- a/src/test/java/omsparser/JsonMakerTest.java +++ b/src/test/java/omsparser/JsonMakerTest.java @@ -34,20 +34,6 @@ public void setUp() { public void tearDown() { } - @Test - public void methodCreateJsonFile(){ - JsonMaker maker = new JsonMaker(); - Map j = new HashMap<>(); - long count = 0; - try{ - maker.getNodeJson(j,"test"); - count = Files.list(Paths.get("")).filter(n -> n.toString().contains("test.json")).count(); - } catch (IOException e) { - e.printStackTrace(); - } - Assert.assertEquals(1, count); - } - @Test public void convertIdsWorks(){ JsonMaker maker = new JsonMaker(); diff --git a/src/test/java/omsparser/OsmparserTest.java b/src/test/java/omsparser/OsmparserTest.java index f2f814e..a8cc5e6 100644 --- a/src/test/java/omsparser/OsmparserTest.java +++ b/src/test/java/omsparser/OsmparserTest.java @@ -35,28 +35,4 @@ public void setUp() { public void tearDown() { } - @Test - public void masterTest() throws IOException { - Osmparser osmparser = new Osmparser(); - osmparser.main(new String[]{""}); - Assert.assertEquals(1, Files.list(Paths.get("")).filter(n -> n.toString().contains("graph.json")).count()); - } - - @Test - public void masterTestWithFile() throws IOException { - Charset utf8 = StandardCharsets.UTF_8; - List lines = Arrays.asList("\n" + - "\n" + - " \n" + - "\n"); - try { - Files.write(Paths.get("map/map-01.osm"), lines, utf8); - } catch (IOException e) { - e.printStackTrace(); - } - Osmparser osmparser = new Osmparser(); - osmparser.main(new String[]{""}); - Assert.assertEquals(1, Files.list(Paths.get("")).filter(n -> n.toString().contains("graph.json")).count()); - } - }