From 1f256f2bfdb31a7ec6b6762269c7d0839d94cbdf Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 17 May 2010 14:35:52 +0000 Subject: [PATCH] add indexing address git-svn-id: https://osmand.googlecode.com/svn/trunk@63 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/data/Region.java | 2 +- .../data/preparation/DataExtraction.java | 4 +- .../data/preparation/DataIndexBuilder.java | 8 +- .../src/com/osmand/osm/io/OsmBaseStorage.java | 6 +- ...IndexStorage.java => OsmIndexStorage.java} | 2 +- ...orageWriter.java => OsmStorageWriter.java} | 2 +- .../src/com/osmand/swing/OsmExtractionUI.java | 4 +- OsmAnd/src/com/osmand/ResourceManager.java | 84 +++++++++++++++++-- .../osmand/activities/MainMenuActivity.java | 1 + 9 files changed, 92 insertions(+), 21 deletions(-) rename DataExtractionOSM/src/com/osmand/osm/io/{OSMIndexStorage.java => OsmIndexStorage.java} (95%) rename DataExtractionOSM/src/com/osmand/osm/io/{OSMStorageWriter.java => OsmStorageWriter.java} (96%) diff --git a/DataExtractionOSM/src/com/osmand/data/Region.java b/DataExtractionOSM/src/com/osmand/data/Region.java index 3aeef8789b..01bc1eba3f 100644 --- a/DataExtractionOSM/src/com/osmand/data/Region.java +++ b/DataExtractionOSM/src/com/osmand/data/Region.java @@ -38,7 +38,7 @@ public class Region extends MapObject { } public Region(){ - this.name = "Region"; + name = "Region"; } public OsmBaseStorage getStorage() { diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java index 303e30217f..819117f6bf 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java @@ -35,7 +35,7 @@ import com.osmand.osm.OSMSettings; import com.osmand.osm.Way; import com.osmand.osm.OSMSettings.OSMTagKey; import com.osmand.osm.io.IOsmStorageFilter; -import com.osmand.osm.io.OSMStorageWriter; +import com.osmand.osm.io.OsmStorageWriter; import com.osmand.osm.io.OsmBaseStorage; @@ -115,7 +115,7 @@ public class DataExtraction { // TODO add interested objects List interestedObjects = new ArrayList(); if (writeTestOsmFile != null) { - OSMStorageWriter writer = new OSMStorageWriter(); + OsmStorageWriter writer = new OsmStorageWriter(); OutputStream output = new FileOutputStream(writeTestOsmFile); if (writeTestOsmFile.endsWith(".bz2")) { output.write('B'); diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java index b491415c2b..1561077e67 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java @@ -13,7 +13,7 @@ import org.apache.tools.bzip2.CBZip2OutputStream; import com.osmand.data.Amenity; import com.osmand.data.Region; -import com.osmand.osm.io.OSMStorageWriter; +import com.osmand.osm.io.OsmStorageWriter; public class DataIndexBuilder { @@ -64,14 +64,14 @@ public class DataIndexBuilder { } OutputStream output = checkFile("POI/"+region.getName()+".osm"); try { - OSMStorageWriter writer = new OSMStorageWriter(); + OsmStorageWriter writer = new OsmStorageWriter(); writer.saveStorage(output, region.getStorage(), interestedObjects, false); } finally { output.close(); } output = checkFile("POI/"+region.getName()+".osmand"); try { - OSMStorageWriter writer = new OSMStorageWriter(); + OsmStorageWriter writer = new OsmStorageWriter(); writer.savePOIIndex(output, region); } finally { output.close(); @@ -82,7 +82,7 @@ public class DataIndexBuilder { public DataIndexBuilder buildAddress() throws XMLStreamException, IOException{ OutputStream output = checkFile("Address/"+region.getName()+".osmand"); try { - OSMStorageWriter writer = new OSMStorageWriter(); + OsmStorageWriter writer = new OsmStorageWriter(); writer.saveAddressIndex(output, region); } finally { output.close(); diff --git a/DataExtractionOSM/src/com/osmand/osm/io/OsmBaseStorage.java b/DataExtractionOSM/src/com/osmand/osm/io/OsmBaseStorage.java index 1194679e05..e5693e3c87 100644 --- a/DataExtractionOSM/src/com/osmand/osm/io/OsmBaseStorage.java +++ b/DataExtractionOSM/src/com/osmand/osm/io/OsmBaseStorage.java @@ -61,10 +61,10 @@ public class OsmBaseStorage extends DefaultHandler { public synchronized void parseOSM(InputStream stream, IProgress progress, InputStream streamForProgress) throws IOException, SAXException { this.inputStream = stream; this.progress = progress; - this.streamForProgress = streamForProgress; if(streamForProgress == null){ streamForProgress = inputStream; } + this.streamForProgress = streamForProgress; SAXParser parser = initSaxParser(); parseStarted = false; entities.clear(); @@ -144,8 +144,8 @@ public class OsmBaseStorage extends DefaultHandler { if(!parseStarted){ initRootElement(uri, localName, name, attributes); } - if (currentParsedEntity == null && streamForProgress != null) { - if(progress != null && !progress.isIndeterminate()){ + if (currentParsedEntity == null) { + if(progress != null && !progress.isIndeterminate() && streamForProgress != null){ try { progress.remaining(streamForProgress.available()); } catch (IOException e) { diff --git a/DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java b/DataExtractionOSM/src/com/osmand/osm/io/OsmIndexStorage.java similarity index 95% rename from DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java rename to DataExtractionOSM/src/com/osmand/osm/io/OsmIndexStorage.java index 0cca34c3a8..e0ee851f80 100644 --- a/DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java +++ b/DataExtractionOSM/src/com/osmand/osm/io/OsmIndexStorage.java @@ -93,7 +93,7 @@ public class OSMIndexStorage extends OsmBaseStorage { a.setType(AmenityType.fromString(attributes.getValue(ATTR_TYPE))); a.setSubType(attributes.getValue(ATTR_SUBTYPE)); parseMapObject(a, attributes); - region.registerAmenity(a); + currentParsedStreet.registerBuilding(building); } else { super.startElement(uri, localName, name, attributes); } diff --git a/DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java b/DataExtractionOSM/src/com/osmand/osm/io/OsmStorageWriter.java similarity index 96% rename from DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java rename to DataExtractionOSM/src/com/osmand/osm/io/OsmStorageWriter.java index b64a681f4d..30786ab6e3 100644 --- a/DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java +++ b/DataExtractionOSM/src/com/osmand/osm/io/OsmStorageWriter.java @@ -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_TAG; 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.OutputStream; diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java index f70777bc30..e5d6b941f3 100644 --- a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java @@ -80,7 +80,7 @@ import com.osmand.osm.LatLon; import com.osmand.osm.MapUtils; import com.osmand.osm.Node; 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.swing.MapPanel.MapSelectionArea; @@ -601,7 +601,7 @@ public class OsmExtractionUI implements IMapLocationListener { } public void saveCountry(final File f){ - final OSMStorageWriter writer = new OSMStorageWriter(); + final OsmStorageWriter writer = new OsmStorageWriter(); try { final ProgressDialog dlg = new ProgressDialog(frame, "Saving osm file"); dlg.setRunnable(new Runnable() { diff --git a/OsmAnd/src/com/osmand/ResourceManager.java b/OsmAnd/src/com/osmand/ResourceManager.java index e2fe644781..23b461a149 100644 --- a/OsmAnd/src/com/osmand/ResourceManager.java +++ b/OsmAnd/src/com/osmand/ResourceManager.java @@ -1,6 +1,5 @@ package com.osmand; -import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -9,6 +8,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import java.util.Stack; +import java.util.TreeMap; import org.apache.commons.logging.Log; import org.apache.tools.bzip2.CBZip2InputStream; @@ -20,12 +20,15 @@ import android.os.Environment; import com.osmand.data.Amenity; import com.osmand.data.DataTileManager; +import com.osmand.data.Region; import com.osmand.data.preparation.MapTileDownloader; import com.osmand.data.preparation.MapTileDownloader.DownloadRequest; import com.osmand.map.ITileSource; import com.osmand.osm.Entity; +import com.osmand.osm.LatLon; import com.osmand.osm.Node; import com.osmand.osm.io.OsmBaseStorage; +import com.osmand.osm.io.OsmIndexStorage; /** * Resource manager is responsible to work with all resources @@ -38,6 +41,7 @@ import com.osmand.osm.io.OsmBaseStorage; public class ResourceManager { 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 Log log = LogUtil.getLog(ResourceManager.class); @@ -56,6 +60,8 @@ public class ResourceManager { private DataTileManager poiIndex = null; + private Map addressMap = new TreeMap(); + protected Map cacheOfImages = new LinkedHashMap(); protected File dirWithTiles ; @@ -198,14 +204,22 @@ public class ResourceManager { poiIndex = new DataTileManager(); if (file.exists() && file.canRead()) { 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()) { log.debug("Starting index POI " + f.getAbsolutePath()); } boolean zipped = f.getName().endsWith(".bz2"); InputStream stream = null; 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 BufferedInputStream(stream); InputStream streamForProgress = stream; @@ -222,13 +236,21 @@ public class ResourceManager { progress.startTask("Indexing poi " + f.getName(), stream.available()); } storage.parseOSM(stream, progress, streamForProgress); - 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(indexStorage){ + Region region = ((OsmIndexStorage) storage).getRegion(); + 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()) { - log.debug("Finishing index POI " + f.getAbsolutePath()); + log.debug("Finishing index POI " + f.getAbsolutePath() + " " +(System.currentTimeMillis() - start)+"ms"); } } catch (IOException 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 getPoiIndex() { if(poiIndex == null){ indexingPoi(null); diff --git a/OsmAnd/src/com/osmand/activities/MainMenuActivity.java b/OsmAnd/src/com/osmand/activities/MainMenuActivity.java index b77642a5e9..18ac314e8e 100644 --- a/OsmAnd/src/com/osmand/activities/MainMenuActivity.java +++ b/OsmAnd/src/com/osmand/activities/MainMenuActivity.java @@ -77,6 +77,7 @@ public class MainMenuActivity extends Activity { public void run() { try { ResourceManager.getResourceManager().indexingPoi(impl); + ResourceManager.getResourceManager().indexingAddresses(impl); } finally { dlg.dismiss(); }