栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从shp文件读取坐标并计算距离

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从shp文件读取坐标并计算距离

功能部件具有一种

getDefaultGeometry
可以为您提供所需要点的方法。然后,您可以从该点获取坐标。

编辑

您的问题是单位不匹配,您

MinDist
将边界框的宽度设置为度(以度为单位,大约为360),但将其与以米为单位的距离(大约为7800000)进行比较,因此您找不到一个足以保存的点。

我开始通过限制初始搜索范围来提高搜索效率,但是即使使用了我无法确定是否有效的填充位置数据集,它也足够快。

    final double MAX_SEARCH_DISTANCE = Math.max(index.getBounds().getWidth(), index.getBounds().getHeight());    double searchDist = 0.01;    while (searchDist < MAX_SEARCH_DISTANCE) {        // start point (user input)        Coordinate coordinate = p.getCoordinate();        ReferencedEnvelope search = new ReferencedEnvelope(new Envelope(coordinate),     index.getSchema().getCoordinateReferenceSystem());        search.expandBy(searchDist);        BBOX bbox = ff.bbox(ff.property(index.getSchema().getGeometryDescriptor().getName()), (BoundingBox) search);        SimpleFeatureCollection candidates = index.subCollection(bbox);        double minDist = Double.POSITIVE_INFINITY; // can't use        // MAX_Search_dist here        // as it is degrees and        // dists are meters        Coordinate minDistPoint = null;        double dist = 0;        Point dest = null;        SimpleFeatureIterator itr = candidates.features();        CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;        try { SimpleFeature feature = null; while (itr.hasNext()) {     feature = itr.next();     // destination point     dest = (Point) feature.getDefaultGeometry();     GeodeticCalculator gc = new GeodeticCalculator(crs);     gc.setStartingPosition(JTS.toDirectPosition(p.getCoordinate(), crs));     gc.setDestinationPosition(JTS.toDirectPosition(dest.getCoordinate(), crs));     // Calculate distance between points     dist = gc.getOrthodromicDistance();     // System.out.println(feature.getID()+": "+dist);     if (dist < minDist) {         minDist = dist;         minDistPoint = dest.getCoordinate();         lastMatched = feature;     } }        } finally { itr.close();        }        Point ret = null;        if (minDistPoint == null) { searchDist *= 2.0; System.out.println("repeat search");        } else { ret = gf.createPoint(minDistPoint); return ret;        }    }    return gf.createPoint(new Coordinate());}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号