2017-07-19 3 views
1

地点からshpファイル(ports.shp)までの距離を自然地理データから計算したい。shpファイルから座標を読み取り、距離を計算する

例えば、私は、ファイルの特徴をロードしています:

... 
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(); 
ファイルをロードするから

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

私はfeatures持っているが、それはどんなgetCoordinates()を持っていない:

が、私は先の点の座標(ports.shpファイル)を見つける方法がわかりません方法。

また、ports.shpは多くのPOINTジオメトリで構成されています。私は何らかの理由で基準点のすべての点を計算してから、最も近い点を選択する必要がありますか?

+0

これは、[gis.se] Stack Exchangeでもっと話題になっているようです。 – PolyGeo

+0

「目的地」とは、開始座標に最も近い点を意味しますか?もしそうなら、あなたは確かに他のすべての点に対してチェックする必要があります。どのように移動することに決めましたか? – DarkCygnus

+0

@GrayCygnus:はい、私は始点から最も近い点(ports.shpから)を意味しています。私の問題は、port.shpファイルの座標を正しく読み取る方法です。次に、始点に最も近い点を見つけます。 – George

答えて

1

フィーチャーには、必要なポイントを与えるgetDefaultGeometryメソッドがあります。次にポイントから座標を取得できます。

EDITあなたの問題は、あなたがユニットのミスマッチ(、度でそう約360)バウンディングボックスの幅にMinDistを設定するが、メートル単位での距離と比較することしてた

(その程度780万)ので、あなたは保存するのに十分近いところにポイントを見つけることはありませんでした。

最初の検索範囲を制限して検索を効率的にすることでプレイを開始しましたが、それが役立つかどうかは実際には分かりません。

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

よろしくお願い致します!私は小さな実行例を書いています。私は "ports.shp"ファイルを使用しています。 port.shpがポイントしています。しかし、 'dest.getCoordinate()'はそうではなくても 'minDistPoint'はnullです。私が正しいパスにいるかどうか私に教えてください。[** Codeis here **](https ://pastebin.com/gU0R6dam)。おめでとう!(上書きされました!) – George

+0

上記で私を助けることができると思えば、私は感謝します! [**このコードは現在利用可能です**](https://pastebin.com/nV9G6vyg) – George

関連する問題