add progress implementation
git-svn-id: https://osmand.googlecode.com/svn/trunk@48 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
parent
6d7a01ed9d
commit
2a94d0beb1
9 changed files with 408 additions and 109 deletions
|
@ -19,6 +19,7 @@ import com.osmand.data.Amenity;
|
||||||
import com.osmand.data.City;
|
import com.osmand.data.City;
|
||||||
import com.osmand.data.DataTileManager;
|
import com.osmand.data.DataTileManager;
|
||||||
import com.osmand.data.Region;
|
import com.osmand.data.Region;
|
||||||
|
import com.osmand.impl.ConsoleProgressImplementation;
|
||||||
import com.osmand.osm.Entity;
|
import com.osmand.osm.Entity;
|
||||||
import com.osmand.osm.LatLon;
|
import com.osmand.osm.LatLon;
|
||||||
import com.osmand.osm.MapUtils;
|
import com.osmand.osm.MapUtils;
|
||||||
|
@ -102,7 +103,7 @@ public class DataExtraction {
|
||||||
|
|
||||||
|
|
||||||
OsmExtractionUI ui = new OsmExtractionUI(country);
|
OsmExtractionUI ui = new OsmExtractionUI(country);
|
||||||
ui.runUI();
|
ui.createUI();
|
||||||
|
|
||||||
List<Long> interestedObjects = new ArrayList<Long>();
|
List<Long> interestedObjects = new ArrayList<Long>();
|
||||||
// MapUtils.addIdsToList(places, interestedObjects);
|
// MapUtils.addIdsToList(places, interestedObjects);
|
||||||
|
@ -167,8 +168,6 @@ public class DataExtraction {
|
||||||
}
|
}
|
||||||
if (n.getTag(OSMTagKey.PLACE) != null) {
|
if (n.getTag(OSMTagKey.PLACE) != null) {
|
||||||
places.add(n);
|
places.add(n);
|
||||||
if (places.size() % 500 == 0) System.out.println();
|
|
||||||
System.out.print("-");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -189,7 +188,7 @@ public class DataExtraction {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
storage.parseOSM(stream);
|
storage.parseOSM(stream, new ConsoleProgressImplementation());
|
||||||
System.out.println("File parsed : " +(System.currentTimeMillis() - st));
|
System.out.println("File parsed : " +(System.currentTimeMillis() - st));
|
||||||
|
|
||||||
// 1. found towns !
|
// 1. found towns !
|
||||||
|
|
27
DataExtractionOSM/src/com/osmand/IProgress.java
Normal file
27
DataExtractionOSM/src/com/osmand/IProgress.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package com.osmand;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* That common interface that could be used by background operations.
|
||||||
|
* Implementation of it depends on chosen UI platform
|
||||||
|
*/
|
||||||
|
public interface IProgress {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param taskName
|
||||||
|
* @param work - -1 means that indeterminate task,
|
||||||
|
* otherwise number of could be specified
|
||||||
|
*/
|
||||||
|
public void startTask(String taskName, int work);
|
||||||
|
|
||||||
|
public void startWork(int work);
|
||||||
|
|
||||||
|
public void progress(int deltaWork);
|
||||||
|
|
||||||
|
public void remaining(int remainingWork);
|
||||||
|
|
||||||
|
public void finishTask();
|
||||||
|
|
||||||
|
public boolean isIndeterminate();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,72 @@
|
||||||
|
package com.osmand.impl;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import com.osmand.Algoritms;
|
||||||
|
import com.osmand.IProgress;
|
||||||
|
|
||||||
|
public class ConsoleProgressImplementation implements IProgress {
|
||||||
|
public static double deltaPercentsToPrint = 3.5;
|
||||||
|
|
||||||
|
String currentTask;
|
||||||
|
int work;
|
||||||
|
int currentDone;
|
||||||
|
|
||||||
|
double lastPercentPrint = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finishTask() {
|
||||||
|
System.out.println("Task " + currentTask + " is finished ");
|
||||||
|
this.currentTask = null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isIndeterminate() {
|
||||||
|
return work == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void progress(int deltaWork) {
|
||||||
|
this.currentDone += deltaWork;
|
||||||
|
printIfNeeded();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printIfNeeded() {
|
||||||
|
if(getCurrentPercent() - lastPercentPrint >= deltaPercentsToPrint){
|
||||||
|
System.out.println(MessageFormat.format("Done {0} %.", getCurrentPercent()));
|
||||||
|
this.lastPercentPrint = getCurrentPercent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getCurrentPercent(){
|
||||||
|
return (double) currentDone * 100d / work;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remaining(int remainingWork) {
|
||||||
|
this.currentDone = work - remainingWork;
|
||||||
|
printIfNeeded();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(String taskName, int work) {
|
||||||
|
if(!Algoritms.objectEquals(currentTask, taskName)){
|
||||||
|
this.currentTask = taskName;
|
||||||
|
System.out.println("Started new task : " + currentTask + " - " + work);
|
||||||
|
}
|
||||||
|
startWork(work);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startWork(int work) {
|
||||||
|
if(this.work != work){
|
||||||
|
this.work = work;
|
||||||
|
System.out.println("Amount of work was changed to " + work);
|
||||||
|
}
|
||||||
|
this.currentDone = 0;
|
||||||
|
this.lastPercentPrint = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,6 +15,7 @@ import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
|
import com.osmand.IProgress;
|
||||||
import com.osmand.osm.Entity;
|
import com.osmand.osm.Entity;
|
||||||
import com.osmand.osm.Node;
|
import com.osmand.osm.Node;
|
||||||
import com.osmand.osm.Relation;
|
import com.osmand.osm.Relation;
|
||||||
|
@ -48,6 +49,10 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
|
|
||||||
protected Map<Long, Entity> entities = new LinkedHashMap<Long, Entity>();
|
protected Map<Long, Entity> entities = new LinkedHashMap<Long, Entity>();
|
||||||
|
|
||||||
|
// this is used to show feedback to user
|
||||||
|
private IProgress progress;
|
||||||
|
private InputStream inputStream;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,11 +60,20 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws SAXException - could be
|
* @throws SAXException - could be
|
||||||
*/
|
*/
|
||||||
public synchronized void parseOSM(InputStream stream) throws IOException, SAXException {
|
public synchronized void parseOSM(InputStream stream, IProgress progress) throws IOException, SAXException {
|
||||||
|
this.inputStream = stream;
|
||||||
|
this.progress = progress;
|
||||||
SAXParser parser = initSaxParser();
|
SAXParser parser = initSaxParser();
|
||||||
parseStarted = false;
|
parseStarted = false;
|
||||||
entities.clear();
|
entities.clear();
|
||||||
|
if(progress != null){
|
||||||
|
progress.startWork(stream.available());
|
||||||
|
}
|
||||||
|
|
||||||
parser.parse(stream, this);
|
parser.parse(stream, this);
|
||||||
|
if(progress != null){
|
||||||
|
progress.finishTask();
|
||||||
|
}
|
||||||
completeReading();
|
completeReading();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +124,6 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
@Override
|
@Override
|
||||||
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
|
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
|
||||||
name = saxParser.isNamespaceAware() ? localName : name;
|
name = saxParser.isNamespaceAware() ? localName : name;
|
||||||
|
|
||||||
if(!parseStarted){
|
if(!parseStarted){
|
||||||
if(!ELEM_OSM.equals(name) || !supportedVersions.contains(attributes.getValue(ATTR_VERSION))){
|
if(!ELEM_OSM.equals(name) || !supportedVersions.contains(attributes.getValue(ATTR_VERSION))){
|
||||||
throw new OsmVersionNotSupported();
|
throw new OsmVersionNotSupported();
|
||||||
|
@ -118,6 +131,13 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
parseStarted = true;
|
parseStarted = true;
|
||||||
}
|
}
|
||||||
if (currentParsedEntity == null) {
|
if (currentParsedEntity == null) {
|
||||||
|
if(progress != null && !progress.isIndeterminate()){
|
||||||
|
try {
|
||||||
|
progress.remaining(inputStream.available());
|
||||||
|
} catch (IOException e) {
|
||||||
|
progress.startWork(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ELEM_NODE.equals(name)) {
|
if (ELEM_NODE.equals(name)) {
|
||||||
currentParsedEntity = new Node(parseDouble(attributes, ATTR_LAT, 0), parseDouble(attributes, ATTR_LON, 0),
|
currentParsedEntity = new Node(parseDouble(attributes, ATTR_LAT, 0), parseDouble(attributes, ATTR_LON, 0),
|
||||||
parseId(attributes, ATTR_ID, -1));
|
parseId(attributes, ATTR_ID, -1));
|
||||||
|
|
|
@ -3,12 +3,15 @@ package com.osmand.swing;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.Container;
|
import java.awt.Container;
|
||||||
|
import java.awt.Menu;
|
||||||
import java.awt.MenuBar;
|
import java.awt.MenuBar;
|
||||||
|
import java.awt.MenuItem;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -19,6 +22,7 @@ import java.util.TreeMap;
|
||||||
|
|
||||||
import javax.swing.DefaultListCellRenderer;
|
import javax.swing.DefaultListCellRenderer;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JList;
|
import javax.swing.JList;
|
||||||
|
@ -34,8 +38,13 @@ import javax.swing.event.TreeSelectionEvent;
|
||||||
import javax.swing.event.TreeSelectionListener;
|
import javax.swing.event.TreeSelectionListener;
|
||||||
import javax.swing.event.UndoableEditEvent;
|
import javax.swing.event.UndoableEditEvent;
|
||||||
import javax.swing.event.UndoableEditListener;
|
import javax.swing.event.UndoableEditListener;
|
||||||
|
import javax.swing.filechooser.FileFilter;
|
||||||
import javax.swing.tree.DefaultMutableTreeNode;
|
import javax.swing.tree.DefaultMutableTreeNode;
|
||||||
|
import javax.swing.tree.DefaultTreeModel;
|
||||||
|
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
import com.osmand.DataExtraction;
|
||||||
import com.osmand.DefaultLauncherConstants;
|
import com.osmand.DefaultLauncherConstants;
|
||||||
import com.osmand.IMapLocationListener;
|
import com.osmand.IMapLocationListener;
|
||||||
import com.osmand.data.Amenity;
|
import com.osmand.data.Amenity;
|
||||||
|
@ -57,29 +66,39 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
|
|
||||||
private DefaultMutableTreeNode amenitiesTree;
|
private DefaultMutableTreeNode amenitiesTree;
|
||||||
private JTree treePlaces;
|
private JTree treePlaces;
|
||||||
|
private JList searchList;
|
||||||
|
|
||||||
private final Region r;
|
private Region region;
|
||||||
|
|
||||||
|
private JFrame frame;
|
||||||
|
|
||||||
|
private JTextField searchTextField;
|
||||||
|
|
||||||
public OsmExtractionUI(final Region r){
|
public OsmExtractionUI(final Region r){
|
||||||
this.r = r;
|
this.region = r;
|
||||||
|
createUI();
|
||||||
|
setRegion(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
public void runUI(){
|
OsmExtractionUI ui = new OsmExtractionUI(null);
|
||||||
JFrame frame = new JFrame("Tree of choose");
|
ui.runUI();
|
||||||
try {
|
|
||||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
DefaultMutableTreeNode root = new DataExtractionTreeNode(r.getName(), r);
|
|
||||||
amenitiesTree = new DataExtractionTreeNode("Amenities", r);
|
public void setRegion(Region region){
|
||||||
amenitiesTree.add(new DataExtractionTreeNode("closest", r));
|
if(this.region == region){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.region = region;
|
||||||
|
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
|
||||||
|
amenitiesTree = new DataExtractionTreeNode("Amenities", region);
|
||||||
|
amenitiesTree.add(new DataExtractionTreeNode("closest", region));
|
||||||
root.add(amenitiesTree);
|
root.add(amenitiesTree);
|
||||||
|
|
||||||
for(CityType t : CityType.values()){
|
for(CityType t : CityType.values()){
|
||||||
DefaultMutableTreeNode cityTree = new DataExtractionTreeNode(t.toString(), t);
|
DefaultMutableTreeNode cityTree = new DataExtractionTreeNode(t.toString(), t);
|
||||||
root.add(cityTree);
|
root.add(cityTree);
|
||||||
for(City ct : r.getCitiesByType(t)){
|
for(City ct : region.getCitiesByType(t)){
|
||||||
DefaultMutableTreeNode cityNodeTree = new DataExtractionTreeNode(ct.getName(), ct);
|
DefaultMutableTreeNode cityNodeTree = new DataExtractionTreeNode(ct.getName(), ct);
|
||||||
cityTree.add(cityNodeTree);
|
cityTree.add(cityNodeTree);
|
||||||
|
|
||||||
|
@ -94,17 +113,35 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DataTileManager<LatLon> amenitiesManager = new DataTileManager<LatLon>();
|
||||||
|
for(Amenity a : region.getAmenityManager().getAllObjects()){
|
||||||
|
amenitiesManager.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a.getNode().getLatLon());
|
||||||
|
}
|
||||||
|
mapPanel.setPoints(amenitiesManager);
|
||||||
|
updateListCities(region, searchTextField.getText(), searchList);
|
||||||
|
mapPanel.updateUI();
|
||||||
|
treePlaces.setModel(new DefaultTreeModel(root, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void createUI(){
|
||||||
|
frame = new JFrame("Tree of choose");
|
||||||
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
frame.addWindowListener(new ExitListener());
|
frame.addWindowListener(new ExitListener());
|
||||||
Container content = frame.getContentPane();
|
Container content = frame.getContentPane();
|
||||||
frame.setFocusable(true);
|
frame.setFocusable(true);
|
||||||
|
|
||||||
|
treePlaces = new JTree();
|
||||||
treePlaces = new JTree(root);
|
treePlaces.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Region"), false));
|
||||||
final JList jList = new JList();
|
searchList = new JList();
|
||||||
jList.setCellRenderer(new DefaultListCellRenderer(){
|
searchList.setCellRenderer(new DefaultListCellRenderer(){
|
||||||
private static final long serialVersionUID = 4661949460526837891L;
|
private static final long serialVersionUID = 4661949460526837891L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -126,14 +163,8 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
mapPanel.addMapLocationListener(this);
|
mapPanel.addMapLocationListener(this);
|
||||||
|
|
||||||
|
|
||||||
DataTileManager<LatLon> amenitiesManager = new DataTileManager<LatLon>();
|
|
||||||
for(Amenity a : r.getAmenityManager().getAllObjects()){
|
|
||||||
amenitiesManager.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a.getNode().getLatLon());
|
|
||||||
}
|
|
||||||
mapPanel.setPoints(amenitiesManager);
|
|
||||||
|
|
||||||
|
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(searchList), panelForTreeAndImage);
|
||||||
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(jList), panelForTreeAndImage);
|
|
||||||
pane.setResizeWeight(0.2);
|
pane.setResizeWeight(0.2);
|
||||||
content.add(pane, BorderLayout.CENTER);
|
content.add(pane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
@ -141,37 +172,37 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
content.add(label, BorderLayout.SOUTH);
|
content.add(label, BorderLayout.SOUTH);
|
||||||
|
|
||||||
JPanel panel = new JPanel(new BorderLayout());
|
JPanel panel = new JPanel(new BorderLayout());
|
||||||
final JTextField textField = new JTextField();
|
searchTextField = new JTextField();
|
||||||
final JButton button = new JButton();
|
final JButton button = new JButton();
|
||||||
button.setText("Set town");
|
button.setText("Set town");
|
||||||
|
|
||||||
|
|
||||||
panel.add(textField, BorderLayout.CENTER);
|
panel.add(searchTextField, BorderLayout.CENTER);
|
||||||
panel.add(button, BorderLayout.WEST);
|
panel.add(button, BorderLayout.WEST);
|
||||||
|
|
||||||
content.add(panel, BorderLayout.NORTH);
|
content.add(panel, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
|
||||||
updateListCities(r, textField.getText(), jList);
|
updateListCities(region, searchTextField.getText(), searchList);
|
||||||
textField.getDocument().addUndoableEditListener(new UndoableEditListener(){
|
searchTextField.getDocument().addUndoableEditListener(new UndoableEditListener(){
|
||||||
@Override
|
@Override
|
||||||
public void undoableEditHappened(UndoableEditEvent e) {
|
public void undoableEditHappened(UndoableEditEvent e) {
|
||||||
updateListCities(r, textField.getText(), jList);
|
updateListCities(region, searchTextField.getText(), searchList);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
button.addActionListener(new ActionListener(){
|
button.addActionListener(new ActionListener(){
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
selectedCity = (City)jList.getSelectedValue();
|
selectedCity = (City)searchList.getSelectedValue();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
jList.addListSelectionListener(new ListSelectionListener(){
|
searchList.addListSelectionListener(new ListSelectionListener(){
|
||||||
@Override
|
@Override
|
||||||
public void valueChanged(ListSelectionEvent e) {
|
public void valueChanged(ListSelectionEvent e) {
|
||||||
if(jList.getSelectedValue() != null){
|
if(searchList.getSelectedValue() != null){
|
||||||
Node node = ((City)jList.getSelectedValue()).getNode();
|
Node node = ((City)searchList.getSelectedValue()).getNode();
|
||||||
String text = "Lat : " + node.getLatitude() + " Lon " + node.getLongitude();
|
String text = "Lat : " + node.getLatitude() + " Lon " + node.getLongitude();
|
||||||
if(selectedCity != null){
|
if(selectedCity != null){
|
||||||
text += " distance " + MapUtils.getDistance(selectedCity.getNode(), node);
|
text += " distance " + MapUtils.getDistance(selectedCity.getNode(), node);
|
||||||
|
@ -223,23 +254,77 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
});
|
});
|
||||||
|
|
||||||
MenuBar bar = new MenuBar();
|
MenuBar bar = new MenuBar();
|
||||||
bar.add(MapPanel.getMenuToChooseSource(mapPanel));
|
fillMenuWithActions(bar);
|
||||||
|
|
||||||
frame.setMenuBar(bar);
|
frame.setMenuBar(bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runUI(){
|
||||||
frame.setSize(1024, 768);
|
frame.setSize(1024, 768);
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void fillMenuWithActions(MenuBar bar){
|
||||||
|
Menu menu = new Menu("File");
|
||||||
|
bar.add(menu);
|
||||||
|
MenuItem loadFile = new MenuItem("Load file...");
|
||||||
|
menu.add(loadFile);
|
||||||
|
bar.add(MapPanel.getMenuToChooseSource(mapPanel));
|
||||||
|
|
||||||
|
loadFile.addActionListener(new ActionListener(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser fc = new JFileChooser();
|
||||||
|
fc.setDialogTitle("Choose osm file");
|
||||||
|
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||||
|
fc.setAcceptAllFileFilterUsed(true);
|
||||||
|
fc.setCurrentDirectory(new File(DefaultLauncherConstants.pathToDirWithTiles));
|
||||||
|
//System.out.println("opening fc for extension " + extension);
|
||||||
|
fc.setFileFilter(new FileFilter(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return f.isDirectory() || f.getName().endsWith(".bz2") || f.getName().endsWith(".osm");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "Osm Files (*.bz2, *.osm)";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
int answer = fc.showOpenDialog(frame) ;
|
||||||
|
if (answer == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null){
|
||||||
|
try {
|
||||||
|
Region region = new DataExtraction().readCountry(fc.getSelectedFile().getAbsolutePath());
|
||||||
|
setRegion(region);
|
||||||
|
} catch (IOException e1) {
|
||||||
|
// TODO
|
||||||
|
e1.printStackTrace();
|
||||||
|
} catch (SAXException e1) {
|
||||||
|
// TODO
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void locationChanged(final double newLatitude, final double newLongitude, Object source){
|
public void locationChanged(final double newLatitude, final double newLongitude, Object source){
|
||||||
|
if (amenitiesTree != null) {
|
||||||
Region reg = (Region) amenitiesTree.getUserObject();
|
Region reg = (Region) amenitiesTree.getUserObject();
|
||||||
List<Amenity> closestAmenities = reg.getClosestAmenities(newLatitude, newLongitude);
|
List<Amenity> closestAmenities = reg.getClosestAmenities(newLatitude, newLongitude);
|
||||||
Collections.sort(closestAmenities, new Comparator<Amenity>() {
|
Collections.sort(closestAmenities, new Comparator<Amenity>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(Amenity o1, Amenity o2) {
|
public int compare(Amenity o1, Amenity o2) {
|
||||||
return Double.compare(MapUtils.getDistance(o1.getNode(), newLatitude, newLongitude),
|
return Double.compare(MapUtils.getDistance(o1.getNode(), newLatitude, newLongitude), MapUtils.getDistance(o2.getNode(),
|
||||||
MapUtils.getDistance(o2.getNode(), newLatitude, newLongitude));
|
newLatitude, newLongitude));
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Map<String, List<Amenity>> filter = new TreeMap<String, List<Amenity>>();
|
Map<String, List<Amenity>> filter = new TreeMap<String, List<Amenity>>();
|
||||||
|
@ -257,16 +342,13 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
((DefaultMutableTreeNode) amenitiesTree.getChildAt(0)).removeAllChildren();
|
((DefaultMutableTreeNode) amenitiesTree.getChildAt(0)).removeAllChildren();
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < 15 && i < closestAmenities.size(); i++) {
|
for (int i = 0; i < 15 && i < closestAmenities.size(); i++) {
|
||||||
Amenity n = closestAmenities.get(i);
|
Amenity n = closestAmenities.get(i);
|
||||||
int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude));
|
int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude));
|
||||||
String str = n.getSimpleFormat() + " [" + dist + " m ]";
|
String str = n.getSimpleFormat() + " [" + dist + " m ]";
|
||||||
((DefaultMutableTreeNode)amenitiesTree.getChildAt(0)).add(
|
((DefaultMutableTreeNode) amenitiesTree.getChildAt(0)).add(new DataExtractionTreeNode(str, n));
|
||||||
new DataExtractionTreeNode(str, n));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String s : filter.keySet()) {
|
for (String s : filter.keySet()) {
|
||||||
|
@ -292,9 +374,16 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
}
|
}
|
||||||
treePlaces.updateUI();
|
treePlaces.updateUI();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void updateListCities(Region r, String text, JList jList){
|
public void updateListCities(Region r, String text, JList jList){
|
||||||
Collection<City> city = r.getSuggestedCities(text, 100);
|
|
||||||
|
Collection<City> city;
|
||||||
|
if(r == null){
|
||||||
|
city = Collections.emptyList();
|
||||||
|
} else {
|
||||||
|
city = r.getSuggestedCities(text, 100);
|
||||||
|
}
|
||||||
City[] names = new City[city.size()];
|
City[] names = new City[city.size()];
|
||||||
int i=0;
|
int i=0;
|
||||||
for(City c : city){
|
for(City c : city){
|
||||||
|
|
|
@ -15,8 +15,6 @@
|
||||||
<activity android:name=".activities.SettingsActivity"
|
<activity android:name=".activities.SettingsActivity"
|
||||||
android:label="@string/settings_activity"></activity>
|
android:label="@string/settings_activity"></activity>
|
||||||
|
|
||||||
|
|
||||||
<activity android:name=".activities.SettingsActivity" android:label="@string/settings_activity"></activity>
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|
||||||
|
|
97
OsmAnd/src/com/osmand/ProgressDialogImplementation.java
Normal file
97
OsmAnd/src/com/osmand/ProgressDialogImplementation.java
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
package com.osmand;
|
||||||
|
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
|
|
||||||
|
public class ProgressDialogImplementation implements IProgress {
|
||||||
|
|
||||||
|
private static final float deltaToChange = 0.04f;
|
||||||
|
private String taskName;
|
||||||
|
private int progress;
|
||||||
|
private int work;
|
||||||
|
private int deltaWork;
|
||||||
|
private String message = "";
|
||||||
|
|
||||||
|
private Handler mViewUpdateHandler;
|
||||||
|
|
||||||
|
public ProgressDialogImplementation(final ProgressDialog dlg){
|
||||||
|
mViewUpdateHandler = new Handler(){
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
super.handleMessage(msg);
|
||||||
|
dlg.setMessage(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void progress(int deltaWork) {
|
||||||
|
this.deltaWork += deltaWork;
|
||||||
|
if(change(progress + deltaWork)){
|
||||||
|
this.progress += deltaWork;
|
||||||
|
updateMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateMessage() {
|
||||||
|
message = taskName + String.format(" %.1f %%", this.progress * 100f / ((float) this.work));
|
||||||
|
mViewUpdateHandler.sendEmptyMessage(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean change(int newProgress) {
|
||||||
|
if (newProgress < progress) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((newProgress - progress) / ((float) work) < deltaToChange) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void remaining(int remainingWork) {
|
||||||
|
if(change(work - remainingWork)){
|
||||||
|
this.progress = work - remainingWork;
|
||||||
|
updateMessage();
|
||||||
|
}
|
||||||
|
deltaWork = work - remainingWork - this.progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIndeterminate(){
|
||||||
|
return work == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(String taskName, int work) {
|
||||||
|
if(taskName == null){
|
||||||
|
taskName = "";
|
||||||
|
}
|
||||||
|
message = taskName;
|
||||||
|
mViewUpdateHandler.sendEmptyMessage(0);
|
||||||
|
this.taskName = taskName;
|
||||||
|
startWork(work);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finishTask() {
|
||||||
|
if (taskName != null) {
|
||||||
|
message = "Finished : " + taskName;
|
||||||
|
mViewUpdateHandler.sendEmptyMessage(0);
|
||||||
|
}
|
||||||
|
work = -1;
|
||||||
|
progress = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startWork(int work) {
|
||||||
|
this.work = work;
|
||||||
|
if(this.work == 0){
|
||||||
|
this.work = 1;
|
||||||
|
}
|
||||||
|
progress = 0;
|
||||||
|
deltaWork = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -14,7 +14,6 @@ import org.apache.commons.logging.Log;
|
||||||
import org.apache.tools.bzip2.CBZip2InputStream;
|
import org.apache.tools.bzip2.CBZip2InputStream;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
import android.app.ProgressDialog;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
@ -192,7 +191,7 @@ public class ResourceManager {
|
||||||
|
|
||||||
|
|
||||||
// POI INDEX //
|
// POI INDEX //
|
||||||
public void indexingPoi(ProgressDialog dlg){
|
public void indexingPoi(IProgress progress){
|
||||||
if (poiIndex == null) {
|
if (poiIndex == null) {
|
||||||
File file = new File(Environment.getExternalStorageDirectory(), POI_PATH);
|
File file = new File(Environment.getExternalStorageDirectory(), POI_PATH);
|
||||||
poiIndex = new DataTileManager<Amenity>();
|
poiIndex = new DataTileManager<Amenity>();
|
||||||
|
@ -202,9 +201,6 @@ public class ResourceManager {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Starting index POI " + f.getAbsolutePath());
|
log.debug("Starting index POI " + f.getAbsolutePath());
|
||||||
}
|
}
|
||||||
if(dlg != null){
|
|
||||||
dlg.setMessage("Indexing poi " + f.getName());
|
|
||||||
}
|
|
||||||
boolean zipped = f.getName().endsWith(".bz2");
|
boolean zipped = f.getName().endsWith(".bz2");
|
||||||
InputStream stream = null;
|
InputStream stream = null;
|
||||||
try {
|
try {
|
||||||
|
@ -220,7 +216,10 @@ public class ResourceManager {
|
||||||
stream = new CBZip2InputStream(stream);
|
stream = new CBZip2InputStream(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
storage.parseOSM(stream);
|
if(progress != null){
|
||||||
|
progress.startTask("Indexing poi " + f.getName(), stream.available());
|
||||||
|
}
|
||||||
|
storage.parseOSM(stream, progress);
|
||||||
for (Entity e : storage.getRegisteredEntities().values()) {
|
for (Entity e : storage.getRegisteredEntities().values()) {
|
||||||
if (e instanceof Node && Amenity.isAmenity((Node) e)) {
|
if (e instanceof Node && Amenity.isAmenity((Node) e)) {
|
||||||
poiIndex.registerObject(((Node)e).getLatitude(), ((Node)e).getLongitude(), new Amenity((Node) e));
|
poiIndex.registerObject(((Node)e).getLatitude(), ((Node)e).getLongitude(), new Amenity((Node) e));
|
||||||
|
|
|
@ -12,6 +12,7 @@ import android.view.Window;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
|
||||||
|
import com.osmand.ProgressDialogImplementation;
|
||||||
import com.osmand.R;
|
import com.osmand.R;
|
||||||
import com.osmand.ResourceManager;
|
import com.osmand.ResourceManager;
|
||||||
|
|
||||||
|
@ -33,10 +34,10 @@ public class MainMenuActivity extends Activity {
|
||||||
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
Intent mapIndent = new Intent(MainMenuActivity.this, MapActivity.class);
|
Intent mapIndent = new Intent(MainMenuActivity.this, MapActivity.class);
|
||||||
Notification notification = new Notification(R.drawable.icon, "Notify",
|
Notification notification = new Notification(R.drawable.icon, "",
|
||||||
System.currentTimeMillis());
|
System.currentTimeMillis());
|
||||||
notification.setLatestEventInfo(MainMenuActivity.this, "OsmAnd",
|
notification.setLatestEventInfo(MainMenuActivity.this, "OsmAnd",
|
||||||
"OsmAnd are running in background", PendingIntent.getActivity(
|
"OsmAnd is running in background", PendingIntent.getActivity(
|
||||||
this.getBaseContext(), 0, mapIndent,
|
this.getBaseContext(), 0, mapIndent,
|
||||||
PendingIntent.FLAG_CANCEL_CURRENT));
|
PendingIntent.FLAG_CANCEL_CURRENT));
|
||||||
mNotificationManager.notify(APP_NOTIFICATION_ID, notification);
|
mNotificationManager.notify(APP_NOTIFICATION_ID, notification);
|
||||||
|
@ -69,16 +70,13 @@ public class MainMenuActivity extends Activity {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// add notification
|
final ProgressDialog dlg = ProgressDialog.show(this, "Loading data", "Reading indices...");
|
||||||
|
final ProgressDialogImplementation impl = new ProgressDialogImplementation(dlg);
|
||||||
final ProgressDialog dlg = ProgressDialog.show(this, "Loading data",
|
|
||||||
"Reading indices...");
|
|
||||||
new Thread() {
|
new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
dlg.setMessage("Indexing poi...");
|
ResourceManager.getResourceManager().indexingPoi(impl);
|
||||||
ResourceManager.getResourceManager().indexingPoi(dlg);
|
|
||||||
} finally {
|
} finally {
|
||||||
dlg.dismiss();
|
dlg.dismiss();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue