From 2060618cb852e8dc8b19f0cb03767c40990fd173 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 16 May 2010 21:49:11 +0000 Subject: [PATCH] implement index functionality git-svn-id: https://osmand.googlecode.com/svn/trunk@61 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/ToDoConstants.java | 4 +- .../src/com/osmand/data/Amenity.java | 51 ++++++-- .../src/com/osmand/data/Building.java | 4 +- .../src/com/osmand/data/City.java | 56 +++++--- .../src/com/osmand/data/MapObject.java | 17 +++ .../src/com/osmand/data/Region.java | 7 + .../src/com/osmand/data/Street.java | 12 +- .../data/preparation/DataExtraction.java | 4 +- .../data/preparation/DataIndexBuilder.java | 22 +++- .../com/osmand/osm/io/OSMIndexStorage.java | 50 ++++++++ .../com/osmand/osm/io/OSMStorageWriter.java | 120 ++++++++++++++++-- .../src/com/osmand/swing/MapPanel.java | 2 +- .../src/com/osmand/swing/OsmExtractionUI.java | 74 ++++++++--- 13 files changed, 351 insertions(+), 72 deletions(-) diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index 748e8f7f45..48517bbd7b 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -23,7 +23,8 @@ public class ToDoConstants { public int DESCRIBE_ABOUT_AUTHORS = 8; // TODO see all calculations x, y for layers & for MapView - + + // 0. Minimize memory used for index & improve time for read index //// TODO for releasing version // 1. POI SEARCH NEAR TO YOU // 2. FIX BACK TO your location & gps & point of view (may be compass) @@ -45,5 +46,6 @@ public class ToDoConstants { // 4. Config file log & see log from file // 6. Predefine what should be extracted from osm (building, poi or roads) // 7. Fix TODO in files (accept amenity - way) + // 8. add progress for data generation process } diff --git a/DataExtractionOSM/src/com/osmand/data/Amenity.java b/DataExtractionOSM/src/com/osmand/data/Amenity.java index 8211f64afb..99f52746cb 100644 --- a/DataExtractionOSM/src/com/osmand/data/Amenity.java +++ b/DataExtractionOSM/src/com/osmand/data/Amenity.java @@ -21,6 +21,15 @@ public class Amenity extends MapObject { SHOP, // convenience (product), clothes... LEISURE, // sport OTHER, // grave-yard, police, post-office + ; + + public static AmenityType fromString(String s){ + return AmenityType.valueOf(s.toUpperCase()); + } + + public static String valueToString(AmenityType t){ + return t.toString().toLowerCase(); + } } private static Map prebuiltMap = new LinkedHashMap(); static { @@ -78,12 +87,25 @@ public class Amenity extends MapObject { private final Node node; + private String subType; + private AmenityType type; public Amenity(Node node){ this.node = node; + this.type = getType(node); + this.subType = getSubType(node); } - public String getSubType(){ + public Amenity(){ + this.node = null; + } + + @Override + public Node getEntity() { + return node; + } + + protected String getSubType(Node node){ if(node.getTag(OSMTagKey.AMENITY) != null){ return node.getTag(OSMTagKey.AMENITY); } else if(node.getTag(OSMTagKey.SHOP) != null){ @@ -96,7 +118,7 @@ public class Amenity extends MapObject { return ""; } - public AmenityType getType(){ + protected AmenityType getType(Node node){ if(node.getTag(OSMTagKey.SHOP) != null){ return AmenityType.SHOP; } else if(node.getTag(OSMTagKey.TOURISM) != null){ @@ -109,6 +131,22 @@ public class Amenity extends MapObject { return AmenityType.OTHER; } + public AmenityType getType(){ + return type; + } + + public String getSubType(){ + return subType; + } + + public void setType(AmenityType type) { + this.type = type; + } + + public void setSubType(String subType) { + this.subType = subType; + } + public static boolean isAmenity(Entity n){ if(!(n instanceof Node)){ return false; @@ -125,20 +163,15 @@ public class Amenity extends MapObject { return false; } - @Override - public Node getEntity() { - return node; - } public String getSimpleFormat(){ - String name = node.getTag(OSMTagKey.NAME); + String name = getName(); return Algoritms.capitalizeFirstLetterAndLowercase(getType().toString()) + " : " + getSubType() + " " +(name == null ? node.getId() : name); } public String getStringWithoutType(){ - String name = node.getTag(OSMTagKey.NAME); - return getSubType() + " " +(name == null ? node.getId() : name); + return getSubType() + " " +getName(); } @Override diff --git a/DataExtractionOSM/src/com/osmand/data/Building.java b/DataExtractionOSM/src/com/osmand/data/Building.java index ee08103f97..98c8e2fe4a 100644 --- a/DataExtractionOSM/src/com/osmand/data/Building.java +++ b/DataExtractionOSM/src/com/osmand/data/Building.java @@ -4,11 +4,13 @@ import com.osmand.osm.Entity; public class Building extends MapObject { - private final Entity e; + private Entity e; public Building(Entity e){ this.e = e; } + + public Building(){} @Override public Entity getEntity() { diff --git a/DataExtractionOSM/src/com/osmand/data/City.java b/DataExtractionOSM/src/com/osmand/data/City.java index d5a1d17fa8..5df8da9c4d 100644 --- a/DataExtractionOSM/src/com/osmand/data/City.java +++ b/DataExtractionOSM/src/com/osmand/data/City.java @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.Map; import java.util.TreeMap; +import com.osmand.Algoritms; import com.osmand.osm.Entity; import com.osmand.osm.Node; import com.osmand.osm.OSMSettings.OSMTagKey; @@ -11,32 +12,46 @@ import com.osmand.osm.OSMSettings.OSMTagKey; public class City extends MapObject { public enum CityType { - CITY(10000), TOWN(5000), VILLAGE(1000), HAMLET(300), SUBURB(300); - - private double radius; - private CityType(double radius){ - this.radius = radius; - } - - public double getRadius() { + CITY(10000), TOWN(5000), VILLAGE(1000), HAMLET(300), SUBURB(300); + + private double radius; + + private CityType(double radius) { + this.radius = radius; + } + + public double getRadius() { return radius; } - + + public static String valueToString(CityType t) { + return t.toString().toLowerCase(); + } + + public static CityType valueFromString(String place) { + if (place == null) { + return null; + } + for (CityType t : CityType.values()) { + if (t.name().equalsIgnoreCase(place)) { + return t; + } + } + return null; + } } - private final Node el; + private Node el; private CityType type = null; private Map streets = new TreeMap(); public City(Node el){ this.el = el; - String place = el.getTag(OSMTagKey.PLACE); - for(CityType t : CityType.values()){ - if(t.name().equalsIgnoreCase(place)){ - type = t; - break; - } - } + type = CityType.valueFromString(el.getTag(OSMTagKey.PLACE)); + } + + public City(CityType type){ + this.type = type; } public Street registerStreet(String street){ @@ -46,6 +61,13 @@ public class City extends MapObject { return streets.get(street); } + public Street registerStreet(Street street){ + if(!Algoritms.isEmpty(street.getName())){ + return streets.put(street.getName(), street); + } + return null; + } + public Street registerBuilding(Entity e){ String number = e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER); String street = e.getTag(OSMTagKey.ADDR_STREET); diff --git a/DataExtractionOSM/src/com/osmand/data/MapObject.java b/DataExtractionOSM/src/com/osmand/data/MapObject.java index 7ad8476c3b..076981e373 100644 --- a/DataExtractionOSM/src/com/osmand/data/MapObject.java +++ b/DataExtractionOSM/src/com/osmand/data/MapObject.java @@ -9,9 +9,26 @@ public abstract class MapObject implements Comparable { - private final String name; private List buildings = new ArrayList(); private List wayNodes = new ArrayList(); private Node center = null; @@ -21,21 +20,22 @@ public class Street extends MapObject { this.name = name; } + public Street(){} + public void registerBuilding(Entity e){ Building building = new Building(e); building.setName(e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER)); buildings.add(building); } + public void registerBuilding(Building building){ + buildings.add(building); + } + public List getBuildings() { return buildings; } - public String getName() { - return name; - } - - public LatLon getLocation(){ if(center == null){ calculateCenter(); diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java index da8e2f71dc..303e30217f 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java @@ -115,7 +115,7 @@ public class DataExtraction { // TODO add interested objects List interestedObjects = new ArrayList(); if (writeTestOsmFile != null) { - OSMStorageWriter writer = new OSMStorageWriter(country.getStorage().getRegisteredEntities()); + OSMStorageWriter writer = new OSMStorageWriter(); OutputStream output = new FileOutputStream(writeTestOsmFile); if (writeTestOsmFile.endsWith(".bz2")) { output.write('B'); @@ -123,7 +123,7 @@ public class DataExtraction { output = new CBZip2OutputStream(output); } - writer.saveStorage(output, interestedObjects, false); + writer.saveStorage(output, country.getStorage(), interestedObjects, false); output.close(); } diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java index 5d9c7dba1b..b491415c2b 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java @@ -64,8 +64,26 @@ public class DataIndexBuilder { } OutputStream output = checkFile("POI/"+region.getName()+".osm"); try { - OSMStorageWriter writer = new OSMStorageWriter(region.getStorage().getRegisteredEntities()); - writer.saveStorage(output, interestedObjects, false); + 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(); + writer.savePOIIndex(output, region); + } finally { + output.close(); + } + return this; + } + + public DataIndexBuilder buildAddress() throws XMLStreamException, IOException{ + OutputStream output = checkFile("Address/"+region.getName()+".osmand"); + try { + OSMStorageWriter writer = new OSMStorageWriter(); + writer.saveAddressIndex(output, region); } finally { output.close(); } diff --git a/DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java b/DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java index cc75538519..0cca34c3a8 100644 --- a/DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java +++ b/DataExtractionOSM/src/com/osmand/osm/io/OSMIndexStorage.java @@ -3,7 +3,15 @@ package com.osmand.osm.io; import org.xml.sax.Attributes; import org.xml.sax.SAXException; +import com.osmand.data.Amenity; +import com.osmand.data.Building; +import com.osmand.data.City; +import com.osmand.data.MapObject; import com.osmand.data.Region; +import com.osmand.data.Street; +import com.osmand.data.Amenity.AmenityType; +import com.osmand.data.City.CityType; +import com.osmand.osm.Entity; public class OSMIndexStorage extends OsmBaseStorage { protected static final String ELEM_OSMAND = "osmand"; @@ -11,6 +19,12 @@ public class OSMIndexStorage extends OsmBaseStorage { protected static final String ELEM_CITY = "city"; protected static final String ELEM_STREET = "street"; protected static final String ELEM_BUILDING = "building"; + protected static final String ELEM_AMENITY = "amenity"; + + protected static final String ATTR_CITYTYPE = "citytype"; + protected static final String ATTR_TYPE = "type"; + protected static final String ATTR_SUBTYPE = "subtype"; + protected static final String ATTR_NAME = "name"; public static final String OSMAND_VERSION = "0.1"; @@ -21,6 +35,9 @@ public class OSMIndexStorage extends OsmBaseStorage { this.region = region; } + protected City currentParsedCity = null; + protected Street currentParsedStreet = null; + @Override protected void initRootElement(String uri, String localName, String name, Attributes attributes) throws OsmVersionNotSupported { if(ELEM_OSM.equals(name)){ @@ -37,6 +54,17 @@ public class OSMIndexStorage extends OsmBaseStorage { parseStarted = true; } + public void parseMapObject(MapObject c, Attributes attributes){ + double lat = parseDouble(attributes, ATTR_LAT, 0); + double lon = parseDouble(attributes, ATTR_LON, 0); + long id = parseId(attributes, ATTR_ID, -1); + c.setId(id); + if(lat != 0 || lon != 0){ + c.setLocation(lat, lon); + } + c.setName(attributes.getValue(ATTR_NAME)); + } + @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { name = saxParser.isNamespaceAware() ? localName : name; @@ -44,6 +72,28 @@ public class OSMIndexStorage extends OsmBaseStorage { initRootElement(uri, localName, name, attributes); } else if(ELEM_INDEX.equals(name)){ } else if(ELEM_CITY.equals(name)){ + CityType t = CityType.valueOf(attributes.getValue(ATTR_CITYTYPE)); + City c = new City(t); + parseMapObject(c, attributes); + region.registerCity(c); + currentParsedCity = c; + } else if(ELEM_STREET.equals(name)){ + assert currentParsedCity != null; + Street street = new Street(); + parseMapObject(street, attributes); + currentParsedCity.registerStreet(street); + currentParsedStreet = street; + } else if(ELEM_BUILDING.equals(name)){ + assert currentParsedStreet != null; + Building building = new Building(); + parseMapObject(building, attributes); + currentParsedStreet.registerBuilding(building); + } else if(ELEM_AMENITY.equals(name)){ + Amenity a = new Amenity(); + a.setType(AmenityType.fromString(attributes.getValue(ATTR_TYPE))); + a.setSubType(attributes.getValue(ATTR_SUBTYPE)); + parseMapObject(a, attributes); + region.registerAmenity(a); } 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 index 27341ab00c..b64a681f4d 100644 --- a/DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java +++ b/DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java @@ -16,6 +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 java.io.IOException; import java.io.OutputStream; @@ -29,7 +30,17 @@ import java.util.Map.Entry; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import com.osmand.Algoritms; +import com.osmand.data.Amenity; +import com.osmand.data.Building; +import com.osmand.data.City; +import com.osmand.data.MapObject; +import com.osmand.data.Region; +import com.osmand.data.Street; +import com.osmand.data.Amenity.AmenityType; +import com.osmand.data.City.CityType; import com.osmand.osm.Entity; +import com.osmand.osm.LatLon; import com.osmand.osm.Node; import com.osmand.osm.Relation; import com.osmand.osm.Way; @@ -38,17 +49,17 @@ import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl; public class OSMStorageWriter { - private final Map entities; private final String INDENT = " "; private final String INDENT2 = INDENT + INDENT; + private final String INDENT3 = INDENT + INDENT + INDENT; - public OSMStorageWriter(Map entities){ - this.entities = entities; + public OSMStorageWriter(){ } - public void saveStorage(OutputStream output, Collection interestedObjects, boolean includeLinks) throws XMLStreamException, IOException { + public void saveStorage(OutputStream output, OsmBaseStorage storage, Collection interestedObjects, boolean includeLinks) throws XMLStreamException, IOException { + Map entities = storage.getRegisteredEntities(); PropertyManager propertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // String indent = "{http://xml.apache.org/xslt}indent-amount"; @@ -92,7 +103,7 @@ public class OSMStorageWriter { streamWriter.writeAttribute(ATTR_LON, n.getLongitude()+""); streamWriter.writeAttribute(ATTR_ID, n.getId()+""); writeTags(streamWriter, n); - streamWriter.writeEndElement(); + writeEndElement(streamWriter, INDENT); } for(Way w : ways){ @@ -101,10 +112,10 @@ public class OSMStorageWriter { for(Long r : w.getNodeIds()){ writeStartElement(streamWriter, ELEM_ND, INDENT2); streamWriter.writeAttribute(ATTR_REF, r+""); - streamWriter.writeEndElement(); + writeEndElement(streamWriter, INDENT2); } writeTags(streamWriter, w); - streamWriter.writeEndElement(); + writeEndElement(streamWriter, INDENT); } for(Relation r : relations){ @@ -118,19 +129,19 @@ public class OSMStorageWriter { s = ""; } streamWriter.writeAttribute(ATTR_ROLE, s); - streamWriter.writeAttribute(ATTR_TYPE, getEntityType(e.getKey())); - streamWriter.writeEndElement(); + streamWriter.writeAttribute(ATTR_TYPE, getEntityType(entities, e.getKey())); + writeEndElement(streamWriter, INDENT2); } writeTags(streamWriter, r); - streamWriter.writeEndElement(); + writeEndElement(streamWriter, INDENT); } - streamWriter.writeEndElement(); // osm + writeEndElement(streamWriter, ""); // osm streamWriter.writeEndDocument(); streamWriter.flush(); } - private String getEntityType(Long id){ + private String getEntityType(Map entities , Long id){ Entity e = entities.get(id); if(e instanceof Way){ return "way"; @@ -140,11 +151,96 @@ public class OSMStorageWriter { return "node"; } + + public void savePOIIndex(OutputStream output, Region region) throws XMLStreamException, IOException { + PropertyManager propertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER); + XMLStreamWriter streamWriter = new XMLStreamWriterImpl(output, propertyManager); + + writeStartElement(streamWriter, ELEM_OSMAND, ""); + streamWriter.writeAttribute(ATTR_VERSION, OSMAND_VERSION); + List amenities = region.getAmenityManager().getAllObjects(); + for(Amenity n : amenities){ + if (couldBeWrited(n)) { + writeStartElement(streamWriter, ELEM_AMENITY, INDENT); + writeAttributesMapObject(streamWriter, n); + streamWriter.writeAttribute(ATTR_TYPE, AmenityType.valueToString(n.getType())); + streamWriter.writeAttribute(ATTR_SUBTYPE, n.getSubType()); + writeEndElement(streamWriter, INDENT); + } + } + writeEndElement(streamWriter, ""); // osmand + streamWriter.writeEndDocument(); + streamWriter.flush(); + } + + private void writeCity(XMLStreamWriter streamWriter, City c) throws XMLStreamException{ + writeStartElement(streamWriter, ELEM_CITY, INDENT); + writeAttributesMapObject(streamWriter, c); + streamWriter.writeAttribute(ATTR_CITYTYPE, CityType.valueToString(c.getType())); + for(Street s : c.getStreets()){ + if (couldBeWrited(s)) { + writeStartElement(streamWriter, ELEM_STREET, INDENT2); + writeAttributesMapObject(streamWriter, s); + for(Building b : s.getBuildings()) { + if (couldBeWrited(b)) { + writeStartElement(streamWriter, ELEM_BUILDING, INDENT3); + writeAttributesMapObject(streamWriter, b); + writeEndElement(streamWriter, INDENT3); + } + } + writeEndElement(streamWriter, INDENT2); + } + } + writeEndElement(streamWriter, INDENT); + } + + public void saveAddressIndex(OutputStream output, Region region) throws XMLStreamException, IOException { + PropertyManager propertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER); + XMLStreamWriter streamWriter = new XMLStreamWriterImpl(output, propertyManager); + + writeStartElement(streamWriter, ELEM_OSMAND, ""); + streamWriter.writeAttribute(ATTR_VERSION, OSMAND_VERSION); + for(CityType t : CityType.values()){ + Collection cities = region.getCitiesByType(t); + if(cities != null){ + for(City c : cities){ + if (couldBeWrited(c)) { + writeCity(streamWriter, c); + } + } + } + } + writeEndElement(streamWriter, ""); // osmand + streamWriter.writeEndDocument(); + streamWriter.flush(); + } + + public boolean couldBeWrited(MapObject e){ + if(!Algoritms.isEmpty(e.getName()) && e.getLocation() != null){ + return true; + } + return false; + } + + public void writeAttributesMapObject(XMLStreamWriter streamWriter, MapObject e) throws XMLStreamException{ + LatLon location = e.getLocation(); + streamWriter.writeAttribute(ATTR_LAT, location.getLatitude()+""); + streamWriter.writeAttribute(ATTR_LON, location.getLongitude()+""); + streamWriter.writeAttribute(ATTR_NAME, e.getName()); + streamWriter.writeAttribute(ATTR_ID, e.getId()+""); + } + + + private void writeStartElement(XMLStreamWriter writer, String name, String indent) throws XMLStreamException{ writer.writeCharacters("\n"+indent); writer.writeStartElement(name); } + private void writeEndElement(XMLStreamWriter writer, String indent) throws XMLStreamException{ + writer.writeCharacters("\n"+indent); + writer.writeEndElement(); + } private void writeTags(XMLStreamWriter writer, Entity e) throws XMLStreamException{ for(Entry en : e.getTags().entrySet()){ diff --git a/DataExtractionOSM/src/com/osmand/swing/MapPanel.java b/DataExtractionOSM/src/com/osmand/swing/MapPanel.java index cc253fde8f..59bd81473a 100644 --- a/DataExtractionOSM/src/com/osmand/swing/MapPanel.java +++ b/DataExtractionOSM/src/com/osmand/swing/MapPanel.java @@ -65,7 +65,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { public static final int divNonLoadedImage = 16; public static JMenu getMenuToChooseSource(final MapPanel panel){ - final JMenu tiles = new JMenu("Source tile"); + final JMenu tiles = new JMenu("Source of tiles"); final List list = TileSourceManager.getKnownSourceTemplates(); for(final TileSourceTemplate l : list){ JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(l.getName()); diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java index c0bdb8505c..9fff880606 100644 --- a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java @@ -110,6 +110,7 @@ public class OsmExtractionUI implements IMapLocationListener { private JCheckBox buildPoiIndex; private JCheckBox buildAddressIndex; private TreeModelListener treeModelListener; + private JCheckBox zipIndexFiles; @@ -279,6 +280,7 @@ public class OsmExtractionUI implements IMapLocationListener { generateDataButton.setEnabled(region != null); buildAddressIndex.setEnabled(generateDataButton.isEnabled() && region.getCitiesCount(null) > 0); buildPoiIndex.setEnabled(generateDataButton.isEnabled() && !region.getAmenityManager().isEmpty()); + zipIndexFiles.setEnabled(generateDataButton.isEnabled()); } public void createButtonsBar(Container content){ @@ -295,26 +297,8 @@ public class OsmExtractionUI implements IMapLocationListener { @Override public void actionPerformed(ActionEvent e) { - DataIndexBuilder builder = new DataIndexBuilder(DataExtractionSettings.getSettings().getDefaultWorkingDir(), region); - StringBuilder msg = new StringBuilder(); - try { - msg.append("Indices checked for ").append(region.getName()); - if(buildPoiIndex.isEnabled()){ - builder.buildPOI(); - msg.append(", POI index ").append("successfully created"); - } - msg.append("."); - JOptionPane pane = new JOptionPane(msg); - JDialog dialog = pane.createDialog(frame, "Generation data"); - dialog.setVisible(true); - } catch (XMLStreamException e1) { - ExceptionHandler.handle(e1); - } catch (IOException e1) { - ExceptionHandler.handle(e1); - } - + generateData(); } - }); buildPoiIndex = new JCheckBox(); @@ -327,8 +311,56 @@ public class OsmExtractionUI implements IMapLocationListener { panel.add(buildAddressIndex); buildAddressIndex.setSelected(true); + zipIndexFiles = new JCheckBox(); + zipIndexFiles.setText("Zip index files"); + panel.add(zipIndexFiles); + zipIndexFiles.setSelected(true); + updateButtonsBar(); } + + protected void generateData() { + try { + final ProgressDialog dlg = new ProgressDialog(frame, "Generating data"); + dlg.setRunnable(new Runnable(){ + + @Override + public void run() { + dlg.startTask("Generating indices...", -1); + DataIndexBuilder builder = new DataIndexBuilder(DataExtractionSettings.getSettings().getDefaultWorkingDir(), region); + StringBuilder msg = new StringBuilder(); + try { + builder.setZipped(zipIndexFiles.isSelected()); + msg.append("Indices checked for ").append(region.getName()); + if(buildPoiIndex.isEnabled()){ + dlg.startTask("Generating POI index...", -1); + builder.buildPOI(); + msg.append(", POI index ").append("successfully created"); + } + if(buildAddressIndex.isEnabled()){ + dlg.startTask("Generating address index...", -1); + builder.buildAddress(); + msg.append(", Address index ").append("successfully created"); + } + msg.append("."); + JOptionPane pane = new JOptionPane(msg); + JDialog dialog = pane.createDialog(frame, "Generation data"); + dialog.setVisible(true); + } catch (XMLStreamException e1) { + throw new IllegalArgumentException(e1); + } catch (IOException e1) { + throw new IllegalArgumentException(e1); + } + } + }); + dlg.run(); + } catch (InterruptedException e1) { + log.error("Interrupted", e1); + } catch (InvocationTargetException e1) { + ExceptionHandler.handle((Exception) e1.getCause()); + } + + } public void createCitySearchPanel(Container content){ JPanel panel = new JPanel(new BorderLayout()); @@ -564,7 +596,7 @@ public class OsmExtractionUI implements IMapLocationListener { } public void saveCountry(final File f){ - final OSMStorageWriter writer = new OSMStorageWriter(region.getStorage().getRegisteredEntities()); + final OSMStorageWriter writer = new OSMStorageWriter(); try { final ProgressDialog dlg = new ProgressDialog(frame, "Saving osm file"); dlg.setRunnable(new Runnable() { @@ -578,7 +610,7 @@ public class OsmExtractionUI implements IMapLocationListener { output.write('Z'); output = new CBZip2OutputStream(output); } - writer.saveStorage(output, null, false); + writer.saveStorage(output, region.getStorage(), null, false); } finally { output.close(); }