move PausableThread to mapgenerator package
- add experimental regionlookup
This commit is contained in:
parent
e2da87d8e0
commit
caea9cd7c9
143
src/org/oscim/stuff/RegionLookup.java
Normal file
143
src/org/oscim/stuff/RegionLookup.java
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||||
|
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License along with
|
||||||
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package org.oscim.stuff;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.oscim.core.MapPosition;
|
||||||
|
import org.oscim.view.MapView;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class RegionLookup {
|
||||||
|
/* package */final static String TAG = RegionLookup.class.getName();
|
||||||
|
|
||||||
|
private Connection connection = null;
|
||||||
|
private PreparedStatement prepQuery = null;
|
||||||
|
|
||||||
|
private MapView mMapView;
|
||||||
|
|
||||||
|
private final Handler mHandler = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
Log.d(TAG, "message: " + msg.what);
|
||||||
|
// switch (msg.what) {
|
||||||
|
// handle update
|
||||||
|
// .....
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final String QUERY = "" +
|
||||||
|
"SELECT * from __get_regions_around(?,?)";
|
||||||
|
|
||||||
|
public RegionLookup(MapView mapView) {
|
||||||
|
mMapView = mapView;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean connect() {
|
||||||
|
Connection conn = null;
|
||||||
|
String dburl = "jdbc:postgresql://city.informatik.uni-bremen.de:5432/gis";
|
||||||
|
|
||||||
|
Properties dbOpts = new Properties();
|
||||||
|
dbOpts.setProperty("user", "osm");
|
||||||
|
dbOpts.setProperty("password", "osm");
|
||||||
|
dbOpts.setProperty("socketTimeout", "50");
|
||||||
|
dbOpts.setProperty("tcpKeepAlive", "true");
|
||||||
|
|
||||||
|
try {
|
||||||
|
DriverManager.setLoginTimeout(20);
|
||||||
|
Log.d(TAG, "Creating JDBC connection...");
|
||||||
|
|
||||||
|
Class.forName("org.postgresql.Driver");
|
||||||
|
conn = DriverManager.getConnection(dburl, dbOpts);
|
||||||
|
connection = conn;
|
||||||
|
prepQuery = conn.prepareStatement(QUERY);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.d(TAG, "Aborted due to error:" + e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean updatePosition() {
|
||||||
|
if (connection == null) {
|
||||||
|
if (!connect())
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultSet r;
|
||||||
|
MapPosition pos = mMapView.getMapPosition().getMapPosition();
|
||||||
|
|
||||||
|
try {
|
||||||
|
prepQuery.setDouble(1, pos.lat);
|
||||||
|
prepQuery.setDouble(2, pos.lon);
|
||||||
|
|
||||||
|
Log.d(TAG, "" + prepQuery.toString());
|
||||||
|
prepQuery.execute();
|
||||||
|
r = prepQuery.getResultSet();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
connection = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String level, name, boundary;
|
||||||
|
double minx, miny, maxx, maxy;
|
||||||
|
try {
|
||||||
|
while (r != null && r.next()) {
|
||||||
|
level = r.getString(1);
|
||||||
|
boundary = r.getString(2);
|
||||||
|
name = r.getString(3);
|
||||||
|
minx = r.getDouble(4);
|
||||||
|
miny = r.getDouble(5);
|
||||||
|
maxx = r.getDouble(6);
|
||||||
|
maxy = r.getDouble(7);
|
||||||
|
|
||||||
|
Log.d(TAG, "got:" + level + " b:" + boundary + " n:" + name + " " + minx
|
||||||
|
+ " " + miny + " " + maxx + " " + maxy);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
connection = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateRegion() {
|
||||||
|
new AsyncTask<Object, Integer, Long>() {
|
||||||
|
@Override
|
||||||
|
protected Long doInBackground(Object... params) {
|
||||||
|
RegionLookup.this.updatePosition();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// protected void onPostExecute(Long result) {
|
||||||
|
// Log.d(TAG, "got sth " + result);
|
||||||
|
// }
|
||||||
|
}.execute(null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -30,6 +30,7 @@ import org.oscim.database.MapDatabaseFactory;
|
|||||||
import org.oscim.database.MapDatabases;
|
import org.oscim.database.MapDatabases;
|
||||||
import org.oscim.database.MapInfo;
|
import org.oscim.database.MapInfo;
|
||||||
import org.oscim.database.OpenResult;
|
import org.oscim.database.OpenResult;
|
||||||
|
import org.oscim.stuff.RegionLookup;
|
||||||
import org.oscim.theme.ExternalRenderTheme;
|
import org.oscim.theme.ExternalRenderTheme;
|
||||||
import org.oscim.theme.InternalRenderTheme;
|
import org.oscim.theme.InternalRenderTheme;
|
||||||
import org.oscim.theme.RenderTheme;
|
import org.oscim.theme.RenderTheme;
|
||||||
@ -84,6 +85,7 @@ public class MapView extends GLSurfaceView {
|
|||||||
private int mNumMapWorkers = 4;
|
private int mNumMapWorkers = 4;
|
||||||
private DebugSettings debugSettings;
|
private DebugSettings debugSettings;
|
||||||
private String mRenderTheme;
|
private String mRenderTheme;
|
||||||
|
private Map<String, String> mMapOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param context
|
* @param context
|
||||||
@ -145,6 +147,7 @@ public class MapView extends GLSurfaceView {
|
|||||||
mJobQueue = new JobQueue();
|
mJobQueue = new JobQueue();
|
||||||
|
|
||||||
mMapRenderer = MapRendererFactory.createMapRenderer(this, mapGeneratorType);
|
mMapRenderer = MapRendererFactory.createMapRenderer(this, mapGeneratorType);
|
||||||
|
|
||||||
mMapWorkers = new MapWorker[mNumMapWorkers];
|
mMapWorkers = new MapWorker[mNumMapWorkers];
|
||||||
|
|
||||||
for (int i = 0; i < mNumMapWorkers; i++) {
|
for (int i = 0; i < mNumMapWorkers; i++) {
|
||||||
@ -162,7 +165,7 @@ public class MapView extends GLSurfaceView {
|
|||||||
if (i == 0)
|
if (i == 0)
|
||||||
mMapDatabase = mapDatabase;
|
mMapDatabase = mapDatabase;
|
||||||
|
|
||||||
mMapWorkers[i] = new MapWorker(i, this, mapGenerator, mMapRenderer);
|
mMapWorkers[i] = new MapWorker(i, mJobQueue, mapGenerator, mMapRenderer);
|
||||||
mMapWorkers[i].start();
|
mMapWorkers[i].start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,21 +189,12 @@ public class MapView extends GLSurfaceView {
|
|||||||
|
|
||||||
if (!debugFrameTime)
|
if (!debugFrameTime)
|
||||||
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
|
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
|
||||||
|
|
||||||
|
mRegionLookup = new RegionLookup(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
RegionLookup mRegionLookup;
|
||||||
* @return the debug settings which are used in this MapView.
|
|
||||||
*/
|
|
||||||
public DebugSettings getDebugSettings() {
|
|
||||||
return debugSettings;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the job queue which is used in this MapView.
|
|
||||||
*/
|
|
||||||
public JobQueue getJobQueue() {
|
|
||||||
return mJobQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the map database which is used for reading map files.
|
* @return the map database which is used for reading map files.
|
||||||
@ -216,13 +210,6 @@ public class MapView extends GLSurfaceView {
|
|||||||
return mMapViewPosition;
|
return mMapViewPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the currently used projection of the map. Do not keep this object for a longer time.
|
|
||||||
*/
|
|
||||||
public Projection getProjection() {
|
|
||||||
return mProjection;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent motionEvent) {
|
public boolean onTouchEvent(MotionEvent motionEvent) {
|
||||||
if (this.isClickable())
|
if (this.isClickable())
|
||||||
@ -247,7 +234,12 @@ public class MapView extends GLSurfaceView {
|
|||||||
mMapRenderer.updateMap(true);
|
mMapRenderer.updateMap(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, String> mMapOptions;
|
/**
|
||||||
|
* @return the debug settings which are used in this MapView.
|
||||||
|
*/
|
||||||
|
public DebugSettings getDebugSettings() {
|
||||||
|
return debugSettings;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, String> getMapOptions() {
|
public Map<String, String> getMapOptions() {
|
||||||
return mMapOptions;
|
return mMapOptions;
|
||||||
|
|||||||
@ -334,22 +334,24 @@ public class TouchHandler {
|
|||||||
public void onLongPress(MotionEvent e) {
|
public void onLongPress(MotionEvent e) {
|
||||||
Log.d("mapsforge", "long press");
|
Log.d("mapsforge", "long press");
|
||||||
|
|
||||||
|
mMapView.mRegionLookup.updateRegion();
|
||||||
|
|
||||||
// mMapView.zoom((byte) -1);
|
// mMapView.zoom((byte) -1);
|
||||||
|
|
||||||
mPrevScale = 0;
|
// mPrevScale = 0;
|
||||||
|
|
||||||
mTimer = new CountDownTimer((int) mScaleDuration, 30) {
|
// mTimer = new CountDownTimer((int) mScaleDuration, 30) {
|
||||||
@Override
|
// @Override
|
||||||
public void onTick(long tick) {
|
// public void onTick(long tick) {
|
||||||
scale2(tick);
|
// scale2(tick);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void onFinish() {
|
// public void onFinish() {
|
||||||
scale2(0);
|
// scale2(0);
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
}.start();
|
// }.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,6 @@
|
|||||||
package org.oscim.view.mapgenerator;
|
package org.oscim.view.mapgenerator;
|
||||||
|
|
||||||
import org.oscim.view.IMapRenderer;
|
import org.oscim.view.IMapRenderer;
|
||||||
import org.oscim.view.MapView;
|
|
||||||
import org.oscim.view.utils.PausableThread;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A MapWorker uses a {@link IMapGenerator} to generate map tiles. It runs in a separate thread to avoid blocking the UI
|
* A MapWorker uses a {@link IMapGenerator} to generate map tiles. It runs in a separate thread to avoid blocking the UI
|
||||||
@ -33,17 +31,15 @@ public class MapWorker extends PausableThread {
|
|||||||
/**
|
/**
|
||||||
* @param id
|
* @param id
|
||||||
* thread id
|
* thread id
|
||||||
* @param mapView
|
|
||||||
* the MapView for which this MapWorker generates map tiles.
|
|
||||||
* @param mapGenerator
|
* @param mapGenerator
|
||||||
* ...
|
* ...
|
||||||
* @param mapRenderer
|
* @param mapRenderer
|
||||||
* ...
|
* ...
|
||||||
*/
|
*/
|
||||||
public MapWorker(int id, MapView mapView, IMapGenerator mapGenerator,
|
public MapWorker(int id, JobQueue jobQueue, IMapGenerator mapGenerator,
|
||||||
IMapRenderer mapRenderer) {
|
IMapRenderer mapRenderer) {
|
||||||
super();
|
super();
|
||||||
mJobQueue = mapView.getJobQueue();
|
mJobQueue = jobQueue;
|
||||||
mMapGenerator = mapGenerator;
|
mMapGenerator = mapGenerator;
|
||||||
mMapRenderer = mapRenderer;
|
mMapRenderer = mapRenderer;
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
* You should have received a copy of the GNU Lesser General Public License along with
|
* You should have received a copy of the GNU Lesser General Public License along with
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package org.oscim.view.utils;
|
package org.oscim.view.mapgenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An abstract base class for threads which support pausing and resuming.
|
* An abstract base class for threads which support pausing and resuming.
|
||||||
Loading…
x
Reference in New Issue
Block a user