代码之家  ›  专栏  ›  技术社区  ›  George

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

  •  1
  • George  · 技术社区  · 7 年前

    例如,我正在加载文件的功能:

    ...
    String filename = "10m_cultural/ne_10m_ports.shp";
    ...
    
    
     public static void Calcs(String filename) 
        throws IOException, NoSuchAuthorityCodeException, FactoryException, TransformException {
    
        HashMap<String, Object> params = new HashMap<>();
        params.put("url", DataUtilities.fileToURL(new File(filename)));
        DataStore ds = DataStoreFinder.getDataStore(params);
    
        String name = ds.getTypeNames()[0];
        SimpleFeatureSource source = ds.getFeatureSource(name);
        SimpleFeatureCollection features = source.getFeatures();
    

    现在,我想计算距离的一个点是:

    GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
    Point p = gf.createPoint(new Coordinate(43, 18));
    

    我知道要计算距离,我会做:

         CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");           
    
    
          Point start = gf.createPoint(new Coordinate(43, 18));
          Point dest = gf.createPoint(new Coordinate(?????));
    
          GeodeticCalculator gc = new GeodeticCalculator(crs);
          gc.setStartingPosition(JTS.toDirectPosition(start.getCoordinate(), crs));
          gc.setDestinationPosition(JTS.toDirectPosition(dest.getCoordinate(), crs));
    
    
          double distance = gc.getOrthodromicDistance();
    

    但我不知道如何找到目标点的坐标(ports.shp文件):

    Point dest = gf.createPoint(new Coordinate(?????));

    我有 features getCoordinates() 方法

    而且,正如我所见 ports.shp 由许多 POINT 几何学我是否必须用参考点计算每个点,然后选择最近的点?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ian Turton    7 年前

    功能具有 getDefaultGeometry

    编辑

    您的问题是单元不匹配,您正在设置 MinDist

    我开始尝试通过限制初始搜索边界来提高搜索效率,但即使在使用填充位置数据集时,它也足够快,我真的不知道它是否有帮助。

        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());
    }