Browse Source

Handling relations

- Pending : Next round processing
pull/4/head
LO Kam Tao Leo 3 years ago
parent
commit
7e22297ed1
  1. 7
      pom.xml
  2. 89
      src/main/java/org/leolo/map/osm/extract/ExtractElement.java

7
pom.xml

@ -51,11 +51,6 @@
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.openstreetmap.pbf</groupId>
<artifactId>osmpbf</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
@ -68,7 +63,7 @@
<dependency>
<groupId>de.topobyte</groupId>
<artifactId>osm4j-pbf</artifactId>
<version>0.1.0</version>
<version>1.2.0</version>
</dependency>
</dependencies>
<repositories>

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

@ -1,8 +1,13 @@
package org.leolo.map.osm.extract;
import crosby.binary.BinaryParser;
import crosby.binary.Osmformat;
import crosby.binary.file.BlockInputStream;
import de.topobyte.osm4j.core.access.DefaultOsmHandler;
import de.topobyte.osm4j.core.access.OsmInputException;
import de.topobyte.osm4j.core.access.OsmIterator;
import de.topobyte.osm4j.core.access.OsmReader;
import de.topobyte.osm4j.core.model.iface.OsmRelation;
import de.topobyte.osm4j.core.model.iface.OsmRelationMember;
import de.topobyte.osm4j.core.model.iface.OsmTag;
import de.topobyte.osm4j.pbf.seq.PbfReader;
import org.apache.commons.cli.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -137,8 +142,11 @@ public class ExtractElement {
actionFile = InputUtil.getInputFile(actionFilePath);
log.atInfo().log("Database file size {}MB", dbFile.length()/1_000_000);
log.atInfo().log("Action file size {}kB", actionFile.length()/1_000);
doExtract(dbFile, actionFile, outputStream);
try {
doExtract(dbFile, actionFile, outputStream);
}catch(IOException e){
log.atError().withThrowable(e).log("Exception when extracting the data!");
}
} catch(MissingOptionException|MissingArgumentException moe) {
System.out.println("FATAL ERROR : "+moe.getMessage());
printHelp(1);
@ -152,7 +160,7 @@ public class ExtractElement {
}
}
private void doExtract(InputFile dbFile, InputFile actionFile, PrintStream outputStream) {
private void doExtract(InputFile dbFile, InputFile actionFile, PrintStream outputStream) throws IOException{
//Step 1: parse the actionFile
ActionFile af;
try {
@ -162,10 +170,20 @@ public class ExtractElement {
System.exit(3);
return;
}
for(ExtractItem ei:af.getRelation()){
pendingRelations.add(ei.getId());
}
for(ExtractItem ei:af.getWay()){
pendingWays.add(ei.getId());
}
for(ExtractItem ei:af.getNode()){
pendingNodes.add(ei.getId());
}
doSearch(dbFile, af);
expandRelations(dbFile);
expandRelations(dbFile, 1);
processWay(dbFile);
processNode(dbFile);
log.atInfo().log("Done!");
}
/**
@ -173,23 +191,70 @@ public class ExtractElement {
* @param dbFile The database file going to be searched
* @param af The file which defines the action to be performed
*/
private void doSearch(InputFile dbFile, ActionFile af) {
private void doSearch(InputFile dbFile, ActionFile af) throws IOException{
//Placeholder
}
private void processNode(InputFile dbFile) {
private void processNode(InputFile dbFile) throws IOException{
if(pendingNodes.isEmpty())
return;
}
private void processWay(InputFile dbFile) {
private void processWay(InputFile dbFile) throws IOException{
if(pendingWays.isEmpty())
return;
}
private void expandRelations(InputFile dbFile) {
if(pendingRelations.isEmpty())
private void expandRelations(InputFile dbFile, int round) throws IOException{
if(pendingRelations.isEmpty()) {
log.atInfo().log("No relation to be expanded");
return;
}
HashSet<Long> foundRelations = new HashSet<>();
HashSet<Long> nextRound = new HashSet<>();
log.atInfo().log("Expanding relations round {} with {} relation(s)", round, pendingRelations.size());
OsmReader reader = new PbfReader(dbFile.getInputStream(), true);
reader.setHandler(new DefaultOsmHandler() {
@Override
public void handle(OsmRelation relation) throws IOException {
super.handle(relation);
if(pendingRelations.contains(relation.getId())){
log.atDebug().log("Relation {} found with {} member(s) and {} tag(s).",
relation.getId(),
relation.getNumberOfMembers(),
relation.getNumberOfTags()
);
Relation rel = new Relation();
for(int i=0;i<relation.getNumberOfMembers();i++){
OsmRelationMember orm = relation.getMember(i);
switch (orm.getType()){
case Relation -> nextRound.add(orm.getId());
case Way -> pendingWays.add(orm.getId());
case Node -> pendingNodes.add(orm.getId());
}
rel.addMember(orm.getId());
}
for(int i=0;i<relation.getNumberOfTags();i++){
OsmTag tag = relation.getTag(i);
log.atDebug().log("{}->{}", tag.getKey(), tag.getValue());
rel.putTag(tag.getKey(), tag.getValue());
}
allEntry.put(relation.getId(), rel);
relations.put(relation.getId(), rel);
}
}
@Override
public void complete() throws IOException {
super.complete();
log.atInfo().log("Finish reading round {} when expanding relation", round);
}
});
try {
reader.read();
} catch (OsmInputException e) {
log.atError().withThrowable(e).log("Error when reading the input!");
}
}
private void printHelp(int returnValue){

Loading…
Cancel
Save