How to use MOBAC created OSMDroid SQLite tile source file offline? - android

I've been pulling my hair out trying to get my own offline Mobile Atlas Creator OSMDroid SQLite map working with OSMDroid 3.0.8 without luck. It's been a long 3 days. I'll try to explain with clips from my application. I've been extending ItemizedIconOverlay and OverlayItem so I hope it doesn't get too confusing.
I created my own OSMDroid SQLite map file with 3 different zoom levels for a small are, like 10 square kms. I copied the resulting "base.sqlite" file into my project /res/raw/ directory. Note that the GeoPoints in my application should be well within the map's tile range. The "base.sqlite" file should get saved to the application specific data directory.
Next I turfed the /osmdroid directory on my phone so I could get the previously cached maps off. I thought I had my own offline maps working until I turned on Airplane mode and noticed the cached maps were still available.
Now all I get is blanks. I have no clue how to get this going. I've see a couple of examples but after a ton of experimentation I haven't been successful in getting any of them working.
private Hashtable<String, NodeOverlayItem> nodes = new Hashtable<String, NodeOverlayItem>();
private MapView mapView;
private Context context;
private LocationManager locManager;
private MapController mapController;
private MapTileProviderArray mapProvider;
private String mapTileArchivePath = "base.sqlite";
private ResourceProxy resourceProxy;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
this.mapView = new MapView(this, 256);
this.mapView.setBuiltInZoomControls(true);
this.mapView.setMultiTouchControls(true);
this.context = this.getApplicationContext();
this.resourceProxy = new DefaultResourceProxyImpl(context);
XYTileSource TILERENDERER = new XYTileSource("test",
ResourceProxy.string.offline_mode,
1, 20, 256, ".png", "http://127.0.0.1");
SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.context);
IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(this.getMapsFile()) };
MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(
simpleReceiver,
TILERENDERER,
archives);
this.mapProvider = new MapTileProviderArray(TILERENDERER, null, new MapTileModuleProviderBase[] { moduleProvider });
this.mapProvider.setUseDataConnection(false);
this.mapView = new MapView(this, 256, this.resourceProxy, this.mapProvider);
this.mapView.setUseDataConnection(false);
mapController = mapView.getController();
mapController.setZoom(18);
mapController.setCenter(new GeoPoint((int)(45.349622 * 1E6), (int)(-75.880700 *1E6)));
this.setContentView(mapView);
} catch(Exception ex) {
Log.e("test", ex.getMessage());
}
}
public File getMapsFile() throws IOException {
Log.d("test", "Trying to load map tiles to: " + this.mapTileArchivePath);
FileOutputStream fos = this.openFileOutput(this.mapTileArchivePath, Context.MODE_PRIVATE);
InputStream in = getResources().openRawResource(R.raw.osmdroid);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
fos.write(buff, 0, read);
}
} finally {
in.close();
fos.close();
}
return new File(this.getFilesDir(), this.mapTileArchivePath);
}

OK! I know what I doing wrong and I have it all working now! (I'm excited :)
Firstly, I had some trouble with writing my Raw resource map file to the application specific directory (e.g. openFileOutput()) I'm using a Galaxy Nexus which doesn't have an SD slot so I can't dump the map file to SD. Ensure the maps file you intend to use is byte compared with the original copy. Eclipse's DDMS perspective is useful to view a device's file structure.
I also switched to the OSMdroid Zip format. I then made sure the XYTileSource() name matched the directory created in the Zip file by MOBAC, plus ensure the tile size and zoom levels match.
XYTileSource TILERENDERER = new XYTileSource("OSM CloudMade 1", ResourceProxy.string.offline_mode, 16, 18, 256, ".png", "http://127.0.0.1");
MOBAC by default will create 256 pixel tiles. I created an atlas file with 16, 17, and 18 zoom levels. PNG is the default MOBAC tile image format.
Also, if your map file has any issues, ArchiveFileFactory.getArchiveFile() will catch them, even before MapTileFileArchiveProvider.
Here's my usage. Just make every effort to get your IArchive setup correctly and you should be ok:
XYTileSource TILERENDERER = new XYTileSource("OSM CloudMade 1", ResourceProxy.string.offline_mode, 16, 18, 256, ".png", "http://127.0.0.1");
SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.context);
IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(this.getMapsSdCard()) };
MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(
simpleReceiver,
TILERENDERER,
archives);
this.mapProvider = new MapTileProviderArray(TILERENDERER, null, new MapTileModuleProviderBase[] { moduleProvider });
this.mapProvider.setUseDataConnection(false);
this.mapView = new MapView(this, 256, this.resourceProxy, this.mapProvider);
this.mapView.setUseDataConnection(false);
Maybe I'm the only one who had trouble with this, but osmdroid doesn't clearly document how to do this, and when I opened the issue I couldn't get them to comment on my usage. If they had said I was implementing MapTileFileArchiveProvider correctly or included a good offline mapping sample, I would have focused on everything else first.

