All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hypertrack.utils.GeoJsonUtils Maven / Gradle / Ivy

The newest version!
package io.hypertrack.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Utility class to work with GeoJson objects and POJOs.
 */
public class GeoJsonUtils {
    /**
     * Convert [latitude, longitude] to GeoJson string for request body.
     *
     * @param   latLng      List of latitude and longitude
     * @return              Serialized GeoJSON object
     */
    public static String geoJsonFromLatLng(List latLng) {
        Double longitude = latLng.get(1);
        Double latitude = latLng.get(0);
        String geoJson = "{\"coordinates\": [" + longitude.toString() + ", " + latitude.toString() + "], \"type\": \"Point\"}";
        return geoJson;
    }

    /**
     * Convert GeoJson object to [latitude, longitude].
     *
     * @param   geoJson     GeoJson object as Map
     * @return              List of latitude and longitude
     */
    public static ArrayList latLngFromGeoJson(Map geoJson) {
        ArrayList lngLat = (ArrayList)geoJson.get("coordinates");
        ArrayList latLng = new ArrayList<>();
        latLng.add(lngLat.get(1));
        latLng.add(lngLat.get(0));
        return latLng;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy