Skip to content

Commit

Permalink
Merge pull request #1 from cxcorp/refactorings
Browse files Browse the repository at this point in the history
Refactorings ja streamaava parseri
  • Loading branch information
rovaniemi authored Jun 19, 2017
2 parents 16c2761 + ccef9cf commit e696d0b
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 129 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ You will need a Java Runtime Environment (JRE) to run java programs. You can dow
## Authors

* **Mauri Karlin** - *Owner* - [Rovaniemi](https://github.com/Rovaniemi)
* **Joona Heikkilä** - Optimizations and refactorings - [cxcorp](https://github.com/cxcorp)

## License

Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ repositories { jcenter() }
dependencies { testCompile 'org.mockito:mockito-core:2.8.9' }

dependencies {
compile group: 'com.google.code.gson', name: 'gson', version:'2.8.0'
compile (
'com.google.code.gson:gson:2.8.0',
'com.fasterxml:aalto-xml:1.0.0'
)
testCompile group: 'junit', name: 'junit', version:'4.12'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.util.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Parser {
public class DomXmlGraphParser implements IGraphParser {

private final String filename = "map";

private XmlReader xmlReader;
private final XmlReader xmlReader = new XmlReader();
private final String[] requiredWayTags;
private Graph graph;
private List<String> tags;

public Parser(List<String> tags){
this.xmlReader = new XmlReader();
this.graph = new Graph();
this.tags = tags;
}

public void startParsing() throws IOException {
parseDocumentsToGraph();
new JsonMaker().getNodeJson(this.graph.getGraph(),"graph");
public DomXmlGraphParser(String... requiredWayTags) {
this.requiredWayTags = requiredWayTags;
}

public void parseDocumentsToGraph(){
for (int i = 1; i <= this.xmlReader.howManyDocuments(); i++) {
parseDocument(this.xmlReader.getDocument(this.filename,i));
@Override
public void parseXml(File file, Graph outputGraph) {
this.graph = outputGraph;
Document document = this.xmlReader.getDocument(file);
if (document != null) {
parseDocument(document);
}
}

private void parseDocument(Document document){
private void parseDocument(Document document) {
parseNodes(document.getElementsByTagName("node"));
parseWeights(document.getElementsByTagName("way"));
}
Expand Down Expand Up @@ -60,7 +57,7 @@ private void parseWeights(NodeList nodeList){
org.w3c.dom.Node way = nodeList.item(i);
if (way.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
Element wayElement = (Element) way;
if(containsTags(wayElement, this.tags)){
if (containsRequiredTags(wayElement)){
addEdges(getIds(wayElement.getElementsByTagName("nd")));
}
}
Expand All @@ -80,7 +77,7 @@ private List<Long> getIds(NodeList nodeList){
return nodeIds;
}

private boolean containsTags(Element wayElement, List<String> tags){
private boolean containsRequiredTags(Element wayElement){
Set<String> set = new HashSet<>();
NodeList tagList = wayElement.getElementsByTagName("tag");
final int tagListLenght = tagList.getLength();
Expand All @@ -91,10 +88,9 @@ private boolean containsTags(Element wayElement, List<String> tags){
set.add(tagElement.getAttribute("k"));
}
}
for (int i = 0; i < tags.size(); i++) {
if (!set.contains(tags.get(i))){
for (int i = 0; i < requiredWayTags.length; i++) {
if (!set.contains(requiredWayTags[i])){
return false;

}
}
return true;
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/osmparser/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@

public class Graph {

public final static double AVERAGE_RADIUS_OF_EARTH_CM = 6371 * 1000 * 100;
private Map<Long, Node> graph;
private final static double AVERAGE_RADIUS_OF_EARTH_CM = 6371 * 1000 * 100;
private final Map<Long, Node> graph;

public Graph() {
this.graph = new HashMap<>();
}

public void addNode(Node node){
public void addNode(Node node) {
this.graph.put(node.getId(), node);
}

public void addEdge(long from, long to){
if(this.graph.containsKey(from) && this.graph.containsKey(to)){
Node fromN = this.graph.get(from);
Node toN = this.graph.get(to);
long distance = distance(fromN.getLat(), fromN.getLon(), toN.getLat(), toN.getLon());
this.graph.get(from).addEdge(to, distance);
public void addEdge(long fromId, long toId) {
Node from = this.graph.get(fromId);
Node to = this.graph.get(toId);
if (from != null && to != null) {
long distance = distanceOnEarth(from.getLat(), from.getLon(), to.getLat(), to.getLon());
from.addEdgeTo(toId, distance);
}
}

public Map<Long, Node> getGraph() {
public Map<Long, Node> getNodesWithEdges() {
Map<Long,Node> map = new HashMap<>();
for (Long l:graph.keySet()) {
if(this.graph.get(l).haveEdges()){
map.put(l, this.graph.get(l));
for (Map.Entry<Long, Node> entry : graph.entrySet()) {
if (entry.getValue().haveEdges()) {
map.put(entry.getKey(), entry.getValue());
}
}
return map;
}

public int distance(double lat1, double lon1, double lat2, double lon2) {
private int distanceOnEarth(double lat1, double lon1, double lat2, double lon2) {

double latDistance = Math.toRadians(lat1 - lat2);
double lngDistance = Math.toRadians(lon1 - lon2);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/osmparser/IGraphParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package osmparser;

import java.io.File;

public interface IGraphParser {
void parseXml(File file, Graph outputGraph);
}
57 changes: 57 additions & 0 deletions src/main/java/osmparser/IdNormalizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package osmparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IdNormalizer {
private Map<Long, Long> oldIdToNewId;
private Map<Long, Node> newGraph;
private Map<Long, Node> oldGraph;
private long nextId;

public List<Node> normalizeIds(Map<Long, Node> graph) {
reset();
oldGraph = graph;
createNodesWithNewIds();
addNewEdges();
return new ArrayList<>(newGraph.values());
}

private void reset() {
oldIdToNewId = new HashMap<>();
newGraph = new HashMap<>();
nextId = 0;
}

private void createNodesWithNewIds() {
for (Map.Entry<Long, Node> entry : oldGraph.entrySet()) {
Node n = entry.getValue();
long newId = newId();
newGraph.put(newId, new Node(newId, n.getLat(), n.getLon()));
oldIdToNewId.put(entry.getKey(), newId);
}
}

private void addNewEdges() {
for (Map.Entry<Long, Node> entry : oldGraph.entrySet()) {
Node oldNode = entry.getValue();
Node newNode = newGraph.get(oldIdToNewId.get(entry.getKey()));
addEdges(oldNode, newNode);
}
}

private void addEdges(Node oldNode, Node newNode) {
Set<Weight> weights = oldNode.getEdges();
for (Weight weight : weights) {
Long newTargetId = oldIdToNewId.get(weight.getId());
newNode.addEdgeTo(newTargetId, weight.getWeight());
}
}

private long newId() {
return nextId++;
}
}
53 changes: 0 additions & 53 deletions src/main/java/osmparser/JsonMaker.java

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/java/osmparser/MapFileDiscoverer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package osmparser;

import java.io.File;
import java.io.FilenameFilter;

public class MapFileDiscoverer {

public static File[] discover(String directory, String mapFilePrefix) {
return new File(directory).listFiles(filterPrefixedOsmFiles(mapFilePrefix));
}

private static FilenameFilter filterPrefixedOsmFiles(final String filePrefix) {
if (filePrefix != null) {
return new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(filePrefix) && name.endsWith(".osm");
}
};
} else {
return new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".osm");
}
};
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/osmparser/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Node(long id, double lat, double lon) {
this.edges = new HashSet<>();
}

public void addEdge(long id, long distance){
public void addEdgeTo(long id, long distance){
this.edges.add(new Weight(id, distance));
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/osmparser/NormalizedGraphDeepComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package osmparser;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class NormalizedGraphDeepComparator implements Comparator<List<Node>> {

@Override
public int compare(List<Node> o1, List<Node> o2) {
if (o1.size() != o2.size()) return o1.size() - o2.size();
for (int i = 0; i < o1.size(); i++) {
int nodeC = compareNode(o1.get(i), o2.get(i));
if (nodeC != 0) return nodeC;
}
return 0;
}

private int compareNode(Node o1, Node o2) {
if (o1.getId() != o2.getId()) return (int) (o1.getId() - o2.getId());
Set<Weight> o1Edges = o1.getEdges();
Set<Weight> o2Edges = o2.getEdges();
if (o1Edges.size() != o2Edges.size()) return o1Edges.size() - o2Edges.size();
Iterator o1Iterator = o1Edges.iterator();
Iterator o2Iterator = o2Edges.iterator();
while (o1Iterator.hasNext()) {
Weight o1Weight = (Weight) o1Iterator.next();
Weight o2Weight = (Weight) o2Iterator.next();
if (o1Weight.getId() != o2Weight.getId())
return (int) (o1Weight.getId() - o2Weight.getId());
if (o1Weight.getWeight() != o2Weight.getWeight())
return (int) (o1Weight.getWeight() - o2Weight.getWeight());
}
return 0;
}
}
Loading

0 comments on commit e696d0b

Please sign in to comment.