If you want to use sqlite db you only have to change
ArchiveFileFactory.getArchiveFile(this.getMapsSdCard())
to
MBTilesFileArchive.getDatabaseFileArchive(f)
where f is a File that points to your sqlite database.

Related

Create an offline map using ArcGIS (.mmpk map)

My goal is to create an Android app which download a map from ArcGIS portal when connected to internet, then use them offline. I would like to use service pattern, so later the app can have synchronization feature. I followed a tutorial from ArcGIS here.
I am currently stuck at downloading the map part. I expect the downloaded map is in mobile map package (.mmpk), but instead my download directory have a package.info file, and a folder of geodatabase and .mmap files as image shown here. Based on my understanding, I should have an .mmpk file to use them offline.
Following the tutorial steps, I am able to (1) create an offline map task, (2) specify the parameters, and (3) examine the offline capabilities. However in step (4) generate and download the offline map, I expect the downloaded map will be in mobile map package (.mmpk) but its not; as i mentioned above with image shown. In step (5) open and use the offline map, i am able to view offline map when using mobile map package (.mmpk) file that i transfer manually into the device. I also tried to open and use my downloaded (.mmap) file but no map showed up.
My full code by steps is shown below:
(1) create an offline map task
// Load map from a portal item
final Portal portal = new Portal("http://www.arcgis.com");
final PortalItem webmapItem = new PortalItem(portal, "acc027394bc84c2fb04d1ed317aac674");
// Create map and add it to the view
myMap = new ArcGISMap(webmapItem);
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.setMap(myMap);
// Create task and set parameters
final OfflineMapTask offlineMapTask = new OfflineMapTask(myMap);
(2) specify the parameters
// Create default parameters
final ListenableFuture<GenerateOfflineMapParameters> parametersFuture = offlineMapTask.createDefaultGenerateOfflineMapParametersAsync(areaOfInterest);
parametersFuture.addDoneListener(new Runnable() {
#Override
public void run() {
try {
final GenerateOfflineMapParameters parameters = parametersFuture.get();
// Update the parameters if needed
// Limit maximum scale to 5000 but take all the scales above (use 0 as a MinScale)
parameters.setMaxScale(5000);
parameters.setIncludeBasemap(false);
// Set attachment options
parameters.setAttachmentSyncDirection(GenerateGeodatabaseParameters.AttachmentSyncDirection.UPLOAD);
parameters.setReturnLayerAttachmentOption(GenerateOfflineMapParameters.ReturnLayerAttachmentOption.EDITABLE_LAYERS);
// Request the table schema only (existing features won't be included)
parameters.setReturnSchemaOnlyForEditableLayers(true);
// Update the title to contain the region
parameters.getItemInfo().setTitle(parameters.getItemInfo().getTitle() + " (Central)");
// Create new item info
final OfflineMapItemInfo itemInfo = new OfflineMapItemInfo();
// Override thumbnail with the new image based on the extent
final ListenableFuture<Bitmap> exportImageFuture = mMapView.exportImageAsync();
exportImageFuture.addDoneListener(new Runnable() {
#Override
public void run() {
try {
Bitmap mapImage = exportImageFuture.get();
// Scale to thumbnail size
Bitmap thumbnailImage = Bitmap.createScaledBitmap(mapImage, 200, 133, false);
// Convert to byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnailImage.compress(Bitmap.CompressFormat.JPEG, 50, stream);
byte[] thumbnailBytes = stream.toByteArray();
stream.close();
// Set values to the itemInfo
itemInfo.setThumbnailData(thumbnailBytes);
itemInfo.setTitle("Water network (Central)");
itemInfo.setSnippet(webmapItem.getSnippet()); // Copy from the source map
itemInfo.setDescription(webmapItem.getDescription()); // Copy from the source map
itemInfo.setAccessInformation(webmapItem.getAccessInformation()); // Copy from the source map
itemInfo.getTags().add("Water network");
itemInfo.getTags().add("Data validation");
// Set metadata to parameters
parameters.setItemInfo(itemInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
});
(3) examine the offline capabilities
final ListenableFuture<OfflineMapCapabilities> offlineMapCapabilitiesFuture =
offlineMapTask.getOfflineMapCapabilitiesAsync(parameters);
offlineMapCapabilitiesFuture.addDoneListener(new Runnable() {
#Override
public void run() {
try {
OfflineMapCapabilities offlineMapCapabilities = offlineMapCapabilitiesFuture.get();
if (offlineMapCapabilities.hasErrors()) {
// Handle possible errors with layers
for (java.util.Map.Entry<Layer, OfflineCapability> layerCapability :
offlineMapCapabilities.getLayerCapabilities().entrySet()) {
if (!layerCapability.getValue().isSupportsOffline()) {
showMessage(layerCapability.getKey().getName() + " cannot be taken offline.");
showMessage("Error : " + layerCapability.getValue().getError().getMessage());
}
}
// Handle possible errors with tables
for (java.util.Map.Entry<FeatureTable, OfflineCapability> tableCapability :
offlineMapCapabilities.getTableCapabilities().entrySet()) {
if (!tableCapability.getValue().isSupportsOffline()) {
showMessage(tableCapability.getKey().getTableName() + " cannot be taken offline.");
showMessage("Error : " + tableCapability.getValue().getError().getMessage());
}
}
} else {
// All layers and tables can be taken offline!
showMessage("All layers are good to go!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
(4) generate and download the offline map
String mExportPath = String.valueOf(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)) + File.separator + "New";
showMessage(mExportPath);
// Create and start a job to generate the offline map
final GenerateOfflineMapJob generateOfflineJob =
offlineMapTask.generateOfflineMap(parameters, mExportPath);
// Show that job started
final ProgressBar progressBarOffline = (ProgressBar) findViewById(R.id.progressBarOffline);
progressBarOffline.setVisibility(View.VISIBLE);
generateOfflineJob.start();
generateOfflineJob.addJobDoneListener(new Runnable() {
#Override
public void run() {
// Generate the offline map and download it
GenerateOfflineMapResult result = generateOfflineJob.getResult();
if (!result.hasErrors()) {
showMessage("no error");
mobileMapPackage = result.getMobileMapPackage();
// Job is finished and all content was generated
showMessage("Map " + mobileMapPackage.getItem().getTitle() +
" saved to " + mobileMapPackage.getPath());
// Show offline map in a MapView
mMapView.setMap(result.getOfflineMap());
// Show that job completed
progressBarOffline.setVisibility(View.INVISIBLE);
} else {
showMessage("error");
// Job is finished but some of the layers/tables had errors
if (result.getLayerErrors().size() > 0) {
for (java.util.Map.Entry<Layer, ArcGISRuntimeException> layerError : result.getLayerErrors().entrySet()) {
showMessage("Error occurred when taking " + layerError.getKey().getName() + " offline.");
showMessage("Error : " + layerError.getValue().getMessage());
}
}
if (result.getTableErrors().size() > 0) {
for (java.util.Map.Entry<FeatureTable, ArcGISRuntimeException> tableError : result.getTableErrors().entrySet()) {
showMessage("Error occurred when taking " + tableError.getKey().getTableName() + " offline.");
showMessage("Error : " + tableError.getValue().getMessage());
}
}
// Show that job completed
progressBarOffline.setVisibility(View.INVISIBLE);
}
}
});
(5) open and use the offline map
// Create the mobile map package
final MobileMapPackage mapPackage = new MobileMapPackage(mobileMapPackage.getPath());
// Load the mobile map package asynchronously
mapPackage.loadAsync();
// Add done listener which will invoke when mobile map package has loaded
mapPackage.addDoneLoadingListener(new Runnable() {
#Override
public void run() {
// Check load status and that the mobile map package has maps
if(mapPackage.getLoadStatus() == LoadStatus.LOADED && mapPackage.getMaps().size() > 0){
// Cdd the map from the mobile map package to the MapView
mMapView.setMap(mapPackage.getMaps().get(0));
}else{
// Log an issue if the mobile map package fails to load
showMessage(mapPackage.getLoadError().getMessage());
}
}
});
showMessage() in my code is showing Toast.
public void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
I worry if my .mmpk expectation is wrong, or my step goes wrong somewhere because I still not fully understand the whole process. This is my first time working with ArcGIS map in Android. I could not find much sample code to experiment, so really appreciate someone who could help.
Thank you!
The task created an exploded mobile map package, which works just the same as a .mmpk file. Open it like this:
final MobileMapPackage mapPackage =
new MobileMapPackage("/data/com.geoinfo.asmasyakirah.arcgis/files/Documents/New");
(If you can't access it there, you might want to generate the mobile map package in Environment.getExternalStorageDirectory() instead of Environment.DIRECTORY_DOCUMENTS.)
According to the documentation for the MobileMapPackage constructor:
Creates a new MobileMapPackage from the .mmpk file or exploded mobile map package at the given path.
If you really must have it as a .mmpk file, simply zip it using an Android API for making zip files and name it .mmpk instead of .zip.
Kinda late on the topic but i had several days working on this and found out something that may help some of you :
I created my mapData via this class : https://github.com/Esri/arcgis-runtime-samples-java/blob/master/src/main/java/com/esri/samples/map/generate_offline_map/GenerateOfflineMapSample.java
As you can see it creates a folder containing package.info + p13 (in which you find geodatabase file + mmap file)
WHen i tried offline to load this data, no errors appeared but the layer was empty and i could just see the carroying.
In fact after much more tries, i had to check that besides geodatabase and mmap file i could find a .tpk file (TilePackaged)
This one was never available (somehow due to network issues during the online download) and nothing alerted me.
Now that this tpk file is there, all items are clearly displayed like 'water network'
TL;DR; : check that tpk file is donwloaded during the online preparation.

Android: How to update Skobbler map style

I am working with Skobbler SDK 2.3 and I am wondering how to update the map styles in SKMaps.zip.
The problem:
I modified the contained "daystyle", but I noticed that this only takes effect after deleting the app data. This is really not what I want. It looks like SKPrepareMapTextureThread(android.content.Context context, java.lang.String mapTexturesPath,java.lang.String zipName, SKPrepareMapTextureListener listener).start()
only copies the files if SKMaps folder doesn't exists. Does anybody know if there is such a check inside the start() method, or (better) how to deliver modified SKMap styles to the users?
SKPrepareMapTextureThread only copies the files if SKMaps folder doesn't exists and this is how it is intended to be, as the unziping of the map resources takes a rather long time and is intended to be executed only once.
To update a style a workaround will be required:
1) delete the file .installed.txt from map resources path and call SKPrepareMapTextureThread so that the resources will be restored from assets. Although this is the easiest way, it is also the most time consuming:
final File txtPreparedFile = new File(mapResourcesDirPath + "/.installed.txt");
if (!txtPreparedFile.exists()) {
txtPreparedFile.delete();
}
new SKPrepareMapTextureThread(context, mapResourcesDirPath, "SKMaps.zip", skPrepareMapTextureListener).start();
2) a more optimal approach would be to write a routine that replace the old style with the new one
copyFile(new File("path/daystyle.json"), new File("mapResourcesDirPath/SKMaps/daystyle/daystyle.json"));
...
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
// previous code: destination.transferFrom(source, 0, source.size());
// to avoid infinite loops, should be:
long count = 0;
long size = source.size();
while((count += destination.transferFrom(source, count, size-count))<size);
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}

Nutiteq Android- Offline map is not displaying

Here is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setComponents(new Components());
RasterDataSource datasource = new PackagedRasterDataSource(new EPSG3857(), 11, 12, "t{zoom}_{x}_{y}", getApplicationContext());
RasterLayer mapLayer = new RasterLayer(datasource, 16);
mapView.getLayers().setBaseLayer(mapLayer);
mapView.setFocusPoint(mapView.getLayers().getBaseLayer().getProjection().fromWgs84(217884.21f, 1928068.13f));
//mapView.setZoom(15);
}
I have added .map file from http://www.mapcacher.com/ and converted it to PNG using http://dev.nutiteq.ee/jarmaps/ . I have mentioned the correct zoom level, checked that t11 and t12 files exist under res/raw. Also I have converted lat/lon to the required format using http://www.latlong.net/lat-long-utm.html . What is it that I am doing wrong.I don't get any errors in log cat but a blank page with Nutiteq logo is diplayed.
This line is wrong, instead of UTM coordinates
mapView.setFocusPoint(mapView.getLayers().getBaseLayer().getProjection().fromWgs84(217884.21f, 1928068.13f));
you should use WGS84, lat-long coordinates (first parameter long as x, then lat as y), like method name fromWgs84 suggests. No need to convert from/to UTM externally.

Loading Tile Source

I'm trying to load a tile source, but for some reason it doenst work.
Here's my code:
final IRegisterReceiver registerReceiver = new SimpleRegisterReceiver(this);
// Create a custom tile source
final ITileSource tileSource = TileSourceFactory.MAPNIK;
// Create a file cache modular provider
final TileWriter tileWriter = new TileWriter();
final MapTileFilesystemProvider fileSystemProvider = new MapTileFilesystemProvider(registerReceiver, tileSource);
File myMapTileSource = new File(mapsDirectory.getPath()+File.separator+"Lisboa.gemf");
// Create an archive file modular tile provider
IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(myMapTileSource) };
MapTileFileArchiveProvider fileArchiveProvider = new MapTileFileArchiveProvider(registerReceiver, tileSource, archives);
// Create a download modular tile provider
final NetworkAvailabliltyCheck networkAvailablilityCheck = new NetworkAvailabliltyCheck(this);
final MapTileDownloader downloaderProvider = new MapTileDownloader(tileSource, tileWriter, networkAvailablilityCheck);
// Create a custom tile provider array with the custom tile source and the custom tile providers
final MapTileProviderArray tileProviderArray = new MapTileProviderArray(tileSource, registerReceiver, new MapTileModuleProviderBase[] { fileSystemProvider, fileArchiveProvider, downloaderProvider });
// Create the mapview with the custom tile provider array
mMapView = new MapView(this, 256, new DefaultResourceProxyImpl(this), tileProviderArray);
mMapView.setUseDataConnection(false);
For some reason this works and i can see the map of lisbon offline.
if i switch the line
File myMapTileSource = new File(mapsDirectory.getPath()+File.separator+"Lisboa.gemf");
for
File myMapTileSource = new File(mapsDirectory.getPath()+File.separator+"Lissabon.osm");
it doenst work. It returns an error "Error loading tile" from MapTileFileArchiveProvider.
The gemf file was created in MOBAC. The osm file was downloaded from here.
Could anyone explain me why it doenst work?
If you take a look at ArchiveFileFactory, you'll see that GEMF files are supported, but .osm files are not. I'm not sure what format .osm files are (maybe it's one of the supported formats and just needs to be renamed?)

How to use .shp file in android

I want to create application to show offline map using GIS shape file(.shp) any one have idea that how to use shape file to display map in android.
Thanks in advance
If you are loading data from SD card, you can work with openmap it has a class SHapefile and try to transform your shapefile to an graphicslayer it will work i've already done it.
static public GraphicsLayer SHPtoPOINT(String shpfile) {
SpatialReference lSR = SpatialReference.create(26192);
Envelope lEnvolope = getSHPEnvelope(shpfile);//to create an extent for your graphics layer
GraphicsLayer graphicLayer = new GraphicsLayer(lSR, lEnvolope);
try {
File file = new File(shpfile);
ShapeFile shp = new ShapeFile(file);
ESRIPointRecord e = (ESRIPointRecord) shp.getNextRecord();
SimpleMarkerSymbol c_point = new SimpleMarkerSymbol(Color.BLACK, 1,
STYLE.CIRCLE);
while (e != null) {
graphicLayer.addGraphic(new Graphic(new Point(e.getX(), e.getY()), c_point));
e = (ESRIPointRecord) shp.getNextRecord();
}
shp.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return graphicLayer;
}
Source
EDIT:
BBN Technologies' OpenMap TM package is an Open Source JavaBeans TM
based programmer's toolkit. Using OpenMap, you can quickly build
applications and applets that access data from legacy databases and
applications. OpenMap provides the means to allow users to see and
manipulate geospatial information.
Link to OpenMap info.

Categories

Resources