【问题标题】:How to find nearest cities in a given radius?如何找到给定半径内最近的城市?
【发布时间】:2010-10-13 00:36:18
【问题描述】:

您是否知道一些实用程序或网站,我可以在其中提供美国城市、州和径向距离(以英里为单位)作为输入,它会返回该半径内的所有城市?

谢谢!

【问题讨论】:

标签: web-services geography


【解决方案1】:

也许这会有所帮助。该项目以公里为单位进行配置。您可以在 CityDAO.java 中修改这些

public List<City> findCityInRange(GeoPoint geoPoint, double distance) {
    List<City> cities = new ArrayList<City>();
    QueryBuilder queryBuilder = geoDistanceQuery("geoPoint")
            .point(geoPoint.getLat(), geoPoint.getLon())
            //.distance(distance, DistanceUnit.KILOMETERS) original
            .distance(distance, DistanceUnit.MILES)
            .optimizeBbox("memory")
            .geoDistance(GeoDistance.ARC);

    SearchRequestBuilder builder = esClient.getClient()
            .prepareSearch(INDEX)
            .setTypes("city")
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setScroll(new TimeValue(60000))
            .setSize(100).setExplain(true)
            .setPostFilter(queryBuilder)
            .addSort(SortBuilders.geoDistanceSort("geoPoint")
                    .order(SortOrder.ASC)
                    .point(geoPoint.getLat(), geoPoint.getLon())
                    //.unit(DistanceUnit.KILOMETERS)); Original
                    .unit(DistanceUnit.MILES));

    SearchResponse response = builder
            .execute()
            .actionGet();


    SearchHit[] hits = response.getHits().getHits();

    scroll:
    while (true) {

        for (SearchHit hit : hits) {
            Map<String, Object> result = hit.getSource();
            cities.add(mapper.convertValue(result, City.class));
        }

        response = esClient.getClient().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
        if (response.getHits().getHits().length == 0) {
            break scroll;
        }
    }

    return cities;
}

“LocationFinder\src\main\resources\json\cities.json”文件包含比利时的所有城市。如果需要,您也可以删除或创建条目。只要您不更改名称和/或结构,就不需要更改代码。

请务必阅读自述文件https://github.com/GlennVanSchil/LocationFinder

【讨论】:

    【解决方案2】:

    这是我的做法。

    您可以获得城市、st、邮政编码及其纬度和经度的列表。 (我不记得我们从哪里得到的)

    编辑:http://geonames.usgs.gov/domestic/download_data.htm 就像上面提到的人可能会工作。

    然后你可以编写一个方法来计算基于半径的最小和最大纬度和经度,并查询这些最小和最大之间的所有城市。然后循环遍历并计算距离并删除任何不在半径内的东西

    double latitude1 = Double.parseDouble(zipCodes.getLatitude().toString());
    double longitude1 = Double.parseDouble(zipCodes.getLongitude().toString());
    
    //Upper reaches of possible boundaries
    double upperLatBound = latitude1 + Double.parseDouble(distance)/40.0;
    double lowerLatBound = latitude1 - Double.parseDouble(distance)/40.0;
    
    double upperLongBound = longitude1 + Double.parseDouble(distance)/40.0;
    double lowerLongBound = longitude1 - Double.parseDouble(distance)/40.0;
    
    //pull back possible matches
    SimpleCriteria zipCriteria = new SimpleCriteria();
    zipCriteria.isBetween(ZipCodesPeer.LONGITUDE, lowerLongBound, upperLongBound);
    zipCriteria.isBetween(ZipCodesPeer.LATITUDE, lowerLatBound, upperLatBound);
    List zipList = ZipCodesPeer.doSelect(zipCriteria);
    ArrayList acceptList = new ArrayList();
    
    if(zipList != null)
    {
        for(int i = 0; i < zipList.size(); i++)
        {
            ZipCodes tempZip = (ZipCodes)zipList.get(i);
            double tempLat = new Double(tempZip.getLatitude().toString()).doubleValue();
            double tempLon = new Double(tempZip.getLongitude().toString()).doubleValue();
            double d = 3963.0 * Math.acos(Math.sin(latitude1 * Math.PI/180) * Math.sin(tempLat * Math.PI/180) + Math.cos(latitude1 * Math.PI/180) * Math.cos(tempLat * Math.PI/180) *  Math.cos(tempLon*Math.PI/180 -longitude1 * Math.PI/180));
    
            if(d < Double.parseDouble(distance))
            {
                acceptList.add(((ZipCodes)zipList.get(i)).getZipCd());  
            }
        }
    }
    

    我的代码有一段摘录,希望你能看到发生了什么。我从一个 ZipCodes(我的数据库中的一个表)开始,然后我拉回可能的匹配项,最后我清除了那些不在半径范围内的人。

    【讨论】:

      【解决方案3】:

      您可以从 http://geonames.usgs.gov 获得一个非常好的地理定位城市/地名数据库 - 找到一个合适的数据库转储,将其导入您的数据库,然后执行您需要的查询非常简单,特别是如果您的 DBMS 支持某些空间查询类型(例如Oracle Spatial、MySQL Spatial Extensions、PostGIS 或 SQLServer 2008)

      另请参阅:how to do location based search

      【讨论】:

        【解决方案4】:

        Oracle、PostGIS、带有GIS扩展的mysql、带有GIS扩展的sqlite都支持这种查询。

        如果你没有数据集看看:

        http://www.geonames.org/

        【讨论】:

          【解决方案5】:

          看看在 xmethods.net 上发布的这个网络服务。它需要订阅才能实际使用,但声称可以满足您的需求。

          有问题的广告方法描述:

          GetPlacesWithin 返回地理列表 指定距离内的地方 从给定的地方。参数:地点 - 地名(最多 65 个字符),州 - 2 个字母的州代码(不需要 邮政编码),距离 - 距离 英里,placeTypeToFind - 地点类型 寻找:邮政编码或城市 (包括任何村庄、城镇等)。

          http://xmethods.net/ve2/ViewListing.po?key=uuid:5428B3DD-C7C6-E1A8-87D6-461729AF02C0

          【讨论】:

            【解决方案6】:

            我没有网站,但我们在 Oracle 中将其实现为数据库功能,在 SAS 中实现为统计宏。它只需要一个包含所有城市及其经纬度的数据库。

            【讨论】:

              猜你喜欢
              • 2011-12-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-22
              • 2012-09-02
              • 1970-01-01
              相关资源
              最近更新 更多