Created
January 28, 2013 21:33
-
-
Save nutiteq/4659257 to your computer and use it in GitHub Desktop.
SqliteXYTextLayer.java - Layer sample, takes texts from SQLite database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Layer which reads text,x,y from SQLite database and shows labels in given coordinates. x and y must be in layer projection, | |
which means EPSG:3857 (spherical mercator), or you can use projection.fromWgs() of to convert it. | |
Note that with layer definition you give database file name and SQL which must return x, y and text - in this order, | |
also filter arguments must be in same number and order (xmin,xmax,ymin,ymax). Do not use this for big datasets, as this query | |
is not indexed properly and will become slow with thousands of rows. Use Spatialite for big datasets, as it can do spatial indexes | |
and fast reading from tables with millions of items (e.g. global geonames.org database dump). | |
Usage sample reading from a table "NamesTable" with columns mappos_x,mappos_y,name : | |
try { | |
TextStyle capitalTextStyle40 = TextStyle.builder().setSize(22).setOffset2DY(0.2f).setColor(Color.BLACK).build(); | |
StyleSet<TextStyle> myTextStyleSet = new StyleSet<TextStyle>(); | |
myTextStyleSet.setZoomStyle(10, capitalTextStyle40); | |
Layer myTextLayer = new SqliteXYTextLayer("/sdcard/mydatabase.sqlite", | |
"SELECT mappos_x, mappos_y, name FROM NamesTable WHERE (mappos_x>=? AND mappos_x<=?) AND (mappos_y>=? AND mappos_y<=?)", | |
myTextStyleSet); | |
mapView.getLayers().addLayer(myTextLayer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
*/ | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.LinkedList; | |
import java.util.List; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteCantOpenDatabaseException; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.graphics.Bitmap; | |
import com.nutiteq.components.Envelope; | |
import com.nutiteq.components.MapPos; | |
import com.nutiteq.geometry.Marker; | |
import com.nutiteq.geometry.Text; | |
import com.nutiteq.log.Log; | |
import com.nutiteq.projections.EPSG3857; | |
import com.nutiteq.style.MarkerStyle; | |
import com.nutiteq.style.StyleSet; | |
import com.nutiteq.style.TextStyle; | |
import com.nutiteq.ui.DefaultLabel; | |
import com.nutiteq.vectorlayers.MarkerLayer; | |
import com.nutiteq.vectorlayers.TextLayer; | |
public class SqliteXYTextLayer extends TextLayer { | |
Bitmap bitmap = null; | |
private StyleSet<TextStyle> styleSet; | |
private SQLiteDatabase db; | |
private String sql; | |
public SqliteXYTextLayer(String dbPath, String sql, StyleSet<TextStyle> styleSet) throws IOException { | |
super(new EPSG3857()); | |
this.styleSet = styleSet; | |
this.sql = sql; | |
if(!(new File(dbPath)).exists()){ | |
throw new IOException("db file not found: "+dbPath); | |
} | |
try { | |
this.db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS); | |
} catch (SQLiteCantOpenDatabaseException e) { | |
throw new IOException("SQLiteCantOpenDatabaseException: "+dbPath); | |
} | |
if(this.db == null){ | |
throw new IOException("db cannot be opened: "+dbPath); | |
} | |
} | |
@Override | |
public void add(Text element) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void remove(Text element) { | |
throw new UnsupportedOperationException(); | |
} | |
public void calculateVisibleElements(final Envelope envelope, final int zoom) { | |
MapPos bottomLeft = projection.fromInternal((float) envelope.getMinX(), | |
(float) envelope.getMinY()); | |
MapPos topRight = projection.fromInternal((float) envelope.getMaxX(), | |
(float) envelope.getMaxY()); | |
final List<Text> newVisibleElementsList = new LinkedList<Text>(); | |
Cursor cursor = db.rawQuery(this.sql, new String[] { String.valueOf(bottomLeft.x), String.valueOf(topRight.x), String.valueOf(bottomLeft.y), String.valueOf(topRight.y)} ); | |
if (cursor.moveToFirst()) { | |
do { | |
double x = cursor.getDouble(0); | |
double y = cursor.getDouble(1); | |
String name = cursor.getString(2); | |
Text text = new Text(new MapPos(x,y), | |
name, styleSet, null); | |
text.attachToLayer(this); | |
text.setActiveStyle(zoom); | |
newVisibleElementsList.add(text); | |
}while (cursor.moveToNext()); | |
} | |
Log.debug("sqlitexytexts added " + newVisibleElementsList.size() | |
+ " elements"); | |
setVisibleElementsList(newVisibleElementsList); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment