From a2dbdbd3995b6548f8bb89e2b54834f703aa2845 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 12 Nov 2010 20:31:35 +0000 Subject: [PATCH] fix issue 146 git-svn-id: https://osmand.googlecode.com/svn/trunk@663 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/net/osmand/data/Amenity.java | 61 +++++++++---------- .../osmand/data/preparation/IndexCreator.java | 39 ++++++------ .../net/osmand/AmenityIndexRepository.java | 20 ++++-- .../osmand/activities/EditingPOIActivity.java | 40 ++++++------ 4 files changed, 85 insertions(+), 75 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/data/Amenity.java b/DataExtractionOSM/src/net/osmand/data/Amenity.java index 71200480f5..75068f479a 100644 --- a/DataExtractionOSM/src/net/osmand/data/Amenity.java +++ b/DataExtractionOSM/src/net/osmand/data/Amenity.java @@ -1,6 +1,7 @@ package net.osmand.data; import java.util.Collection; +import java.util.List; import net.osmand.osm.Entity; import net.osmand.osm.MapRenderingTypes; @@ -17,11 +18,12 @@ public class Amenity extends MapObject { private String phone; private String site; - public Amenity(Entity entity){ + public Amenity(Entity entity, AmenityType type, String subtype){ super(entity); // manipulate with id to distinguish way and nodes this.id = entity.getId() << 1 + ((entity instanceof Node)? 0 : 1); - initTypeSubtype(entity, this); + this.type = type; + this.subType = subtype; this.openingHours = entity.getTag(OSMTagKey.OPENING_HOURS); this.phone = entity.getTag(OSMTagKey.PHONE); if (this.phone == null) { @@ -51,32 +53,6 @@ public class Amenity extends MapObject { public Amenity(){ } - private static AmenityType initTypeSubtype(Entity entity, Amenity init) { - Collection keySet = entity.getTagKeySet(); - if (!keySet.isEmpty()) { - for (String t : keySet) { - AmenityType type = MapRenderingTypes.getAmenityType(t, entity.getTag(t)); - if (type != null) { - if (init != null) { - init.type = type; - init.subType = MapRenderingTypes.getAmenitySubtype(t, entity.getTag(t)); - } - return type; - } - } - for (String t : keySet) { - AmenityType type = MapRenderingTypes.getAmenityType(t, null); - if (type != null) { - if (init != null) { - init.type = type; - init.subType = MapRenderingTypes.getAmenitySubtype(t, entity.getTag(t)); - } - return type; - } - } - } - return null; - } public AmenityType getType(){ return type; } @@ -93,12 +69,33 @@ public class Amenity extends MapObject { this.subType = subType; } - public static boolean isAmenity(Entity n){ - if(n instanceof Relation){ + public static List parseAmenities(Entity entity, List amenitiesList){ + if(entity instanceof Relation){ // it could be collection of amenities - return false; + return amenitiesList; } - return initTypeSubtype(n, null) != null; + + Collection keySet = entity.getTagKeySet(); + if (!keySet.isEmpty()) { + int shift = 0; + for (String t : keySet) { + AmenityType type = MapRenderingTypes.getAmenityType(t, entity.getTag(t)); + if (type != null) { + String subtype = MapRenderingTypes.getAmenitySubtype(t, entity.getTag(t)); + amenitiesList.add(shift, new Amenity(entity, type, subtype)); + shift++; + } else { + type = MapRenderingTypes.getAmenityType(t, null); + if (type != null) { + String subtype = MapRenderingTypes.getAmenitySubtype(t, entity.getTag(t)); + // add amenity to the end + amenitiesList.add(new Amenity(entity, type, subtype)); + } + + } + } + } + return amenitiesList; } public String getOpeningHours() { diff --git a/DataExtractionOSM/src/net/osmand/data/preparation/IndexCreator.java b/DataExtractionOSM/src/net/osmand/data/preparation/IndexCreator.java index ff8a8c5802..a4d06586f4 100644 --- a/DataExtractionOSM/src/net/osmand/data/preparation/IndexCreator.java +++ b/DataExtractionOSM/src/net/osmand/data/preparation/IndexCreator.java @@ -1109,16 +1109,23 @@ public class IndexCreator { return foundId; } + private List tempAmenityList = new ArrayList(); + private void iterateEntity(Entity e, int step) throws SQLException { if (step == STEP_MAIN) { - if (indexPOI && Amenity.isAmenity(e)) { - loadEntityData(e, false); - if (poiPreparedStatement != null) { - Amenity a = new Amenity(e); - if (a.getLocation() != null) { - // do not convert english name - // convertEnglishName(a); - DataIndexWriter.insertAmenityIntoPoi(poiPreparedStatement, pStatements, a, BATCH_SIZE); + if (indexPOI) { + tempAmenityList.clear(); + tempAmenityList = Amenity.parseAmenities(e, tempAmenityList); + if(!tempAmenityList.isEmpty() && poiPreparedStatement != null){ + // load data for way (location etc...) + loadEntityData(e, false); + for (Amenity a : tempAmenityList) { + a.setEntity(e); + if (a.getLocation() != null) { + // do not convert english name + // convertEnglishName(a); + DataIndexWriter.insertAmenityIntoPoi(poiPreparedStatement, pStatements, a, BATCH_SIZE); + } } } } @@ -2494,18 +2501,16 @@ public class IndexCreator { public static void main(String[] args) throws IOException, SAXException, SQLException { long time = System.currentTimeMillis(); IndexCreator creator = new IndexCreator(new File("e:/Information/OSM maps/osmand/")); - creator.setIndexMap(true); - creator.setIndexAddress(true); - creator.setSaveAddressWays(true); - creator.setNormalizeStreets(true); +// creator.setIndexMap(true); +// creator.setIndexAddress(true); creator.setIndexPOI(true); - creator.setIndexTransport(true); +// creator.setIndexTransport(true); creator.recreateOnlyBinaryFile = false; creator.deleteDatabaseIndexes = false; -// creator.setNodesDBFile(new File("e:/Information/OSM maps/osmand/minsk.tmp.odb")); -// creator.generateIndexes(new File("e:/Information/OSM maps/belarus osm/minsk.osm"), new ConsoleProgressImplementation(3), null); + creator.setNodesDBFile(new File("e:/Information/OSM maps/osmand/minsk.tmp.odb")); + creator.generateIndexes(new File("e:/Information/OSM maps/belarus osm/minsk.osm"), new ConsoleProgressImplementation(3), null); // creator.setNodesDBFile(new File("e:/Information/OSM maps/osmand/belarus_nodes.tmp.odb")); // creator.generateIndexes(new File("e:/Information/OSM maps/belarus osm/belarus.osm.bz2"), new ConsoleProgressImplementation(3), null); @@ -2521,8 +2526,8 @@ public class IndexCreator { // creator.generateIndexes(new File("e:/Information/OSM maps/osm_map/forest_complex.osm"), new ConsoleProgressImplementation(25), null); - creator.setNodesDBFile(new File("e:/Information/OSM maps/osmand/luxembourg.tmp.odb")); - creator.generateIndexes(new File("e:/Information/OSM maps/osm_map/luxembourg.osm.pbf"), new ConsoleProgressImplementation(15), null); +// creator.setNodesDBFile(new File("e:/Information/OSM maps/osmand/luxembourg.tmp.odb")); +// creator.generateIndexes(new File("e:/Information/OSM maps/osm_map/luxembourg.osm.pbf"), new ConsoleProgressImplementation(15), null); System.out.println("WHOLE GENERATION TIME : " + (System.currentTimeMillis() - time)); System.out.println("COORDINATES_SIZE " + BinaryMapIndexWriter.COORDINATES_SIZE + " count " + BinaryMapIndexWriter.COORDINATES_COUNT); diff --git a/OsmAnd/src/net/osmand/AmenityIndexRepository.java b/OsmAnd/src/net/osmand/AmenityIndexRepository.java index ecfb39181e..35969df074 100644 --- a/OsmAnd/src/net/osmand/AmenityIndexRepository.java +++ b/OsmAnd/src/net/osmand/AmenityIndexRepository.java @@ -7,7 +7,9 @@ import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import net.osmand.data.Amenity; import net.osmand.data.AmenityType; @@ -110,7 +112,7 @@ public class AmenityIndexRepository extends BaseLocationIndexRepository return true; } - public boolean deleteAmenity(long id){ + public boolean deleteAmenities(long id){ db.execSQL("DELETE FROM " + IndexConstants.POI_TABLE+ " WHERE id="+id); //$NON-NLS-1$ //$NON-NLS-2$ return true; } @@ -215,12 +217,17 @@ public class AmenityIndexRepository extends BaseLocationIndexRepository log.info("Start loading poi : " + u); //$NON-NLS-1$ InputStream is = url.openStream(); OsmBaseStorage st = new OsmBaseStorage(); - final List amen = new ArrayList(); + final Map amen = new LinkedHashMap(); + final List tempList = new ArrayList(); st.getFilters().add(new IOsmStorageFilter(){ @Override public boolean acceptEntityToLoad(OsmBaseStorage storage, Entity.EntityId id, Entity entity) { - if(Amenity.isAmenity(entity)){ - amen.add(entity); + Amenity.parseAmenities(entity, tempList); + if(!tempList.isEmpty()){ + for(Amenity a : tempList){ + amen.put(a, entity); + } + tempList.clear(); return true; } // to @@ -228,8 +235,9 @@ public class AmenityIndexRepository extends BaseLocationIndexRepository } }); st.parseOSM(is, null, null, false); - for (Entity e : amen) { - Amenity am = new Amenity(e); + for (Amenity am : amen.keySet()) { + // update location (when all nodes of way are loaded) + am.setEntity(amen.get(am)); if(am.getEnName().length() == 0){ am.setEnName(Junidecode.unidecode(am.getName())); } diff --git a/OsmAnd/src/net/osmand/activities/EditingPOIActivity.java b/OsmAnd/src/net/osmand/activities/EditingPOIActivity.java index 4f9ceded39..b9a88d95e8 100644 --- a/OsmAnd/src/net/osmand/activities/EditingPOIActivity.java +++ b/OsmAnd/src/net/osmand/activities/EditingPOIActivity.java @@ -111,7 +111,7 @@ public class EditingPOIActivity { if(n != null){ dlg = new Dialog(ctx); dlg.setTitle(R.string.poi_edit_title); - showDialog(n); + showDialog(n, a.getType(), a.getSubType()); } else { Toast.makeText(ctx, ctx.getString(R.string.poi_error_poi_not_found), Toast.LENGTH_SHORT).show(); } @@ -123,7 +123,7 @@ public class EditingPOIActivity { n.putTag(OSMTagKey.AMENITY.getValue(), ""); //$NON-NLS-1$ n.putTag(OSMTagKey.OPENING_HOURS.getValue(), ""); //$NON-NLS-1$ dlg.setTitle(R.string.poi_create_title); - showDialog(n); + showDialog(n, null, null); } public void showDeleteDialog(Amenity a){ @@ -157,8 +157,8 @@ public class EditingPOIActivity { builder.show(); } - private void showDialog(final Node n){ - final Amenity a = new Amenity(n); + private void showDialog(final Node n, AmenityType type, String subtype){ + final Amenity a = new Amenity(n, type, subtype); dlg.setContentView(R.layout.editing_poi); nameText =((EditText)dlg.findViewById(R.id.Name)); nameText.setText(a.getName()); @@ -564,26 +564,26 @@ public class EditingPOIActivity { ser.endTag(null, "node"); //$NON-NLS-1$ } - private void updateNodeInIndexes(String action, Node n){ + private void updateNodeInIndexes(String action, Node n) { List repos = app.getResourceManager().searchAmenityRepositories(n.getLatitude(), n.getLongitude()); - if(DELETE_ACTION.equals(action)){ - for(AmenityIndexRepository r: repos){ - r.deleteAmenity(n.getId()); - r.clearCache(); - } - } else { - boolean changed = MODIFY_ACTION.equals(action); - Amenity a = new Amenity(n); - for(AmenityIndexRepository r: repos){ - if(changed){ - r.updateAmenity(a); - } else { - r.addAmenity(a); - } + // delete all amenities with same id + if (DELETE_ACTION.equals(action) || MODIFY_ACTION.equals(action)) { + for (AmenityIndexRepository r : repos) { + r.deleteAmenities(n.getId() << 1); r.clearCache(); } } - + // add amenities + if (!DELETE_ACTION.equals(action)) { + List ams = Amenity.parseAmenities(n, new ArrayList()); + for (Amenity a : ams) { + for (AmenityIndexRepository r : repos) { + r.addAmenity(a); + r.clearCache(); + } + } + } + }