Browse Source

Performing search

develop^2
LO Kam Tao Leo 3 years ago
parent
commit
9be4e2aadc
  1. 64
      src/main/java/org/leolo/map/osm/extract/ExtractElement.java
  2. 6
      src/main/java/org/leolo/map/osm/extract/format/SimpleStringSearchProvider.java
  3. 13
      src/main/java/org/leolo/map/osm/extract/model/ActionFile.java
  4. 42
      src/main/java/org/leolo/map/osm/extract/model/SearchItem.java

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

@ -1,9 +1,6 @@
package org.leolo.map.osm.extract; package org.leolo.map.osm.extract;
import de.topobyte.osm4j.core.access.DefaultOsmHandler; import de.topobyte.osm4j.core.access.*;
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.*; import de.topobyte.osm4j.core.model.iface.*;
import de.topobyte.osm4j.pbf.seq.PbfReader; import de.topobyte.osm4j.pbf.seq.PbfReader;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
@ -168,6 +165,7 @@ public class ExtractElement {
System.exit(3); System.exit(3);
return; return;
} }
doSearch(dbFile, af);
for(ExtractItem ei:af.getRelation()){ for(ExtractItem ei:af.getRelation()){
pendingRelations.add(ei.getId()); pendingRelations.add(ei.getId());
} }
@ -177,7 +175,6 @@ public class ExtractElement {
for(ExtractItem ei:af.getNode()){ for(ExtractItem ei:af.getNode()){
pendingNodes.add(ei.getId()); pendingNodes.add(ei.getId());
} }
doSearch(dbFile, af);
expandRelations(dbFile, 1); expandRelations(dbFile, 1);
processWay(dbFile); processWay(dbFile);
processNode(dbFile); processNode(dbFile);
@ -190,7 +187,62 @@ public class ExtractElement {
* @param af The file which defines the action to be performed * @param af The file which defines the action to be performed
*/ */
private void doSearch(InputFile dbFile, ActionFile af) throws IOException{ private void doSearch(InputFile dbFile, ActionFile af) throws IOException{
//Placeholder if(af.getSearches().isEmpty()){
return;
}
OsmReader reader = new PbfReader(dbFile.getInputStream(), true);
reader.setHandler(new OsmHandler() {
@Override
public void handle(OsmBounds osmBounds) throws IOException {
}
@Override
public void handle(OsmNode osmNode) throws IOException {
for(SearchItem si:af.getSearches()){
if(si.matchNode(osmNode)){
log.atInfo().log("Matched Node {}", osmNode.getId());
ExtractItem ei = new ExtractItem();
ei.setId(osmNode.getId());
af.addNode(ei);
}
}
}
@Override
public void handle(OsmWay osmWay) throws IOException {
for(SearchItem si:af.getSearches()){
if(si.matchWay(osmWay)){
log.atInfo().log("Matched Way {}", osmWay.getId());
ExtractItem ei = new ExtractItem();
ei.setId(osmWay.getId());
af.addWay(ei);
}
}
}
@Override
public void handle(OsmRelation osmRelation) throws IOException {
for(SearchItem si:af.getSearches()){
if(si.matchRelation(osmRelation)){
log.atInfo().log("Matched Relation {}", osmRelation.getId());
ExtractItem ei = new ExtractItem();
ei.setId(osmRelation.getId());
af.addRelation(ei);
}
}
}
@Override
public void complete() throws IOException {
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 processNode(InputFile dbFile) throws IOException{ private void processNode(InputFile dbFile) throws IOException{

6
src/main/java/org/leolo/map/osm/extract/format/SimpleStringSearchProvider.java

@ -9,7 +9,7 @@ public class SimpleStringSearchProvider extends SearchItem {
private String searchKey; private String searchKey;
@Override @Override
protected boolean matchString(String target) { public boolean matchString(String target) {
return searchKey.equalsIgnoreCase(target.strip()); return searchKey.equalsIgnoreCase(target.strip());
} }
@ -17,4 +17,8 @@ public class SimpleStringSearchProvider extends SearchItem {
public void setSearchKey(String searchKey) { public void setSearchKey(String searchKey) {
this.searchKey = searchKey.strip(); this.searchKey = searchKey.strip();
} }
@Override public String toString(){
return "SimpleString["+searchKey+"]";
}
} }

13
src/main/java/org/leolo/map/osm/extract/model/ActionFile.java

@ -122,6 +122,7 @@ public class ActionFile {
try { try {
SearchItem si = (SearchItem) clazz.getConstructor().newInstance(); SearchItem si = (SearchItem) clazz.getConstructor().newInstance();
si.setSearchKey(child.getTextContent()); si.setSearchKey(child.getTextContent());
si.setActionFile(this);
this.searches.add(si); this.searches.add(si);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -162,4 +163,16 @@ public class ActionFile {
if(!way.containsKey(ei.getId())) if(!way.containsKey(ei.getId()))
way.put(ei.getId(), ei); way.put(ei.getId(), ei);
} }
public String getBaseLanguage() {
return baseLanguage;
}
public HashSet<String> getOtherLanguage() {
return otherLanguage;
}
public Vector<SearchItem> getSearches() {
return searches;
}
} }

42
src/main/java/org/leolo/map/osm/extract/model/SearchItem.java

@ -1,33 +1,67 @@
package org.leolo.map.osm.extract.model; package org.leolo.map.osm.extract.model;
import de.topobyte.osm4j.core.model.iface.OsmEntity;
import de.topobyte.osm4j.core.model.iface.OsmNode; import de.topobyte.osm4j.core.model.iface.OsmNode;
import de.topobyte.osm4j.core.model.iface.OsmRelation; import de.topobyte.osm4j.core.model.iface.OsmRelation;
import de.topobyte.osm4j.core.model.iface.OsmWay; import de.topobyte.osm4j.core.model.iface.OsmWay;
import java.util.HashSet;
import java.util.Set;
public abstract class SearchItem { public abstract class SearchItem {
private ActionFile af; private ActionFile af;
private HashSet<String> tagName = new HashSet<>();
public SearchItem(ActionFile af){ public SearchItem(ActionFile af){
this.af = af; this.af = af;
setTags();
} }
public SearchItem(){ public SearchItem(){
//Empty constructor //Empty constructor
} }
protected abstract boolean matchString(String target); protected final void setActionFile(ActionFile af){
this.af = af;
setTags();
}
private void setTags(){
tagName.add("name:"+af.getBaseLanguage());
for(String lang:af.getOtherLanguage()){
tagName.add("name:"+lang);
}
}
public abstract boolean matchString(String target);
public abstract void setSearchKey(String searchKey); public abstract void setSearchKey(String searchKey);
public boolean matchNode(OsmNode node){ private boolean matchEntity(OsmEntity entity){
for(int i=0;i<entity.getNumberOfTags();i++){
if("name".equalsIgnoreCase(entity.getTag(i).getKey()) && matchString(entity.getTag(i).getValue())){
return true;
}
if(af != null){
for(String tag:tagName){
if(tag.equalsIgnoreCase(entity.getTag(i).getKey()) && matchString(entity.getTag(i).getValue())){
return true;
}
}
}
}
return false; return false;
} }
public boolean matchNode(OsmNode node){
return matchEntity(node);
}
public boolean matchWay(OsmWay way){ public boolean matchWay(OsmWay way){
return false; return matchEntity(way);
} }
public boolean matchRelation(OsmRelation relation){ public boolean matchRelation(OsmRelation relation){
return false; return matchEntity(relation);
} }
} }

Loading…
Cancel
Save