add indexing address

git-svn-id: https://osmand.googlecode.com/svn/trunk@63 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-05-17 14:35:52 +00:00
parent 86395f8bc9
commit 1f256f2bfd
9 changed files with 92 additions and 21 deletions

View file

@ -38,7 +38,7 @@ public class Region extends MapObject<Entity> {
} }
public Region(){ public Region(){
this.name = "Region"; name = "Region";
} }
public OsmBaseStorage getStorage() { public OsmBaseStorage getStorage() {

View file

@ -35,7 +35,7 @@ import com.osmand.osm.OSMSettings;
import com.osmand.osm.Way; import com.osmand.osm.Way;
import com.osmand.osm.OSMSettings.OSMTagKey; import com.osmand.osm.OSMSettings.OSMTagKey;
import com.osmand.osm.io.IOsmStorageFilter; import com.osmand.osm.io.IOsmStorageFilter;
import com.osmand.osm.io.OSMStorageWriter; import com.osmand.osm.io.OsmStorageWriter;
import com.osmand.osm.io.OsmBaseStorage; import com.osmand.osm.io.OsmBaseStorage;
@ -115,7 +115,7 @@ public class DataExtraction {
// TODO add interested objects // TODO add interested objects
List<Long> interestedObjects = new ArrayList<Long>(); List<Long> interestedObjects = new ArrayList<Long>();
if (writeTestOsmFile != null) { if (writeTestOsmFile != null) {
OSMStorageWriter writer = new OSMStorageWriter(); OsmStorageWriter writer = new OsmStorageWriter();
OutputStream output = new FileOutputStream(writeTestOsmFile); OutputStream output = new FileOutputStream(writeTestOsmFile);
if (writeTestOsmFile.endsWith(".bz2")) { if (writeTestOsmFile.endsWith(".bz2")) {
output.write('B'); output.write('B');

View file

@ -13,7 +13,7 @@ import org.apache.tools.bzip2.CBZip2OutputStream;
import com.osmand.data.Amenity; import com.osmand.data.Amenity;
import com.osmand.data.Region; import com.osmand.data.Region;
import com.osmand.osm.io.OSMStorageWriter; import com.osmand.osm.io.OsmStorageWriter;
public class DataIndexBuilder { public class DataIndexBuilder {
@ -64,14 +64,14 @@ public class DataIndexBuilder {
} }
OutputStream output = checkFile("POI/"+region.getName()+".osm"); OutputStream output = checkFile("POI/"+region.getName()+".osm");
try { try {
OSMStorageWriter writer = new OSMStorageWriter(); OsmStorageWriter writer = new OsmStorageWriter();
writer.saveStorage(output, region.getStorage(), interestedObjects, false); writer.saveStorage(output, region.getStorage(), interestedObjects, false);
} finally { } finally {
output.close(); output.close();
} }
output = checkFile("POI/"+region.getName()+".osmand"); output = checkFile("POI/"+region.getName()+".osmand");
try { try {
OSMStorageWriter writer = new OSMStorageWriter(); OsmStorageWriter writer = new OsmStorageWriter();
writer.savePOIIndex(output, region); writer.savePOIIndex(output, region);
} finally { } finally {
output.close(); output.close();
@ -82,7 +82,7 @@ public class DataIndexBuilder {
public DataIndexBuilder buildAddress() throws XMLStreamException, IOException{ public DataIndexBuilder buildAddress() throws XMLStreamException, IOException{
OutputStream output = checkFile("Address/"+region.getName()+".osmand"); OutputStream output = checkFile("Address/"+region.getName()+".osmand");
try { try {
OSMStorageWriter writer = new OSMStorageWriter(); OsmStorageWriter writer = new OsmStorageWriter();
writer.saveAddressIndex(output, region); writer.saveAddressIndex(output, region);
} finally { } finally {
output.close(); output.close();

View file

@ -61,10 +61,10 @@ public class OsmBaseStorage extends DefaultHandler {
public synchronized void parseOSM(InputStream stream, IProgress progress, InputStream streamForProgress) throws IOException, SAXException { public synchronized void parseOSM(InputStream stream, IProgress progress, InputStream streamForProgress) throws IOException, SAXException {
this.inputStream = stream; this.inputStream = stream;
this.progress = progress; this.progress = progress;
this.streamForProgress = streamForProgress;
if(streamForProgress == null){ if(streamForProgress == null){
streamForProgress = inputStream; streamForProgress = inputStream;
} }
this.streamForProgress = streamForProgress;
SAXParser parser = initSaxParser(); SAXParser parser = initSaxParser();
parseStarted = false; parseStarted = false;
entities.clear(); entities.clear();
@ -144,8 +144,8 @@ public class OsmBaseStorage extends DefaultHandler {
if(!parseStarted){ if(!parseStarted){
initRootElement(uri, localName, name, attributes); initRootElement(uri, localName, name, attributes);
} }
if (currentParsedEntity == null && streamForProgress != null) { if (currentParsedEntity == null) {
if(progress != null && !progress.isIndeterminate()){ if(progress != null && !progress.isIndeterminate() && streamForProgress != null){
try { try {
progress.remaining(streamForProgress.available()); progress.remaining(streamForProgress.available());
} catch (IOException e) { } catch (IOException e) {

View file

@ -93,7 +93,7 @@ public class OSMIndexStorage extends OsmBaseStorage {
a.setType(AmenityType.fromString(attributes.getValue(ATTR_TYPE))); a.setType(AmenityType.fromString(attributes.getValue(ATTR_TYPE)));
a.setSubType(attributes.getValue(ATTR_SUBTYPE)); a.setSubType(attributes.getValue(ATTR_SUBTYPE));
parseMapObject(a, attributes); parseMapObject(a, attributes);
region.registerAmenity(a); currentParsedStreet.registerBuilding(building);
} else { } else {
super.startElement(uri, localName, name, attributes); super.startElement(uri, localName, name, attributes);
} }

View file

@ -16,7 +16,7 @@ import static com.osmand.osm.io.OsmBaseStorage.ELEM_OSM;
import static com.osmand.osm.io.OsmBaseStorage.ELEM_RELATION; import static com.osmand.osm.io.OsmBaseStorage.ELEM_RELATION;
import static com.osmand.osm.io.OsmBaseStorage.ELEM_TAG; import static com.osmand.osm.io.OsmBaseStorage.ELEM_TAG;
import static com.osmand.osm.io.OsmBaseStorage.ELEM_WAY; import static com.osmand.osm.io.OsmBaseStorage.ELEM_WAY;
import static com.osmand.osm.io.OSMIndexStorage.*; import static com.osmand.osm.io.OsmIndexStorage.*;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;

View file

@ -80,7 +80,7 @@ import com.osmand.osm.LatLon;
import com.osmand.osm.MapUtils; import com.osmand.osm.MapUtils;
import com.osmand.osm.Node; import com.osmand.osm.Node;
import com.osmand.osm.io.IOsmStorageFilter; import com.osmand.osm.io.IOsmStorageFilter;
import com.osmand.osm.io.OSMStorageWriter; import com.osmand.osm.io.OsmStorageWriter;
import com.osmand.osm.io.OsmBoundsFilter; import com.osmand.osm.io.OsmBoundsFilter;
import com.osmand.swing.MapPanel.MapSelectionArea; import com.osmand.swing.MapPanel.MapSelectionArea;
@ -601,7 +601,7 @@ public class OsmExtractionUI implements IMapLocationListener {
} }
public void saveCountry(final File f){ public void saveCountry(final File f){
final OSMStorageWriter writer = new OSMStorageWriter(); final OsmStorageWriter writer = new OsmStorageWriter();
try { try {
final ProgressDialog dlg = new ProgressDialog(frame, "Saving osm file"); final ProgressDialog dlg = new ProgressDialog(frame, "Saving osm file");
dlg.setRunnable(new Runnable() { dlg.setRunnable(new Runnable() {

View file

@ -1,6 +1,5 @@
package com.osmand; package com.osmand;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@ -9,6 +8,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
import java.util.TreeMap;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.tools.bzip2.CBZip2InputStream; import org.apache.tools.bzip2.CBZip2InputStream;
@ -20,12 +20,15 @@ import android.os.Environment;
import com.osmand.data.Amenity; import com.osmand.data.Amenity;
import com.osmand.data.DataTileManager; import com.osmand.data.DataTileManager;
import com.osmand.data.Region;
import com.osmand.data.preparation.MapTileDownloader; import com.osmand.data.preparation.MapTileDownloader;
import com.osmand.data.preparation.MapTileDownloader.DownloadRequest; import com.osmand.data.preparation.MapTileDownloader.DownloadRequest;
import com.osmand.map.ITileSource; import com.osmand.map.ITileSource;
import com.osmand.osm.Entity; import com.osmand.osm.Entity;
import com.osmand.osm.LatLon;
import com.osmand.osm.Node; import com.osmand.osm.Node;
import com.osmand.osm.io.OsmBaseStorage; import com.osmand.osm.io.OsmBaseStorage;
import com.osmand.osm.io.OsmIndexStorage;
/** /**
* Resource manager is responsible to work with all resources * Resource manager is responsible to work with all resources
@ -38,6 +41,7 @@ import com.osmand.osm.io.OsmBaseStorage;
public class ResourceManager { public class ResourceManager {
private static final String POI_PATH = "osmand/poi/"; private static final String POI_PATH = "osmand/poi/";
private static final String ADDRESS_PATH = "osmand/address/";
private static final String TILES_PATH = "osmand/tiles/"; private static final String TILES_PATH = "osmand/tiles/";
private static final Log log = LogUtil.getLog(ResourceManager.class); private static final Log log = LogUtil.getLog(ResourceManager.class);
@ -56,6 +60,8 @@ public class ResourceManager {
private DataTileManager<Amenity> poiIndex = null; private DataTileManager<Amenity> poiIndex = null;
private Map<String, Region> addressMap = new TreeMap<String, Region>();
protected Map<String, Bitmap> cacheOfImages = new LinkedHashMap<String, Bitmap>(); protected Map<String, Bitmap> cacheOfImages = new LinkedHashMap<String, Bitmap>();
protected File dirWithTiles ; protected File dirWithTiles ;
@ -198,14 +204,22 @@ public class ResourceManager {
poiIndex = new DataTileManager<Amenity>(); poiIndex = new DataTileManager<Amenity>();
if (file.exists() && file.canRead()) { if (file.exists() && file.canRead()) {
for (File f : file.listFiles()) { for (File f : file.listFiles()) {
if (f.getName().endsWith(".bz2") || f.getName().endsWith(".osm")) { if (f.getName().endsWith(".bz2") || f.getName().endsWith(".osm") || f.getName().endsWith(".osmand")) {
long start = System.currentTimeMillis();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Starting index POI " + f.getAbsolutePath()); log.debug("Starting index POI " + f.getAbsolutePath());
} }
boolean zipped = f.getName().endsWith(".bz2"); boolean zipped = f.getName().endsWith(".bz2");
InputStream stream = null; InputStream stream = null;
try { try {
OsmBaseStorage storage = new OsmBaseStorage(); OsmBaseStorage storage;
boolean indexStorage = false;
if(f.getName().contains(".osmand")){
storage = new OsmIndexStorage(new Region());
indexStorage = true;
} else {
storage = new OsmBaseStorage();
}
stream = new FileInputStream(f); stream = new FileInputStream(f);
// stream = new BufferedInputStream(stream); // stream = new BufferedInputStream(stream);
InputStream streamForProgress = stream; InputStream streamForProgress = stream;
@ -222,13 +236,21 @@ public class ResourceManager {
progress.startTask("Indexing poi " + f.getName(), stream.available()); progress.startTask("Indexing poi " + f.getName(), stream.available());
} }
storage.parseOSM(stream, progress, streamForProgress); storage.parseOSM(stream, progress, streamForProgress);
for (Entity e : storage.getRegisteredEntities().values()) { if(indexStorage){
if (e instanceof Node && Amenity.isAmenity((Node) e)) { Region region = ((OsmIndexStorage) storage).getRegion();
poiIndex.registerObject(((Node)e).getLatitude(), ((Node)e).getLongitude(), new Amenity((Node) e)); for(Amenity a : region.getAmenityManager().getAllObjects()){
LatLon location = a.getLocation();
poiIndex.registerObject(location.getLatitude(), location.getLongitude(), a);
}
} else {
for (Entity e : storage.getRegisteredEntities().values()) {
if (e instanceof Node && Amenity.isAmenity((Node) e)) {
poiIndex.registerObject(((Node) e).getLatitude(), ((Node) e).getLongitude(), new Amenity((Node) e));
}
} }
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Finishing index POI " + f.getAbsolutePath()); log.debug("Finishing index POI " + f.getAbsolutePath() + " " +(System.currentTimeMillis() - start)+"ms");
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Can't read poi file " + f.getAbsolutePath(), e); log.error("Can't read poi file " + f.getAbsolutePath(), e);
@ -243,6 +265,54 @@ public class ResourceManager {
} }
} }
public void indexingAddresses(IProgress progress){
File file = new File(Environment.getExternalStorageDirectory(), ADDRESS_PATH);
if (file.exists() && file.canRead()) {
for (File f : file.listFiles()) {
if (f.getName().endsWith(".osmand.bz2") || f.getName().endsWith(".osmand")) {
long start = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Starting index address " + f.getAbsolutePath());
}
boolean zipped = f.getName().endsWith(".bz2");
InputStream stream = null;
String name = f.getName().substring(0, f.getName().indexOf('.'));
Region region = new Region();
region.setName(name);
addressMap.put(name, region);
try {
OsmIndexStorage storage = new OsmIndexStorage(region);
stream = new FileInputStream(f);
// stream = new BufferedInputStream(stream);
InputStream streamForProgress = stream;
if (zipped) {
if (stream.read() != 'B' || stream.read() != 'Z') {
log.error("Can't read index file " + f.getAbsolutePath()
+ "The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
continue;
} else {
stream = new CBZip2InputStream(stream);
}
}
if(progress != null){
progress.startTask("Indexing address " + f.getName(), stream.available());
}
storage.parseOSM(stream, progress, streamForProgress);
if (log.isDebugEnabled()) {
log.debug("Finishing index address " + f.getAbsolutePath() + " " +(System.currentTimeMillis() - start)+"ms");
}
} catch (IOException e) {
log.error("Can't read index file " + f.getAbsolutePath(), e);
} catch (SAXException e) {
log.error("Can't read index file " + f.getAbsolutePath(), e);
} finally {
Algoritms.closeStream(stream);
}
}
}
}
}
public DataTileManager<Amenity> getPoiIndex() { public DataTileManager<Amenity> getPoiIndex() {
if(poiIndex == null){ if(poiIndex == null){
indexingPoi(null); indexingPoi(null);

View file

@ -77,6 +77,7 @@ public class MainMenuActivity extends Activity {
public void run() { public void run() {
try { try {
ResourceManager.getResourceManager().indexingPoi(impl); ResourceManager.getResourceManager().indexingPoi(impl);
ResourceManager.getResourceManager().indexingAddresses(impl);
} finally { } finally {
dlg.dismiss(); dlg.dismiss();
} }