Skip to content Skip to sidebar Skip to footer

How To Work Osmdroid With Web Map Service (wms) Used By The Private Provider?

I'm trying to put the OSMDroid to work with WMS, but I do not find a way to put the WMS working. Objective: OSMDroid working with WMS (projection EPSG:4326) Tentative: I followed t

Solution 1:

You need to convert the ESPG:4326 to WebMercantor BB format. I have successfully used this code for doing that:

/*
* Sources http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
* code based on  https://www.azavea.com/blog/2013/01/14/wms-on-android/
*/publicclassWebMercatorBoundingBox {
    double north;
    double south;
    double east;   
    double west;


    // Web Mercator n/w corner of the map.privatestaticfinaldouble[] TILE_ORIGIN = {-20037508.34789244,    20037508.34789244};
    //array indexes for that dataprivatestaticfinalintORIG_X=0;
    privatestaticfinalintORIG_Y=1; // "// Size of square world map in meters, using WebMerc projection.privatestaticfinaldoubleMAP_SIZE=20037508.34789244 * 2;

    protectedWebMercatorBoundingBox(finalint x, finalint y, finalint zoom) {
        north = tile2lat(y, zoom);
        south = tile2lat(y + 1, zoom);
        west = tile2lon(x, zoom);
        east = tile2lon(x + 1, zoom);
    }
    doubletile2lon(int x, int z) {
        doubletileSize= MAP_SIZE / Math.pow(2.0, z);
        return  TILE_ORIGIN[ORIG_X] + x * tileSize;
    }

    doubletile2lat(int y, int z) {
        doubletileSize= MAP_SIZE / Math.pow(2.0, z);
        return TILE_ORIGIN[ORIG_Y] - y * tileSize;
    }

    publicdoublegetNorth(){
        returnthis.north;
    }
    publicdoublegetSouth(){
        returnthis.south;
    }
    publicdoublegetEast(){
        returnthis.east;
    }
    publicdoublegetWest(){
        returnthis.west;
    }

}

Then you can create a tile with a WMSTileProvider source which extends the OnlineTileSourceBase and overrides the getTileURLString by converting it to the X,Y,Zoom to WebMercatorBoundingBox.

publicclassTileProviderFactory{

     publicstaticWMSTileProvidergetWmsTileProvider(String version, String url, String layers, String fileFormat) {
        String[] baseUrl = {url};
        final StringWMS_FORMAT_STRING =
            url +
            "?service=WMS" +
            "&version=" + version +
            "&request=GetMap" +
            "&LAYERS=" + layers +
            "&bbox=%f,%f,%f,%f" +
            "&width=256" +
            "&height=256" +
            "&srs=EPSG:3857" +
            "&format=" + fileFormat;

    WMSTileProvider tileProvider = newWMSTileProvider(baseUrl, 256) {

        @OverridepublicStringgetTileURLString(MapTile mapTile) {
            WebMercatorBoundingBox bb = newWebMercatorBoundingBox(mapTile.getX(), mapTile.getY(),     mapTile.getZoomLevel());
                String s = String.format(
                        Locale.ENGLISH,
                        WMS_FORMAT_STRING,
                        bb.getWest(),
                        bb.getSouth(),
                        bb.getEast(),
                        bb.getNorth());
               Log.d(TAG,"Fetching map tile: " + s);
               return s;
           }
        };
        return tileProvider;
    }
}


publicabstractclassWMSTileProviderextendsOnlineTileSourceBase {

    // cql filtersprivateString cqlString = "";

    // Construct with tile size in pixels, normally 256, see parent class.publicWMSTileProvider(String[] baseurl, int tileSizeInPixels) {
        super("WMS tile source", 0 ,20,tileSizeInPixels,"png",baseurl);

    }

    protectedStringgetCql() {
        returnURLEncoder.encode(cqlString);
    }

    publicvoidsetCql(String c) {
        cqlString = c;
    }

}

Post a Comment for "How To Work Osmdroid With Web Map Service (wms) Used By The Private Provider?"