Search in sources :

Example 1 with GeoPoint

use of com.quickutil.platform.entity.GeoPoint in project quickutil by quickutil.

the class GeoUtil method inPolygon.

public static boolean inPolygon(GeoPoint[] points, GeoPoint point) {
    int j = points.length - 1;
    boolean oddNodes = false;
    for (int i = 0; i < points.length; i++) {
        // 边界条件: 若点在多边形的顶点上
        if (point.equals(points[i])) {
            return true;
        }
        // 经纬度计算斜率(假定在一个平面内)
        double slop_A = (point.latitude - points[i].latitude) / (point.longitude - points[i].longitude);
        double slop_B = (point.latitude - points[j].latitude) / (point.longitude - points[j].longitude);
        if ((points[i].latitude < point.latitude && points[j].latitude >= point.latitude) || (points[j].latitude < point.latitude && points[i].latitude >= point.latitude)) {
            // 边界条件: 点在多边形边上,即与两个端点的斜率相等
            if (Math.abs(slop_A - slop_B) < 1e-6) {
                return true;
            }
            if (points[i].longitude + (point.latitude - points[i].latitude) / (points[j].latitude - points[i].latitude) * (points[j].longitude - points[i].longitude) < point.longitude) {
                oddNodes = !oddNodes;
            }
        }
        j = i;
    }
    return oddNodes;
}
Also used : GeoPoint(com.quickutil.platform.entity.GeoPoint)

Example 2 with GeoPoint

use of com.quickutil.platform.entity.GeoPoint in project quickutil by quickutil.

the class GeoUtil method geoGpsByBaidu.

/**
 * 根据经纬度查询地理信息-百度批量
 */
public static List<GeoDef> geoGpsByBaidu(List<GeoPoint> points) {
    try {
        if (points.size() > 20) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < points.size(); i++) {
            double[] delta = WGSToGCJPointer(points.get(i).latitude, points.get(i).longitude);
            delta = GCJToBDPointer(delta[0], delta[1]);
            builder.append(delta[0] + "," + delta[1] + "|");
        }
        String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s&output=json&batch=true", baiduKeyIn, URLEncoder.encode(builder.substring(0, builder.length() - 1), "UTF-8"));
        JsonObject object = JsonUtil.toJsonObject(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
        JsonArray array = object.getAsJsonArray("areas");
        List<GeoDef> geodefList = new ArrayList<GeoDef>();
        for (int i = 0; i < points.size(); i++) {
            object = array.get(i).getAsJsonObject();
            String country = object.get("country").getAsString();
            if (country.equals("中国")) {
                country = "China";
            }
            if (country.equals("England")) {
                country = "United Kingdom";
            }
            String countryCode = countryCodeByCountryName(country);
            String countryChinese = countryChineseByCountryCode(countryCode);
            String state = object.get("province").getAsString();
            String stateCode = "";
            if (country.equals("China")) {
                stateCode = stateCodeByStateChinese(countryCode, state);
            } else {
                stateCode = stateCodeByStateName(countryCode, state);
            }
            state = stateNameByStateCode(countryCode, stateCode);
            String stateChinese = stateChineseByStateCode(countryCode, stateCode);
            String city = object.get("city").getAsString();
            String description = object.get("district").getAsString();
            countryCode = (countryCode == null) ? UNKNOWN : countryCode;
            country = (country == null) ? UNKNOWN : country;
            countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
            stateCode = (stateCode == null) ? UNKNOWN : stateCode;
            state = (state == null) ? UNKNOWN : state;
            stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
            city = (city == null) ? UNKNOWN : city;
            geodefList.add(new GeoDef(points.get(i).latitude, points.get(i).longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description));
        }
        return geodefList;
    } catch (Exception e) {
        LOGGER.error(Symbol.BLANK, e);
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) GeoPoint(com.quickutil.platform.entity.GeoPoint) GeoDef(com.quickutil.platform.entity.GeoDef) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Aggregations

GeoPoint (com.quickutil.platform.entity.GeoPoint)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)1 GeoDef (com.quickutil.platform.entity.GeoDef)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1