Newer
Older
mountainnavigation / app / src / main / java / de / apps4ics / mountainnavigation / handlers / OsmHandler.java
package de.apps4ics.mountainnavigation.handlers;

import android.os.AsyncTask;
import android.util.Log;

import org.osmdroid.bonuspack.location.NominatimPOIProvider;
import org.osmdroid.bonuspack.location.POI;
import org.osmdroid.util.GeoPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import de.apps4ics.mountainnavigation.MainActivity;
import de.apps4ics.mountainnavigation.pois.Poi;

/**
 * This file is part of MountainNavigation.
 *
 * MountainNavigation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MountainNavigation 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MountainNavigation.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright Copyright (c) 2016 Vinzenz Rosenkanz <vinzenz.rosenkranz@gmail.com>
 *
 * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 */
public class OsmHandler {
    public static final String OSM_TYPE_PATH = "path";
    public static final String OSM_TYPE_SPRING = "spring";
    public static final String OSM_TYPE_STREAM = "stream";
    public static final String OSM_TYPE_PEAK = "peak";
    public static final String OSM_TYPE_WILD_HUT = "wilderness_hut";
    public static final String OSM_TYPE_ALP_HUT = "alpine_hut";
    public static final String OSM_TYPE_WATER = "water";
    public static final String OSM_TYPE_AERIALWAY = "aerialway";
    public static final String[] OSM_TYPES = new String[]{ OSM_TYPE_PATH, OSM_TYPE_SPRING, OSM_TYPE_STREAM, OSM_TYPE_PEAK, OSM_TYPE_WILD_HUT, OSM_TYPE_ALP_HUT, OSM_TYPE_WATER, OSM_TYPE_AERIALWAY };

    public List<POI> getPoisAround(double lat, double lon, int limit) {
        List<POI> poisAround = new ArrayList<>();
        for(String type : OSM_TYPES) {
            try {
                poisAround.addAll(new GetPoiFromOsm().execute(new Object[]{type, lat, lon, limit}).get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        if(limit != -1) return poisAround.subList(0, Math.min(limit*OSM_TYPES.length, poisAround.size()));
        else return poisAround;
    }
}

class GetPoiFromOsm extends AsyncTask<Object, Void, List> {
    protected List<POI> doInBackground(Object... params) {
        try {
            String type = (String) params[0];
            double lat = (Double) params[1];
            double lon = (Double) params[2];
            int limit = (Integer) params[3];
            NominatimPOIProvider poiProvider = new NominatimPOIProvider("");
            if(limit == -1) limit = 50;
            List<POI> foundPois = poiProvider.getPOICloseTo(new GeoPoint(lat, lon), type, limit, 0.1);
            return foundPois;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(List arrayList) {
        /*for(POI p : (ArrayList<Poi>) arrayList) {
            Log.d(MainActivity.TAG, p.mCategory + ", " + p.mType + ", " + p.mUrl);
        }*/
    }
}