Browse Source

Handling way and nodes

pull/4/head
LO Kam Tao Leo 3 years ago
parent
commit
a920051582
  1. 69
      src/main/java/org/leolo/map/osm/extract/ExtractElement.java

69
src/main/java/org/leolo/map/osm/extract/ExtractElement.java

@ -4,9 +4,7 @@ import de.topobyte.osm4j.core.access.DefaultOsmHandler;
import de.topobyte.osm4j.core.access.OsmInputException; import de.topobyte.osm4j.core.access.OsmInputException;
import de.topobyte.osm4j.core.access.OsmIterator; import de.topobyte.osm4j.core.access.OsmIterator;
import de.topobyte.osm4j.core.access.OsmReader; import de.topobyte.osm4j.core.access.OsmReader;
import de.topobyte.osm4j.core.model.iface.OsmRelation; import de.topobyte.osm4j.core.model.iface.*;
import de.topobyte.osm4j.core.model.iface.OsmRelationMember;
import de.topobyte.osm4j.core.model.iface.OsmTag;
import de.topobyte.osm4j.pbf.seq.PbfReader; import de.topobyte.osm4j.pbf.seq.PbfReader;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -198,11 +196,75 @@ public class ExtractElement {
private void processNode(InputFile dbFile) throws IOException{ private void processNode(InputFile dbFile) throws IOException{
if(pendingNodes.isEmpty()) if(pendingNodes.isEmpty())
return; return;
log.atInfo().log("Processing {} node(s)", pendingNodes.size());
OsmReader reader = new PbfReader(dbFile.getInputStream(), true);
reader.setHandler(new DefaultOsmHandler() {
@Override
public void handle(OsmNode node) throws IOException {
super.handle(node);
if(pendingNodes.contains(node.getId())){
log.atDebug().log("Found node #{}", node.getId());
Node n = new Node(node.getLatitude(), node.getLongitude());
for(int i=0;i<node.getNumberOfTags();i++){
OsmTag t = node.getTag(i);
log.atDebug().log("{}->{}", t.getKey(), t.getValue());
n.putTag(t.getKey(), t.getValue());
}
allEntry.put(node.getId(), n);
nodes.put(node.getId(), n);
}
}
@Override
public void complete() throws IOException {
super.complete();
log.atInfo().log("Finish reading the DB file");
}
});
try {
reader.read();
} catch (OsmInputException e) {
log.atError().withThrowable(e).log("Error when reading the input!");
}
} }
private void processWay(InputFile dbFile) throws IOException{ private void processWay(InputFile dbFile) throws IOException{
if(pendingWays.isEmpty()) if(pendingWays.isEmpty())
return; return;
log.atInfo().log("Processing {} way(s)", pendingWays.size());
OsmReader reader = new PbfReader(dbFile.getInputStream(), true);
reader.setHandler(new DefaultOsmHandler() {
@Override
public void handle(OsmWay w) throws IOException {
super.handle(w);
if(pendingWays.contains(w.getId())){
Way way = new Way();
log.atDebug().log("Found way #{}", w.getId());
for(int i=0;i<w.getNumberOfNodes();i++){
way.addMember(w.getNodeId(i));
pendingNodes.add(w.getNodeId(i));
}
for(int i=0;i<w.getNumberOfTags();i++){
OsmTag tag = w.getTag(i);
log.atDebug().log("{}->{}", tag.getKey(), tag.getValue());
way.putTag(tag.getKey(), tag.getValue());
}
allEntry.put(w.getId(), way);
ways.put(w.getId(), way);
}
}
@Override
public void complete() throws IOException {
super.complete();
log.atInfo().log("Finish reading the DB file");
}
});
try {
reader.read();
} catch (OsmInputException e) {
log.atError().withThrowable(e).log("Error when reading the input!");
}
} }
private void expandRelations(InputFile dbFile,final int round) throws IOException{ private void expandRelations(InputFile dbFile,final int round) throws IOException{
@ -210,7 +272,6 @@ public class ExtractElement {
log.atInfo().log("No relation to be expanded"); log.atInfo().log("No relation to be expanded");
return; return;
} }
HashSet<Long> foundRelations = new HashSet<>();
HashSet<Long> nextRound = new HashSet<>(); HashSet<Long> nextRound = new HashSet<>();
log.atInfo().log("Expanding relations round {} with {} relation(s)", round, pendingRelations.size()); log.atInfo().log("Expanding relations round {} with {} relation(s)", round, pendingRelations.size());
OsmReader reader = new PbfReader(dbFile.getInputStream(), true); OsmReader reader = new PbfReader(dbFile.getInputStream(), true);

Loading…
Cancel
Save