-
Notifications
You must be signed in to change notification settings - Fork 75
Spatialite layer
Nutiteq edited this page Apr 19, 2014
·
14 revisions
AdvancedLayers includes already pre-compiled version of Spatialite native library with dependencies (sqlite and proj.4).
Following code shows usage of the layer, with several tables in one view:
// minimum zoom to show data
int minZoom = 10;
// open database connection, query metadata
try {
spatialLite = new SpatialLiteDbHelper(dbPath);
} catch (IOException e) {
Log.error(e.getLocalizedMessage());
Toast.makeText(this, "ERROR "+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
return;
}
dbMetaData = spatialLite.qrySpatialLayerMetadata();
ArrayList<String> tables = new ArrayList<String>();
for (String layerKey : dbMetaData.keySet()) {
SpatialLiteDbHelper.DbLayer layer = dbMetaData.get(layerKey);
Log.debug("layer: " + layer.table + " " + layer.type + " geom:"
+ layer.geomColumn+ " SRID: "+layer.srid);
}
// define styles for all 3 object types: point, line and polygon
StyleSet<PointStyle> pointStyleSet = new StyleSet<PointStyle>();
PointStyle pointStyle = PointStyle.builder().setSize(0.05f).setColor(Color.BLACK).build();
pointStyleSet.setZoomStyle(minZoom, pointStyle);
StyleSet<LineStyle> lineStyleSet = new StyleSet<LineStyle>();
lineStyleSet.setZoomStyle(minZoom, LineStyle.builder().setWidth(0.1f).setColor(Color.GREEN).build());
PolygonStyle polygonStyle = PolygonStyle.builder().setColor(Color.BLUE).build();
StyleSet<PolygonStyle> polygonStyleSet = new StyleSet<PolygonStyle>(null);
polygonStyleSet.setZoomStyle(minZoom, polygonStyle);
// define datasource for Spatialite. Here you define also labels and styles for elements.
// replace TABLE_NAME and GEOMETRY_COLUMN_NAME for your table name and column name
SpatialiteDataSource dataSource = new SpatialiteDataSource(new EPSG3857(), spatialLite, "TABLE_NAME", "GEOMETRY_COLUMN_NAME", null, null) {
@Override
protected Label createLabel(Map<String, String> userData) {
// create popup label for object based on attribute values - here just add all table fields
StringBuffer labelTxt = new StringBuffer();
for(Map.Entry<String, String> entry : userData.entrySet()){
labelTxt.append(entry.getKey() + ": " + entry.getValue() + "\n");
}
return new DefaultLabel("Data:", labelTxt.toString(), labelStyle);
}
// to make styles depending on object attributes use userData (see createLabel)
@Override
protected StyleSet<PointStyle> createPointStyleSet(Map<String, String> userData, int zoom) {
return pointStyleSet;
}
@Override
protected StyleSet<LineStyle> createLineStyleSet(Map<String, String> userData, int zoom) {
return lineStyleSet;
}
@Override
protected StyleSet<PolygonStyle> createPolygonStyleSet(Map<String, String> userData, int zoom) {
return polygonStyleSet;
}
};
// do not load more than 1000 objects - avoid memory overflow. you can load more points, less polygons
dataSource.setMaxElements(1000);
// define 2 pixel accuracy (based on screen width) for automatic polygon/line simplification
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
dataSource.setAutoSimplify(2, metrics.widthPixels);
// create and add layer to map
GeometryLayer spatialiteLayer = new GeometryLayer(dataSource);
mapView.getLayers().addLayer(spatialiteLayer);
// finally zoom mapView to map data extents
Envelope extent = spatialiteLayer.getDataExtent();
mapView.setBoundingBox(new Bounds(extent.minX, extent.maxY, extent.maxX, extent.minY), false);