add ability to download tiles from internet

git-svn-id: https://osmand.googlecode.com/svn/trunk@22 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-04-29 22:47:07 +00:00
parent e673c3bc82
commit fcaecf9d63
6 changed files with 178 additions and 48 deletions

View file

@ -12,6 +12,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -42,6 +43,7 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import org.apache.tools.bzip2.CBZip2InputStream; import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.bzip2.CBZip2OutputStream;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import com.osmand.data.City; import com.osmand.data.City;
@ -114,7 +116,7 @@ public class DataExtraction implements IMapLocationListener {
stream = new FileInputStream(DefaultLauncherConstants.pathToOsmFile); stream = new FileInputStream(DefaultLauncherConstants.pathToOsmFile);
} else { } else {
stream = new FileInputStream(DefaultLauncherConstants.pathToOsmBz2File); stream = new FileInputStream(DefaultLauncherConstants.pathToOsmBz2File);
if (stream.read() != 66 || stream.read() != 90) if (stream.read() != 'B' || stream.read() != 'Z')
throw new RuntimeException( throw new RuntimeException(
"The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); "The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
else else
@ -157,6 +159,10 @@ public class DataExtraction implements IMapLocationListener {
// TODO temp solution // TODO temp solution
n.putTag(OSMTagKey.AMENITY.getValue(), OSMTagKey.LEISURE.getValue()); n.putTag(OSMTagKey.AMENITY.getValue(), OSMTagKey.LEISURE.getValue());
amenities.add(n); amenities.add(n);
} else if (n.getTag(OSMTagKey.TOURISM) != null) {
// TODO temp solution
n.putTag(OSMTagKey.AMENITY.getValue(), OSMTagKey.TOURISM.getValue());
amenities.add(n);
} }
if (n.getTag(OSMTagKey.PLACE) != null) { if (n.getTag(OSMTagKey.PLACE) != null) {
places.add(n); places.add(n);
@ -220,8 +226,10 @@ public class DataExtraction implements IMapLocationListener {
} }
} }
DataTileManager<LatLon> amenitiesManager = new DataTileManager<LatLon>();
for(Node node : amenities){ for(Node node : amenities){
country.registerAmenity(node); country.registerAmenity(node);
amenitiesManager.registerObject(node.getLatitude(), node.getLongitude(), node.getLatLon());
} }
@ -234,18 +242,24 @@ public class DataExtraction implements IMapLocationListener {
} }
} }
} }
mapPanel.setPoints(waysManager); mapPanel.setPoints(amenitiesManager);
runUI(country); runUI(country);
List<Long> interestedObjects = new ArrayList<Long>(); List<Long> interestedObjects = new ArrayList<Long>();
MapUtils.addIdsToList(places, interestedObjects); // MapUtils.addIdsToList(places, interestedObjects);
MapUtils.addIdsToList(amenities, interestedObjects); MapUtils.addIdsToList(amenities, interestedObjects);
MapUtils.addIdsToList(mapWays, interestedObjects); // MapUtils.addIdsToList(mapWays, interestedObjects);
// MapUtils.addIdsToList(buildings, interestedObjects); // MapUtils.addIdsToList(buildings, interestedObjects);
if (DefaultLauncherConstants.writeTestOsmFile != null) { if (DefaultLauncherConstants.writeTestOsmFile != null) {
OSMStorageWriter writer = new OSMStorageWriter(storage.getRegisteredEntities()); OSMStorageWriter writer = new OSMStorageWriter(storage.getRegisteredEntities());
writer.saveStorage(new FileOutputStream(DefaultLauncherConstants.writeTestOsmFile), interestedObjects, true); OutputStream output = new FileOutputStream(DefaultLauncherConstants.writeTestOsmFile);
output.write('B');
output.write('Z');
output = new CBZip2OutputStream(output);
writer.saveStorage(output, interestedObjects, true);
output.close();
} }
System.out.println(); System.out.println();

View file

@ -4,6 +4,7 @@ import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Container; import java.awt.Container;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point; import java.awt.Point;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
@ -11,18 +12,28 @@ import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack;
import javax.imageio.IIOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.UIManager; import javax.swing.UIManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.osmand.DataExtraction.ExitListener; import com.osmand.DataExtraction.ExitListener;
import com.osmand.data.DataTileManager; import com.osmand.data.DataTileManager;
import com.osmand.osm.LatLon; import com.osmand.osm.LatLon;
@ -32,6 +43,8 @@ public class MapPanel extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(MapPanel.class);
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Tree of choose"); JFrame frame = new JFrame("Tree of choose");
@ -78,18 +91,20 @@ public class MapPanel extends JPanel {
private List<IMapLocationListener> listeners = new ArrayList<IMapLocationListener>(); private List<IMapLocationListener> listeners = new ArrayList<IMapLocationListener>();
// cached data to draw image // cached data to draw image
private BufferedImage[][] images; private Image[][] images;
private int xStartingImage = 0; private int xStartingImage = 0;
private int yStartingImage = 0; private int yStartingImage = 0;
private List<Point> pointsToDraw = new ArrayList<Point>(); private List<Point> pointsToDraw = new ArrayList<Point>();
private AsyncLoadTileThread autoThread = new AsyncLoadTileThread();
Map<String, Image> cache = new HashMap<String, Image>();
public MapPanel(File fileWithTiles) { public MapPanel(File fileWithTiles) {
this.fileWithTiles = fileWithTiles; this.fileWithTiles = fileWithTiles;
autoThread.start();
initUI(); initUI();
} }
@ -105,6 +120,7 @@ public class MapPanel extends JPanel {
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
if (images != null) { if (images != null) {
for (int i = 0; i < images.length; i++) { for (int i = 0; i < images.length; i++) {
for (int j = 0; j < images[i].length; j++) { for (int j = 0; j < images[i].length; j++) {
@ -123,8 +139,6 @@ public class MapPanel extends JPanel {
} }
} }
} else { } else {
g.drawImage(images[i][j], i * tileSize+xStartingImage, j * tileSize + yStartingImage, this); g.drawImage(images[i][j], i * tileSize+xStartingImage, j * tileSize + yStartingImage, this);
} }
@ -138,19 +152,22 @@ public class MapPanel extends JPanel {
g.fillOval(p.x, p.y, 3, 3); g.fillOval(p.x, p.y, 3, 3);
} }
String s = MessageFormat.format("Lat : {0}, lon : {1}, zoom : {2}",latitude, longitude, zoom);
g.drawString(s, 5, 20);
g.fillOval(getWidth()/2 - 2, getHeight()/2 -2, 4, 4); g.fillOval(getWidth()/2 - 2, getHeight()/2 -2, 4, 4);
g.drawOval(getWidth()/2 - 2, getHeight()/2 -2, 4, 4); g.drawOval(getWidth()/2 - 2, getHeight()/2 -2, 4, 4);
g.drawOval(getWidth()/2 - 5, getHeight()/2 -5, 10, 10); g.drawOval(getWidth()/2 - 5, getHeight()/2 -5, 10, 10);
} }
public String getFile(int x, int y){ public String getFile(int zoom, int x, int y){
return map +"/"+zoom+"/"+(x) +"/"+y+".png"; return map +"/"+zoom+"/"+(x) +"/"+y+".png";
} }
Map<String, BufferedImage> cache = new HashMap<String, BufferedImage>();
public BufferedImage getImageFor(int x, int y) throws IOException{ public Image getImageFor(int x, int y) throws IOException{
String file = getFile(x, y); String file = getFile(zoom, x, y);
if(!cache.containsKey(file)){ if(!cache.containsKey(file)){
// ZipEntry en = fileWithTiles.getEntry(file); // ZipEntry en = fileWithTiles.getEntry(file);
File en = new File(fileWithTiles, file); File en = new File(fileWithTiles, file);
@ -167,13 +184,104 @@ public class MapPanel extends JPanel {
cache.put(file, ImageIO.read(en)); cache.put(file, ImageIO.read(en));
System.out.println("Loaded " + (System.currentTimeMillis() - time)); System.out.println("Loaded " + (System.currentTimeMillis() - time));
} else { } else {
cache.put(file, null); autoThread.requestTileToLoad(zoom, x, y);
} }
} }
return cache.get(file); return cache.get(file);
} }
public void loadImage(int zoom, int x, int y){
try {
Image img = null;
File en = new File(fileWithTiles, getFile(zoom, x, y));
URL url = new URL("http://tile.openstreetmap.org/" + zoom + "/" + x + "/" + y + ".png");
en.getParentFile().mkdirs();
try {
ImageIO.setUseCache(true);
javax.imageio.ImageIO.setCacheDirectory(en.getParentFile());
} catch (Exception e) {
log.warn("cannot set ImageIO cache-directory to " + en.getParent(), e);
}
try {
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "osmand TilePainter/0.1");
img = javax.imageio.ImageIO.read(connection.getInputStream());
} catch (UnknownHostException e) {
log.info("UnknownHostException, cannot download tile " + url.toExternalForm());
} catch (IIOException e) {
if (e.getCause() != null && e.getCause() instanceof UnknownHostException) {
log.info("UnknownHostException, cannot download tile " + url.toExternalForm(), e);
} else {
log.warn("cannot download tile " + url.toExternalForm(), e);
}
}
cache.put(getFile(zoom, x, y), img);
if (img != null) {
try {
ImageIO.write((RenderedImage) img, "png", en);
} catch (IOException e) {
log.warn("cannot save img to " + en.getAbsolutePath(), e);
}
}
} catch (IOException e) {
log.warn("cannot download pre-rendered tile [" + e.getClass().getSimpleName() + "]", e);
}
}
public class AsyncLoadTileThread extends Thread {
Stack<TileIndex> tilesToLoad = new Stack<TileIndex>();
private class TileIndex {
int zoom;
int x;
int y;
}
public AsyncLoadTileThread() {
super("AsyncLoadingTiles");
}
@Override
public void run() {
try {
while (true) {
synchronized (this) {
wait();
}
while (!tilesToLoad.isEmpty()) {
TileIndex p = tilesToLoad.pop();
if (cache.get(getFile(p.zoom, p.x, p.y)) == null) {
loadImage(p.zoom, p.x, p.y);
// TODO dead cycle (causes cancelAllPreviousTile)
prepareImage();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void cancelAllPreviousTile(){
tilesToLoad.clear();
}
public void requestTileToLoad(int zoom, int x, int y){
synchronized (this) {
TileIndex ind = new TileIndex();
ind.zoom = zoom;
ind.x = x;
ind.y = y;
tilesToLoad.add(ind);
notify();
}
}
}
// TODO async loading images (show busy cursor while it is loaded) // TODO async loading images (show busy cursor while it is loaded)
public void prepareImage(){ public void prepareImage(){
@ -187,41 +295,38 @@ public class MapPanel extends JPanel {
} }
} }
double xTileLeft = getXTile() -getSize().width/(2d*tileSize); double xTileLeft = getXTile() - getSize().width / (2d * tileSize);
double xTileRight = getXTile() + getSize().width/(2d*tileSize); double xTileRight = getXTile() + getSize().width / (2d * tileSize);
double yTileUp = getYTile() -getSize().height/(2d*tileSize); double yTileUp = getYTile() - getSize().height / (2d * tileSize);
double yTileDown = getYTile() + getSize().height/(2d*tileSize); double yTileDown = getYTile() + getSize().height / (2d * tileSize);
xStartingImage = - (int) ((xTileLeft - Math.floor(xTileLeft))*tileSize); xStartingImage = -(int) ((xTileLeft - Math.floor(xTileLeft)) * tileSize);
yStartingImage = - (int) ((yTileUp - Math.floor(yTileUp))*tileSize); yStartingImage = -(int) ((yTileUp - Math.floor(yTileUp)) * tileSize);
int tileXCount = ((int)xTileRight - (int) xTileLeft + 1); autoThread.cancelAllPreviousTile();
int tileYCount = ((int)yTileDown - (int) yTileUp + 1); int tileXCount = ((int) xTileRight - (int) xTileLeft + 1);
int tileYCount = ((int) yTileDown - (int) yTileUp + 1);
images = new BufferedImage[tileXCount][tileYCount]; images = new BufferedImage[tileXCount][tileYCount];
for(int i=0; i<images.length; i++){ for (int i = 0; i < images.length; i++) {
for(int j=0; j<images[i].length; j++){ for (int j = 0; j < images[i].length; j++) {
images[i][j]= getImageFor((int)xTileLeft + i, (int) yTileUp + j); images[i][j] = getImageFor((int) xTileLeft + i, (int) yTileUp + j);
} }
} }
if(points != null){ if (points != null) {
double latDown = MapUtils.getLatitudeFromTile(zoom, yTileDown); double latDown = MapUtils.getLatitudeFromTile(zoom, yTileDown);
double longDown = MapUtils.getLongitudeFromTile(zoom, xTileRight); double longDown = MapUtils.getLongitudeFromTile(zoom, xTileRight);
double latUp = MapUtils.getLatitudeFromTile(zoom, yTileUp); double latUp = MapUtils.getLatitudeFromTile(zoom, yTileUp);
double longUp = MapUtils.getLongitudeFromTile(zoom, xTileLeft); double longUp = MapUtils.getLongitudeFromTile(zoom, xTileLeft);
List<LatLon> objects = points.getObjects(latUp, longUp, latDown, longDown); List<LatLon> objects = points.getObjects(latUp, longUp, latDown, longDown);
pointsToDraw.clear(); pointsToDraw.clear();
for(LatLon n : objects){ for (LatLon n : objects) {
int pixX = MapUtils.getPixelShiftX(zoom, n.getLongitude(), this.longitude, tileSize) + int pixX = MapUtils.getPixelShiftX(zoom, n.getLongitude(), this.longitude, tileSize) + getWidth() / 2;
getWidth() / 2; int pixY = MapUtils.getPixelShiftY(zoom, n.getLatitude(), this.latitude, tileSize) + getHeight() / 2;
int pixY = MapUtils.getPixelShiftY(zoom, n.getLatitude(), this.latitude, tileSize) + if (pixX >= 0 && pixY >= 0) {
getHeight() / 2;
if(pixX >= 0 && pixY >= 0){
pointsToDraw.add(new Point(pixX, pixY)); pointsToDraw.add(new Point(pixX, pixY));
} }
} }
} }
repaint(); repaint();
@ -317,11 +422,15 @@ public class MapPanel extends JPanel {
} }
if(e.getID() == KeyEvent.KEY_TYPED){ if(e.getID() == KeyEvent.KEY_TYPED){
if(e.getKeyChar() == '+'){ if(e.getKeyChar() == '+'){
zoom ++; if(zoom < 18){
processed = true; zoom ++;
processed = true;
}
} else if(e.getKeyChar() == '-'){ } else if(e.getKeyChar() == '-'){
zoom --; if(zoom > 1){
processed = true; zoom --;
processed = true;
}
} }
} }

View file

@ -95,6 +95,7 @@ public class Region {
amenities.registerObject(n.getLatitude(), n.getLongitude(), n); amenities.registerObject(n.getLatitude(), n.getLongitude(), n);
} }
public City registerCity(Node c){ public City registerCity(Node c){
City city = new City(c); City city = new City(c);
if(city.getType() != null && !Algoritms.isEmpty(city.getName())){ if(city.getType() != null && !Algoritms.isEmpty(city.getName())){

View file

@ -17,6 +17,7 @@ public class OSMSettings {
AMENITY("amenity"), AMENITY("amenity"),
SHOP("shop"), SHOP("shop"),
LEISURE("leisure"), LEISURE("leisure"),
TOURISM("tourism"),
; ;
private final String value; private final String value;

View file

@ -5,7 +5,7 @@
android:versionName="1.0"> android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".MapActivity" <activity android:name=".MapActivity"
android:label="@string/app_name"> android:label="@string/app_name" android:launchMode="singleTop">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
@ -19,4 +19,5 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
</manifest> </manifest>

View file

@ -9,6 +9,7 @@ import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.view.View; import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ZoomControls; import android.widget.ZoomControls;
@ -32,8 +33,12 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mapView = (OsmandMapTileView) findViewById(R.id.View01); mapView = (OsmandMapTileView) findViewById(R.id.View01);
mapView.setFileWithTiles(new File(Environment.getExternalStorageDirectory(), "osmand/tiles/Mapnik")); mapView.setFileWithTiles(new File(Environment.getExternalStorageDirectory(), "osmand/tiles/Mapnik"));
mapView.addMapLocationListener(this); mapView.addMapLocationListener(this);
@ -72,8 +77,6 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
service.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this); service.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
service.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
} }
@ -98,6 +101,7 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat
@Override @Override
public void onStatusChanged(String provider, int status, Bundle extras) { public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO when provider disabled reset lastKnownLocation!
} }
public void validatePointOfView(){ public void validatePointOfView(){